|
@@ -504,7 +504,7 @@ void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*
|
504
|
504
|
stopSDPrint();
|
505
|
505
|
|
506
|
506
|
SdFile *curDir;
|
507
|
|
- const char * const fname = diveToFile(curDir, path);
|
|
507
|
+ const char * const fname = diveToFile(true, curDir, path);
|
508
|
508
|
if (!fname) return;
|
509
|
509
|
|
510
|
510
|
if (file.open(curDir, fname, O_READ)) {
|
|
@@ -532,7 +532,7 @@ void CardReader::openFileWrite(char * const path) {
|
532
|
532
|
stopSDPrint();
|
533
|
533
|
|
534
|
534
|
SdFile *curDir;
|
535
|
|
- const char * const fname = diveToFile(curDir, path);
|
|
535
|
+ const char * const fname = diveToFile(false, curDir, path);
|
536
|
536
|
if (!fname) return;
|
537
|
537
|
|
538
|
538
|
if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
|
|
@@ -557,7 +557,7 @@ void CardReader::removeFile(const char * const name) {
|
557
|
557
|
//stopSDPrint();
|
558
|
558
|
|
559
|
559
|
SdFile *curDir;
|
560
|
|
- const char * const fname = diveToFile(curDir, name);
|
|
560
|
+ const char * const fname = diveToFile(false, curDir, name);
|
561
|
561
|
if (!fname) return;
|
562
|
562
|
|
563
|
563
|
if (file.remove(curDir, fname)) {
|
|
@@ -704,26 +704,28 @@ uint16_t CardReader::countFilesInWorkDir() {
|
704
|
704
|
*
|
705
|
705
|
* A nullptr result indicates an unrecoverable error.
|
706
|
706
|
*/
|
707
|
|
-const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo/*=false*/) {
|
|
707
|
+const char* CardReader::diveToFile(const bool update_cwd, SdFile*& curDir, const char * const path, const bool echo/*=false*/) {
|
708
|
708
|
// Track both parent and subfolder
|
709
|
709
|
static SdFile newDir1, newDir2;
|
710
|
710
|
SdFile *sub = &newDir1, *startDir;
|
711
|
711
|
|
|
712
|
+ // Parsing the path string
|
712
|
713
|
const char *item_name_adr = path;
|
713
|
|
- if (path[0] == '/') {
|
|
714
|
+
|
|
715
|
+ if (path[0] == '/') { // Starting at the root directory?
|
714
|
716
|
curDir = &root;
|
715
|
|
- workDirDepth = 0;
|
|
717
|
+ if (update_cwd) workDirDepth = 0; // The cwd can be updated for the benefit of sub-programs
|
716
|
718
|
item_name_adr++;
|
717
|
719
|
}
|
718
|
720
|
else
|
719
|
|
- curDir = &workDir;
|
|
721
|
+ curDir = &workDir; // Dive from workDir (as set by the UI)
|
720
|
722
|
|
721
|
723
|
startDir = curDir;
|
722
|
|
-
|
723
|
|
- // Start dive
|
724
|
724
|
while (item_name_adr) {
|
725
|
|
- // Find next sub
|
|
725
|
+ // Find next subdirectory delimiter
|
726
|
726
|
char * const name_end = strchr(item_name_adr, '/');
|
|
727
|
+
|
|
728
|
+ // Last atom in the path? Item found.
|
727
|
729
|
if (name_end <= item_name_adr) break;
|
728
|
730
|
|
729
|
731
|
// Set subDirName
|
|
@@ -746,13 +748,16 @@ const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, con
|
746
|
748
|
// curDir now subDir
|
747
|
749
|
curDir = sub;
|
748
|
750
|
|
749
|
|
- // Update workDirParents and workDirDepth
|
750
|
|
- if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir;
|
|
751
|
+ // Update workDirParents, workDirDepth, and workDir
|
|
752
|
+ if (update_cwd) {
|
|
753
|
+ if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir;
|
|
754
|
+ workDir = *curDir;
|
|
755
|
+ }
|
751
|
756
|
|
752
|
|
- // Point sub pointer to unused newDir
|
|
757
|
+ // Point sub at the other scratch object
|
753
|
758
|
sub = (curDir != &newDir1) ? &newDir1 : &newDir2;
|
754
|
759
|
|
755
|
|
- // item_name_adr point to next sub
|
|
760
|
+ // Next path atom address
|
756
|
761
|
item_name_adr = name_end + 1;
|
757
|
762
|
}
|
758
|
763
|
return item_name_adr;
|