@@ -19,7 +19,11 @@ class SystemFileWalker final : public abstract::FileWalker {
1919public:
2020 SystemFileWalker (Path root, const Path &path)
2121 : m_root{std::move (root)},
22- m_iterator{std::filesystem::recursive_directory_iterator (path)} {}
22+ m_iterator{std::filesystem::recursive_directory_iterator (path)} {
23+ if (path.relative ()) {
24+ throw InvalidPath (" SystemFileWalker: path must be absolute" );
25+ }
26+ }
2327
2428 [[nodiscard]] std::unique_ptr<FileWalker> clone () const final {
2529 return std::make_unique<SystemFileWalker>(*this );
@@ -69,40 +73,77 @@ Path SystemFilesystem::to_system_path_(const Path &path) const {
6973}
7074
7175bool SystemFilesystem::exists (const Path &path) const {
76+ if (path.relative ()) {
77+ throw InvalidPath (" SystemFilesystem::exists: path must be absolute" );
78+ }
79+
7280 return std::filesystem::exists (to_system_path_ (path));
7381}
7482
7583bool SystemFilesystem::is_file (const Path &path) const {
84+ if (path.relative ()) {
85+ throw InvalidPath (" SystemFilesystem::is_file: path must be absolute" );
86+ }
87+
7688 return std::filesystem::is_regular_file (to_system_path_ (path));
7789}
7890
7991bool SystemFilesystem::is_directory (const Path &path) const {
92+ if (path.relative ()) {
93+ throw InvalidPath (" SystemFilesystem::is_directory: path must be absolute" );
94+ }
95+
8096 return std::filesystem::is_directory (to_system_path_ (path));
8197}
8298
8399std::unique_ptr<abstract::FileWalker>
84100SystemFilesystem::file_walker (const Path &path) const {
101+ if (path.relative ()) {
102+ throw InvalidPath (" SystemFilesystem::file_walker: path must be absolute" );
103+ }
104+
85105 return std::make_unique<SystemFileWalker>(m_root, to_system_path_ (path));
86106}
87107
88108std::shared_ptr<abstract::File> SystemFilesystem::open (const Path &path) const {
109+ if (path.relative ()) {
110+ throw InvalidPath (" SystemFilesystem::open: path must be absolute" );
111+ }
112+
89113 return std::make_unique<DiskFile>(to_system_path_ (path));
90114}
91115
92116std::unique_ptr<std::ostream> SystemFilesystem::create_file (const Path &path) {
117+ if (path.relative ()) {
118+ throw InvalidPath (" SystemFilesystem::create_file: path must be absolute" );
119+ }
120+
93121 return std::make_unique<std::ofstream>(
94122 util::file::create (to_system_path_ (path).string ()));
95123}
96124
97125bool SystemFilesystem::create_directory (const Path &path) {
126+ if (path.relative ()) {
127+ throw InvalidPath (
128+ " SystemFilesystem::create_directory: path must be absolute" );
129+ }
130+
98131 return std::filesystem::create_directory (to_system_path_ (path));
99132}
100133
101134bool SystemFilesystem::remove (const Path &path) {
135+ if (path.relative ()) {
136+ throw InvalidPath (" SystemFilesystem::remove: path must be absolute" );
137+ }
138+
102139 return std::filesystem::remove (to_system_path_ (path));
103140}
104141
105142bool SystemFilesystem::copy (const Path &from, const Path &to) {
143+ if (from.relative () || to.relative ()) {
144+ throw InvalidPath (" SystemFilesystem::copy: paths must be absolute" );
145+ }
146+
106147 std::error_code error_code;
107148 std::filesystem::copy (to_system_path_ (from), to_system_path_ (to), error_code);
108149 if (error_code) {
@@ -113,6 +154,10 @@ bool SystemFilesystem::copy(const Path &from, const Path &to) {
113154
114155std::shared_ptr<abstract::File>
115156SystemFilesystem::copy (const abstract::File &from, const Path &to) {
157+ if (to.relative ()) {
158+ throw InvalidPath (" SystemFilesystem::copy: path must be absolute" );
159+ }
160+
116161 auto istream = from.stream ();
117162 auto ostream = create_file (to_system_path_ (to));
118163
@@ -123,10 +168,18 @@ SystemFilesystem::copy(const abstract::File &from, const Path &to) {
123168
124169std::shared_ptr<abstract::File>
125170SystemFilesystem::copy (std::shared_ptr<abstract::File> from, const Path &to) {
171+ if (to.relative ()) {
172+ throw InvalidPath (" SystemFilesystem::copy: path must be absolute" );
173+ }
174+
126175 return copy (*from, to_system_path_ (to));
127176}
128177
129178bool SystemFilesystem::move (const Path &from, const Path &to) {
179+ if (from.relative () || to.relative ()) {
180+ throw InvalidPath (" SystemFilesystem::move: paths must be absolute" );
181+ }
182+
130183 std::error_code error_code;
131184 std::filesystem::rename (to_system_path_ (from), to_system_path_ (to),
132185 error_code);
@@ -142,6 +195,10 @@ class VirtualFileWalker final : public abstract::FileWalker {
142195 VirtualFileWalker (
143196 const Path &root,
144197 const std::map<Path, std::shared_ptr<abstract::File>> &files) {
198+ if (root.relative ()) {
199+ throw InvalidPath (" VirtualFileWalker: path must be absolute" );
200+ }
201+
145202 for (auto &&f : files) {
146203 if (f.first .ancestor_of (root)) {
147204 m_files[f.first ] = f.second ;
@@ -195,10 +252,18 @@ class VirtualFileWalker final : public abstract::FileWalker {
195252} // namespace
196253
197254bool VirtualFilesystem::exists (const Path &path) const {
255+ if (path.relative ()) {
256+ throw InvalidPath (" VirtualFilesystem::exists: path must be absolute" );
257+ }
258+
198259 return m_files.find (path) != std::end (m_files);
199260}
200261
201262bool VirtualFilesystem::is_file (const Path &path) const {
263+ if (path.relative ()) {
264+ throw InvalidPath (" VirtualFilesystem::is_file: path must be absolute" );
265+ }
266+
202267 auto file_it = m_files.find (path);
203268 if (file_it == std::end (m_files)) {
204269 return false ;
@@ -207,6 +272,10 @@ bool VirtualFilesystem::is_file(const Path &path) const {
207272}
208273
209274bool VirtualFilesystem::is_directory (const Path &path) const {
275+ if (path.relative ()) {
276+ throw InvalidPath (" VirtualFilesystem::is_directory: path must be absolute" );
277+ }
278+
210279 auto file_it = m_files.find (path);
211280 if (file_it == std::end (m_files)) {
212281 return false ;
@@ -216,11 +285,19 @@ bool VirtualFilesystem::is_directory(const Path &path) const {
216285
217286std::unique_ptr<abstract::FileWalker>
218287VirtualFilesystem::file_walker (const Path &path) const {
219- return std::make_unique<VirtualFileWalker>(std::move (path), m_files);
288+ if (path.relative ()) {
289+ throw InvalidPath (" VirtualFilesystem::file_walker: path must be absolute" );
290+ }
291+
292+ return std::make_unique<VirtualFileWalker>(path, m_files);
220293}
221294
222295std::shared_ptr<abstract::File>
223296VirtualFilesystem::open (const Path &path) const {
297+ if (path.relative ()) {
298+ throw InvalidPath (" VirtualFilesystem::open: path must be absolute" );
299+ }
300+
224301 auto file_it = m_files.find (path);
225302 if (file_it == std::end (m_files)) {
226303 return {};
@@ -234,6 +311,11 @@ VirtualFilesystem::create_file(const Path & /*path*/) {
234311}
235312
236313bool VirtualFilesystem::create_directory (const Path &path) {
314+ if (path.relative ()) {
315+ throw InvalidPath (
316+ " VirtualFilesystem::create_directory: path must be absolute" );
317+ }
318+
237319 if (exists (path)) {
238320 return false ;
239321 }
@@ -242,6 +324,10 @@ bool VirtualFilesystem::create_directory(const Path &path) {
242324}
243325
244326bool VirtualFilesystem::remove (const Path &path) {
327+ if (path.relative ()) {
328+ throw InvalidPath (" VirtualFilesystem::remove: path must be absolute" );
329+ }
330+
245331 auto file_it = m_files.find (path);
246332 if (file_it == std::end (m_files)) {
247333 return false ;
@@ -251,6 +337,10 @@ bool VirtualFilesystem::remove(const Path &path) {
251337}
252338
253339bool VirtualFilesystem::copy (const Path &from, const Path &to) {
340+ if (from.relative () || to.relative ()) {
341+ throw InvalidPath (" VirtualFilesystem::copy: paths must be absolute" );
342+ }
343+
254344 // TODO what about directories?
255345
256346 auto from_it = m_files.find (from);
@@ -271,6 +361,10 @@ VirtualFilesystem::copy(const abstract::File & /*from*/, const Path & /*to*/) {
271361
272362std::shared_ptr<abstract::File>
273363VirtualFilesystem::copy (std::shared_ptr<abstract::File> from, const Path &to) {
364+ if (to.relative ()) {
365+ throw InvalidPath (" VirtualFilesystem::copy: path must be absolute" );
366+ }
367+
274368 if (exists (to)) {
275369 return {};
276370 }
@@ -279,6 +373,10 @@ VirtualFilesystem::copy(std::shared_ptr<abstract::File> from, const Path &to) {
279373}
280374
281375bool VirtualFilesystem::move (const Path &from, const Path &to) {
376+ if (from.relative () || to.relative ()) {
377+ throw InvalidPath (" VirtualFilesystem::move: paths must be absolute" );
378+ }
379+
282380 if (!copy (from, to)) {
283381 return false ;
284382 }
0 commit comments