BsUnixFileSystem.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. {
  49. return S_ISREG(st_buf.st_mode);
  50. }
  51. }
  52. bool unix_isDirectory(const String& path)
  53. {
  54. struct stat st_buf;
  55. if (unix_stat(path, &st_buf))
  56. {
  57. return S_ISDIR(st_buf.st_mode);
  58. }
  59. return false;
  60. }
  61. bool unix_createDirectory(const String& path)
  62. {
  63. if (unix_pathExists(path) && unix_isDirectory(path))
  64. return false;
  65. if (mkdir(path.c_str(), 0755))
  66. {
  67. HANDLE_PATH_ERROR(path, errno);
  68. return false;
  69. }
  70. return true;
  71. }
  72. void FileSystem::removeFile(const Path& path)
  73. {
  74. String pathStr = path.toString();
  75. if (unix_isDirectory(pathStr))
  76. {
  77. if (rmdir(pathStr.c_str()))
  78. HANDLE_PATH_ERROR(pathStr, errno);
  79. }
  80. else
  81. {
  82. if (unlink(pathStr.c_str()))
  83. HANDLE_PATH_ERROR(pathStr, errno);
  84. }
  85. }
  86. void FileSystem::copyFile(const Path& source, const Path& destination)
  87. {
  88. std::ifstream sourceStream(source.toString().c_str(), std::ios::binary);
  89. std::ofstream destinationStream(destination.toString().c_str(), std::ios::binary);
  90. destinationStream << sourceStream.rdbuf();
  91. sourceStream.close();
  92. destinationStream.close();
  93. }
  94. void FileSystem::moveFile(const Path& oldPath, const Path& newPath)
  95. {
  96. String oldPathStr = oldPath.toString();
  97. String newPathStr = newPath.toString();
  98. if (std::rename(oldPathStr.c_str(), newPathStr.c_str()) == -1)
  99. {
  100. LOGERR(String(__FUNCTION__) + ": renaming " + oldPathStr + " to " + newPathStr +
  101. ": " + strerror(errno));
  102. }
  103. }
  104. SPtr<DataStream> FileSystem::openFile(const Path& path, bool readOnly)
  105. {
  106. String pathString = path.toString();
  107. DataStream::AccessMode accessMode = DataStream::READ;
  108. if (!readOnly)
  109. accessMode = (DataStream::AccessMode)(accessMode | (UINT32)DataStream::WRITE);
  110. return bs_shared_ptr_new<FileDataStream>(path, accessMode, true);
  111. }
  112. SPtr<DataStream> FileSystem::createAndOpenFile(const Path& path)
  113. {
  114. return bs_shared_ptr_new<FileDataStream>(path, DataStream::AccessMode::WRITE, true);
  115. }
  116. UINT64 FileSystem::getFileSize(const Path& path)
  117. {
  118. struct stat st_buf;
  119. if (stat(path.toString().c_str(), &st_buf) == 0)
  120. {
  121. return st_buf.st_size;
  122. }
  123. else
  124. {
  125. HANDLE_PATH_ERROR(path.toString(), errno);
  126. return -1;
  127. }
  128. }
  129. bool FileSystem::exists(const Path& path)
  130. {
  131. return unix_pathExists(path.toString());
  132. }
  133. bool FileSystem::isFile(const Path& path)
  134. {
  135. String pathStr = path.toString();
  136. return unix_pathExists(pathStr) && unix_isFile(pathStr);
  137. }
  138. bool FileSystem::isDirectory(const Path& path)
  139. {
  140. String pathStr = path.toString();
  141. return unix_pathExists(pathStr) && unix_isDirectory(pathStr);
  142. }
  143. void FileSystem::createDir(const Path& path)
  144. {
  145. Path parentPath = path;
  146. while (!exists(parentPath) && parentPath.getNumDirectories() > 0)
  147. {
  148. parentPath = parentPath.getParent();
  149. }
  150. for (UINT32 i = parentPath.getNumDirectories(); i < path.getNumDirectories(); i++)
  151. {
  152. parentPath.append(path[i]);
  153. unix_createDirectory(parentPath.toString());
  154. }
  155. }
  156. void FileSystem::getChildren(const Path& dirPath, Vector<Path>& files, Vector<Path>& directories)
  157. {
  158. const String pathStr = dirPath.toString();
  159. if (unix_isFile(pathStr))
  160. return;
  161. DIR *dp = opendir(pathStr.c_str());
  162. if (dp == NULL)
  163. {
  164. HANDLE_PATH_ERROR(pathStr, errno);
  165. return;
  166. }
  167. struct dirent *ep;
  168. while ( (ep = readdir(dp)) )
  169. {
  170. const String filename(ep->d_name);
  171. if (filename != "." && filename != "..")
  172. {
  173. if (unix_isDirectory(pathStr + "/" + filename))
  174. {
  175. directories.push_back(dirPath + (filename + "/"));
  176. }
  177. else
  178. {
  179. files.push_back(dirPath + filename);
  180. }
  181. }
  182. }
  183. closedir(dp);
  184. }
  185. std::time_t FileSystem::getLastModifiedTime(const Path& path)
  186. {
  187. struct stat st_buf;
  188. stat(path.toString().c_str(), &st_buf);
  189. std::time_t time = st_buf.st_mtime;
  190. return time;
  191. }
  192. Path FileSystem::getWorkingDirectoryPath()
  193. {
  194. char *buffer = bs_newN<char>(PATH_MAX);
  195. String wd;
  196. if (getcwd(buffer, PATH_MAX) != NULL)
  197. wd = buffer;
  198. bs_free(buffer);
  199. const int error = errno;
  200. if (error)
  201. LOGERR(String("Error when calling getcwd(): ") + strerror(error));
  202. return Path(wd);
  203. }
  204. bool FileSystem::iterate(const Path& dirPath, std::function<bool(const Path&)> fileCallback,
  205. std::function<bool(const Path&)> dirCallback, bool recursive)
  206. {
  207. BS_ASSERT(!"TODO: implement FileSystem::iterate()");
  208. return true;
  209. }
  210. Path FileSystem::getTempDirectoryPath()
  211. {
  212. String tmpdir;
  213. // Try different things:
  214. // 1) If defined, honor the TMPDIR environnement variable
  215. char *TMPDIR = getenv("TMPDIR");
  216. if (TMPDIR != NULL)
  217. tmpdir = TMPDIR;
  218. else
  219. {
  220. // 2) If defined, honor the P_tmpdir macro
  221. #ifdef P_tmpdir
  222. tmpdir = String(P_tmpdir);
  223. #else
  224. // 3) If everything else fails, simply default to /tmp
  225. tmpdir = String("/tmp");
  226. #endif
  227. }
  228. tmpdir.append("/banshee-XXXXXX");
  229. size_t len = tmpdir.size()+1;
  230. char *nameTemplate = bs_newN<char>(len);
  231. snprintf(nameTemplate, len, "%s", tmpdir.c_str());
  232. char *directoryName = mkdtemp(nameTemplate);
  233. if (directoryName == NULL)
  234. {
  235. LOGERR(String(__FUNCTION__) + ": " + strerror(errno));
  236. return Path(StringUtil::BLANK);
  237. }
  238. return Path(String(directoryName) + "/");
  239. }
  240. }