Directory.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(Directory.hxx)
  5. #include KWSYS_HEADER(Configure.hxx)
  6. #include KWSYS_HEADER(Encoding.hxx)
  7. #include KWSYS_HEADER(SystemTools.hxx)
  8. // Work-around CMake dependency scanning limitation. This must
  9. // duplicate the above list of headers.
  10. #if 0
  11. # include "Configure.hxx.in"
  12. # include "Directory.hxx.in"
  13. # include "Encoding.hxx.in"
  14. #endif
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #if defined(_WIN32) && !defined(__CYGWIN__)
  19. # include <windows.h>
  20. # include <ctype.h>
  21. # include <fcntl.h>
  22. # include <io.h>
  23. # include <stdio.h>
  24. # include <stdlib.h>
  25. # include <string.h>
  26. # include <sys/stat.h>
  27. # include <sys/types.h>
  28. #endif
  29. namespace KWSYS_NAMESPACE {
  30. class DirectoryInternals
  31. {
  32. public:
  33. struct FileData
  34. {
  35. std::string Name;
  36. #if defined(_WIN32) && !defined(__CYGWIN__)
  37. _wfinddata_t FindData;
  38. #endif
  39. FileData(std::string name
  40. #if defined(_WIN32) && !defined(__CYGWIN__)
  41. ,
  42. _wfinddata_t data
  43. #endif
  44. )
  45. : Name(std::move(name))
  46. #if defined(_WIN32) && !defined(__CYGWIN__)
  47. , FindData(std::move(data))
  48. #endif
  49. {
  50. }
  51. };
  52. // Array of Files
  53. std::vector<FileData> Files;
  54. // Path to Open'ed directory
  55. std::string Path;
  56. };
  57. Directory::Directory()
  58. {
  59. this->Internal = new DirectoryInternals;
  60. }
  61. Directory::Directory(Directory&& other)
  62. {
  63. this->Internal = other.Internal;
  64. other.Internal = nullptr;
  65. }
  66. Directory& Directory::operator=(Directory&& other)
  67. {
  68. std::swap(this->Internal, other.Internal);
  69. return *this;
  70. }
  71. Directory::~Directory()
  72. {
  73. delete this->Internal;
  74. }
  75. unsigned long Directory::GetNumberOfFiles() const
  76. {
  77. return static_cast<unsigned long>(this->Internal->Files.size());
  78. }
  79. const char* Directory::GetFile(unsigned long dindex) const
  80. {
  81. return this->Internal->Files[dindex].Name.c_str();
  82. }
  83. std::string const& Directory::GetFileName(std::size_t i) const
  84. {
  85. return this->Internal->Files[i].Name;
  86. }
  87. std::string Directory::GetFilePath(std::size_t i) const
  88. {
  89. std::string abs = this->Internal->Path;
  90. if (!abs.empty() && abs.back() != '/') {
  91. abs += '/';
  92. }
  93. abs += this->Internal->Files[i].Name;
  94. return abs;
  95. }
  96. bool Directory::FileIsDirectory(std::size_t i) const
  97. {
  98. #if defined(_WIN32) && !defined(__CYGWIN__)
  99. _wfinddata_t const& data = this->Internal->Files[i].FindData;
  100. return (data.attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
  101. #else
  102. std::string const& path = this->GetFilePath(i);
  103. return kwsys::SystemTools::FileIsDirectory(path);
  104. #endif
  105. }
  106. bool Directory::FileIsSymlink(std::size_t i) const
  107. {
  108. std::string const& path = this->GetFilePath(i);
  109. #if defined(_WIN32) && !defined(__CYGWIN__)
  110. _wfinddata_t const& data = this->Internal->Files[i].FindData;
  111. return kwsys::SystemTools::FileIsSymlinkWithAttr(
  112. Encoding::ToWindowsExtendedPath(path), data.attrib);
  113. #else
  114. return kwsys::SystemTools::FileIsSymlink(path);
  115. #endif
  116. }
  117. const char* Directory::GetPath() const
  118. {
  119. return this->Internal->Path.c_str();
  120. }
  121. void Directory::Clear()
  122. {
  123. this->Internal->Path.resize(0);
  124. this->Internal->Files.clear();
  125. }
  126. } // namespace KWSYS_NAMESPACE
  127. // First Windows platforms
  128. #if defined(_WIN32) && !defined(__CYGWIN__)
  129. namespace KWSYS_NAMESPACE {
  130. Status Directory::Load(std::string const& name, std::string* errorMessage)
  131. {
  132. this->Clear();
  133. intptr_t srchHandle;
  134. char* buf;
  135. size_t bufLength;
  136. size_t n = name.size();
  137. if (name.back() == '/' || name.back() == '\\') {
  138. bufLength = n + 1 + 1;
  139. buf = new char[bufLength];
  140. snprintf(buf, bufLength, "%s*", name.c_str());
  141. } else {
  142. // Make sure the slashes in the wildcard suffix are consistent with the
  143. // rest of the path
  144. bufLength = n + 2 + 1;
  145. buf = new char[bufLength];
  146. if (name.find('\\') != std::string::npos) {
  147. snprintf(buf, bufLength, "%s\\*", name.c_str());
  148. } else {
  149. snprintf(buf, bufLength, "%s/*", name.c_str());
  150. }
  151. }
  152. struct _wfinddata_t data; // data of current file
  153. // Now put them into the file array
  154. srchHandle =
  155. _wfindfirst((wchar_t*)Encoding::ToWindowsExtendedPath(buf).c_str(), &data);
  156. delete[] buf;
  157. if (srchHandle == -1) {
  158. Status status = Status::POSIX_errno();
  159. if (errorMessage) {
  160. *errorMessage = status.GetString();
  161. }
  162. return status;
  163. }
  164. // Loop through names
  165. do {
  166. this->Internal->Files.emplace_back(Encoding::ToNarrow(data.name), data);
  167. } while (_wfindnext(srchHandle, &data) != -1);
  168. this->Internal->Path = name;
  169. if (_findclose(srchHandle) == -1) {
  170. Status status = Status::POSIX_errno();
  171. if (errorMessage) {
  172. *errorMessage = status.GetString();
  173. }
  174. return status;
  175. }
  176. return Status::Success();
  177. }
  178. unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
  179. std::string* errorMessage)
  180. {
  181. intptr_t srchHandle;
  182. char* buf;
  183. size_t bufLength;
  184. size_t n = name.size();
  185. if (name.back() == '/') {
  186. bufLength = n + 1 + 1;
  187. buf = new char[n + 1 + 1];
  188. snprintf(buf, bufLength, "%s*", name.c_str());
  189. } else {
  190. bufLength = n + 2 + 1;
  191. buf = new char[n + 2 + 1];
  192. snprintf(buf, bufLength, "%s/*", name.c_str());
  193. }
  194. struct _wfinddata_t data; // data of current file
  195. // Now put them into the file array
  196. srchHandle = _wfindfirst((wchar_t*)Encoding::ToWide(buf).c_str(), &data);
  197. delete[] buf;
  198. if (srchHandle == -1) {
  199. if (errorMessage) {
  200. if (unsigned int errorId = GetLastError()) {
  201. LPSTR message = nullptr;
  202. DWORD size = FormatMessageA(
  203. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  204. FORMAT_MESSAGE_IGNORE_INSERTS,
  205. nullptr, errorId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  206. (LPSTR)&message, 0, nullptr);
  207. *errorMessage = std::string(message, size);
  208. LocalFree(message);
  209. } else {
  210. *errorMessage = "Unknown error.";
  211. }
  212. }
  213. return 0;
  214. }
  215. // Loop through names
  216. unsigned long count = 0;
  217. do {
  218. count++;
  219. } while (_wfindnext(srchHandle, &data) != -1);
  220. _findclose(srchHandle);
  221. return count;
  222. }
  223. } // namespace KWSYS_NAMESPACE
  224. #else
  225. // Now the POSIX style directory access
  226. # include <sys/types.h>
  227. # include <dirent.h>
  228. # include <errno.h>
  229. # include <string.h>
  230. // PGI with glibc has trouble with dirent and large file support:
  231. // http://www.pgroup.com/userforum/viewtopic.php?
  232. // p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
  233. // Work around the problem by mapping dirent the same way as readdir.
  234. # if defined(__PGI) && defined(__GLIBC__)
  235. # define kwsys_dirent_readdir dirent
  236. # define kwsys_dirent_readdir64 dirent64
  237. # define kwsys_dirent kwsys_dirent_lookup(readdir)
  238. # define kwsys_dirent_lookup(x) kwsys_dirent_lookup_delay(x)
  239. # define kwsys_dirent_lookup_delay(x) kwsys_dirent_##x
  240. # else
  241. # define kwsys_dirent dirent
  242. # endif
  243. namespace KWSYS_NAMESPACE {
  244. Status Directory::Load(std::string const& name, std::string* errorMessage)
  245. {
  246. this->Clear();
  247. errno = 0;
  248. DIR* dir = opendir(name.c_str());
  249. if (!dir) {
  250. if (errorMessage != nullptr) {
  251. *errorMessage = std::string(strerror(errno));
  252. }
  253. return Status::POSIX_errno();
  254. }
  255. errno = 0;
  256. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir)) {
  257. this->Internal->Files.emplace_back(d->d_name);
  258. }
  259. if (errno != 0) {
  260. if (errorMessage != nullptr) {
  261. *errorMessage = std::string(strerror(errno));
  262. }
  263. return Status::POSIX_errno();
  264. }
  265. this->Internal->Path = name;
  266. closedir(dir);
  267. return Status::Success();
  268. }
  269. unsigned long Directory::GetNumberOfFilesInDirectory(const std::string& name,
  270. std::string* errorMessage)
  271. {
  272. errno = 0;
  273. DIR* dir = opendir(name.c_str());
  274. if (!dir) {
  275. if (errorMessage != nullptr) {
  276. *errorMessage = std::string(strerror(errno));
  277. }
  278. return 0;
  279. }
  280. unsigned long count = 0;
  281. for (kwsys_dirent* d = readdir(dir); d; d = readdir(dir)) {
  282. count++;
  283. }
  284. if (errno != 0) {
  285. if (errorMessage != nullptr) {
  286. *errorMessage = std::string(strerror(errno));
  287. }
  288. return false;
  289. }
  290. closedir(dir);
  291. return count;
  292. }
  293. } // namespace KWSYS_NAMESPACE
  294. #endif