1 // dirsearch.cc -- directory searching for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
27 #include <sys/types.h>
31 #include "gold-threads.h"
33 #include "workqueue.h"
34 #include "dirsearch.h"
39 // Read all the files in a directory.
44 Dir_cache(const char* dirname
)
45 : dirname_(dirname
), files_()
48 // Read the files in the directory.
51 // Return whether a file (a base name) is present in the directory.
52 bool find(const std::string
&) const;
55 // We can not copy this class.
56 Dir_cache(const Dir_cache
&);
57 Dir_cache
& operator=(const Dir_cache
&);
60 Unordered_set
<std::string
> files_
;
64 Dir_cache::read_files()
66 DIR* d
= opendir(this->dirname_
);
69 // We ignore directories which do not exist or are actually file
71 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
72 gold::gold_error(_("%s: can not read directory: %s"),
73 this->dirname_
, strerror(errno
));
78 while ((de
= readdir(d
)) != NULL
)
79 this->files_
.insert(std::string(de
->d_name
));
82 gold::gold_warning("%s: closedir failed: %s", this->dirname_
,
87 Dir_cache::find(const std::string
& basename
) const
89 return this->files_
.find(basename
) != this->files_
.end();
92 // A mapping from directory names to caches. A lock permits
93 // concurrent update. There is no lock for read operations--some
94 // other mechanism must be used to prevent reads from conflicting with
106 // Add a cache for a directory.
107 void add(const char*);
109 // Look up a directory in the cache. This much be locked against
111 Dir_cache
* lookup(const char*) const;
114 // We can not copy this class.
115 Dir_caches(const Dir_caches
&);
116 Dir_caches
& operator=(const Dir_caches
&);
118 typedef Unordered_map
<const char*, Dir_cache
*> Cache_hash
;
124 Dir_caches::~Dir_caches()
126 for (Cache_hash::iterator p
= this->caches_
.begin();
127 p
!= this->caches_
.end();
133 Dir_caches::add(const char* dirname
)
136 gold::Hold_lock
hl(this->lock_
);
137 if (this->lookup(dirname
) != NULL
)
141 Dir_cache
* cache
= new Dir_cache(dirname
);
146 gold::Hold_lock
hl(this->lock_
);
148 std::pair
<const char*, Dir_cache
*> v(dirname
, cache
);
149 std::pair
<Cache_hash::iterator
, bool> p
= this->caches_
.insert(v
);
150 gold_assert(p
.second
);
155 Dir_caches::lookup(const char* dirname
) const
157 Cache_hash::const_iterator p
= this->caches_
.find(dirname
);
158 if (p
== this->caches_
.end())
167 // A Task to read the directory.
169 class Dir_cache_task
: public gold::Task
172 Dir_cache_task(const char* dir
, gold::Task_token
& token
)
173 : dir_(dir
), token_(token
)
180 locks(gold::Task_locker
*);
183 run(gold::Workqueue
*);
187 { return std::string("Dir_cache_task ") + this->dir_
; }
191 gold::Task_token
& token_
;
194 // We can always run the task to read the directory.
197 Dir_cache_task::is_runnable()
202 // Return the locks to hold. We use a blocker lock to prevent file
203 // lookups from starting until the directory contents have been read.
206 Dir_cache_task::locks(gold::Task_locker
* tl
)
208 tl
->add(this, &this->token_
);
211 // Run the task--read the directory contents.
214 Dir_cache_task::run(gold::Workqueue
*)
216 caches
->add(this->dir_
);
227 Dirsearch::initialize(Workqueue
* workqueue
,
228 const General_options::Dir_list
* directories
)
230 gold_assert(caches
== NULL
);
231 caches
= new Dir_caches
;
232 this->directories_
= directories
;
233 this->token_
.add_blockers(directories
->size());
234 for (General_options::Dir_list::const_iterator p
= directories
->begin();
235 p
!= directories
->end();
237 workqueue
->queue(new Dir_cache_task(p
->name().c_str(), this->token_
));
240 // Search for a file. NOTE: we only log failed file-lookup attempts
241 // here. Successfully lookups will eventually get logged in
245 Dirsearch::find(const std::vector
<std::string
>& names
,
246 bool* is_in_sysroot
, int* pindex
,
247 std::string
*found_name
) const
249 gold_assert(!this->token_
.is_blocked());
250 gold_assert(*pindex
>= 0);
252 for (unsigned int i
= static_cast<unsigned int>(*pindex
);
253 i
< this->directories_
->size();
256 const Search_directory
* p
= &this->directories_
->at(i
);
257 Dir_cache
* pdc
= caches
->lookup(p
->name().c_str());
258 gold_assert(pdc
!= NULL
);
259 for (std::vector
<std::string
>::const_iterator n
= names
.begin();
265 *is_in_sysroot
= p
->is_in_sysroot();
268 return p
->name() + '/' + *n
;
271 gold_debug(DEBUG_FILES
, "Attempt to open %s/%s failed",
272 p
->name().c_str(), (*n
).c_str());
277 return std::string();
280 } // End namespace gold.