Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <stdexcept>
  23. #include "filesystem.hpp"
  24. namespace CodeDweller {
  25. FileReference::FileReference(std::string fileName) :
  26. name(fileName),
  27. timestamp(0),
  28. size_bytes(0),
  29. fileExists(false),
  30. fileIsDirectory(false) {
  31. refresh();
  32. }
  33. void FileReference::refresh() {
  34. // Load path if necessary.
  35. if (path.empty()) {
  36. char *realPath = realpath(name.c_str(), NULL);
  37. if (NULL == path) {
  38. // Nothing to do if the file doesn't exist.
  39. if (ENOENT == errno) {
  40. return;
  41. }
  42. // Something went wrong.
  43. throw std::runtime_error("Error checking file \"" + name "\": " +
  44. getErrorText());
  45. }
  46. path = *realPath;
  47. free(realPath);
  48. }
  49. // Load info.
  50. struct stat statBuffer;
  51. int status = stat(path.c_str(), &statBuffer);
  52. if (-1 == status) {
  53. // File no longer exists.
  54. if (ENOENT == errno) {
  55. reset();
  56. return;
  57. }
  58. // Something went wrong.
  59. throw std::runtime_error("Error updating status of file \"" +
  60. path "\": " + getErrorText());
  61. }
  62. modTimestamp = statBuffer.st_mtime;
  63. size_bytes = statBuffer.st_size;
  64. fileExists = true;
  65. fileIsDirectory = S_ISDIR(statBuffer.st_mode);
  66. }
  67. void FileReference:: reset() {
  68. modTimestamp = 0;
  69. size_bytes = 0;
  70. fileExists = false;
  71. fileIsDirectory = false;
  72. }
  73. time_t FileReference::ModTimestamp() const {
  74. return modTimestamp;
  75. }
  76. long FileReference::Size() const {
  77. return size_bytes;
  78. }
  79. std::string FileReference::FullPath() const {
  80. return path;
  81. }
  82. time_t FileReference::exists() const {
  83. return fileExists;
  84. }
  85. time_t FileReference::isDirectory() const {
  86. return fileIsDirectory;
  87. }
  88. std::string FileReference::getErrorText() {
  89. #ifdef _WIN32
  90. LPVOID winMsgBuf;
  91. DWORD lastError = GetLastError();
  92. FormatMessage(
  93. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  94. FORMAT_MESSAGE_FROM_SYSTEM |
  95. FORMAT_MESSAGE_IGNORE_INSERTS,
  96. NULL,
  97. lastError,
  98. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  99. (char *) &winMsgBuf,
  100. 0, NULL );
  101. std::string errMsg((char *) winMsgBuf);
  102. LocalFree(winMsgBuf);
  103. return errMsg;
  104. #else
  105. return strerror(errno);
  106. #endif
  107. }
  108. }