Full support for --sysroot.
[deliverable/binutils-gdb.git] / gold / dirsearch.cc
1 // dirsearch.cc -- directory searching for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <sys/types.h>
27 #include <dirent.h>
28
29 #include "gold-threads.h"
30 #include "dirsearch.h"
31
32 namespace
33 {
34
35 // Read all the files in a directory.
36
37 class Dir_cache
38 {
39 public:
40 Dir_cache(const char* dirname)
41 : dirname_(dirname), files_()
42 { }
43
44 // Read the files in the directory.
45 void read_files();
46
47 // Return whether a file (a base name) is present in the directory.
48 bool find(const std::string&) const;
49
50 private:
51 // We can not copy this class.
52 Dir_cache(const Dir_cache&);
53 Dir_cache& operator=(const Dir_cache&);
54
55 const char* dirname_;
56 Unordered_set<std::string> files_;
57 };
58
59 void
60 Dir_cache::read_files()
61 {
62 DIR* d = opendir(this->dirname_);
63 if (d == NULL)
64 {
65 // We ignore directories which do not exist.
66 if (errno == ENOENT)
67 return;
68
69 char *s = NULL;
70 if (asprintf(&s, _("can not read directory %s"), this->dirname_) < 0)
71 gold::gold_nomem();
72 gold::gold_fatal(s, true);
73 }
74
75 dirent* de;
76 while ((de = readdir(d)) != NULL)
77 this->files_.insert(std::string(de->d_name));
78
79 if (closedir(d) != 0)
80 gold::gold_fatal("closedir failed", true);
81 }
82
83 bool
84 Dir_cache::find(const std::string& basename) const
85 {
86 return this->files_.find(basename) != this->files_.end();
87 }
88
89 // A mapping from directory names to caches. A lock permits
90 // concurrent update. There is no lock for read operations--some
91 // other mechanism must be used to prevent reads from conflicting with
92 // writes.
93
94 class Dir_caches
95 {
96 public:
97 Dir_caches()
98 : lock_(), caches_()
99 { }
100
101 ~Dir_caches();
102
103 // Add a cache for a directory.
104 void add(const char*);
105
106 // Look up a directory in the cache. This much be locked against
107 // calls to Add.
108 Dir_cache* lookup(const char*) const;
109
110 private:
111 // We can not copy this class.
112 Dir_caches(const Dir_caches&);
113 Dir_caches& operator=(const Dir_caches&);
114
115 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
116
117 gold::Lock lock_;
118 Cache_hash caches_;
119 };
120
121 Dir_caches::~Dir_caches()
122 {
123 for (Cache_hash::iterator p = this->caches_.begin();
124 p != this->caches_.end();
125 ++p)
126 delete p->second;
127 }
128
129 void
130 Dir_caches::add(const char* dirname)
131 {
132 {
133 gold::Hold_lock hl(this->lock_);
134 if (this->lookup(dirname) != NULL)
135 return;
136 }
137
138 Dir_cache* cache = new Dir_cache(dirname);
139
140 cache->read_files();
141
142 {
143 gold::Hold_lock hl(this->lock_);
144
145 std::pair<const char*, Dir_cache*> v(dirname, cache);
146 std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
147 gold_assert(p.second);
148 }
149 }
150
151 Dir_cache*
152 Dir_caches::lookup(const char* dirname) const
153 {
154 Cache_hash::const_iterator p = this->caches_.find(dirname);
155 if (p == this->caches_.end())
156 return NULL;
157 return p->second;
158 }
159
160 // The caches.
161
162 Dir_caches caches;
163
164 // A Task to read the directory.
165
166 class Dir_cache_task : public gold::Task
167 {
168 public:
169 Dir_cache_task(const char* dir, gold::Task_token& token)
170 : dir_(dir), token_(token)
171 { }
172
173 Is_runnable_type is_runnable(gold::Workqueue*);
174
175 gold::Task_locker* locks(gold::Workqueue*);
176
177 void run(gold::Workqueue*);
178
179 private:
180 const char* dir_;
181 gold::Task_token& token_;
182 };
183
184 // We can always run the task to read the directory.
185
186 gold::Task::Is_runnable_type
187 Dir_cache_task::is_runnable(gold::Workqueue*)
188 {
189 return IS_RUNNABLE;
190 }
191
192 // Return the locks to hold. We use a blocker lock to prevent file
193 // lookups from starting until the directory contents have been read.
194
195 gold::Task_locker*
196 Dir_cache_task::locks(gold::Workqueue* workqueue)
197 {
198 return new gold::Task_locker_block(this->token_, workqueue);
199 }
200
201 // Run the task--read the directory contents.
202
203 void
204 Dir_cache_task::run(gold::Workqueue*)
205 {
206 caches.add(this->dir_);
207 }
208
209 }
210
211 namespace gold
212 {
213
214 void
215 Dirsearch::initialize(Workqueue* workqueue,
216 const General_options::Dir_list* directories)
217 {
218 this->directories_ = directories;
219 for (General_options::Dir_list::const_iterator p = directories->begin();
220 p != directories->end();
221 ++p)
222 {
223 this->token_.add_blocker();
224 workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_));
225 }
226 }
227
228 std::string
229 Dirsearch::find(const std::string& n1, const std::string& n2,
230 bool *is_in_sysroot) const
231 {
232 gold_assert(!this->token_.is_blocked());
233
234 for (General_options::Dir_list::const_iterator p =
235 this->directories_->begin();
236 p != this->directories_->end();
237 ++p)
238 {
239 Dir_cache* pdc = caches.lookup(p->name().c_str());
240 gold_assert(pdc != NULL);
241 if (pdc->find(n1))
242 {
243 *is_in_sysroot = p->is_in_sysroot();
244 return p->name() + '/' + n1;
245 }
246 if (!n2.empty() && pdc->find(n2))
247 {
248 *is_in_sysroot = p->is_in_sysroot();
249 return p->name() + '/' + n2;
250 }
251 }
252
253 return std::string();
254 }
255
256 } // End namespace gold.
This page took 0.038529 seconds and 5 git commands to generate.