* readelf.c (dump_section_as_strings): Use %6tx instead of %6zx.
[binutils.git] / gold / dirsearch.cc
blob896590308a76b308e9bbe5ce77b6da660e6524f9
1 // dirsearch.cc -- directory searching for gold
3 // Copyright 2006, 2007 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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <sys/types.h>
27 #include <dirent.h>
29 #include "gold-threads.h"
30 #include "dirsearch.h"
32 namespace
35 // Read all the files in a directory.
37 class Dir_cache
39 public:
40 Dir_cache(const char* dirname)
41 : dirname_(dirname), files_()
42 { }
44 // Read the files in the directory.
45 void read_files();
47 // Return whether a file (a base name) is present in the directory.
48 bool find(const std::string&) const;
50 private:
51 // We can not copy this class.
52 Dir_cache(const Dir_cache&);
53 Dir_cache& operator=(const Dir_cache&);
55 const char* dirname_;
56 Unordered_set<std::string> files_;
59 void
60 Dir_cache::read_files()
62 DIR* d = opendir(this->dirname_);
63 if (d == NULL)
65 // We ignore directories which do not exist.
66 if (errno == ENOENT)
67 return;
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);
75 dirent* de;
76 while ((de = readdir(d)) != NULL)
77 this->files_.insert(std::string(de->d_name));
79 if (closedir(d) != 0)
80 gold::gold_fatal("closedir failed", true);
83 bool
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
92 // writes.
94 class Dir_caches
96 public:
97 Dir_caches()
98 : lock_(), caches_()
99 { }
101 ~Dir_caches();
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
107 // calls to Add.
108 Dir_cache* lookup(const char*) const;
110 private:
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;
117 gold::Lock lock_;
118 Cache_hash caches_;
121 Dir_caches::~Dir_caches()
123 for (Cache_hash::iterator p = this->caches_.begin();
124 p != this->caches_.end();
125 ++p)
126 delete p->second;
129 void
130 Dir_caches::add(const char* dirname)
133 gold::Hold_lock hl(this->lock_);
134 if (this->lookup(dirname) != NULL)
135 return;
138 Dir_cache* cache = new Dir_cache(dirname);
140 cache->read_files();
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);
151 Dir_cache*
152 Dir_caches::lookup(const char* dirname) const
154 Cache_hash::const_iterator p = this->caches_.find(dirname);
155 if (p == this->caches_.end())
156 return NULL;
157 return p->second;
160 // The caches.
162 Dir_caches caches;
164 // A Task to read the directory.
166 class Dir_cache_task : public gold::Task
168 public:
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*);
179 private:
180 const char* dir_;
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*)
189 return IS_RUNNABLE;
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.
195 gold::Task_locker*
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.
203 void
204 Dir_cache_task::run(gold::Workqueue*)
206 caches.add(this->dir_);
211 namespace gold
214 Dirsearch::Dirsearch()
215 : directories_(), token_()
219 void
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_));
227 void
228 Dirsearch::add(Workqueue* workqueue, const General_options::Dir_list& list)
230 for (General_options::Dir_list::const_iterator p = list.begin();
231 p != list.end();
232 ++p)
233 this->add(workqueue, *p);
236 std::string
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();
243 ++p)
245 Dir_cache* pdc = caches.lookup(*p);
246 gold_assert(pdc != NULL);
247 if (pdc->find(n1))
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.