-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotectedFS.cpp
219 lines (178 loc) · 4.54 KB
/
protectedFS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// protectedFS.cpp
#include "protectedFS.h"
#include "FluidNC_UI.h"
#define DEBUG_PROTECTED_FS 0
portMUX_TYPE protected_fs_mutex = portMUX_INITIALIZER_UNLOCKED;
proFile::proFile()
{
}
proFile::proFile(File file) :
_file(file)
{
}
void proFile::close()
{
#if DEBUG_PROTECTED_FS
g_debug("close(%s)",name());
#endif
if (p_fs)
p_fs->unlinkFile(this);
_file.close();
}
proFile proFile::openNextFile()
{
File file = _file.openNextFile();
proFile pf(file);
return pf;
}
const char *proFile::leafName()
{
const char *p = name();
const char *rslt = p;
while (*p)
{
if (*p == '/')
rslt = p+1;
p++;
}
return rslt;
}
protectedFS::protectedFS(fs::FS& fs) :
_fs(fs)
{
}
void protectedFS::linkFile(proFile *pf)
{
vTaskEnterCritical(&protected_fs_mutex);
pf->p_prev = p_last;
if (p_last)
{
p_last->p_next = pf;
p_last = pf;
}
if (!p_first)
{
p_first = pf;
p_last = pf;
}
pf->p_fs = this;
vTaskExitCritical(&protected_fs_mutex);
}
void protectedFS::unlinkFile(proFile *pf)
{
if (pf->p_fs)
{
vTaskEnterCritical(&protected_fs_mutex);
pf->p_fs = 0;
if (pf->p_next) pf->p_next->p_prev = pf->p_prev;
if (pf->p_prev) pf->p_prev->p_next = pf->p_next;
if (p_last == pf)
p_last = pf->p_prev;
if (p_first == pf)
p_first = pf->p_next;
pf->p_next = 0;
pf->p_prev = 0;
vTaskExitCritical(&protected_fs_mutex);
}
}
proFile protectedFS::open(const char* path, const char* mode/* = FILE_READ*/)
{
#if DEBUG_PROTECTED_FS
g_debug("open(%s)",path);
#endif
File file = _fs.open(path,mode);
proFile pf(file);
if (pf)
linkFile(&pf);
return pf;
}
proFile protectedFS::open(const String& path, const char* mode/* = FILE_READ*/)
{
return open(path.c_str(),mode);
}
bool protectedFS::exists(const char* path)
{
return _fs.exists(path);
}
bool protectedFS::exists(const String& path)
{
return _fs.exists(path);
}
bool protectedFS::remove(const char* path)
{
if (!check_path(path))
return false;
return _fs.remove(path);
}
bool protectedFS::remove(const String& path)
{
return remove(path.c_str());
}
bool protectedFS::rename(const char* pathFrom, const char* pathTo)
{
if (!check_path(pathFrom))
return false;
if (!check_path(pathTo))
return false;
return _fs.rename(pathFrom,pathTo);
}
bool protectedFS::rename(const String& pathFrom, const String& pathTo)
{
return rename(pathFrom.c_str(),pathTo.c_str());
}
bool protectedFS::mkdir(const char *path)
{
if (!check_path(path))
return false;
return _fs.mkdir(path);
}
bool protectedFS::mkdir(const String &path)
{
return mkdir(path.c_str());
}
bool protectedFS::rmdir(const char *path)
{
if (!check_path(path))
return false;
return _fs.rmdir(path);
}
bool protectedFS::rmdir(const String &path)
{
return rmdir(path.c_str());
}
bool protectedFS::check_path(const char *path)
{
proFile *ptr = p_first;
int len = strlen(path);
// g_debug("check_path len(%d) %s",len,path);
// the name() is fully qualified
while (ptr)
{
const char *tpath = ptr->name(); // fully qualified path including terminal node
int tlen = strlen(tpath);
// g_debug(" checking tlen(%d) %s",tlen,tpath);
if (!strncmp(path,tpath,len)) // if they match upto the length of the path being removed
{
if (len == tlen) // they are the same
{
#if DEBUG_PROTECTED_FS
g_debug("---> check_path(%s) fail len==tlen",path);
#endif
return false; // can't remove the directory
}
// tlen must be greater than len here
// so it's ok to delete the path if the next
// character in tpath is not a forward slash
// i.e. it's a different path ..
if (tpath[len] == '/')
{
#if DEBUG_PROTECTED_FS
g_debug("---> check_path(%s) fail tpath[%d]='/' path2(%s)",path,len,tpath);
#endif
return false;
}
}
ptr = ptr->p_next;
}
return true;
}