BsFileSystem.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "FileSystem/BsFileSystem.h"
  4. #include "Debug/BsDebug.h"
  5. namespace bs
  6. {
  7. void FileSystem::copy(const Path& oldPath, const Path& newPath, bool overwriteExisting)
  8. {
  9. Stack<std::tuple<Path, Path>> todo;
  10. todo.push(std::make_tuple(oldPath, newPath));
  11. while (!todo.empty())
  12. {
  13. auto current = todo.top();
  14. todo.pop();
  15. Path sourcePath = std::get<0>(current);
  16. if (!FileSystem::exists(sourcePath))
  17. continue;
  18. bool srcIsFile = FileSystem::isFile(sourcePath);
  19. Path destinationPath = std::get<1>(current);
  20. bool destExists = FileSystem::exists(destinationPath);
  21. if (destExists)
  22. {
  23. if (FileSystem::isFile(destinationPath))
  24. {
  25. if (overwriteExisting)
  26. FileSystem::remove(destinationPath);
  27. else
  28. {
  29. LOGWRN("Copy operation failed because another file already exists at the new path: \"" + destinationPath.toString() + "\"");
  30. return;
  31. }
  32. }
  33. }
  34. if (srcIsFile)
  35. {
  36. FileSystem::copyFile(sourcePath, destinationPath);
  37. }
  38. else
  39. {
  40. if (!destExists)
  41. FileSystem::createDir(destinationPath);
  42. Vector<Path> files;
  43. Vector<Path> directories;
  44. getChildren(destinationPath, files, directories);
  45. for (auto& file : files)
  46. {
  47. Path fileDestPath = destinationPath;
  48. fileDestPath.append(file.getWTail());
  49. todo.push(std::make_tuple(file, fileDestPath));
  50. }
  51. for (auto& dir : directories)
  52. {
  53. Path dirDestPath = destinationPath;
  54. dirDestPath.append(dir.getWTail());
  55. todo.push(std::make_tuple(dir, dirDestPath));
  56. }
  57. }
  58. }
  59. }
  60. void FileSystem::remove(const Path& path, bool recursively)
  61. {
  62. if (!FileSystem::exists(path))
  63. return;
  64. if (recursively)
  65. {
  66. Vector<Path> files;
  67. Vector<Path> directories;
  68. getChildren(path, files, directories);
  69. for (auto& file : files)
  70. remove(file, false);
  71. for (auto& dir : directories)
  72. remove(dir, true);
  73. }
  74. FileSystem::removeFile(path);
  75. }
  76. void FileSystem::move(const Path& oldPath, const Path& newPath, bool overwriteExisting)
  77. {
  78. if (FileSystem::exists(newPath))
  79. {
  80. if (overwriteExisting)
  81. FileSystem::remove(newPath);
  82. else
  83. {
  84. LOGWRN("Move operation failed because another file already exists at the new path: \"" + newPath.toString() + "\"");
  85. return;
  86. }
  87. }
  88. FileSystem::moveFile(oldPath, newPath);
  89. }
  90. }