Browse Source

Preserve CWD for write/remove file (#16667)

Robby Candra 5 years ago
parent
commit
48098b1675
2 changed files with 20 additions and 15 deletions
  1. 19
    14
      Marlin/src/sd/cardreader.cpp
  2. 1
    1
      Marlin/src/sd/cardreader.h

+ 19
- 14
Marlin/src/sd/cardreader.cpp View File

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

+ 1
- 1
Marlin/src/sd/cardreader.h View File

125
   static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
125
   static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
126
 
126
 
127
   // Helper for open and remove
127
   // Helper for open and remove
128
-  static const char* diveToFile(SdFile*& curDir, const char * const path, const bool echo=false);
128
+  static const char* diveToFile(const bool update_cwd, SdFile*& curDir, const char * const path, const bool echo=false);
129
 
129
 
130
   #if ENABLED(SDCARD_SORT_ALPHA)
130
   #if ENABLED(SDCARD_SORT_ALPHA)
131
     static void presort();
131
     static void presort();

Loading…
Cancel
Save