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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <unistd.h>
  26. #include <sys/types.h>
  27. #include <cstring>
  28. #include <cerrno>
  29. #endif
  30. #include <sys/stat.h>
  31. #include <stdexcept>
  32. #include "filesystem.hpp"
  33. namespace CodeDweller {
  34. FileReference::FileReference(std::string fileName) :
  35. name(fileName),
  36. modTimestamp(0),
  37. size_bytes(0),
  38. fileExists(false),
  39. fileIsDirectory(false) {
  40. refresh();
  41. }
  42. void FileReference::refresh() {
  43. reset();
  44. // Load info.
  45. struct stat statBuffer;
  46. int status = stat(name.c_str(), &statBuffer);
  47. if (-1 == status) {
  48. // File no longer exists.
  49. if (ENOENT == errno) {
  50. return;
  51. }
  52. // Something went wrong.
  53. throw std::runtime_error("Error updating status of file \"" +
  54. name + "\": " + getErrorText());
  55. }
  56. modTimestamp = statBuffer.st_mtime;
  57. size_bytes = statBuffer.st_size;
  58. fileExists = true;
  59. fileIsDirectory = S_ISDIR(statBuffer.st_mode);
  60. }
  61. void FileReference:: reset() {
  62. modTimestamp = 0;
  63. size_bytes = 0;
  64. fileExists = false;
  65. fileIsDirectory = false;
  66. path.clear();
  67. }
  68. time_t FileReference::ModTimestamp() const {
  69. return modTimestamp;
  70. }
  71. size_t FileReference::Size() const {
  72. return size_bytes;
  73. }
  74. std::string FileReference::FullPath() {
  75. if (!path.empty()) {
  76. return path;
  77. }
  78. if (!fileExists) {
  79. return "";
  80. }
  81. #ifdef _WIN32
  82. // Get the size of the full path name.
  83. DWORD nTchars = GetFullPathName(name.c_str(), 0, NULL, NULL);
  84. if (0 == nTchars) {
  85. throw std::runtime_error("Error getting full path length for \"" + name
  86. + "\": " + getErrorText());
  87. }
  88. size_t bufSize = nTchars * sizeof(TCHAR);
  89. TCHAR fullPath[bufSize];
  90. nTchars = GetFullPathName(name.c_str(), bufSize, fullPath, NULL);
  91. if (0 == nTchars) {
  92. throw std::runtime_error("Error getting full path for \"" + name
  93. + "\": " + getErrorText());
  94. }
  95. path.assign(fullPath);
  96. #else
  97. char *realPath = realpath(name.c_str(), NULL);
  98. if (NULL == realPath) {
  99. // Nothing to do if the file doesn't exist.
  100. if (ENOENT == errno) {
  101. reset();
  102. return "";
  103. }
  104. // Something went wrong.
  105. throw std::runtime_error("Error checking file \"" + name + "\": " +
  106. getErrorText());
  107. }
  108. path.assign(realPath);
  109. free(realPath);
  110. #endif
  111. return path;
  112. }
  113. bool FileReference::exists() const {
  114. return fileExists;
  115. }
  116. bool FileReference::isDirectory() const {
  117. return fileIsDirectory;
  118. }
  119. std::string FileReference::getErrorText() {
  120. #ifdef _WIN32
  121. LPVOID winMsgBuf;
  122. DWORD lastError = GetLastError();
  123. FormatMessage(
  124. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  125. FORMAT_MESSAGE_FROM_SYSTEM |
  126. FORMAT_MESSAGE_IGNORE_INSERTS,
  127. NULL,
  128. lastError,
  129. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  130. (char *) &winMsgBuf,
  131. 0, NULL );
  132. std::string errMsg((char *) winMsgBuf);
  133. LocalFree(winMsgBuf);
  134. return errMsg;
  135. #else
  136. return strerror(errno);
  137. #endif
  138. }
  139. }