FilesystemPosix.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {
  32. Bool fileExists(const CString& filename)
  33. {
  34. struct stat s;
  35. if(stat(filename.cstr(), &s) == 0)
  36. {
  37. return S_ISREG(s.st_mode);
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. Bool directoryExists(const CString& filename)
  45. {
  46. struct stat s;
  47. if(stat(filename.cstr(), &s) == 0)
  48. {
  49. return S_ISDIR(s.st_mode);
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56. class WalkDirectoryTreeCallbackContext
  57. {
  58. public:
  59. const Function<Error(const CString&, Bool)>* m_callback = nullptr;
  60. U32 m_prefixLen;
  61. Error m_err = {Error::NONE};
  62. };
  63. static thread_local WalkDirectoryTreeCallbackContext g_walkDirectoryTreeContext;
  64. static int walkDirectoryTreeCallback(const char* filepath, const struct stat* info, const int typeflag,
  65. struct FTW* pathinfo)
  66. {
  67. Bool isDir;
  68. Bool ignored = true;
  69. if(typeflag == FTW_F)
  70. {
  71. isDir = false;
  72. ignored = false;
  73. }
  74. else if(typeflag == FTW_D || typeflag == FTW_DP)
  75. {
  76. isDir = true;
  77. ignored = false;
  78. }
  79. if(!ignored)
  80. {
  81. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  82. ANKI_ASSERT(ctx.m_callback);
  83. if(ctx.m_err || strlen(filepath) <= ctx.m_prefixLen)
  84. {
  85. return 0;
  86. }
  87. ctx.m_err = (*ctx.m_callback)(filepath + ctx.m_prefixLen, isDir);
  88. }
  89. return 0;
  90. }
  91. Error walkDirectoryTreeInternal(const CString& dir, const Function<Error(const CString&, Bool)>& callback)
  92. {
  93. ANKI_ASSERT(dir.getLength() > 0);
  94. Error err = Error::NONE;
  95. // Compute how long it will be the prefix fts_read will add
  96. U32 prefixLen = dir.getLength();
  97. if(dir[prefixLen - 1] != '/')
  98. {
  99. ++prefixLen;
  100. }
  101. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  102. ctx.m_callback = &callback;
  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. #if ANKI_OS_LINUX
  172. const char* home = getenv("HOME");
  173. if(home == nullptr)
  174. {
  175. ANKI_UTIL_LOGE("HOME environment variable not set");
  176. return Error::FUNCTION_FAILED;
  177. }
  178. out.create(home);
  179. #else
  180. out.create(g_androidApp->activity->internalDataPath);
  181. #endif
  182. return Error::NONE;
  183. }
  184. Error getTempDirectory(StringAuto& out)
  185. {
  186. #if ANKI_OS_LINUX
  187. out.create("/tmp/");
  188. #else
  189. out.create(g_androidApp->activity->internalDataPath);
  190. #endif
  191. return Error::NONE;
  192. }
  193. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second)
  194. {
  195. struct stat buff;
  196. if(lstat(filename.cstr(), &buff))
  197. {
  198. ANKI_UTIL_LOGE("stat() failed");
  199. return Error::NONE;
  200. }
  201. struct tm t;
  202. localtime_r(&buff.st_mtim.tv_sec, &t);
  203. year = 1900 + t.tm_year;
  204. month = t.tm_mon + 1;
  205. day = t.tm_mday;
  206. hour = t.tm_hour;
  207. second = t.tm_sec;
  208. return Error::NONE;
  209. }
  210. } // end namespace anki