1 // dirsearch.cc -- directory searching for gold
9 #include "gold-threads.h"
10 #include "dirsearch.h"
15 // Read all the files in a directory.
20 Dir_cache(const char* dirname
)
21 : dirname_(dirname
), files_()
24 // Read the files in the directory.
27 // Return whether a file (a base name) is present in the directory.
28 bool find(const std::string
&) const;
31 // We can not copy this class.
32 Dir_cache(const Dir_cache
&);
33 Dir_cache
& operator=(const Dir_cache
&);
36 Unordered_set
<std::string
> files_
;
40 Dir_cache::read_files()
42 DIR* d
= opendir(this->dirname_
);
45 // We ignore directories which do not exist.
50 if (asprintf(&s
, _("can not read directory %s"), this->dirname_
) < 0)
52 gold::gold_fatal(s
, true);
56 while ((de
= readdir(d
)) != NULL
)
57 this->files_
.insert(std::string(de
->d_name
));
60 gold::gold_fatal("closedir failed", true);
64 Dir_cache::find(const std::string
& basename
) const
66 return this->files_
.find(basename
) != this->files_
.end();
69 // A mapping from directory names to caches. A lock permits
70 // concurrent update. There is no lock for read operations--some
71 // other mechanism must be used to prevent reads from conflicting with
83 // Add a cache for a directory.
84 void add(const char*);
86 // Look up a directory in the cache. This much be locked against
88 Dir_cache
* lookup(const char*) const;
91 // We can not copy this class.
92 Dir_caches(const Dir_caches
&);
93 Dir_caches
& operator=(const Dir_caches
&);
95 typedef Unordered_map
<const char*, Dir_cache
*> Cache_hash
;
101 Dir_caches::~Dir_caches()
103 for (Cache_hash::iterator p
= this->caches_
.begin();
104 p
!= this->caches_
.end();
110 Dir_caches::add(const char* dirname
)
113 gold::Hold_lock
hl(this->lock_
);
114 if (this->lookup(dirname
) != NULL
)
118 Dir_cache
* cache
= new Dir_cache(dirname
);
123 gold::Hold_lock
hl(this->lock_
);
125 std::pair
<const char*, Dir_cache
*> v(dirname
, cache
);
126 std::pair
<Cache_hash::iterator
, bool> p
= this->caches_
.insert(v
);
127 gold_assert(p
.second
);
132 Dir_caches::lookup(const char* dirname
) const
134 Cache_hash::const_iterator p
= this->caches_
.find(dirname
);
135 if (p
== this->caches_
.end())
144 // A Task to read the directory.
146 class Dir_cache_task
: public gold::Task
149 Dir_cache_task(const char* dir
, gold::Task_token
& token
)
150 : dir_(dir
), token_(token
)
153 Is_runnable_type
is_runnable(gold::Workqueue
*);
155 gold::Task_locker
* locks(gold::Workqueue
*);
157 void run(gold::Workqueue
*);
161 gold::Task_token
& token_
;
164 // We can always run the task to read the directory.
166 gold::Task::Is_runnable_type
167 Dir_cache_task::is_runnable(gold::Workqueue
*)
172 // Return the locks to hold. We use a blocker lock to prevent file
173 // lookups from starting until the directory contents have been read.
176 Dir_cache_task::locks(gold::Workqueue
* workqueue
)
178 return new gold::Task_locker_block(this->token_
, workqueue
);
181 // Run the task--read the directory contents.
184 Dir_cache_task::run(gold::Workqueue
*)
186 caches
.add(this->dir_
);
194 Dirsearch::Dirsearch()
195 : directories_(), token_()
200 Dirsearch::add(Workqueue
* workqueue
, const char* d
)
202 this->directories_
.push_back(d
);
203 this->token_
.add_blocker();
204 workqueue
->queue(new Dir_cache_task(d
, this->token_
));
208 Dirsearch::add(Workqueue
* workqueue
, const General_options::Dir_list
& list
)
210 for (General_options::Dir_list::const_iterator p
= list
.begin();
213 this->add(workqueue
, *p
);
217 Dirsearch::find(const std::string
& n1
, const std::string
& n2
) const
219 gold_assert(!this->token_
.is_blocked());
221 for (std::list
<const char*>::const_iterator p
= this->directories_
.begin();
222 p
!= this->directories_
.end();
225 Dir_cache
* pdc
= caches
.lookup(*p
);
226 gold_assert(pdc
!= NULL
);
228 return std::string(*p
) + '/' + n1
;
229 if (!n2
.empty() && pdc
->find(n2
))
230 return std::string(*p
) + '/' + n2
;
233 return std::string();
236 } // End namespace gold.
This page took 0.033089 seconds and 4 git commands to generate.