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