Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // dirsearch.cc -- directory searching for gold |
2 | ||
fa17a3f4 | 3 | // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
6cb15b7f ILT |
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 | ||
bae7f79e ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <cerrno> | |
75f2446e | 26 | #include <cstring> |
bae7f79e ILT |
27 | #include <sys/types.h> |
28 | #include <dirent.h> | |
29 | ||
2285a610 | 30 | #include "debug.h" |
bae7f79e | 31 | #include "gold-threads.h" |
17a1d0a9 ILT |
32 | #include "options.h" |
33 | #include "workqueue.h" | |
bae7f79e ILT |
34 | #include "dirsearch.h" |
35 | ||
36 | namespace | |
37 | { | |
38 | ||
39 | // Read all the files in a directory. | |
40 | ||
41 | class Dir_cache | |
42 | { | |
43 | public: | |
44 | Dir_cache(const char* dirname) | |
45 | : dirname_(dirname), files_() | |
46 | { } | |
47 | ||
48 | // Read the files in the directory. | |
49 | void read_files(); | |
50 | ||
51 | // Return whether a file (a base name) is present in the directory. | |
52 | bool find(const std::string&) const; | |
53 | ||
54 | private: | |
55 | // We can not copy this class. | |
56 | Dir_cache(const Dir_cache&); | |
57 | Dir_cache& operator=(const Dir_cache&); | |
58 | ||
59 | const char* dirname_; | |
60 | Unordered_set<std::string> files_; | |
61 | }; | |
62 | ||
63 | void | |
64 | Dir_cache::read_files() | |
65 | { | |
66 | DIR* d = opendir(this->dirname_); | |
67 | if (d == NULL) | |
68 | { | |
69 | // We ignore directories which do not exist. | |
75f2446e ILT |
70 | if (errno != ENOENT) |
71 | gold::gold_error(_("%s: can not read directory: %s"), | |
72 | this->dirname_, strerror(errno)); | |
73 | return; | |
bae7f79e ILT |
74 | } |
75 | ||
76 | dirent* de; | |
77 | while ((de = readdir(d)) != NULL) | |
78 | this->files_.insert(std::string(de->d_name)); | |
79 | ||
80 | if (closedir(d) != 0) | |
75f2446e ILT |
81 | gold::gold_warning("%s: closedir failed: %s", this->dirname_, |
82 | strerror(errno)); | |
bae7f79e ILT |
83 | } |
84 | ||
85 | bool | |
86 | Dir_cache::find(const std::string& basename) const | |
87 | { | |
88 | return this->files_.find(basename) != this->files_.end(); | |
89 | } | |
90 | ||
91 | // A mapping from directory names to caches. A lock permits | |
92 | // concurrent update. There is no lock for read operations--some | |
93 | // other mechanism must be used to prevent reads from conflicting with | |
94 | // writes. | |
95 | ||
96 | class Dir_caches | |
97 | { | |
98 | public: | |
99 | Dir_caches() | |
100 | : lock_(), caches_() | |
101 | { } | |
102 | ||
103 | ~Dir_caches(); | |
104 | ||
105 | // Add a cache for a directory. | |
106 | void add(const char*); | |
107 | ||
108 | // Look up a directory in the cache. This much be locked against | |
109 | // calls to Add. | |
110 | Dir_cache* lookup(const char*) const; | |
111 | ||
112 | private: | |
113 | // We can not copy this class. | |
114 | Dir_caches(const Dir_caches&); | |
115 | Dir_caches& operator=(const Dir_caches&); | |
116 | ||
117 | typedef Unordered_map<const char*, Dir_cache*> Cache_hash; | |
118 | ||
119 | gold::Lock lock_; | |
120 | Cache_hash caches_; | |
121 | }; | |
122 | ||
123 | Dir_caches::~Dir_caches() | |
124 | { | |
125 | for (Cache_hash::iterator p = this->caches_.begin(); | |
126 | p != this->caches_.end(); | |
127 | ++p) | |
128 | delete p->second; | |
129 | } | |
130 | ||
131 | void | |
132 | Dir_caches::add(const char* dirname) | |
133 | { | |
134 | { | |
135 | gold::Hold_lock hl(this->lock_); | |
136 | if (this->lookup(dirname) != NULL) | |
137 | return; | |
138 | } | |
139 | ||
140 | Dir_cache* cache = new Dir_cache(dirname); | |
141 | ||
142 | cache->read_files(); | |
143 | ||
144 | { | |
145 | gold::Hold_lock hl(this->lock_); | |
146 | ||
147 | std::pair<const char*, Dir_cache*> v(dirname, cache); | |
148 | std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v); | |
a3ad94ed | 149 | gold_assert(p.second); |
bae7f79e ILT |
150 | } |
151 | } | |
152 | ||
153 | Dir_cache* | |
154 | Dir_caches::lookup(const char* dirname) const | |
155 | { | |
156 | Cache_hash::const_iterator p = this->caches_.find(dirname); | |
157 | if (p == this->caches_.end()) | |
158 | return NULL; | |
159 | return p->second; | |
160 | } | |
161 | ||
162 | // The caches. | |
163 | ||
c7912668 | 164 | Dir_caches* caches; |
bae7f79e ILT |
165 | |
166 | // A Task to read the directory. | |
167 | ||
168 | class Dir_cache_task : public gold::Task | |
169 | { | |
170 | public: | |
171 | Dir_cache_task(const char* dir, gold::Task_token& token) | |
172 | : dir_(dir), token_(token) | |
173 | { } | |
174 | ||
17a1d0a9 ILT |
175 | gold::Task_token* |
176 | is_runnable(); | |
bae7f79e | 177 | |
17a1d0a9 ILT |
178 | void |
179 | locks(gold::Task_locker*); | |
bae7f79e | 180 | |
c7912668 ILT |
181 | void |
182 | run(gold::Workqueue*); | |
183 | ||
184 | std::string | |
185 | get_name() const | |
186 | { return std::string("Dir_cache_task ") + this->dir_; } | |
bae7f79e ILT |
187 | |
188 | private: | |
189 | const char* dir_; | |
190 | gold::Task_token& token_; | |
191 | }; | |
192 | ||
193 | // We can always run the task to read the directory. | |
194 | ||
17a1d0a9 ILT |
195 | gold::Task_token* |
196 | Dir_cache_task::is_runnable() | |
bae7f79e | 197 | { |
17a1d0a9 | 198 | return NULL; |
bae7f79e ILT |
199 | } |
200 | ||
201 | // Return the locks to hold. We use a blocker lock to prevent file | |
202 | // lookups from starting until the directory contents have been read. | |
203 | ||
17a1d0a9 ILT |
204 | void |
205 | Dir_cache_task::locks(gold::Task_locker* tl) | |
bae7f79e | 206 | { |
17a1d0a9 | 207 | tl->add(this, &this->token_); |
bae7f79e ILT |
208 | } |
209 | ||
210 | // Run the task--read the directory contents. | |
211 | ||
212 | void | |
213 | Dir_cache_task::run(gold::Workqueue*) | |
214 | { | |
c7912668 | 215 | caches->add(this->dir_); |
bae7f79e ILT |
216 | } |
217 | ||
218 | } | |
219 | ||
220 | namespace gold | |
221 | { | |
222 | ||
15f8229b ILT |
223 | // Initialize. |
224 | ||
bae7f79e | 225 | void |
ad2d6943 ILT |
226 | Dirsearch::initialize(Workqueue* workqueue, |
227 | const General_options::Dir_list* directories) | |
bae7f79e | 228 | { |
c7912668 ILT |
229 | gold_assert(caches == NULL); |
230 | caches = new Dir_caches; | |
ad2d6943 | 231 | this->directories_ = directories; |
fa17a3f4 | 232 | this->token_.add_blockers(directories->size()); |
ad2d6943 ILT |
233 | for (General_options::Dir_list::const_iterator p = directories->begin(); |
234 | p != directories->end(); | |
bae7f79e | 235 | ++p) |
fa17a3f4 | 236 | workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_)); |
bae7f79e ILT |
237 | } |
238 | ||
15f8229b ILT |
239 | // Search for a file. NOTE: we only log failed file-lookup attempts |
240 | // here. Successfully lookups will eventually get logged in | |
241 | // File_read::open. | |
2285a610 | 242 | |
bae7f79e | 243 | std::string |
ad2d6943 | 244 | Dirsearch::find(const std::string& n1, const std::string& n2, |
15f8229b | 245 | bool* is_in_sysroot, int* pindex) const |
bae7f79e | 246 | { |
a3ad94ed | 247 | gold_assert(!this->token_.is_blocked()); |
15f8229b | 248 | gold_assert(*pindex >= 0); |
bae7f79e | 249 | |
15f8229b ILT |
250 | for (unsigned int i = static_cast<unsigned int>(*pindex); |
251 | i < this->directories_->size(); | |
252 | ++i) | |
bae7f79e | 253 | { |
15f8229b | 254 | const Search_directory* p = &this->directories_->at(i); |
c7912668 | 255 | Dir_cache* pdc = caches->lookup(p->name().c_str()); |
a3ad94ed | 256 | gold_assert(pdc != NULL); |
bae7f79e | 257 | if (pdc->find(n1)) |
ad2d6943 ILT |
258 | { |
259 | *is_in_sysroot = p->is_in_sysroot(); | |
15f8229b | 260 | *pindex = i; |
ad2d6943 ILT |
261 | return p->name() + '/' + n1; |
262 | } | |
2285a610 ILT |
263 | else |
264 | gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed", | |
265 | p->name().c_str(), n1.c_str()); | |
266 | ||
267 | if (!n2.empty()) | |
268 | { | |
269 | if (pdc->find(n2)) | |
270 | { | |
271 | *is_in_sysroot = p->is_in_sysroot(); | |
15f8229b | 272 | *pindex = i; |
2285a610 ILT |
273 | return p->name() + '/' + n2; |
274 | } | |
275 | else | |
276 | gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed", | |
277 | p->name().c_str(), n2.c_str()); | |
ad2d6943 | 278 | } |
bae7f79e ILT |
279 | } |
280 | ||
15f8229b | 281 | *pindex = -2; |
bae7f79e ILT |
282 | return std::string(); |
283 | } | |
284 | ||
285 | } // End namespace gold. |