dirsearch.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // dirsearch.cc -- directory searching for gold
  2. // Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cerrno>
  19. #include <cstring>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. #include "debug.h"
  24. #include "gold-threads.h"
  25. #include "options.h"
  26. #include "workqueue.h"
  27. #include "dirsearch.h"
  28. namespace
  29. {
  30. // Read all the files in a directory.
  31. class Dir_cache
  32. {
  33. public:
  34. Dir_cache(const char* dirname)
  35. : dirname_(dirname), files_()
  36. { }
  37. // Read the files in the directory.
  38. void read_files();
  39. // Return whether a file (a base name) is present in the directory.
  40. bool find(const std::string&) const;
  41. private:
  42. // We can not copy this class.
  43. Dir_cache(const Dir_cache&);
  44. Dir_cache& operator=(const Dir_cache&);
  45. const char* dirname_;
  46. Unordered_set<std::string> files_;
  47. };
  48. void
  49. Dir_cache::read_files()
  50. {
  51. DIR* d = opendir(this->dirname_);
  52. if (d == NULL)
  53. {
  54. // We ignore directories which do not exist or are actually file
  55. // names.
  56. if (errno != ENOENT && errno != ENOTDIR)
  57. gold::gold_error(_("%s: can not read directory: %s"),
  58. this->dirname_, strerror(errno));
  59. return;
  60. }
  61. dirent* de;
  62. while ((de = readdir(d)) != NULL)
  63. this->files_.insert(std::string(de->d_name));
  64. if (closedir(d) != 0)
  65. gold::gold_warning("%s: closedir failed: %s", this->dirname_,
  66. strerror(errno));
  67. }
  68. bool
  69. Dir_cache::find(const std::string& basename) const
  70. {
  71. return this->files_.find(basename) != this->files_.end();
  72. }
  73. // A mapping from directory names to caches. A lock permits
  74. // concurrent update. There is no lock for read operations--some
  75. // other mechanism must be used to prevent reads from conflicting with
  76. // writes.
  77. class Dir_caches
  78. {
  79. public:
  80. Dir_caches()
  81. : lock_(), caches_()
  82. { }
  83. ~Dir_caches();
  84. // Add a cache for a directory.
  85. void add(const char*);
  86. // Look up a directory in the cache. This much be locked against
  87. // calls to Add.
  88. Dir_cache* lookup(const char*) const;
  89. private:
  90. // We can not copy this class.
  91. Dir_caches(const Dir_caches&);
  92. Dir_caches& operator=(const Dir_caches&);
  93. typedef Unordered_map<const char*, Dir_cache*> Cache_hash;
  94. gold::Lock lock_;
  95. Cache_hash caches_;
  96. };
  97. Dir_caches::~Dir_caches()
  98. {
  99. for (Cache_hash::iterator p = this->caches_.begin();
  100. p != this->caches_.end();
  101. ++p)
  102. delete p->second;
  103. }
  104. void
  105. Dir_caches::add(const char* dirname)
  106. {
  107. {
  108. gold::Hold_lock hl(this->lock_);
  109. if (this->lookup(dirname) != NULL)
  110. return;
  111. }
  112. Dir_cache* cache = new Dir_cache(dirname);
  113. cache->read_files();
  114. {
  115. gold::Hold_lock hl(this->lock_);
  116. std::pair<const char*, Dir_cache*> v(dirname, cache);
  117. std::pair<Cache_hash::iterator, bool> p = this->caches_.insert(v);
  118. gold_assert(p.second);
  119. }
  120. }
  121. Dir_cache*
  122. Dir_caches::lookup(const char* dirname) const
  123. {
  124. Cache_hash::const_iterator p = this->caches_.find(dirname);
  125. if (p == this->caches_.end())
  126. return NULL;
  127. return p->second;
  128. }
  129. // The caches.
  130. Dir_caches* caches;
  131. // A Task to read the directory.
  132. class Dir_cache_task : public gold::Task
  133. {
  134. public:
  135. Dir_cache_task(const char* dir, gold::Task_token& token)
  136. : dir_(dir), token_(token)
  137. { }
  138. gold::Task_token*
  139. is_runnable();
  140. void
  141. locks(gold::Task_locker*);
  142. void
  143. run(gold::Workqueue*);
  144. std::string
  145. get_name() const
  146. { return std::string("Dir_cache_task ") + this->dir_; }
  147. private:
  148. const char* dir_;
  149. gold::Task_token& token_;
  150. };
  151. // We can always run the task to read the directory.
  152. gold::Task_token*
  153. Dir_cache_task::is_runnable()
  154. {
  155. return NULL;
  156. }
  157. // Return the locks to hold. We use a blocker lock to prevent file
  158. // lookups from starting until the directory contents have been read.
  159. void
  160. Dir_cache_task::locks(gold::Task_locker* tl)
  161. {
  162. tl->add(this, &this->token_);
  163. }
  164. // Run the task--read the directory contents.
  165. void
  166. Dir_cache_task::run(gold::Workqueue*)
  167. {
  168. caches->add(this->dir_);
  169. }
  170. }
  171. namespace gold
  172. {
  173. // Initialize.
  174. void
  175. Dirsearch::initialize(Workqueue* workqueue,
  176. const General_options::Dir_list* directories)
  177. {
  178. gold_assert(caches == NULL);
  179. caches = new Dir_caches;
  180. this->directories_ = directories;
  181. this->token_.add_blockers(directories->size());
  182. for (General_options::Dir_list::const_iterator p = directories->begin();
  183. p != directories->end();
  184. ++p)
  185. workqueue->queue(new Dir_cache_task(p->name().c_str(), this->token_));
  186. }
  187. // Search for a file. NOTE: we only log failed file-lookup attempts
  188. // here. Successfully lookups will eventually get logged in
  189. // File_read::open.
  190. std::string
  191. Dirsearch::find(const std::vector<std::string>& names,
  192. bool* is_in_sysroot, int* pindex,
  193. std::string *found_name) const
  194. {
  195. gold_assert(!this->token_.is_blocked());
  196. gold_assert(*pindex >= 0);
  197. for (unsigned int i = static_cast<unsigned int>(*pindex);
  198. i < this->directories_->size();
  199. ++i)
  200. {
  201. const Search_directory* p = &this->directories_->at(i);
  202. Dir_cache* pdc = caches->lookup(p->name().c_str());
  203. gold_assert(pdc != NULL);
  204. for (std::vector<std::string>::const_iterator n = names.begin();
  205. n != names.end();
  206. ++n)
  207. {
  208. if (pdc->find(*n))
  209. {
  210. *is_in_sysroot = p->is_in_sysroot();
  211. *pindex = i;
  212. *found_name = *n;
  213. return p->name() + '/' + *n;
  214. }
  215. else
  216. gold_debug(DEBUG_FILES, "Attempt to open %s/%s failed",
  217. p->name().c_str(), (*n).c_str());
  218. }
  219. }
  220. *pindex = -2;
  221. return std::string();
  222. }
  223. // Search for a file in a directory list. This is a low-level function and
  224. // therefore can be used before options and parameters are set.
  225. std::string
  226. Dirsearch::find_file_in_dir_list(const std::string& name,
  227. const General_options::Dir_list& directories,
  228. const std::string& extra_search_dir)
  229. {
  230. struct stat buf;
  231. std::string extra_name = extra_search_dir + '/' + name;
  232. if (stat(extra_name.c_str(), &buf) == 0)
  233. return extra_name;
  234. for (General_options::Dir_list::const_iterator dir = directories.begin();
  235. dir != directories.end();
  236. ++dir)
  237. {
  238. std::string full_name = dir->name() + '/' + name;
  239. if (stat(full_name.c_str(), &buf) == 0)
  240. return full_name;
  241. }
  242. return name;
  243. }
  244. } // End namespace gold.