|
@@ -461,7 +461,7 @@ void CardReader::openFile(char * const path, const bool read, const bool subcall
|
461
|
461
|
stopSDPrint();
|
462
|
462
|
|
463
|
463
|
SdFile *curDir;
|
464
|
|
- const char * const fname = diveToFile(curDir, path, false);
|
|
464
|
+ const char * const fname = diveToFile(curDir, path);
|
465
|
465
|
if (!fname) return;
|
466
|
466
|
|
467
|
467
|
if (read) {
|
|
@@ -501,7 +501,7 @@ void CardReader::removeFile(const char * const name) {
|
501
|
501
|
//stopSDPrint();
|
502
|
502
|
|
503
|
503
|
SdFile *curDir;
|
504
|
|
- const char * const fname = diveToFile(curDir, name, false);
|
|
504
|
+ const char * const fname = diveToFile(curDir, name);
|
505
|
505
|
if (!fname) return;
|
506
|
506
|
|
507
|
507
|
if (file.remove(curDir, fname)) {
|
|
@@ -641,15 +641,31 @@ uint16_t CardReader::getnrfilenames() {
|
641
|
641
|
*
|
642
|
642
|
* A NULL result indicates an unrecoverable error.
|
643
|
643
|
*/
|
644
|
|
-const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo) {
|
645
|
|
- SdFile myDir;
|
646
|
|
- if (path[0] != '/') { curDir = &workDir; return path; }
|
|
644
|
+const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo/*=false*/) {
|
|
645
|
+ // Track both parent and subfolder
|
|
646
|
+ static SdFile newDir1, newDir2;
|
|
647
|
+ SdFile *sub = &newDir1, *startDir;
|
|
648
|
+
|
|
649
|
+ const char *dirname_start = path;
|
|
650
|
+ char echo_fn[105];
|
|
651
|
+
|
|
652
|
+ if (path[0] == '/') {
|
|
653
|
+ curDir = &root;
|
|
654
|
+ workDirDepth = 0;
|
|
655
|
+ dirname_start++;
|
|
656
|
+ }
|
|
657
|
+ else
|
|
658
|
+ curDir = &workDir;
|
|
659
|
+
|
|
660
|
+ startDir = curDir;
|
647
|
661
|
|
648
|
|
- curDir = &root;
|
649
|
|
- const char *dirname_start = &path[1];
|
|
662
|
+ // Start dive
|
650
|
663
|
while (dirname_start) {
|
|
664
|
+ // Find next sub
|
651
|
665
|
char * const dirname_end = strchr(dirname_start, '/');
|
652
|
666
|
if (dirname_end <= dirname_start) break;
|
|
667
|
+
|
|
668
|
+ // Set subDirName
|
653
|
669
|
const uint8_t len = dirname_end - dirname_start;
|
654
|
670
|
char dosSubdirname[len + 1];
|
655
|
671
|
strncpy(dosSubdirname, dirname_start, len);
|
|
@@ -657,11 +673,25 @@ const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, con
|
657
|
673
|
|
658
|
674
|
if (echo) SERIAL_ECHOLN(dosSubdirname);
|
659
|
675
|
|
660
|
|
- if (!myDir.open(curDir, dosSubdirname, O_READ)) {
|
|
676
|
+ // Open curDir
|
|
677
|
+ if (!sub->open(curDir, dosSubdirname, O_READ)) {
|
661
|
678
|
SERIAL_ECHOLNPAIR(MSG_SD_OPEN_FILE_FAIL, dosSubdirname, ".");
|
662
|
679
|
return NULL;
|
663
|
680
|
}
|
664
|
|
- curDir = &myDir;
|
|
681
|
+
|
|
682
|
+ // Close curDir if not at starting-point
|
|
683
|
+ if (curDir != startDir) curDir->close();
|
|
684
|
+
|
|
685
|
+ // curDir now subDir
|
|
686
|
+ curDir = sub;
|
|
687
|
+
|
|
688
|
+ // Update workDirParents and workDirDepth
|
|
689
|
+ if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir;
|
|
690
|
+
|
|
691
|
+ // Point sub pointer to unused newDir
|
|
692
|
+ sub = (curDir != &newDir1) ? &newDir1 : &newDir2;
|
|
693
|
+
|
|
694
|
+ // dirname_start point to next sub
|
665
|
695
|
dirname_start = dirname_end + 1;
|
666
|
696
|
}
|
667
|
697
|
return dirname_start;
|