daily update
[deliverable/binutils-gdb.git] / gold / dirsearch.cc
CommitLineData
bae7f79e
ILT
1// dirsearch.cc -- directory searching for gold
2
6cb15b7f
ILT
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
bae7f79e
ILT
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
32namespace
33{
34
35// Read all the files in a directory.
36
37class 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
59void
60Dir_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
83bool
84Dir_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
94class 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
121Dir_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
129void
130Dir_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);
a3ad94ed 147 gold_assert(p.second);
bae7f79e
ILT
148 }
149}
150
151Dir_cache*
152Dir_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
162Dir_caches caches;
163
164// A Task to read the directory.
165
166class 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
186gold::Task::Is_runnable_type
187Dir_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
195gold::Task_locker*
196Dir_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
203void
204Dir_cache_task::run(gold::Workqueue*)
205{
206 caches.add(this->dir_);
207}
208
209}
210
211namespace gold
212{
213
214Dirsearch::Dirsearch()
215 : directories_(), token_()
216{
217}
218
219void
220Dirsearch::add(Workqueue* workqueue, const char* d)
221{
222 this->directories_.push_back(d);
223 this->token_.add_blocker();
224 workqueue->queue(new Dir_cache_task(d, this->token_));
225}
226
227void
228Dirsearch::add(Workqueue* workqueue, const General_options::Dir_list& list)
229{
230 for (General_options::Dir_list::const_iterator p = list.begin();
231 p != list.end();
232 ++p)
233 this->add(workqueue, *p);
234}
235
236std::string
237Dirsearch::find(const std::string& n1, const std::string& n2) const
238{
a3ad94ed 239 gold_assert(!this->token_.is_blocked());
bae7f79e
ILT
240
241 for (std::list<const char*>::const_iterator p = this->directories_.begin();
242 p != this->directories_.end();
243 ++p)
244 {
245 Dir_cache* pdc = caches.lookup(*p);
a3ad94ed 246 gold_assert(pdc != NULL);
bae7f79e
ILT
247 if (pdc->find(n1))
248 return std::string(*p) + '/' + n1;
249 if (!n2.empty() && pdc->find(n2))
250 return std::string(*p) + '/' + n2;
251 }
252
253 return std::string();
254}
255
256} // End namespace gold.
This page took 0.072877 seconds and 4 git commands to generate.