roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
fsutil.cpp
Go to the documentation of this file.
1#include "roo_io/fs/fsutil.h"
2
3#include <cstring>
4
5namespace roo_io {
6
7namespace {
8
9Status DeleteDirContentsRecursively(Mount& fs, Directory& dir) {
10 while (dir.read()) {
11 Status s = DeleteRecursively(fs, dir.entry().path());
12 if (s != kOk) return s;
13 }
14 return dir.isOpen() ? kOk : dir.status();
15}
16
17} // namespace
18
20 Stat stat = fs.stat(path);
21 if (!stat.exists()) return stat.status();
22 if (stat.isFile()) {
23 return fs.remove(path);
24 } else {
25 Directory dir = fs.opendir(path);
26 if (!dir.isOpen()) return dir.status();
28 if (s != kOk) return s;
29 return fs.rmdir(path);
30 }
31}
32
34 Stat stat = fs.stat(path);
35 if (stat.exists()) {
36 if (stat.isFile()) {
37 return kNotDirectory;
38 } else {
39 return kDirectoryExists;
40 }
41 }
42 if (stat.status() != kNotFound) return stat.status();
43
44 auto len = std::strlen(path) + 1;
45 std::unique_ptr<char[]> path_copy(new char[len]);
46 std::memcpy(path_copy.get(), path, len);
47 char* p = path_copy.get();
48 while (true) {
49 while (*p == '/') ++p;
50 while (*p != 0 && *p != '/') ++p;
51 bool end = (*p == 0);
52 if (!end) {
53 // Temporarily truncate.
54 *p = 0;
55 }
56 Status s = fs.mkdir(path_copy.get());
57 if (s != kOk && s != kDirectoryExists) return s;
58 if (end) return kOk;
59 *p = '/';
60 }
61}
62
64 Stat stat = fs.stat(path);
65 if (stat.exists()) {
66 return kDirectoryExists;
67 }
68 if (stat.status() != kNotFound) return stat.status();
69
70 auto len = std::strlen(path) + 1;
71 std::unique_ptr<char[]> path_copy(new char[len]);
72 std::memcpy(path_copy.get(), path, len);
73 char* p = path_copy.get();
74 while (true) {
75 while (*p == '/') ++p;
76 while (*p != 0 && *p != '/') ++p;
77 if (*p == 0) return kOk;
78 *p = 0;
79 Status s = fs.mkdir(path_copy.get());
80 if (s != kOk && s != kDirectoryExists) return s;
81 *p = '/';
82 }
83}
84
86 return MultipassInputStreamReader(fs.fopen(path));
87}
88
94
95} // namespace roo_io
Status status() const
Definition directory.h:84
Buffered typed writer over OutputStream.
bool isFile() const
Definition stat.h:31
Status status() const
Definition stat.h:41
bool exists() const
Definition stat.h:18
Definition byte.h:6
Status MkDirRecursively(roo_io::Mount &fs, const char *path)
Definition fsutil.cpp:33
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
Status DeleteRecursively(roo_io::Mount &fs, const char *path)
Definition fsutil.cpp:19
OutputStreamWriter OpenDataFileForWrite(roo_io::Mount &fs, const char *path, roo_io::FileUpdatePolicy update_policy)
Definition fsutil.cpp:89
MultipassInputStreamReader OpenDataFile(roo_io::Mount &fs, const char *path)
Definition fsutil.cpp:85
Status
Definition status.h:7
@ kOk
Definition status.h:8
@ kDirectoryExists
Definition status.h:32
@ kNotDirectory
Definition status.h:35
@ kNotFound
Definition status.h:24
Status MkParentDirRecursively(roo_io::Mount &fs, const char *path)
Definition fsutil.cpp:63