1 // dirsearch.cc -- directory searching for gold
10 #include "gold-threads.h"
11 #include "dirsearch.h"
16 // Read all the files in a directory.
21 Dir_cache(const char* dirname)
22 : dirname_(dirname), files_()
25 // Read the files in the directory.
28 // Return whether a file (a base name) is present in the directory.
29 bool find(const std::string&) const;
32 // We can not copy this class.
33 Dir_cache(const Dir_cache&);
34 Dir_cache& operator=(const Dir_cache&);
37 Unordered_set<std::string> files_;
41 Dir_cache::read_files()
43 DIR* d = opendir(this->dirname_);
46 // We ignore directories which do not exist.
51 if (asprintf(&s, _("can not read directory %s"), this->dirname_) < 0)
53 gold::gold_fatal(s, true);
57 while ((de = readdir(d)) != NULL)
58 this->files_.insert(std::string(de->d_name));
61 gold::gold_fatal("closedir failed", true);
65 Dir_cache::find(const std::string& basename) const
67 return this->files_.find(basename) != this->files_.end();
70 // A mapping from directory names to caches. A lock permits
71 // concurrent update. There is no lock for read operations--some
72 // other mechanism must be used to prevent reads from conflicting with
84 // Add a cache for a directory.
85 void add(const char*);
87 // Look up a directory in the cache. This much be locked against
89 Dir_cache* lookup(const char*) const;
92 // We can not copy this class.
93 Dir_caches(const Dir_caches&);
94 Dir_caches& operator=(const Dir_caches&);
96 typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
102 Dir_caches::~Dir_caches()
104 for (Cache_hash::iterator p = this->caches_.begin();
105 p != this->caches_.end();
111 Dir_caches::add(const char* dirname)
114 gold::Hold_lock hl(this->lock_);
115 if (this->lookup(dirname) != NULL)
119 Dir_cache* cache = new Dir_cache(dirname);
124 gold::Hold_lock hl(this->lock_);
126 std::pair<const char*, Dir_cache*> v(dirname, cache);
127 std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
133 Dir_caches::lookup(const char* dirname) const
135 Cache_hash::const_iterator p = this->caches_.find(dirname);
136 if (p == this->caches_.end())
145 // A Task to read the directory.
147 class Dir_cache_task : public gold::Task
150 Dir_cache_task(const char* dir, gold::Task_token& token)
151 : dir_(dir), token_(token)
154 Is_runnable_type is_runnable(gold::Workqueue*);
156 gold::Task_locker* locks(gold::Workqueue*);
158 void run(gold::Workqueue*);
162 gold::Task_token& token_;
165 // We can always run the task to read the directory.
167 gold::Task::Is_runnable_type
168 Dir_cache_task::is_runnable(gold::Workqueue*)
173 // Return the locks to hold. We use a blocker lock to prevent file
174 // lookups from starting until the directory contents have been read.
177 Dir_cache_task::locks(gold::Workqueue* workqueue)
179 return new gold::Task_locker_block(this->token_, workqueue);
182 // Run the task--read the directory contents.
185 Dir_cache_task::run(gold::Workqueue*)
187 caches.add(this->dir_);
195 Dirsearch::Dirsearch()
196 : directories_(), token_()
201 Dirsearch::add(Workqueue* workqueue, const char* d)
203 this->directories_.push_back(d);
204 this->token_.add_blocker();
205 workqueue->queue(new Dir_cache_task(d, this->token_));
209 Dirsearch::add(Workqueue* workqueue, const General_options::Dir_list& list)
211 for (General_options::Dir_list::const_iterator p = list.begin();
214 this->add(workqueue, *p);
218 Dirsearch::find(const std::string& n1, const std::string& n2) const
220 assert(!this->token_.is_blocked());
222 for (std::list<const char*>::const_iterator p = this->directories_.begin();
223 p != this->directories_.end();
226 Dir_cache* pdc = caches.lookup(*p);
229 return std::string(*p) + '/' + n1;
230 if (!n2.empty() && pdc->find(n2))
231 return std::string(*p) + '/' + n2;
234 return std::string();
237 } // End namespace gold.