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

filesystem.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. FileReference::FileReference(std::string fileName) :
  37. name(fileName),
  38. modTimestamp(0),
  39. size_bytes(0),
  40. fileExists(false),
  41. fileIsDirectory(false) {
  42. refresh();
  43. }
  44. void FileReference::refresh() {
  45. reset();
  46. // Load info.
  47. struct stat statBuffer;
  48. int status = stat(name.c_str(), &statBuffer);
  49. if (-1 == status) {
  50. // File no longer exists.
  51. if (ENOENT == errno) {
  52. return;
  53. }
  54. // Something went wrong.
  55. throw std::runtime_error("Error updating status of file \"" +
  56. name + "\": " + getErrorText());
  57. }
  58. modTimestamp = statBuffer.st_mtime;
  59. size_bytes = statBuffer.st_size;
  60. fileExists = true;
  61. fileIsDirectory = S_ISDIR(statBuffer.st_mode);
  62. }
  63. void FileReference:: reset() {
  64. modTimestamp = 0;
  65. size_bytes = 0;
  66. fileExists = false;
  67. fileIsDirectory = false;
  68. path.clear();
  69. }
  70. time_t FileReference::ModTimestamp() const {
  71. return modTimestamp;
  72. }
  73. size_t FileReference::Size() const {
  74. return size_bytes;
  75. }
  76. std::string FileReference::FullPath() {
  77. if (!path.empty()) {
  78. return path;
  79. }
  80. if (!fileExists) {
  81. return "";
  82. }
  83. #ifdef _WIN32
  84. // Get the size of the full path name.
  85. DWORD nTchars = GetFullPathName(name.c_str(), 0, NULL, NULL);
  86. if (0 == nTchars) {
  87. throw std::runtime_error("Error getting full path length for \"" + name
  88. + "\": " + getErrorText());
  89. }
  90. size_t bufSize = nTchars * sizeof(TCHAR);
  91. TCHAR fullPath[bufSize];
  92. nTchars = GetFullPathName(name.c_str(), bufSize, fullPath, NULL);
  93. if (0 == nTchars) {
  94. throw std::runtime_error("Error getting full path for \"" + name
  95. + "\": " + getErrorText());
  96. }
  97. path.assign(fullPath);
  98. #else
  99. char *realPath = realpath(name.c_str(), NULL);
  100. if (NULL == realPath) {
  101. // Nothing to do if the file doesn't exist.
  102. if (ENOENT == errno) {
  103. reset();
  104. return "";
  105. }
  106. // Something went wrong.
  107. throw std::runtime_error("Error checking file \"" + name + "\": " +
  108. getErrorText());
  109. }
  110. path.assign(realPath);
  111. free(realPath);
  112. #endif
  113. return path;
  114. }
  115. bool FileReference::exists() const {
  116. return fileExists;
  117. }
  118. bool FileReference::isDirectory() const {
  119. return fileIsDirectory;
  120. }
  121. std::string FileReference::getErrorText() {
  122. #ifdef _WIN32
  123. LPVOID winMsgBuf;
  124. DWORD lastError = GetLastError();
  125. FormatMessage(
  126. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  127. FORMAT_MESSAGE_FROM_SYSTEM |
  128. FORMAT_MESSAGE_IGNORE_INSERTS,
  129. NULL,
  130. lastError,
  131. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  132. (char *) &winMsgBuf,
  133. 0, NULL );
  134. std::string errMsg((char *) winMsgBuf);
  135. LocalFree(winMsgBuf);
  136. return errMsg;
  137. #else
  138. return strerror(errno);
  139. #endif
  140. }
  141. DirectoryReference::DirectoryReference(std::string dirName,
  142. bool (*dirFilter)(std::string)) :
  143. name(dirName),
  144. filter(dirFilter) {
  145. refresh();
  146. }
  147. void DirectoryReference::refresh() {
  148. // Clear any entries in this object.
  149. this->clear();
  150. // Get new list.
  151. struct dirent **entries;
  152. int nEntries = scandir(name.c_str(), &entries, 0, 0);
  153. if (nEntries < 0) {
  154. throw std::runtime_error("Error getting file list for \"" + name +
  155. "\": " + FileReference::getErrorText());
  156. }
  157. // Create the FileReference objects.
  158. while (nEntries--) {
  159. std::string tempName;
  160. tempName = name + "/" + entries[nEntries]->d_name;
  161. if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) {
  162. emplace_back(tempName);
  163. }
  164. free(entries[nEntries]);
  165. }
  166. free(entries);
  167. }
  168. }