FilesystemPosix.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (C) 2009-present, 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. #include <unistd.h>
  21. #if ANKI_OS_ANDROID
  22. # include <android_native_app_glue.h>
  23. #endif
  24. #ifndef USE_FDS
  25. # define USE_FDS 15
  26. #endif
  27. // Define PATH_MAX if needed
  28. #ifndef PATH_MAX
  29. # define PATH_MAX 4096
  30. #endif
  31. namespace anki {
  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(WalkDirectoryArgs&)>* m_callback = nullptr;
  60. U32 m_prefixLen;
  61. Error m_error = Error::kNone;
  62. };
  63. static thread_local WalkDirectoryTreeCallbackContext g_walkDirectoryTreeContext;
  64. static int walkDirectoryTreeCallback(const char* filepath, [[maybe_unused]] const struct stat* info, const int typeflag,
  65. [[maybe_unused]] 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(strlen(filepath) <= ctx.m_prefixLen)
  84. {
  85. return 0;
  86. }
  87. WalkDirectoryArgs args;
  88. args.m_path = filepath + ctx.m_prefixLen;
  89. args.m_isDirectory = isDir;
  90. ctx.m_error = (*ctx.m_callback)(args);
  91. if(ctx.m_error || args.m_stopSearch)
  92. {
  93. return 666;
  94. }
  95. }
  96. return 0;
  97. }
  98. Error walkDirectoryTreeInternal(CString dir, const Function<Error(WalkDirectoryArgs& args)>& callback)
  99. {
  100. ANKI_ASSERT(dir.getLength() > 0);
  101. Error err = Error::kNone;
  102. // Compute how long it will be the prefix fts_read will add
  103. U32 prefixLen = dir.getLength();
  104. if(dir[prefixLen - 1] != '/')
  105. {
  106. ++prefixLen;
  107. }
  108. WalkDirectoryTreeCallbackContext& ctx = g_walkDirectoryTreeContext;
  109. ctx.m_callback = &callback;
  110. ctx.m_prefixLen = prefixLen;
  111. ctx.m_error = Error::kNone;
  112. const int result = nftw(dir.cstr(), walkDirectoryTreeCallback, USE_FDS, FTW_PHYS);
  113. if(result != 0 && result != 666)
  114. {
  115. ANKI_UTIL_LOGE("nftw() failed");
  116. err = Error::kFunctionFailed;
  117. }
  118. else
  119. {
  120. err = ctx.m_error;
  121. }
  122. return err;
  123. }
  124. static Error removeDirectoryInternal(const CString& dirname)
  125. {
  126. DIR* dir;
  127. struct dirent* entry;
  128. dir = opendir(dirname.cstr());
  129. if(dir == nullptr)
  130. {
  131. ANKI_UTIL_LOGE("opendir() failed");
  132. return Error::kFunctionFailed;
  133. }
  134. while((entry = readdir(dir)) != nullptr)
  135. {
  136. if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
  137. {
  138. String path;
  139. path.sprintf("%s/%s", dirname.cstr(), entry->d_name);
  140. if(entry->d_type == DT_DIR)
  141. {
  142. Error err = removeDirectoryInternal(path.toCString());
  143. if(err)
  144. {
  145. return err;
  146. }
  147. }
  148. else
  149. {
  150. remove(path.cstr());
  151. }
  152. }
  153. }
  154. closedir(dir);
  155. remove(dirname.cstr());
  156. return Error::kNone;
  157. }
  158. Error removeDirectory(const CString& dirname)
  159. {
  160. return removeDirectoryInternal(dirname);
  161. }
  162. Error createDirectory(const CString& dir)
  163. {
  164. if(directoryExists(dir))
  165. {
  166. return Error::kNone;
  167. }
  168. Error err = Error::kNone;
  169. if(mkdir(dir.cstr(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
  170. {
  171. ANKI_UTIL_LOGE("%s : %s", strerror(errno), dir.cstr());
  172. err = Error::kFunctionFailed;
  173. }
  174. return err;
  175. }
  176. Error getHomeDirectory(String& out)
  177. {
  178. #if ANKI_OS_LINUX
  179. const char* home = getenv("HOME");
  180. if(home == nullptr)
  181. {
  182. ANKI_UTIL_LOGE("HOME environment variable not set");
  183. return Error::kFunctionFailed;
  184. }
  185. out = home;
  186. #else
  187. out = g_androidApp->activity->internalDataPath;
  188. #endif
  189. return Error::kNone;
  190. }
  191. Error getTempDirectory(String& out)
  192. {
  193. #if ANKI_OS_LINUX
  194. out = "/tmp/";
  195. #else
  196. out = g_androidApp->activity->internalDataPath;
  197. #endif
  198. return Error::kNone;
  199. }
  200. Error getFileModificationTime(CString filename, U32& year, U32& month, U32& day, U32& hour, U32& min, U32& second)
  201. {
  202. struct stat buff;
  203. if(lstat(filename.cstr(), &buff))
  204. {
  205. ANKI_UTIL_LOGE("stat() failed");
  206. return Error::kNone;
  207. }
  208. struct tm t;
  209. localtime_r(&buff.st_mtim.tv_sec, &t);
  210. year = 1900 + t.tm_year;
  211. month = t.tm_mon + 1;
  212. day = t.tm_mday;
  213. hour = t.tm_hour;
  214. min = t.tm_min;
  215. second = t.tm_sec;
  216. return Error::kNone;
  217. }
  218. Error getApplicationPath(String& out)
  219. {
  220. #if ANKI_OS_ANDROID
  221. ANKI_ASSERT(0 && "getApplicationPath() doesn't work on Android");
  222. (void)out;
  223. #else
  224. DynamicArray<Char> buff;
  225. buff.resize(1024);
  226. const ssize_t result = readlink("/proc/self/exe", &buff[0], buff.getSize());
  227. if(result < 0)
  228. {
  229. ANKI_UTIL_LOGE("readlink() failed");
  230. return Error::kFunctionFailed;
  231. }
  232. out = String('0', result);
  233. memcpy(&out[0], &buff[0], result);
  234. #endif
  235. return Error::kNone;
  236. }
  237. } // end namespace anki