BsUnixFileSystem.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. LOGERR(String(__FUNCTION__) + ": renaming " + oldPathStr + " to " + newPathStr +
  98. ": " + strerror(errno));
  99. }
  100. }
  101. SPtr<DataStream> FileSystem::openFile(const Path& path, bool readOnly)
  102. {
  103. String pathString = path.toString();
  104. DataStream::AccessMode accessMode = DataStream::READ;
  105. if (!readOnly)
  106. accessMode = (DataStream::AccessMode)(accessMode | (UINT32)DataStream::WRITE);
  107. return bs_shared_ptr_new<FileDataStream>(path, accessMode, true);
  108. }
  109. SPtr<DataStream> FileSystem::createAndOpenFile(const Path& path)
  110. {
  111. return bs_shared_ptr_new<FileDataStream>(path, DataStream::AccessMode::WRITE, true);
  112. }
  113. UINT64 FileSystem::getFileSize(const Path& path)
  114. {
  115. struct stat st_buf;
  116. if (stat(path.toString().c_str(), &st_buf) == 0)
  117. {
  118. return st_buf.st_size;
  119. }
  120. else
  121. {
  122. HANDLE_PATH_ERROR(path.toString(), errno);
  123. return -1;
  124. }
  125. }
  126. bool FileSystem::exists(const Path& path)
  127. {
  128. return unix_pathExists(path.toString());
  129. }
  130. bool FileSystem::isFile(const Path& path)
  131. {
  132. String pathStr = path.toString();
  133. return unix_pathExists(pathStr) && unix_isFile(pathStr);
  134. }
  135. bool FileSystem::isDirectory(const Path& path)
  136. {
  137. String pathStr = path.toString();
  138. return unix_pathExists(pathStr) && unix_isDirectory(pathStr);
  139. }
  140. void FileSystem::createDir(const Path& path)
  141. {
  142. Path parentPath = path;
  143. while (!exists(parentPath) && parentPath.getNumDirectories() > 0)
  144. {
  145. parentPath = parentPath.getParent();
  146. }
  147. for (UINT32 i = parentPath.getNumDirectories(); i < path.getNumDirectories(); i++)
  148. {
  149. parentPath.append(path[i]);
  150. unix_createDirectory(parentPath.toString());
  151. }
  152. }
  153. void FileSystem::getChildren(const Path& dirPath, Vector<Path>& files, Vector<Path>& directories)
  154. {
  155. const String pathStr = dirPath.toString();
  156. if (unix_isFile(pathStr))
  157. return;
  158. DIR *dp = opendir(pathStr.c_str());
  159. if (dp == NULL)
  160. {
  161. HANDLE_PATH_ERROR(pathStr, errno);
  162. return;
  163. }
  164. struct dirent *ep;
  165. while ( (ep = readdir(dp)) )
  166. {
  167. const String filename(ep->d_name);
  168. if (filename != "." && filename != "..")
  169. {
  170. if (unix_isDirectory(pathStr + "/" + filename))
  171. directories.push_back(dirPath + (filename + "/"));
  172. else
  173. files.push_back(dirPath + filename);
  174. }
  175. }
  176. closedir(dp);
  177. }
  178. std::time_t FileSystem::getLastModifiedTime(const Path& path)
  179. {
  180. struct stat st_buf;
  181. stat(path.toString().c_str(), &st_buf);
  182. std::time_t time = st_buf.st_mtime;
  183. return time;
  184. }
  185. Path FileSystem::getWorkingDirectoryPath()
  186. {
  187. char *buffer = bs_newN<char>(PATH_MAX);
  188. String wd;
  189. if (getcwd(buffer, PATH_MAX) != NULL)
  190. wd = buffer;
  191. bs_free(buffer);
  192. const int error = errno;
  193. if (error)
  194. LOGERR(String("Error when calling getcwd(): ") + strerror(error));
  195. return Path(wd);
  196. }
  197. bool FileSystem::iterate(const Path& dirPath, std::function<bool(const Path&)> fileCallback,
  198. std::function<bool(const Path&)> dirCallback, bool recursive)
  199. {
  200. BS_ASSERT(!"TODOPORT: implement FileSystem::iterate()");
  201. return true;
  202. }
  203. Path FileSystem::getTempDirectoryPath()
  204. {
  205. String tmpdir;
  206. // Try different things:
  207. // 1) If defined, honor the TMPDIR environnement variable
  208. char *TMPDIR = getenv("TMPDIR");
  209. if (TMPDIR != NULL)
  210. tmpdir = TMPDIR;
  211. else
  212. {
  213. // 2) If defined, honor the P_tmpdir macro
  214. #ifdef P_tmpdir
  215. tmpdir = String(P_tmpdir);
  216. #else
  217. // 3) If everything else fails, simply default to /tmp
  218. tmpdir = String("/tmp");
  219. #endif
  220. }
  221. tmpdir.append("/banshee-XXXXXX");
  222. size_t len = tmpdir.size()+1;
  223. char *nameTemplate = bs_newN<char>(len);
  224. snprintf(nameTemplate, len, "%s", tmpdir.c_str());
  225. char *directoryName = mkdtemp(nameTemplate);
  226. if (directoryName == NULL)
  227. {
  228. LOGERR(String(__FUNCTION__) + ": " + strerror(errno));
  229. return Path(StringUtil::BLANK);
  230. }
  231. return Path(String(directoryName) + "/");
  232. }
  233. }