Preserve CWD for write/remove file (#16667)

This commit is contained in:
Robby Candra 2020-01-27 07:34:17 +07:00 committed by Scott Lahteine
parent f6a6704b66
commit 48098b1675
2 changed files with 20 additions and 15 deletions

View File

@ -504,7 +504,7 @@ void CardReader::openFileRead(char * const path, const uint8_t subcall_type/*=0*
stopSDPrint(); stopSDPrint();
SdFile *curDir; SdFile *curDir;
const char * const fname = diveToFile(curDir, path); const char * const fname = diveToFile(true, curDir, path);
if (!fname) return; if (!fname) return;
if (file.open(curDir, fname, O_READ)) { if (file.open(curDir, fname, O_READ)) {
@ -532,7 +532,7 @@ void CardReader::openFileWrite(char * const path) {
stopSDPrint(); stopSDPrint();
SdFile *curDir; SdFile *curDir;
const char * const fname = diveToFile(curDir, path); const char * const fname = diveToFile(false, curDir, path);
if (!fname) return; if (!fname) return;
if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) { if (file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
@ -557,7 +557,7 @@ void CardReader::removeFile(const char * const name) {
//stopSDPrint(); //stopSDPrint();
SdFile *curDir; SdFile *curDir;
const char * const fname = diveToFile(curDir, name); const char * const fname = diveToFile(false, curDir, name);
if (!fname) return; if (!fname) return;
if (file.remove(curDir, fname)) { if (file.remove(curDir, fname)) {
@ -704,26 +704,28 @@ uint16_t CardReader::countFilesInWorkDir() {
* *
* A nullptr result indicates an unrecoverable error. * A nullptr result indicates an unrecoverable error.
*/ */
const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, const bool echo/*=false*/) { const char* CardReader::diveToFile(const bool update_cwd, SdFile*& curDir, const char * const path, const bool echo/*=false*/) {
// Track both parent and subfolder // Track both parent and subfolder
static SdFile newDir1, newDir2; static SdFile newDir1, newDir2;
SdFile *sub = &newDir1, *startDir; SdFile *sub = &newDir1, *startDir;
// Parsing the path string
const char *item_name_adr = path; const char *item_name_adr = path;
if (path[0] == '/') {
if (path[0] == '/') { // Starting at the root directory?
curDir = &root; curDir = &root;
workDirDepth = 0; if (update_cwd) workDirDepth = 0; // The cwd can be updated for the benefit of sub-programs
item_name_adr++; item_name_adr++;
} }
else else
curDir = &workDir; curDir = &workDir; // Dive from workDir (as set by the UI)
startDir = curDir; startDir = curDir;
// Start dive
while (item_name_adr) { while (item_name_adr) {
// Find next sub // Find next subdirectory delimiter
char * const name_end = strchr(item_name_adr, '/'); char * const name_end = strchr(item_name_adr, '/');
// Last atom in the path? Item found.
if (name_end <= item_name_adr) break; if (name_end <= item_name_adr) break;
// Set subDirName // Set subDirName
@ -746,13 +748,16 @@ const char* CardReader::diveToFile(SdFile*& curDir, const char * const path, con
// curDir now subDir // curDir now subDir
curDir = sub; curDir = sub;
// Update workDirParents and workDirDepth // Update workDirParents, workDirDepth, and workDir
if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir; if (update_cwd) {
if (workDirDepth < MAX_DIR_DEPTH) workDirParents[workDirDepth++] = *curDir;
workDir = *curDir;
}
// Point sub pointer to unused newDir // Point sub at the other scratch object
sub = (curDir != &newDir1) ? &newDir1 : &newDir2; sub = (curDir != &newDir1) ? &newDir1 : &newDir2;
// item_name_adr point to next sub // Next path atom address
item_name_adr = name_end + 1; item_name_adr = name_end + 1;
} }
return item_name_adr; return item_name_adr;

View File

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