Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

filesystem.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include <Shlwapi.h>
  25. #else
  26. #include <dirent.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <cstdio>
  30. #include <cstdlib>
  31. #include <cstring>
  32. #include <cerrno>
  33. #endif
  34. #include <sys/stat.h>
  35. #include <stdexcept>
  36. #include "filesystem.hpp"
  37. namespace CodeDweller {
  38. void FileOps::moveFile(std::string const &from, std::string const &to) {
  39. #ifdef _WIN32
  40. if (MoveFileEx(from.c_str(), to.c_str(), MOVEFILE_REPLACE_EXISTING) == 0) {
  41. #else
  42. if (rename(from.c_str(), to.c_str()) != 0) {
  43. #endif
  44. throw std::runtime_error("Error moving file \"" + from +
  45. "\" to file \"" + to + "\": " +
  46. FileReference::getErrorText());
  47. }
  48. }
  49. #ifdef _WIN32
  50. char const FilePath::DirectorySeparator = '\\';
  51. #else
  52. char const FilePath::DirectorySeparator = '/';
  53. #endif
  54. bool FilePath::isAbsolute(std::string const &path) {
  55. #ifdef _WIN32
  56. return !PathIsRelative(path.c_str());
  57. #else
  58. if (path.empty()) {
  59. return false;
  60. }
  61. return ('/' == path[0]);
  62. #endif
  63. }
  64. std::string FilePath::join(std::initializer_list<std::string> components) {
  65. std::string path;
  66. for (auto &component : components) {
  67. if (!path.empty() &&
  68. path.back() != FilePath::DirectorySeparator) {
  69. path += FilePath::DirectorySeparator;
  70. if (isAbsolute(component)) {
  71. throw std::invalid_argument("Attempted to use absolute path \"" +
  72. component + "\" where a relative path "
  73. "is required.");
  74. }
  75. }
  76. path += component;
  77. }
  78. if (!path.empty() &&
  79. path.back() == FilePath::DirectorySeparator) {
  80. path.pop_back();
  81. }
  82. return path;
  83. }
  84. FileReference::FileReference(std::string fileName) :
  85. name(fileName),
  86. modTimestamp(0),
  87. size_bytes(0),
  88. fileExists(false),
  89. fileIsDirectory(false) {
  90. refresh();
  91. }
  92. std::string FileReference::FileName() const {
  93. return name;
  94. }
  95. void FileReference::refresh() {
  96. reset();
  97. // Load info.
  98. struct stat statBuffer;
  99. int status = stat(name.c_str(), &statBuffer);
  100. if (-1 == status) {
  101. // File no longer exists.
  102. if (ENOENT == errno) {
  103. return;
  104. }
  105. // Something went wrong.
  106. throw std::runtime_error("Error updating status of file \"" +
  107. name + "\": " + getErrorText());
  108. }
  109. modTimestamp = statBuffer.st_mtime;
  110. size_bytes = statBuffer.st_size;
  111. fileExists = true;
  112. fileIsDirectory = S_ISDIR(statBuffer.st_mode);
  113. }
  114. void FileReference:: reset() {
  115. modTimestamp = 0;
  116. size_bytes = 0;
  117. fileExists = false;
  118. fileIsDirectory = false;
  119. path.clear();
  120. }
  121. time_t FileReference::ModTimestamp() const {
  122. return modTimestamp;
  123. }
  124. size_t FileReference::Size() const {
  125. return size_bytes;
  126. }
  127. std::string FileReference::FullPath() {
  128. if (!path.empty()) {
  129. return path;
  130. }
  131. if (!fileExists) {
  132. return "";
  133. }
  134. #ifdef _WIN32
  135. // Get the size of the full path name.
  136. DWORD nTchars = GetFullPathName(name.c_str(), 0, NULL, NULL);
  137. if (0 == nTchars) {
  138. throw std::runtime_error("Error getting full path length for \"" + name
  139. + "\": " + getErrorText());
  140. }
  141. size_t bufSize = nTchars * sizeof(TCHAR);
  142. TCHAR fullPath[bufSize];
  143. nTchars = GetFullPathName(name.c_str(), bufSize, fullPath, NULL);
  144. if (0 == nTchars) {
  145. throw std::runtime_error("Error getting full path for \"" + name
  146. + "\": " + getErrorText());
  147. }
  148. path.assign(fullPath);
  149. #else
  150. char *realPath = realpath(name.c_str(), NULL);
  151. if (NULL == realPath) {
  152. // Nothing to do if the file doesn't exist.
  153. if (ENOENT == errno) {
  154. reset();
  155. return "";
  156. }
  157. // Something went wrong.
  158. throw std::runtime_error("Error checking file \"" + name + "\": " +
  159. getErrorText());
  160. }
  161. path.assign(realPath);
  162. free(realPath);
  163. #endif
  164. return path;
  165. }
  166. bool FileReference::exists() const {
  167. return fileExists;
  168. }
  169. bool FileReference::isDirectory() const {
  170. return fileIsDirectory;
  171. }
  172. std::string FileReference::getErrorText() {
  173. #ifdef _WIN32
  174. LPVOID winMsgBuf;
  175. DWORD lastError = GetLastError();
  176. FormatMessage(
  177. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  178. FORMAT_MESSAGE_FROM_SYSTEM |
  179. FORMAT_MESSAGE_IGNORE_INSERTS,
  180. NULL,
  181. lastError,
  182. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  183. (char *) &winMsgBuf,
  184. 0, NULL );
  185. std::string errMsg((char *) winMsgBuf);
  186. LocalFree(winMsgBuf);
  187. return errMsg;
  188. #else
  189. return strerror(errno);
  190. #endif
  191. }
  192. DirectoryReference::DirectoryReference(std::string dirName,
  193. bool (*dirFilter)(std::string)) :
  194. name(dirName),
  195. filter(dirFilter) {
  196. refresh();
  197. }
  198. void DirectoryReference::refresh() {
  199. // Clear any entries in this object.
  200. this->clear();
  201. #ifdef _WIN32
  202. HANDLE hDirList;
  203. WIN32_FIND_DATA dirListData;
  204. std::string searchString = FilePath::join({name, "*"});
  205. hDirList = FindFirstFile(searchString.c_str(), &dirListData);
  206. if (INVALID_HANDLE_VALUE == hDirList) {
  207. throw std::runtime_error("Error getting file list for \"" + name +
  208. "\": " + FileReference::getErrorText());
  209. }
  210. std::string tempName;
  211. while (INVALID_HANDLE_VALUE != hDirList) {
  212. tempName = FilePath::join({name, dirListData.cFileName});
  213. if ( (0 == filter) || (*filter)(dirListData.cFileName)) {
  214. emplace_back(tempName);
  215. }
  216. if (!FindNextFile(hDirList, &dirListData)) {
  217. FindClose(hDirList);
  218. hDirList = INVALID_HANDLE_VALUE;
  219. }
  220. }
  221. #else
  222. // Get new list.
  223. struct dirent **entries;
  224. int nEntries = scandir(name.c_str(), &entries, 0, 0);
  225. if (nEntries < 0) {
  226. throw std::runtime_error("Error getting file list for \"" + name +
  227. "\": " + FileReference::getErrorText());
  228. }
  229. // Create the FileReference objects.
  230. while (nEntries--) {
  231. std::string tempName;
  232. tempName = FilePath::join({name, entries[nEntries]->d_name});
  233. if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) {
  234. emplace_back(tempName);
  235. }
  236. free(entries[nEntries]);
  237. }
  238. free(entries);
  239. #endif
  240. }
  241. }