1
- #include " PicoFileSystem .h"
1
+ #include " picoTrackerFileSystem .h"
2
2
3
3
#include " pico/multicore.h"
4
4
5
+ // Global mutex for thread safety
5
6
Mutex mutex;
6
7
7
- PicoFileSystem::PicoFileSystem () {
8
+ picoTrackerFileSystem::picoTrackerFileSystem () {
8
9
// init out access mutex
9
10
std::lock_guard<Mutex> lock (mutex);
10
11
@@ -21,13 +22,13 @@ PicoFileSystem::PicoFileSystem() {
21
22
}
22
23
// Try to mount the whole card as FAT (without partition table)
23
24
if (static_cast <FsVolume *>(&sd)->begin (sd.card (), true , 0 )) {
24
- Trace::Log (" PICOFILESYSTEM " ,
25
+ Trace::Log (" FILESYSTEM " ,
25
26
" Mounted SD Card FAT Filesystem without partition table" );
26
27
return ;
27
28
}
28
29
}
29
30
30
- PI_File * PicoFileSystem ::Open (const char *name, const char *mode) {
31
+ I_File * picoTrackerFileSystem ::Open (const char *name, const char *mode) {
31
32
Trace::Log (" FILESYSTEM" , " Open file:%s, mode:%s" , name, mode);
32
33
std::lock_guard<Mutex> lock (mutex);
33
34
oflag_t rmode;
@@ -47,17 +48,17 @@ PI_File *PicoFileSystem::Open(const char *name, const char *mode) {
47
48
if (!cwd.openCwd ()) {
48
49
return nullptr ;
49
50
}
50
- PI_File *wFile = 0 ;
51
+ I_File *wFile = 0 ;
51
52
if (cwd.open (name, rmode)) {
52
- wFile = new PI_File (cwd);
53
+ wFile = new picoTrackerFile (cwd);
53
54
} else {
54
55
Trace::Error (" FILESYSTEM: Cannot open file:%s" , name, mode);
55
56
}
56
57
return wFile;
57
58
}
58
59
59
- bool PicoFileSystem ::chdir (const char *name) {
60
- Trace::Log (" PICOFILESYSTEM " , " chdir:%s" , name);
60
+ bool picoTrackerFileSystem ::chdir (const char *name) {
61
+ Trace::Log (" FILESYSTEM " , " chdir:%s" , name);
61
62
std::lock_guard<Mutex> lock (mutex);
62
63
63
64
sd.chvol ();
@@ -66,12 +67,12 @@ bool PicoFileSystem::chdir(const char *name) {
66
67
char buf[PFILENAME_SIZE];
67
68
cwd.openCwd ();
68
69
cwd.getName (buf, 128 );
69
- Trace::Log (" PICOFILESYSTEM " , " new CWD:%s\n " , buf);
70
+ Trace::Log (" FILESYSTEM " , " new CWD:%s\n " , buf);
70
71
cwd.close ();
71
72
return res;
72
73
}
73
74
74
- PicoFileType PicoFileSystem ::getFileType (int index) {
75
+ PicoFileType picoTrackerFileSystem ::getFileType (int index) {
75
76
std::lock_guard<Mutex> lock (mutex);
76
77
77
78
FsBaseFile cwd;
@@ -89,8 +90,8 @@ PicoFileType PicoFileSystem::getFileType(int index) {
89
90
return isDir ? PFT_DIR : PFT_FILE;
90
91
}
91
92
92
- void PicoFileSystem ::list (etl::ivector<int > *fileIndexes, const char *filter ,
93
- bool subDirOnly) {
93
+ void picoTrackerFileSystem ::list (etl::ivector<int > *fileIndexes,
94
+ const char *filter, bool subDirOnly) {
94
95
std::lock_guard<Mutex> lock (mutex);
95
96
96
97
fileIndexes->clear ();
@@ -104,7 +105,7 @@ void PicoFileSystem::list(etl::ivector<int> *fileIndexes, const char *filter,
104
105
}
105
106
char buffer[PFILENAME_SIZE];
106
107
cwd.getName (buffer, PFILENAME_SIZE);
107
- Trace::Log (" PICOFILESYSTEM " , " LIST DIR:%s" , buffer);
108
+ Trace::Log (" FILESYSTEM " , " LIST DIR:%s" , buffer);
108
109
109
110
if (!cwd.isDir ()) {
110
111
Trace::Error (" Path is not a directory" );
@@ -123,7 +124,7 @@ void PicoFileSystem::list(etl::ivector<int> *fileIndexes, const char *filter,
123
124
if (strlen (filter) > 0 ) {
124
125
tolowercase (buffer);
125
126
matchesFilter = (strstr (buffer, filter) != nullptr );
126
- Trace::Log (" PICOFILESYSTEM " , " FILTER: %s=%s [%d]\n " , buffer, filter,
127
+ Trace::Log (" FILESYSTEM " , " FILTER: %s=%s [%d]\n " , buffer, filter,
127
128
matchesFilter);
128
129
}
129
130
// filter out "." and files that dont match filter if a filter is given
@@ -136,41 +137,39 @@ void PicoFileSystem::list(etl::ivector<int> *fileIndexes, const char *filter,
136
137
} else {
137
138
fileIndexes->push_back (index );
138
139
}
139
- Trace::Log (" PICOFILESYSTEM " , " [%d] got file: %s" , index , buffer);
140
+ Trace::Log (" FILESYSTEM " , " [%d] got file: %s" , index , buffer);
140
141
count++;
141
142
} else {
142
- Trace::Log (" PICOFILESYSTEM " , " skipped hidden: %s" , buffer);
143
+ Trace::Log (" FILESYSTEM " , " skipped hidden: %s" , buffer);
143
144
}
144
145
entry.close ();
145
146
}
146
147
cwd.close ();
147
- Trace::Log (" PICOFILESYSTEM " , " scanned: %d, added file indexes:%d" , count,
148
+ Trace::Log (" FILESYSTEM " , " scanned: %d, added file indexes:%d" , count,
148
149
fileIndexes->size ());
149
150
}
150
151
151
- void PicoFileSystem ::getFileName (int index, char *name, int length) {
152
+ void picoTrackerFileSystem ::getFileName (int index, char *name, int length) {
152
153
std::lock_guard<Mutex> lock (mutex);
153
-
154
- FsFile cwd;
155
- char dirname[PFILENAME_SIZE];
154
+ FsBaseFile cwd;
156
155
if (!cwd.openCwd ()) {
156
+ char dirname[PFILENAME_SIZE];
157
157
cwd.getName (dirname, PFILENAME_SIZE);
158
158
Trace::Error (" Failed to open cwd:%s" , dirname);
159
159
return ;
160
160
}
161
- FsFile entry;
161
+ FsBaseFile entry;
162
162
entry.open (index );
163
163
entry.getName (name, length);
164
164
entry.close ();
165
165
cwd.close ();
166
166
}
167
167
168
- bool PicoFileSystem ::isParentRoot () {
168
+ bool picoTrackerFileSystem ::isParentRoot () {
169
169
std::lock_guard<Mutex> lock (mutex);
170
-
171
- FsFile cwd;
172
- char dirname[PFILENAME_SIZE];
170
+ FsBaseFile cwd;
173
171
if (!cwd.openCwd ()) {
172
+ char dirname[PFILENAME_SIZE];
174
173
cwd.getName (dirname, PFILENAME_SIZE);
175
174
Trace::Error (" Failed to open cwd:%s" , dirname);
176
175
return false ;
@@ -189,37 +188,28 @@ bool PicoFileSystem::isParentRoot() {
189
188
return result;
190
189
}
191
190
192
- bool PicoFileSystem ::DeleteFile (const char *path) {
191
+ bool picoTrackerFileSystem ::DeleteFile (const char *path) {
193
192
std::lock_guard<Mutex> lock (mutex);
194
193
return sd.remove (path);
195
194
}
196
195
197
- // directory has to be empty
198
- bool PicoFileSystem::DeleteDir (const char *path) {
196
+ bool picoTrackerFileSystem::DeleteDir (const char *path) {
199
197
std::lock_guard<Mutex> lock (mutex);
200
198
auto delDir = sd.open (path, O_READ);
201
199
return delDir.rmdir ();
202
200
}
203
201
204
- bool PicoFileSystem ::exists (const char *path) {
202
+ bool picoTrackerFileSystem ::exists (const char *path) {
205
203
std::lock_guard<Mutex> lock (mutex);
206
204
return sd.exists (path);
207
205
}
208
206
209
- /* *
210
- * Create a directory at the specified path.
211
- *
212
- * \param[in] path The path where the directory will be created.
213
- * \param[in] pFlag If true, create missing parent directories.
214
- *
215
- * \return true if the directory was successfully created, false otherwise.
216
- */
217
- bool PicoFileSystem::makeDir (const char *path, bool pFlag) {
207
+ bool picoTrackerFileSystem::makeDir (const char *path, bool pFlag) {
218
208
std::lock_guard<Mutex> lock (mutex);
219
209
return sd.mkdir (path, pFlag);
220
210
}
221
211
222
- uint64_t PicoFileSystem ::getFileSize (const int index) {
212
+ uint64_t picoTrackerFileSystem ::getFileSize (const int index) {
223
213
std::lock_guard<Mutex> lock (mutex);
224
214
FsBaseFile cwd;
225
215
FsBaseFile entry;
@@ -237,7 +227,8 @@ uint64_t PicoFileSystem::getFileSize(const int index) {
237
227
return size;
238
228
}
239
229
240
- bool PicoFileSystem::CopyFile (const char *srcPath, const char *destPath) {
230
+ bool picoTrackerFileSystem::CopyFile (const char *srcPath,
231
+ const char *destPath) {
241
232
std::lock_guard<Mutex> lock (mutex);
242
233
auto fSrc = sd.open (srcPath, O_READ);
243
234
auto fDest = sd.open (destPath, O_WRITE | O_CREAT);
@@ -262,37 +253,25 @@ bool PicoFileSystem::CopyFile(const char *srcPath, const char *destPath) {
262
253
return true ;
263
254
}
264
255
265
- void PicoFileSystem ::tolowercase (char *temp) {
266
- // Convert to upper case
256
+ void picoTrackerFileSystem ::tolowercase (char *temp) {
257
+ // Convert to lower case
267
258
char *s = temp;
268
259
while (*s != ' \0 ' ) {
269
260
*s = tolower ((unsigned char )*s);
270
261
s++;
271
262
}
272
263
}
273
264
274
- PI_File::PI_File (FsBaseFile file) { file_ = file; };
275
-
276
- /* *
277
- * Read data from a file starting at the current position.
278
- *
279
- * \param[out] buf Pointer to the location that will receive the data.
280
- *
281
- * \param[in] size Maximum number of bytes to read.
282
- *
283
- * \return For success read() returns the number of bytes read.
284
- * A value less than \a count, including zero, will be returned
285
- * if end of file is reached.
286
- * If an error occurs, read() returns -1. Possible errors include
287
- * read() called before a file has been opened, corrupt file system
288
- * or an I/O error occurred.
289
- */
290
- int PI_File::Read (void *ptr, int size) {
265
+ // picoTrackerFile implementation
266
+
267
+ picoTrackerFile::picoTrackerFile (FsBaseFile file) { file_ = file; }
268
+
269
+ int picoTrackerFile::Read (void *ptr, int size) {
291
270
std::lock_guard<Mutex> lock (mutex);
292
271
return file_.read (ptr, size);
293
272
}
294
273
295
- void PI_File ::Seek (long offset, int whence) {
274
+ void picoTrackerFile ::Seek (long offset, int whence) {
296
275
std::lock_guard<Mutex> lock (mutex);
297
276
switch (whence) {
298
277
case SEEK_SET:
@@ -309,32 +288,32 @@ void PI_File::Seek(long offset, int whence) {
309
288
}
310
289
}
311
290
312
- bool PI_File ::DeleteFile () {
291
+ bool picoTrackerFile ::DeleteFile () {
313
292
std::lock_guard<Mutex> lock (mutex);
314
293
return file_.remove ();
315
294
}
316
295
317
- int PI_File ::GetC () {
296
+ int picoTrackerFile ::GetC () {
318
297
std::lock_guard<Mutex> lock (mutex);
319
298
return file_.read ();
320
299
}
321
300
322
- int PI_File ::Write (const void *ptr, int size, int nmemb) {
301
+ int picoTrackerFile ::Write (const void *ptr, int size, int nmemb) {
323
302
std::lock_guard<Mutex> lock (mutex);
324
303
return file_.write (ptr, size * nmemb);
325
304
}
326
305
327
- long PI_File ::Tell () {
306
+ long picoTrackerFile ::Tell () {
328
307
std::lock_guard<Mutex> lock (mutex);
329
308
return file_.curPosition ();
330
309
}
331
310
332
- int PI_File ::Error () {
311
+ int picoTrackerFile ::Error () {
333
312
std::lock_guard<Mutex> lock (mutex);
334
313
return file_.getError ();
335
314
}
336
315
337
- bool PI_File ::Close () {
316
+ bool picoTrackerFile ::Close () {
338
317
std::lock_guard<Mutex> lock (mutex);
339
318
return file_.close ();
340
- }
319
+ }
0 commit comments