BsUnixFileSystem.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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)((UINT32)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 (UINT64)st_buf.st_size;
  119. }
  120. else
  121. {
  122. HANDLE_PATH_ERROR(path.toString(), errno);
  123. return (UINT64)-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. // Last "file" entry is also considered a directory
  153. if(!parentPath.equals(path))
  154. unix_createDirectory(path.toString());
  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. directories.push_back(dirPath + (filename + "/"));
  175. else
  176. files.push_back(dirPath + filename);
  177. }
  178. }
  179. closedir(dp);
  180. }
  181. std::time_t FileSystem::getLastModifiedTime(const Path& path)
  182. {
  183. struct stat st_buf;
  184. stat(path.toString().c_str(), &st_buf);
  185. std::time_t time = st_buf.st_mtime;
  186. return time;
  187. }
  188. Path FileSystem::getWorkingDirectoryPath()
  189. {
  190. char *buffer = bs_newN<char>(PATH_MAX);
  191. String wd;
  192. if (getcwd(buffer, PATH_MAX) != nullptr)
  193. wd = buffer;
  194. else
  195. LOGERR(String("Error when calling getcwd(): ") + strerror(errno));
  196. bs_free(buffer);
  197. return Path(wd);
  198. }
  199. bool FileSystem::iterate(const Path& dirPath, std::function<bool(const Path&)> fileCallback,
  200. std::function<bool(const Path&)> dirCallback, bool recursive)
  201. {
  202. String pathStr = dirPath.toString();
  203. if (unix_isFile(pathStr))
  204. return false;
  205. DIR* dirHandle = opendir(pathStr.c_str());
  206. if (dirHandle == nullptr)
  207. {
  208. HANDLE_PATH_ERROR(pathStr, errno);
  209. return false;
  210. }
  211. dirent* entry;
  212. while((entry = readdir(dirHandle)))
  213. {
  214. String filename(entry->d_name);
  215. if (filename == "." || filename == "..")
  216. continue;
  217. Path fullPath = dirPath;
  218. if (unix_isDirectory(pathStr + "/" + filename))
  219. {
  220. Path childDir = fullPath.append(filename + "/");
  221. if (dirCallback != nullptr)
  222. {
  223. if (!dirCallback(childDir))
  224. {
  225. closedir(dirHandle);
  226. return false;
  227. }
  228. }
  229. if (recursive)
  230. {
  231. if (!iterate(childDir, fileCallback, dirCallback, recursive))
  232. {
  233. closedir(dirHandle);
  234. return false;
  235. }
  236. }
  237. }
  238. else
  239. {
  240. Path filePath = fullPath.append(filename);
  241. if (fileCallback != nullptr)
  242. {
  243. if (!fileCallback(filePath))
  244. {
  245. closedir(dirHandle);
  246. return false;
  247. }
  248. }
  249. }
  250. }
  251. closedir(dirHandle);
  252. return true;
  253. }
  254. Path FileSystem::getTempDirectoryPath()
  255. {
  256. String tmpdir;
  257. // Try different things:
  258. // 1) If defined, honor the TMPDIR environnement variable
  259. char *TMPDIR = getenv("TMPDIR");
  260. if (TMPDIR != NULL)
  261. tmpdir = TMPDIR;
  262. else
  263. {
  264. // 2) If defined, honor the P_tmpdir macro
  265. #ifdef P_tmpdir
  266. tmpdir = String(P_tmpdir);
  267. #else
  268. // 3) If everything else fails, simply default to /tmp
  269. tmpdir = String("/tmp");
  270. #endif
  271. }
  272. tmpdir.append("/banshee-XXXXXX");
  273. size_t len = tmpdir.size()+1;
  274. char *nameTemplate = bs_newN<char>(len);
  275. snprintf(nameTemplate, len, "%s", tmpdir.c_str());
  276. char *directoryName = mkdtemp(nameTemplate);
  277. if (directoryName == NULL)
  278. {
  279. LOGERR(String(__FUNCTION__) + ": " + strerror(errno));
  280. return Path(StringUtil::BLANK);
  281. }
  282. return Path(String(directoryName) + "/");
  283. }
  284. }