1 // dirsearch.cc -- directory searching for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
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.
26 #include <sys/types.h>
29 #include "gold-threads.h"
30 #include "dirsearch.h"
35 // Read all the files in a directory.
40 Dir_cache(const char* dirname)
41 : dirname_(dirname), files_()
44 // Read the files in the directory.
47 // Return whether a file (a base name) is present in the directory.
48 bool find(const std::string&) const;
51 // We can not copy this class.
52 Dir_cache(const Dir_cache&);
53 Dir_cache& operator=(const Dir_cache&);
56 Unordered_set<std::string> files_;
60 Dir_cache::read_files()
62 DIR* d = opendir(this->dirname_);
65 // We ignore directories which do not exist.
70 if (asprintf(&s, _("can not read directory %s"), this->dirname_) < 0)
72 gold::gold_fatal(s, true);
76 while ((de = readdir(d)) != NULL)
77 this->files_.insert(std::string(de->d_name));
80 gold::gold_fatal("closedir failed", true);
84 Dir_cache::find(const std::string& basename) const
86 return this->files_.find(basename) != this->files_.end();
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
103 // Add a cache for a directory.
104 void add(const char*);
106 // Look up a directory in the cache. This much be locked against
108 Dir_cache* lookup(const char*) const;
111 // We can not copy this class.
112 Dir_caches(const Dir_caches&);
113 Dir_caches& operator=(const Dir_caches&);
115 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
121 Dir_caches::~Dir_caches()
123 for (Cache_hash::iterator p = this->caches_.begin();
124 p != this->caches_.end();
130 Dir_caches::add(const char* dirname)
133 gold::Hold_lock hl(this->lock_);
134 if (this->lookup(dirname) != NULL)
138 Dir_cache* cache = new Dir_cache(dirname);
143 gold::Hold_lock hl(this->lock_);
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);
152 Dir_caches::lookup(const char* dirname) const
154 Cache_hash::const_iterator p = this->caches_.find(dirname);
155 if (p == this->caches_.end())
164 // A Task to read the directory.
166 class Dir_cache_task : public gold::Task
169 Dir_cache_task(const char* dir, gold::Task_token& token)
170 : dir_(dir), token_(token)
173 Is_runnable_type is_runnable(gold::Workqueue*);
175 gold::Task_locker* locks(gold::Workqueue*);
177 void run(gold::Workqueue*);
181 gold::Task_token& token_;
184 // We can always run the task to read the directory.
186 gold::Task::Is_runnable_type
187 Dir_cache_task::is_runnable(gold::Workqueue*)
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.
196 Dir_cache_task::locks(gold::Workqueue* workqueue)
198 return new gold::Task_locker_block(this->token_, workqueue);
201 // Run the task--read the directory contents.
204 Dir_cache_task::run(gold::Workqueue*)
206 caches.add(this->dir_);
214 Dirsearch::Dirsearch()
215 : directories_(), token_()
220 Dirsearch::add(Workqueue* workqueue, const char* d)
222 this->directories_.push_back(d);
223 this->token_.add_blocker();
224 workqueue->queue(new Dir_cache_task(d, this->token_));
228 Dirsearch::add(Workqueue* workqueue, const General_options::Dir_list& list)
230 for (General_options::Dir_list::const_iterator p = list.begin();
233 this->add(workqueue, *p);
237 Dirsearch::find(const std::string& n1, const std::string& n2) const
239 gold_assert(!this->token_.is_blocked());
241 for (std::list<const char*>::const_iterator p = this->directories_.begin();
242 p != this->directories_.end();
245 Dir_cache* pdc = caches.lookup(*p);
246 gold_assert(pdc != NULL);
248 return std::string(*p) + '/' + n1;
249 if (!n2.empty() && pdc->find(n2))
250 return std::string(*p) + '/' + n2;
253 return std::string();
256 } // End namespace gold.