1 // dirsearch.cc -- directory searching for gold
3 // Copyright 2006, 2007 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>
30 #include "gold-threads.h"
31 #include "dirsearch.h"
36 // Read all the files in a directory.
41 Dir_cache(const char* dirname
)
42 : dirname_(dirname
), files_()
45 // Read the files in the directory.
48 // Return whether a file (a base name) is present in the directory.
49 bool find(const std::string
&) const;
52 // We can not copy this class.
53 Dir_cache(const Dir_cache
&);
54 Dir_cache
& operator=(const Dir_cache
&);
57 Unordered_set
<std::string
> files_
;
61 Dir_cache::read_files()
63 DIR* d
= opendir(this->dirname_
);
66 // We ignore directories which do not exist.
68 gold::gold_error(_("%s: can not read directory: %s"),
69 this->dirname_
, strerror(errno
));
74 while ((de
= readdir(d
)) != NULL
)
75 this->files_
.insert(std::string(de
->d_name
));
78 gold::gold_warning("%s: closedir failed: %s", this->dirname_
,
83 Dir_cache::find(const std::string
& basename
) const
85 return this->files_
.find(basename
) != this->files_
.end();
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
102 // Add a cache for a directory.
103 void add(const char*);
105 // Look up a directory in the cache. This much be locked against
107 Dir_cache
* lookup(const char*) const;
110 // We can not copy this class.
111 Dir_caches(const Dir_caches
&);
112 Dir_caches
& operator=(const Dir_caches
&);
114 typedef Unordered_map
<const char*, Dir_cache
*> Cache_hash
;
120 Dir_caches::~Dir_caches()
122 for (Cache_hash::iterator p
= this->caches_
.begin();
123 p
!= this->caches_
.end();
129 Dir_caches::add(const char* dirname
)
132 gold::Hold_lock
hl(this->lock_
);
133 if (this->lookup(dirname
) != NULL
)
137 Dir_cache
* cache
= new Dir_cache(dirname
);
142 gold::Hold_lock
hl(this->lock_
);
144 std::pair
<const char*, Dir_cache
*> v(dirname
, cache
);
145 std::pair
<Cache_hash::iterator
, bool> p
= this->caches_
.insert(v
);
146 gold_assert(p
.second
);
151 Dir_caches::lookup(const char* dirname
) const
153 Cache_hash::const_iterator p
= this->caches_
.find(dirname
);
154 if (p
== this->caches_
.end())
163 // A Task to read the directory.
165 class Dir_cache_task
: public gold::Task
168 Dir_cache_task(const char* dir
, gold::Task_token
& token
)
169 : dir_(dir
), token_(token
)
172 Is_runnable_type
is_runnable(gold::Workqueue
*);
174 gold::Task_locker
* locks(gold::Workqueue
*);
176 void run(gold::Workqueue
*);
180 gold::Task_token
& token_
;
183 // We can always run the task to read the directory.
185 gold::Task::Is_runnable_type
186 Dir_cache_task::is_runnable(gold::Workqueue
*)
191 // Return the locks to hold. We use a blocker lock to prevent file
192 // lookups from starting until the directory contents have been read.
195 Dir_cache_task::locks(gold::Workqueue
* workqueue
)
197 return new gold::Task_locker_block(this->token_
, workqueue
);
200 // Run the task--read the directory contents.
203 Dir_cache_task::run(gold::Workqueue
*)
205 caches
.add(this->dir_
);
214 Dirsearch::initialize(Workqueue
* workqueue
,
215 const General_options::Dir_list
* directories
)
217 this->directories_
= directories
;
218 for (General_options::Dir_list::const_iterator p
= directories
->begin();
219 p
!= directories
->end();
222 this->token_
.add_blocker();
223 workqueue
->queue(new Dir_cache_task(p
->name().c_str(), this->token_
));
228 Dirsearch::find(const std::string
& n1
, const std::string
& n2
,
229 bool *is_in_sysroot
) const
231 gold_assert(!this->token_
.is_blocked());
233 for (General_options::Dir_list::const_iterator p
=
234 this->directories_
->begin();
235 p
!= this->directories_
->end();
238 Dir_cache
* pdc
= caches
.lookup(p
->name().c_str());
239 gold_assert(pdc
!= NULL
);
242 *is_in_sysroot
= p
->is_in_sysroot();
243 return p
->name() + '/' + n1
;
245 if (!n2
.empty() && pdc
->find(n2
))
247 *is_in_sysroot
= p
->is_in_sysroot();
248 return p
->name() + '/' + n2
;
252 return std::string();
255 } // End namespace gold.