1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "FileSystem/BsFileSystem.h" |
4 | #include "Debug/BsDebug.h" |
5 | |
6 | namespace bs |
7 | { |
8 | void FileSystem::copy(const Path& oldPath, const Path& newPath, bool overwriteExisting) |
9 | { |
10 | Stack<std::tuple<Path, Path>> todo; |
11 | todo.push(std::make_tuple(oldPath, newPath)); |
12 | |
13 | while (!todo.empty()) |
14 | { |
15 | auto current = todo.top(); |
16 | todo.pop(); |
17 | |
18 | Path sourcePath = std::get<0>(current); |
19 | if (!FileSystem::exists(sourcePath)) |
20 | continue; |
21 | |
22 | bool srcIsFile = FileSystem::isFile(sourcePath); |
23 | Path destinationPath = std::get<1>(current); |
24 | bool destExists = FileSystem::exists(destinationPath); |
25 | |
26 | if (destExists) |
27 | { |
28 | if (FileSystem::isFile(destinationPath)) |
29 | { |
30 | if (overwriteExisting) |
31 | FileSystem::remove(destinationPath); |
32 | else |
33 | { |
34 | LOGWRN("Copy operation failed because another file already exists at the new path: \"" + destinationPath.toString() + "\"" ); |
35 | return; |
36 | } |
37 | } |
38 | } |
39 | |
40 | if (srcIsFile) |
41 | { |
42 | FileSystem::copyFile(sourcePath, destinationPath); |
43 | } |
44 | else |
45 | { |
46 | if (!destExists) |
47 | FileSystem::createDir(destinationPath); |
48 | |
49 | Vector<Path> files; |
50 | Vector<Path> directories; |
51 | getChildren(destinationPath, files, directories); |
52 | |
53 | for (auto& file : files) |
54 | { |
55 | Path fileDestPath = destinationPath; |
56 | fileDestPath.append(file.getTail()); |
57 | |
58 | todo.push(std::make_tuple(file, fileDestPath)); |
59 | } |
60 | |
61 | for (auto& dir : directories) |
62 | { |
63 | Path dirDestPath = destinationPath; |
64 | dirDestPath.append(dir.getTail()); |
65 | |
66 | todo.push(std::make_tuple(dir, dirDestPath)); |
67 | } |
68 | } |
69 | } |
70 | } |
71 | |
72 | void FileSystem::remove(const Path& path, bool recursively) |
73 | { |
74 | if (!FileSystem::exists(path)) |
75 | return; |
76 | |
77 | if (recursively) |
78 | { |
79 | Vector<Path> files; |
80 | Vector<Path> directories; |
81 | |
82 | getChildren(path, files, directories); |
83 | |
84 | for (auto& file : files) |
85 | remove(file, false); |
86 | |
87 | for (auto& dir : directories) |
88 | remove(dir, true); |
89 | } |
90 | |
91 | FileSystem::removeFile(path); |
92 | } |
93 | |
94 | void FileSystem::move(const Path& oldPath, const Path& newPath, bool overwriteExisting) |
95 | { |
96 | if (FileSystem::exists(newPath)) |
97 | { |
98 | if (overwriteExisting) |
99 | FileSystem::remove(newPath); |
100 | else |
101 | { |
102 | LOGWRN("Move operation failed because another file already exists at the new path: \"" + newPath.toString() + "\"" ); |
103 | return; |
104 | } |
105 | } |
106 | |
107 | FileSystem::moveFile(oldPath, newPath); |
108 | } |
109 | |
110 | Mutex FileScheduler::mMutex; |
111 | } |
112 | |