BsUnixFileSystem.cpp 6.7 KB

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