BsUnixFileSystem.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "FileSystem/BsFileSystem.h"
  4. #include "Error/BsException.h"
  5. #include "FileSystem/BsDataStream.h"
  6. #include "Debug/BsDebug.h"
  7. #include <dirent.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <unistd.h>
  12. #include <climits>
  13. #include <cstring>
  14. #include <cstdio>
  15. #include <cstdlib>
  16. #include <fstream>
  17. #define HANDLE_PATH_ERROR(path__, errno__) \
  18. LOGERR(String(__FUNCTION__) + ": " + (path__) + ": " + (strerror(errno__)));
  19. namespace bs
  20. {
  21. bool unix_pathExists(const String& path)
  22. {
  23. struct stat st_buf;
  24. if (stat(path.c_str(), &st_buf) == 0)
  25. return true;
  26. else
  27. if (errno == ENOENT) // No such file or directory
  28. return false;
  29. else
  30. {
  31. HANDLE_PATH_ERROR(path, errno);
  32. return false;
  33. }
  34. }
  35. bool unix_stat(const String& path, struct stat *st_buf)
  36. {
  37. if (stat(path.c_str(), st_buf) != 0)
  38. {
  39. HANDLE_PATH_ERROR(path, errno);
  40. return false;
  41. }
  42. return true;
  43. }
  44. bool unix_isFile(const String& path)
  45. {
  46. struct stat st_buf;
  47. if (unix_stat(path, &st_buf))
  48. return S_ISREG(st_buf.st_mode);
  49. return false;
  50. }
  51. bool unix_isDirectory(const String& path)
  52. {
  53. struct stat st_buf;
  54. if (unix_stat(path, &st_buf))
  55. return S_ISDIR(st_buf.st_mode);
  56. return false;
  57. }
  58. bool unix_createDirectory(const String& path)
  59. {
  60. if (unix_pathExists(path) && unix_isDirectory(path))
  61. return false;
  62. if (mkdir(path.c_str(), 0755))
  63. {
  64. HANDLE_PATH_ERROR(path, errno);
  65. return false;
  66. }
  67. return true;
  68. }
  69. void FileSystem::removeFile(const Path& path)
  70. {
  71. String pathStr = path.toString();
  72. if (unix_isDirectory(pathStr))
  73. {
  74. if (rmdir(pathStr.c_str()))
  75. HANDLE_PATH_ERROR(pathStr, errno);
  76. }
  77. else
  78. {
  79. if (unlink(pathStr.c_str()))
  80. HANDLE_PATH_ERROR(pathStr, errno);
  81. }
  82. }
  83. void FileSystem::copyFile(const Path& source, const Path& destination)
  84. {
  85. std::ifstream sourceStream(source.toString().c_str(), std::ios::binary);
  86. std::ofstream destinationStream(destination.toString().c_str(), std::ios::binary);
  87. destinationStream << sourceStream.rdbuf();
  88. sourceStream.close();
  89. destinationStream.close();
  90. }
  91. void FileSystem::moveFile(const Path& oldPath, const Path& newPath)
  92. {
  93. String oldPathStr = oldPath.toString();
  94. String newPathStr = newPath.toString();
  95. if (std::rename(oldPathStr.c_str(), newPathStr.c_str()) == -1)
  96. {
  97. // Cross-filesystem copy is likely needed (for example, /tmp to Banshee install dir while copying assets)
  98. std::ifstream src(oldPathStr.c_str(), std::ios::binary);
  99. std::ofstream dst(newPathStr.c_str(), std::ios::binary);
  100. dst << src.rdbuf(); // First, copy
  101. // Error handling
  102. src.close();
  103. if (!src)
  104. {
  105. LOGERR(String(__FUNCTION__) + ": renaming " + oldPathStr + " to " + newPathStr +
  106. ": " + strerror(errno));
  107. return; // Do not remove source if we failed!
  108. }
  109. // Then, remove source file (hopefully succeeds)
  110. if (std::remove(oldPathStr.c_str()) == -1)
  111. {
  112. LOGERR(String(__FUNCTION__) + ": renaming " + oldPathStr + " to " + newPathStr +
  113. ": " + strerror(errno));
  114. }
  115. }
  116. }
  117. SPtr<DataStream> FileSystem::openFile(const Path& path, bool readOnly)
  118. {
  119. String pathString = path.toString();
  120. DataStream::AccessMode accessMode = DataStream::READ;
  121. if (!readOnly)
  122. accessMode = (DataStream::AccessMode)((UINT32)accessMode | (UINT32)DataStream::WRITE);
  123. return bs_shared_ptr_new<FileDataStream>(path, accessMode, true);
  124. }
  125. SPtr<DataStream> FileSystem::createAndOpenFile(const Path& path)
  126. {
  127. return bs_shared_ptr_new<FileDataStream>(path, DataStream::AccessMode::WRITE, true);
  128. }
  129. UINT64 FileSystem::getFileSize(const Path& path)
  130. {
  131. struct stat st_buf;
  132. if (stat(path.toString().c_str(), &st_buf) == 0)
  133. {
  134. return (UINT64)st_buf.st_size;
  135. }
  136. else
  137. {
  138. HANDLE_PATH_ERROR(path.toString(), errno);
  139. return (UINT64)-1;
  140. }
  141. }
  142. bool FileSystem::exists(const Path& path)
  143. {
  144. return unix_pathExists(path.toString());
  145. }
  146. bool FileSystem::isFile(const Path& path)
  147. {
  148. String pathStr = path.toString();
  149. return unix_pathExists(pathStr) && unix_isFile(pathStr);
  150. }
  151. bool FileSystem::isDirectory(const Path& path)
  152. {
  153. String pathStr = path.toString();
  154. return unix_pathExists(pathStr) && unix_isDirectory(pathStr);
  155. }
  156. void FileSystem::createDir(const Path& path)
  157. {
  158. Path parentPath = path;
  159. while (!exists(parentPath) && parentPath.getNumDirectories() > 0)
  160. {
  161. parentPath = parentPath.getParent();
  162. }
  163. for (UINT32 i = parentPath.getNumDirectories(); i < path.getNumDirectories(); i++)
  164. {
  165. parentPath.append(path[i]);
  166. unix_createDirectory(parentPath.toString());
  167. }
  168. // Last "file" entry is also considered a directory
  169. if(!parentPath.equals(path))
  170. unix_createDirectory(path.toString());
  171. }
  172. void FileSystem::getChildren(const Path& dirPath, Vector<Path>& files, Vector<Path>& directories)
  173. {
  174. const String pathStr = dirPath.toString();
  175. if (unix_isFile(pathStr))
  176. return;
  177. DIR *dp = opendir(pathStr.c_str());
  178. if (dp == NULL)
  179. {
  180. HANDLE_PATH_ERROR(pathStr, errno);
  181. return;
  182. }
  183. struct dirent *ep;
  184. while ( (ep = readdir(dp)) )
  185. {
  186. const String filename(ep->d_name);
  187. if (filename != "." && filename != "..")
  188. {
  189. if (unix_isDirectory(pathStr + "/" + filename))
  190. directories.push_back(dirPath + (filename + "/"));
  191. else
  192. files.push_back(dirPath + filename);
  193. }
  194. }
  195. closedir(dp);
  196. }
  197. std::time_t FileSystem::getLastModifiedTime(const Path& path)
  198. {
  199. struct stat st_buf;
  200. stat(path.toString().c_str(), &st_buf);
  201. std::time_t time = st_buf.st_mtime;
  202. return time;
  203. }
  204. Path FileSystem::getWorkingDirectoryPath()
  205. {
  206. char *buffer = bs_newN<char>(PATH_MAX);
  207. String wd;
  208. if (getcwd(buffer, PATH_MAX) != nullptr)
  209. wd = buffer;
  210. else
  211. LOGERR(String("Error when calling getcwd(): ") + strerror(errno));
  212. bs_free(buffer);
  213. return Path(wd);
  214. }
  215. bool FileSystem::iterate(const Path& dirPath, std::function<bool(const Path&)> fileCallback,
  216. std::function<bool(const Path&)> dirCallback, bool recursive)
  217. {
  218. String pathStr = dirPath.toString();
  219. if (unix_isFile(pathStr))
  220. return false;
  221. DIR* dirHandle = opendir(pathStr.c_str());
  222. if (dirHandle == nullptr)
  223. {
  224. HANDLE_PATH_ERROR(pathStr, errno);
  225. return false;
  226. }
  227. dirent* entry;
  228. while((entry = readdir(dirHandle)))
  229. {
  230. String filename(entry->d_name);
  231. if (filename == "." || filename == "..")
  232. continue;
  233. Path fullPath = dirPath;
  234. if (unix_isDirectory(pathStr + "/" + filename))
  235. {
  236. Path childDir = fullPath.append(filename + "/");
  237. if (dirCallback != nullptr)
  238. {
  239. if (!dirCallback(childDir))
  240. {
  241. closedir(dirHandle);
  242. return false;
  243. }
  244. }
  245. if (recursive)
  246. {
  247. if (!iterate(childDir, fileCallback, dirCallback, recursive))
  248. {
  249. closedir(dirHandle);
  250. return false;
  251. }
  252. }
  253. }
  254. else
  255. {
  256. Path filePath = fullPath.append(filename);
  257. if (fileCallback != nullptr)
  258. {
  259. if (!fileCallback(filePath))
  260. {
  261. closedir(dirHandle);
  262. return false;
  263. }
  264. }
  265. }
  266. }
  267. closedir(dirHandle);
  268. return true;
  269. }
  270. Path FileSystem::getTempDirectoryPath()
  271. {
  272. String tmpdir;
  273. // Try different things:
  274. // 1) If defined, honor the TMPDIR environnement variable
  275. char *TMPDIR = getenv("TMPDIR");
  276. if (TMPDIR != NULL)
  277. tmpdir = TMPDIR;
  278. else
  279. {
  280. // 2) If defined, honor the P_tmpdir macro
  281. #ifdef P_tmpdir
  282. tmpdir = String(P_tmpdir);
  283. #else
  284. // 3) If everything else fails, simply default to /tmp
  285. tmpdir = String("/tmp");
  286. #endif
  287. }
  288. tmpdir.append("/banshee-XXXXXX");
  289. size_t len = tmpdir.size()+1;
  290. char *nameTemplate = bs_newN<char>(len);
  291. snprintf(nameTemplate, len, "%s", tmpdir.c_str());
  292. char *directoryName = mkdtemp(nameTemplate);
  293. if (directoryName == NULL)
  294. {
  295. LOGERR(String(__FUNCTION__) + ": " + strerror(errno));
  296. return Path(StringUtil::BLANK);
  297. }
  298. return Path(String(directoryName) + "/");
  299. }
  300. }