FilesystemPosix.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Copyright (C) 2009-2020, 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. WalkDirectoryTreeCallback m_callback = nullptr;
  57. void* m_userData = nullptr;
  58. U32 m_prefixLen;
  59. Error m_err = {Error::NONE};
  60. };
  61. static thread_local WalkDirectoryTreeCallbackContext g_walkDirectoryTreeContext;
  62. static int walkDirectoryTreeCallback(const char* filepath, const struct stat* info, const int typeflag,
  63. struct FTW* pathinfo)
  64. {
  65. Bool isDir;
  66. Bool ignored = true;
  67. if(typeflag == FTW_F)
  68. {
  69. isDir = false;
  70. ignored = false;
  71. }
  72. else if(typeflag == FTW_D || typeflag == FTW_DP)
  73. {
  74. isDir = true;
  75. ignored = false;
  76. }
  77. if(!ignored)
  78. {
  79. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  80. ANKI_ASSERT(ctx.m_callback);
  81. if(ctx.m_err || strlen(filepath) <= ctx.m_prefixLen)
  82. {
  83. return 0;
  84. }
  85. ctx.m_err = ctx.m_callback(filepath + ctx.m_prefixLen, ctx.m_userData, isDir);
  86. }
  87. return 0;
  88. }
  89. Error walkDirectoryTree(const CString& dir, void* userData, WalkDirectoryTreeCallback callback)
  90. {
  91. ANKI_ASSERT(callback != nullptr);
  92. ANKI_ASSERT(dir.getLength() > 0);
  93. Error err = Error::NONE;
  94. // Compute how long it will be the prefix fts_read will add
  95. U32 prefixLen = dir.getLength();
  96. if(dir[prefixLen - 1] != '/')
  97. {
  98. ++prefixLen;
  99. }
  100. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  101. ctx.m_callback = callback;
  102. ctx.m_userData = userData;
  103. ctx.m_prefixLen = prefixLen;
  104. ctx.m_err = Error::NONE;
  105. const int result = nftw(dir.cstr(), walkDirectoryTreeCallback, USE_FDS, FTW_PHYS);
  106. if(result != 0)
  107. {
  108. ANKI_UTIL_LOGE("nftw() failed");
  109. err = Error::FUNCTION_FAILED;
  110. }
  111. else
  112. {
  113. err = ctx.m_err;
  114. }
  115. return err;
  116. }
  117. static Error removeDirectoryInternal(const CString& dirname, GenericMemoryPoolAllocator<U8>& alloc)
  118. {
  119. DIR* dir;
  120. struct dirent* entry;
  121. dir = opendir(dirname.cstr());
  122. if(dir == nullptr)
  123. {
  124. ANKI_UTIL_LOGE("opendir() failed");
  125. return Error::FUNCTION_FAILED;
  126. }
  127. while((entry = readdir(dir)) != nullptr)
  128. {
  129. if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
  130. {
  131. StringAuto path(alloc);
  132. path.sprintf("%s/%s", dirname.cstr(), entry->d_name);
  133. if(entry->d_type == DT_DIR)
  134. {
  135. Error err = removeDirectoryInternal(path.toCString(), alloc);
  136. if(err)
  137. {
  138. return err;
  139. }
  140. }
  141. else
  142. {
  143. remove(path.cstr());
  144. }
  145. }
  146. }
  147. closedir(dir);
  148. remove(dirname.cstr());
  149. return Error::NONE;
  150. }
  151. Error removeDirectory(const CString& dirname, GenericMemoryPoolAllocator<U8> alloc)
  152. {
  153. return removeDirectoryInternal(dirname, alloc);
  154. }
  155. Error createDirectory(const CString& dir)
  156. {
  157. if(directoryExists(dir))
  158. {
  159. return Error::NONE;
  160. }
  161. Error err = Error::NONE;
  162. if(mkdir(dir.cstr(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  163. {
  164. ANKI_UTIL_LOGE("%s : %s", strerror(errno), dir.cstr());
  165. err = Error::FUNCTION_FAILED;
  166. }
  167. return err;
  168. }
  169. Error getHomeDirectory(StringAuto& out)
  170. {
  171. const char* home = getenv("HOME");
  172. if(home == nullptr)
  173. {
  174. ANKI_UTIL_LOGE("HOME environment variable not set");
  175. return Error::FUNCTION_FAILED;
  176. }
  177. out.create(home);
  178. return Error::NONE;
  179. }
  180. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second)
  181. {
  182. struct stat buff;
  183. if(lstat(filename.cstr(), &buff))
  184. {
  185. ANKI_UTIL_LOGE("stat() failed");
  186. return Error::NONE;
  187. }
  188. const struct tm& t = *localtime(&buff.st_mtim.tv_sec);
  189. year = 1900 + t.tm_year;
  190. month = t.tm_mon + 1;
  191. day = t.tm_mday;
  192. hour = t.tm_hour;
  193. second = t.tm_sec;
  194. return Error::NONE;
  195. }
  196. } // end namespace anki