瀏覽代碼

Preserve CWD for write/remove file (#16667)

Robby Candra 4 年之前
父節點
當前提交
48098b1675
共有 2 個文件被更改,包括 20 次插入15 次删除
  1. 19
    14
      Marlin/src/sd/cardreader.cpp
  2. 1
    1
      Marlin/src/sd/cardreader.h

+ 19
- 14
Marlin/src/sd/cardreader.cpp 查看文件

@@ -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;

+ 1
- 1
Marlin/src/sd/cardreader.h 查看文件

@@ -125,7 +125,7 @@ public:
125 125
   static inline uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
126 126
 
127 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 130
   #if ENABLED(SDCARD_SORT_ALPHA)
131 131
     static void presort();

Loading…
取消
儲存