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