FilesystemPosix.cpp 5.2 KB

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