FilesystemPosix.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. // Some defines for extra posix features
  6. #define _XOPEN_SOURCE 700
  7. #define _LARGEFILE64_SOURCE
  8. #define _FILE_OFFSET_BITS 64
  9. #include <AnKi/Util/Filesystem.h>
  10. #include <AnKi/Util/Assert.h>
  11. #include <AnKi/Util/Thread.h>
  12. #include <cstring>
  13. #include <sys/stat.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <cerrno>
  17. #include <ftw.h> // For walkDirectoryTree
  18. #include <cstdlib>
  19. #include <time.h>
  20. #ifndef USE_FDS
  21. # define USE_FDS 15
  22. #endif
  23. // Define PATH_MAX if needed
  24. #ifndef PATH_MAX
  25. # define PATH_MAX 4096
  26. #endif
  27. namespace anki
  28. {
  29. Bool fileExists(const CString& filename)
  30. {
  31. struct stat s;
  32. if(stat(filename.cstr(), &s) == 0)
  33. {
  34. return S_ISREG(s.st_mode);
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. Bool directoryExists(const CString& filename)
  42. {
  43. struct stat s;
  44. if(stat(filename.cstr(), &s) == 0)
  45. {
  46. return S_ISDIR(s.st_mode);
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. }
  53. class WalkDirectoryTreeCallbackContext
  54. {
  55. public:
  56. const Function<Error(const CString&, Bool)>* m_callback = nullptr;
  57. U32 m_prefixLen;
  58. Error m_err = {Error::NONE};
  59. };
  60. static thread_local WalkDirectoryTreeCallbackContext g_walkDirectoryTreeContext;
  61. static int walkDirectoryTreeCallback(const char* filepath, const struct stat* info, const int typeflag,
  62. struct FTW* pathinfo)
  63. {
  64. Bool isDir;
  65. Bool ignored = true;
  66. if(typeflag == FTW_F)
  67. {
  68. isDir = false;
  69. ignored = false;
  70. }
  71. else if(typeflag == FTW_D || typeflag == FTW_DP)
  72. {
  73. isDir = true;
  74. ignored = false;
  75. }
  76. if(!ignored)
  77. {
  78. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  79. ANKI_ASSERT(ctx.m_callback);
  80. if(ctx.m_err || strlen(filepath) <= ctx.m_prefixLen)
  81. {
  82. return 0;
  83. }
  84. ctx.m_err = (*ctx.m_callback)(filepath + ctx.m_prefixLen, isDir);
  85. }
  86. return 0;
  87. }
  88. Error walkDirectoryTreeInternal(const CString& dir, const Function<Error(const CString&, Bool)>& callback)
  89. {
  90. ANKI_ASSERT(dir.getLength() > 0);
  91. Error err = Error::NONE;
  92. // Compute how long it will be the prefix fts_read will add
  93. U32 prefixLen = dir.getLength();
  94. if(dir[prefixLen - 1] != '/')
  95. {
  96. ++prefixLen;
  97. }
  98. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  99. ctx.m_callback = &callback;
  100. ctx.m_prefixLen = prefixLen;
  101. ctx.m_err = Error::NONE;
  102. const int result = nftw(dir.cstr(), walkDirectoryTreeCallback, USE_FDS, FTW_PHYS);
  103. if(result != 0)
  104. {
  105. ANKI_UTIL_LOGE("nftw() failed");
  106. err = Error::FUNCTION_FAILED;
  107. }
  108. else
  109. {
  110. err = ctx.m_err;
  111. }
  112. return err;
  113. }
  114. static Error removeDirectoryInternal(const CString& dirname, GenericMemoryPoolAllocator<U8>& alloc)
  115. {
  116. DIR* dir;
  117. struct dirent* entry;
  118. dir = opendir(dirname.cstr());
  119. if(dir == nullptr)
  120. {
  121. ANKI_UTIL_LOGE("opendir() failed");
  122. return Error::FUNCTION_FAILED;
  123. }
  124. while((entry = readdir(dir)) != nullptr)
  125. {
  126. if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
  127. {
  128. StringAuto path(alloc);
  129. path.sprintf("%s/%s", dirname.cstr(), entry->d_name);
  130. if(entry->d_type == DT_DIR)
  131. {
  132. Error err = removeDirectoryInternal(path.toCString(), alloc);
  133. if(err)
  134. {
  135. return err;
  136. }
  137. }
  138. else
  139. {
  140. remove(path.cstr());
  141. }
  142. }
  143. }
  144. closedir(dir);
  145. remove(dirname.cstr());
  146. return Error::NONE;
  147. }
  148. Error removeDirectory(const CString& dirname, GenericMemoryPoolAllocator<U8> alloc)
  149. {
  150. return removeDirectoryInternal(dirname, alloc);
  151. }
  152. Error createDirectory(const CString& dir)
  153. {
  154. if(directoryExists(dir))
  155. {
  156. return Error::NONE;
  157. }
  158. Error err = Error::NONE;
  159. if(mkdir(dir.cstr(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  160. {
  161. ANKI_UTIL_LOGE("%s : %s", strerror(errno), dir.cstr());
  162. err = Error::FUNCTION_FAILED;
  163. }
  164. return err;
  165. }
  166. Error getHomeDirectory(StringAuto& out)
  167. {
  168. const char* home = getenv("HOME");
  169. if(home == nullptr)
  170. {
  171. ANKI_UTIL_LOGE("HOME environment variable not set");
  172. return Error::FUNCTION_FAILED;
  173. }
  174. out.create(home);
  175. return Error::NONE;
  176. }
  177. Error getTempDirectory(StringAuto& out)
  178. {
  179. out.create("/tmp/");
  180. return Error::NONE;
  181. }
  182. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second)
  183. {
  184. struct stat buff;
  185. if(lstat(filename.cstr(), &buff))
  186. {
  187. ANKI_UTIL_LOGE("stat() failed");
  188. return Error::NONE;
  189. }
  190. struct tm t;
  191. localtime_r(&buff.st_mtim.tv_sec, &t);
  192. year = 1900 + t.tm_year;
  193. month = t.tm_mon + 1;
  194. day = t.tm_mday;
  195. hour = t.tm_hour;
  196. second = t.tm_sec;
  197. return Error::NONE;
  198. }
  199. } // end namespace anki