1 // dirsearch.cc -- directory searching for gold
3 // Copyright (C) 2006-2018 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
27 #include <sys/types.h>
32 #include "gold-threads.h"
34 #include "workqueue.h"
35 #include "dirsearch.h"
40 // Read all the files in a directory.
45 Dir_cache(const char* dirname)
46 : dirname_(dirname), files_()
49 // Read the files in the directory.
52 // Return whether a file (a base name) is present in the directory.
53 bool find(const std::string&) const;
56 // We can not copy this class.
57 Dir_cache(const Dir_cache&);
58 Dir_cache& operator=(const Dir_cache&);
61 Unordered_set<std::string> files_;
65 Dir_cache::read_files()
67 DIR* d = opendir(this->dirname_);
70 // We ignore directories which do not exist or are actually file
72 if (errno != ENOENT && errno != ENOTDIR)
73 gold::gold_error(_("%s: can not read directory: %s"),
74 this->dirname_, strerror(errno));
79 while ((de = readdir(d)) != NULL)
80 this->files_.insert(std::string(de->d_name));
83 gold::gold_warning("%s: closedir failed: %s", this->dirname_,
88 Dir_cache::find(const std::string& basename) const
90 return this->files_.find(basename) != this->files_.end();
93 // A mapping from directory names to caches. A lock permits
94 // concurrent update. There is no lock for read operations--some
95 // other mechanism must be used to prevent reads from conflicting with
105 ~Dir_caches() ATTRIBUTE_UNUSED;
107 // Add a cache for a directory.
108 void add(const char*);
110 // Look up a directory in the cache. This much be locked against
112 Dir_cache* lookup(const char*) const;
115 // We can not copy this class.
116 Dir_caches(const Dir_caches&);
117 Dir_caches& operator=(const Dir_caches&);
119 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
125 Dir_caches::~Dir_caches()
127 for (Cache_hash::iterator p = this->caches_.begin();
128 p != this->caches_.end();
134 Dir_caches::add(const char* dirname)
137 gold::Hold_lock hl(this->lock_);
138 if (this->lookup(dirname) != NULL)
142 Dir_cache* cache = new Dir_cache(dirname);
147 gold::Hold_lock hl(this->lock_);
149 std::pair<const char*, Dir_cache*> v(dirname, cache);
150 std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
151 gold_assert(p.second);
156 Dir_caches::lookup(const char* dirname) const
158 Cache_hash::const_iterator p = this->caches_.find(dirname);
159 if (p == this->caches_.end())
168 // A Task to read the directory.
170 class Dir_cache_task : public gold::Task
173 Dir_cache_task(const char* dir, gold::Task_token& token)
174 : dir_(dir), token_(token)
181 locks(gold::Task_locker*);
184 run(gold::Workqueue*);
188 { return std::string("Dir_cache_task ") + this->dir_; }
192 gold::Task_token& token_;
195 // We can always run the task to read the directory.
198 Dir_cache_task::is_runnable()
203 // Return the locks to hold. We use a blocker lock to prevent file
204 // lookups from starting until the directory contents have been read.
207 Dir_cache_task::locks(gold::Task_locker* tl)
209 tl->add(this, &this->token_);
212 // Run the task--read the directory contents.
215 Dir_cache_task::run(gold::Workqueue*)
217 caches->add(this->dir_);
228 Dirsearch::initialize(Workqueue* workqueue,
229 const General_options::Dir_list* directories)
231 gold_assert(caches == NULL);
232 caches = new Dir_caches;
233 this->directories_ = directories;
234 this->token_.add_blockers(directories->size());
235 for (General_options::Dir_list::const_iterator p = directories->begin();
236 p != directories->end();
238 workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_));
241 // Search for a file. NOTE: we only log failed file-lookup attempts
242 // here. Successfully lookups will eventually get logged in
246 Dirsearch::find(const std::vector<std::string>& names,
247 bool* is_in_sysroot, int* pindex,
248 std::string *found_name) const
250 gold_assert(!this->token_.is_blocked());
251 gold_assert(*pindex >= 0);
253 for (unsigned int i = static_cast<unsigned int>(*pindex);
254 i < this->directories_->size();
257 const Search_directory* p = &this->directories_->at(i);
258 Dir_cache* pdc = caches->lookup(p->name().c_str());
259 gold_assert(pdc != NULL);
260 for (std::vector<std::string>::const_iterator n = names.begin();
266 *is_in_sysroot = p->is_in_sysroot();
269 return p->name() + '/' + *n;
272 gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed",
273 p->name().c_str(), (*n).c_str());
278 return std::string();
281 // Search for a file in a directory list. This is a low-level function and
282 // therefore can be used before options and parameters are set.
285 Dirsearch::find_file_in_dir_list(const std::string& name,
286 const General_options::Dir_list& directories,
287 const std::string& extra_search_dir)
290 std::string extra_name = extra_search_dir + '/' + name;
292 if (stat(extra_name.c_str(), &buf) == 0)
294 for (General_options::Dir_list::const_iterator dir = directories.begin();
295 dir != directories.end();
298 std::string full_name = dir->name() + '/' + name;
299 if (stat(full_name.c_str(), &buf) == 0)
305 } // End namespace gold.