Updated Russian translation for the bfd directory
[binutils-gdb.git] / gold / dirsearch.cc
blobff45f175afa5a9df0f1842732af241612f07c401
1 // dirsearch.cc -- directory searching for gold
3 // Copyright (C) 2006-2023 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 <cstring>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
31 #include "debug.h"
32 #include "gold-threads.h"
33 #include "options.h"
34 #include "workqueue.h"
35 #include "dirsearch.h"
37 namespace
40 // Read all the files in a directory.
42 class Dir_cache
44 public:
45 Dir_cache(const char* dirname)
46 : dirname_(dirname), files_()
47 { }
49 // Read the files in the directory.
50 void read_files();
52 // Return whether a file (a base name) is present in the directory.
53 bool find(const std::string&) const;
55 private:
56 // We can not copy this class.
57 Dir_cache(const Dir_cache&);
58 Dir_cache& operator=(const Dir_cache&);
60 const char* dirname_;
61 Unordered_set<std::string> files_;
64 void
65 Dir_cache::read_files()
67 DIR* d = opendir(this->dirname_);
68 if (d == NULL)
70 // We ignore directories which do not exist or are actually file
71 // names.
72 if (errno != ENOENT && errno != ENOTDIR)
73 gold::gold_error(_("%s: can not read directory: %s"),
74 this->dirname_, strerror(errno));
75 return;
78 dirent* de;
79 while ((de = readdir(d)) != NULL)
80 this->files_.insert(std::string(de->d_name));
82 if (closedir(d) != 0)
83 gold::gold_warning("%s: closedir failed: %s", this->dirname_,
84 strerror(errno));
87 bool
88 Dir_cache::find(const std::string& basename) const
90 return this->files_.find(basename) != this->files_.end();
93 // A mapping from directory names to caches. A lock permits
94 // concurrent update. There is no lock for read operations--some
95 // other mechanism must be used to prevent reads from conflicting with
96 // writes.
98 class Dir_caches
100 public:
101 Dir_caches()
102 : lock_(), caches_()
105 ~Dir_caches() ATTRIBUTE_UNUSED;
107 // Add a cache for a directory.
108 void add(const char*);
110 // Look up a directory in the cache. This much be locked against
111 // calls to Add.
112 Dir_cache* lookup(const char*) const;
114 private:
115 // We can not copy this class.
116 Dir_caches(const Dir_caches&);
117 Dir_caches& operator=(const Dir_caches&);
119 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
121 gold::Lock lock_;
122 Cache_hash caches_;
125 Dir_caches::~Dir_caches()
127 for (Cache_hash::iterator p = this->caches_.begin();
128 p != this->caches_.end();
129 ++p)
130 delete p->second;
133 void
134 Dir_caches::add(const char* dirname)
137 gold::Hold_lock hl(this->lock_);
138 if (this->lookup(dirname) != NULL)
139 return;
142 Dir_cache* cache = new Dir_cache(dirname);
144 cache->read_files();
147 gold::Hold_lock hl(this->lock_);
149 std::pair<const char*, Dir_cache*> v(dirname, cache);
150 std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
151 gold_assert(p.second);
155 Dir_cache*
156 Dir_caches::lookup(const char* dirname) const
158 Cache_hash::const_iterator p = this->caches_.find(dirname);
159 if (p == this->caches_.end())
160 return NULL;
161 return p->second;
164 // The caches.
166 Dir_caches* caches;
168 // A Task to read the directory.
170 class Dir_cache_task : public gold::Task
172 public:
173 Dir_cache_task(const char* dir, gold::Task_token& token)
174 : dir_(dir), token_(token)
177 gold::Task_token*
178 is_runnable();
180 void
181 locks(gold::Task_locker*);
183 void
184 run(gold::Workqueue*);
186 std::string
187 get_name() const
188 { return std::string("Dir_cache_task ") + this->dir_; }
190 private:
191 const char* dir_;
192 gold::Task_token& token_;
195 // We can always run the task to read the directory.
197 gold::Task_token*
198 Dir_cache_task::is_runnable()
200 return NULL;
203 // Return the locks to hold. We use a blocker lock to prevent file
204 // lookups from starting until the directory contents have been read.
206 void
207 Dir_cache_task::locks(gold::Task_locker* tl)
209 tl->add(this, &this->token_);
212 // Run the task--read the directory contents.
214 void
215 Dir_cache_task::run(gold::Workqueue*)
217 caches->add(this->dir_);
222 namespace gold
225 // Initialize.
227 void
228 Dirsearch::initialize(Workqueue* workqueue,
229 const General_options::Dir_list* directories)
231 gold_assert(caches == NULL);
232 caches = new Dir_caches;
233 this->directories_ = directories;
234 this->token_.add_blockers(directories->size());
235 for (General_options::Dir_list::const_iterator p = directories->begin();
236 p != directories->end();
237 ++p)
238 workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_));
241 // Search for a file. NOTE: we only log failed file-lookup attempts
242 // here. Successfully lookups will eventually get logged in
243 // File_read::open.
245 std::string
246 Dirsearch::find(const std::vector<std::string>& names,
247 bool* is_in_sysroot, int* pindex,
248 std::string *found_name) const
250 gold_assert(!this->token_.is_blocked());
251 gold_assert(*pindex >= 0);
253 for (unsigned int i = static_cast<unsigned int>(*pindex);
254 i < this->directories_->size();
255 ++i)
257 const Search_directory* p = &this->directories_->at(i);
258 Dir_cache* pdc = caches->lookup(p->name().c_str());
259 gold_assert(pdc != NULL);
260 for (std::vector<std::string>::const_iterator n = names.begin();
261 n != names.end();
262 ++n)
264 if (pdc->find(*n))
266 *is_in_sysroot = p->is_in_sysroot();
267 *pindex = i;
268 *found_name = *n;
269 return p->name() + '/' + *n;
271 else
272 gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed",
273 p->name().c_str(), (*n).c_str());
277 *pindex = -2;
278 return std::string();
281 // Search for a file in a directory list. This is a low-level function and
282 // therefore can be used before options and parameters are set.
284 std::string
285 Dirsearch::find_file_in_dir_list(const std::string& name,
286 const General_options::Dir_list& directories,
287 const std::string& extra_search_dir)
289 struct stat buf;
290 std::string extra_name = extra_search_dir + '/' + name;
292 if (stat(extra_name.c_str(), &buf) == 0)
293 return extra_name;
294 for (General_options::Dir_list::const_iterator dir = directories.begin();
295 dir != directories.end();
296 ++dir)
298 std::string full_name = dir->name() + '/' + name;
299 if (stat(full_name.c_str(), &buf) == 0)
300 return full_name;
302 return name;
305 } // End namespace gold.