FilesystemPosix.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #if ANKI_OS_ANDROID
  21. # include <android_native_app_glue.h>
  22. #endif
  23. #ifndef USE_FDS
  24. # define USE_FDS 15
  25. #endif
  26. // Define PATH_MAX if needed
  27. #ifndef PATH_MAX
  28. # define PATH_MAX 4096
  29. #endif
  30. namespace anki {
  31. Bool fileExists(const CString& filename)
  32. {
  33. struct stat s;
  34. if(stat(filename.cstr(), &s) == 0)
  35. {
  36. return S_ISREG(s.st_mode);
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. Bool directoryExists(const CString& filename)
  44. {
  45. struct stat s;
  46. if(stat(filename.cstr(), &s) == 0)
  47. {
  48. return S_ISDIR(s.st_mode);
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55. class WalkDirectoryTreeCallbackContext
  56. {
  57. public:
  58. const Function<Error(const CString&, Bool)>* m_callback = nullptr;
  59. U32 m_prefixLen;
  60. Error m_err = {Error::NONE};
  61. };
  62. static thread_local WalkDirectoryTreeCallbackContext g_walkDirectoryTreeContext;
  63. static int walkDirectoryTreeCallback(const char* filepath, const struct stat* info, const int typeflag,
  64. struct FTW* pathinfo)
  65. {
  66. Bool isDir;
  67. Bool ignored = true;
  68. if(typeflag == FTW_F)
  69. {
  70. isDir = false;
  71. ignored = false;
  72. }
  73. else if(typeflag == FTW_D || typeflag == FTW_DP)
  74. {
  75. isDir = true;
  76. ignored = false;
  77. }
  78. if(!ignored)
  79. {
  80. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  81. ANKI_ASSERT(ctx.m_callback);
  82. if(ctx.m_err || strlen(filepath) <= ctx.m_prefixLen)
  83. {
  84. return 0;
  85. }
  86. ctx.m_err = (*ctx.m_callback)(filepath + ctx.m_prefixLen, isDir);
  87. }
  88. return 0;
  89. }
  90. Error walkDirectoryTreeInternal(const CString& dir, const Function<Error(const CString&, Bool)>& callback)
  91. {
  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_prefixLen = prefixLen;
  103. ctx.m_err = Error::NONE;
  104. const int result = nftw(dir.cstr(), walkDirectoryTreeCallback, USE_FDS, FTW_PHYS);
  105. if(result != 0)
  106. {
  107. ANKI_UTIL_LOGE("nftw() failed");
  108. err = Error::FUNCTION_FAILED;
  109. }
  110. else
  111. {
  112. err = ctx.m_err;
  113. }
  114. return err;
  115. }
  116. static Error removeDirectoryInternal(const CString& dirname, GenericMemoryPoolAllocator<U8>& alloc)
  117. {
  118. DIR* dir;
  119. struct dirent* entry;
  120. dir = opendir(dirname.cstr());
  121. if(dir == nullptr)
  122. {
  123. ANKI_UTIL_LOGE("opendir() failed");
  124. return Error::FUNCTION_FAILED;
  125. }
  126. while((entry = readdir(dir)) != nullptr)
  127. {
  128. if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
  129. {
  130. StringAuto path(alloc);
  131. path.sprintf("%s/%s", dirname.cstr(), entry->d_name);
  132. if(entry->d_type == DT_DIR)
  133. {
  134. Error err = removeDirectoryInternal(path.toCString(), alloc);
  135. if(err)
  136. {
  137. return err;
  138. }
  139. }
  140. else
  141. {
  142. remove(path.cstr());
  143. }
  144. }
  145. }
  146. closedir(dir);
  147. remove(dirname.cstr());
  148. return Error::NONE;
  149. }
  150. Error removeDirectory(const CString& dirname, GenericMemoryPoolAllocator<U8> alloc)
  151. {
  152. return removeDirectoryInternal(dirname, alloc);
  153. }
  154. Error createDirectory(const CString& dir)
  155. {
  156. if(directoryExists(dir))
  157. {
  158. return Error::NONE;
  159. }
  160. Error err = Error::NONE;
  161. if(mkdir(dir.cstr(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  162. {
  163. ANKI_UTIL_LOGE("%s : %s", strerror(errno), dir.cstr());
  164. err = Error::FUNCTION_FAILED;
  165. }
  166. return err;
  167. }
  168. Error getHomeDirectory(StringAuto& out)
  169. {
  170. #if ANKI_OS_LINUX
  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. #else
  179. out.create(g_androidApp->activity->internalDataPath);
  180. #endif
  181. return Error::NONE;
  182. }
  183. Error getTempDirectory(StringAuto& out)
  184. {
  185. #if ANKI_OS_LINUX
  186. out.create("/tmp/");
  187. #else
  188. out.create(g_androidApp->activity->internalDataPath);
  189. #endif
  190. return Error::NONE;
  191. }
  192. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second)
  193. {
  194. struct stat buff;
  195. if(lstat(filename.cstr(), &buff))
  196. {
  197. ANKI_UTIL_LOGE("stat() failed");
  198. return Error::NONE;
  199. }
  200. struct tm t;
  201. localtime_r(&buff.st_mtim.tv_sec, &t);
  202. year = 1900 + t.tm_year;
  203. month = t.tm_mon + 1;
  204. day = t.tm_mday;
  205. hour = t.tm_hour;
  206. second = t.tm_sec;
  207. return Error::NONE;
  208. }
  209. } // end namespace anki