選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

filesystem.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // \file filesystem.cpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. #ifdef _WIN32
  23. #include <windows.h>
  24. #else
  25. #include <dirent.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <cstdlib>
  29. #include <cstring>
  30. #include <cerrno>
  31. #endif
  32. #include <sys/stat.h>
  33. #include <stdexcept>
  34. #include "filesystem.hpp"
  35. namespace CodeDweller {
  36. #ifdef _WIN32
  37. char const FilePath::DirectorySeparator = '\\';
  38. #else
  39. char const FilePath::DirectorySeparator = '/';
  40. #endif
  41. bool FilePath::isAbsolute(std::string const &path) {
  42. #ifdef _WIN32
  43. return PathIsRelative(path.c_str());
  44. #else
  45. if (path.empty()) {
  46. return false;
  47. }
  48. return ('/' == path[0]);
  49. #endif
  50. }
  51. std::string FilePath::join(std::initializer_list<std::string> components) {
  52. std::string path;
  53. for (auto &component : components) {
  54. if (!path.empty() &&
  55. path.back() != FilePath::DirectorySeparator) {
  56. path += FilePath::DirectorySeparator;
  57. if (isAbsolute(component)) {
  58. throw std::invalid_argument("Attempted to use absolute path \"" +
  59. component + "\" where a relative path "
  60. "is required.");
  61. }
  62. }
  63. path += component;
  64. }
  65. if (!path.empty() &&
  66. path.back() == FilePath::DirectorySeparator) {
  67. path.pop_back();
  68. }
  69. return path;
  70. }
  71. FileReference::FileReference(std::string fileName) :
  72. name(fileName),
  73. modTimestamp(0),
  74. size_bytes(0),
  75. fileExists(false),
  76. fileIsDirectory(false) {
  77. refresh();
  78. }
  79. std::string FileReference::FileName() const {
  80. return name;
  81. }
  82. void FileReference::refresh() {
  83. reset();
  84. // Load info.
  85. struct stat statBuffer;
  86. int status = stat(name.c_str(), &statBuffer);
  87. if (-1 == status) {
  88. // File no longer exists.
  89. if (ENOENT == errno) {
  90. return;
  91. }
  92. // Something went wrong.
  93. throw std::runtime_error("Error updating status of file \"" +
  94. name + "\": " + getErrorText());
  95. }
  96. modTimestamp = statBuffer.st_mtime;
  97. size_bytes = statBuffer.st_size;
  98. fileExists = true;
  99. fileIsDirectory = S_ISDIR(statBuffer.st_mode);
  100. }
  101. void FileReference:: reset() {
  102. modTimestamp = 0;
  103. size_bytes = 0;
  104. fileExists = false;
  105. fileIsDirectory = false;
  106. path.clear();
  107. }
  108. time_t FileReference::ModTimestamp() const {
  109. return modTimestamp;
  110. }
  111. size_t FileReference::Size() const {
  112. return size_bytes;
  113. }
  114. std::string FileReference::FullPath() {
  115. if (!path.empty()) {
  116. return path;
  117. }
  118. if (!fileExists) {
  119. return "";
  120. }
  121. #ifdef _WIN32
  122. // Get the size of the full path name.
  123. DWORD nTchars = GetFullPathName(name.c_str(), 0, NULL, NULL);
  124. if (0 == nTchars) {
  125. throw std::runtime_error("Error getting full path length for \"" + name
  126. + "\": " + getErrorText());
  127. }
  128. size_t bufSize = nTchars * sizeof(TCHAR);
  129. TCHAR fullPath[bufSize];
  130. nTchars = GetFullPathName(name.c_str(), bufSize, fullPath, NULL);
  131. if (0 == nTchars) {
  132. throw std::runtime_error("Error getting full path for \"" + name
  133. + "\": " + getErrorText());
  134. }
  135. path.assign(fullPath);
  136. #else
  137. char *realPath = realpath(name.c_str(), NULL);
  138. if (NULL == realPath) {
  139. // Nothing to do if the file doesn't exist.
  140. if (ENOENT == errno) {
  141. reset();
  142. return "";
  143. }
  144. // Something went wrong.
  145. throw std::runtime_error("Error checking file \"" + name + "\": " +
  146. getErrorText());
  147. }
  148. path.assign(realPath);
  149. free(realPath);
  150. #endif
  151. return path;
  152. }
  153. bool FileReference::exists() const {
  154. return fileExists;
  155. }
  156. bool FileReference::isDirectory() const {
  157. return fileIsDirectory;
  158. }
  159. std::string FileReference::getErrorText() {
  160. #ifdef _WIN32
  161. LPVOID winMsgBuf;
  162. DWORD lastError = GetLastError();
  163. FormatMessage(
  164. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  165. FORMAT_MESSAGE_FROM_SYSTEM |
  166. FORMAT_MESSAGE_IGNORE_INSERTS,
  167. NULL,
  168. lastError,
  169. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  170. (char *) &winMsgBuf,
  171. 0, NULL );
  172. std::string errMsg((char *) winMsgBuf);
  173. LocalFree(winMsgBuf);
  174. return errMsg;
  175. #else
  176. return strerror(errno);
  177. #endif
  178. }
  179. DirectoryReference::DirectoryReference(std::string dirName,
  180. bool (*dirFilter)(std::string)) :
  181. name(dirName),
  182. filter(dirFilter) {
  183. refresh();
  184. }
  185. void DirectoryReference::refresh() {
  186. // Clear any entries in this object.
  187. this->clear();
  188. #ifdef _WIN32
  189. HANDLE hDirList;
  190. WIN32_FIND_DATA dirListData;
  191. std::string searchString = FilePath::join({name, "*"});
  192. hDirList = FindFirstFile(searchString.c_str(), &dirListData);
  193. if (INVALID_HANDLE_VALUE == hDirList) {
  194. throw std::runtime_error("Error getting file list for \"" + name +
  195. "\": " + FileReference::getErrorText());
  196. }
  197. std::string tempName;
  198. while (INVALID_HANDLE_VALUE != hDirList) {
  199. tempName = FilePath::join({name, dirListData.cFileName});
  200. if ( (0 == filter) || (*filter)(dirListData.cFileName)) {
  201. emplace_back(tempName);
  202. }
  203. if (!FindNextFile(hDirList, &dirListData)) {
  204. FindClose(hDirList);
  205. hDirList = INVALID_HANDLE_VALUE;
  206. }
  207. }
  208. #else
  209. // Get new list.
  210. struct dirent **entries;
  211. int nEntries = scandir(name.c_str(), &entries, 0, 0);
  212. if (nEntries < 0) {
  213. throw std::runtime_error("Error getting file list for \"" + name +
  214. "\": " + FileReference::getErrorText());
  215. }
  216. // Create the FileReference objects.
  217. while (nEntries--) {
  218. std::string tempName;
  219. tempName = FilePath::join({name, entries[nEntries]->d_name});
  220. if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) {
  221. emplace_back(tempName);
  222. }
  223. free(entries[nEntries]);
  224. }
  225. free(entries);
  226. #endif
  227. }
  228. }