Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

filesystem.cpp 3.5KB

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