diff options
author | David Crocker <dcrocker@eschertech.com> | 2020-01-13 01:59:32 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2020-01-13 01:59:32 +0300 |
commit | 13084f6566c536df08430cce6c584f5e5c06b5a8 (patch) | |
tree | 44765c911f8a5850ee3d18147658b29b0eba0726 /src/Storage | |
parent | d935cc8c828e44430f6a4dc2eacf53c04268fe96 (diff) |
Various
Refactored how GCode blocks and indentation are tracked
Fixed lack of motor movement on Duet 3
Create path recursively if needed when creating a directory or renaming
a file
Diffstat (limited to 'src/Storage')
-rw-r--r-- | src/Storage/FileStore.cpp | 24 | ||||
-rw-r--r-- | src/Storage/MassStorage.cpp | 48 | ||||
-rw-r--r-- | src/Storage/MassStorage.h | 2 |
3 files changed, 41 insertions, 33 deletions
diff --git a/src/Storage/FileStore.cpp b/src/Storage/FileStore.cpp index 284aa956..c3d2643d 100644 --- a/src/Storage/FileStore.cpp +++ b/src/Storage/FileStore.cpp @@ -64,29 +64,9 @@ bool FileStore::Open(const char* filePath, OpenMode mode, uint32_t preAllocSize) if (writing) { - // Try to create the path of this file if we want to write to it - String<MaxFilenameLength> filePathCopy; - filePathCopy.copy(filePath); - - size_t i = (isdigit(filePathCopy[0]) && filePathCopy[1] == ':') ? 2 : 0; - if (filePathCopy[i] == '/') + if (!MassStorage::EnsurePath(filePath)) { - ++i; - } - - while (i < filePathCopy.strlen()) - { - if (filePathCopy[i] == '/') - { - filePathCopy[i] = 0; - if (!MassStorage::DirectoryExists(filePathCopy.GetRef()) && !MassStorage::MakeDirectory(filePathCopy.c_str())) - { - reprap.GetPlatform().MessageF(ErrorMessage, "Failed to create folder %s while trying to open file %s\n", filePathCopy.c_str(), filePath); - return false; - } - filePathCopy[i] = '/'; - } - ++i; + return false; } // Also try to allocate a write buffer so we can perform faster writes diff --git a/src/Storage/MassStorage.cpp b/src/Storage/MassStorage.cpp index e3d8e6b9..52a94e0b 100644 --- a/src/Storage/MassStorage.cpp +++ b/src/Storage/MassStorage.cpp @@ -374,27 +374,51 @@ bool MassStorage::Delete(const char* filePath) noexcept return true; } -// Create a new directory -bool MassStorage::MakeDirectory(const char *parentDir, const char *dirName) noexcept +// Ensure that the path up to the last '/' (excluding trailing '/' characters) in filePath exists, returning true if successful +bool MassStorage::EnsurePath(const char* filePath) noexcept { - String<MaxFilenameLength> location; - if (!CombineName(location.GetRef(), parentDir, dirName)) + // Try to create the path of this file if we want to write to it + String<MaxFilenameLength> filePathCopy; + filePathCopy.copy(filePath); + + size_t i = (isdigit(filePathCopy[0]) && filePathCopy[1] == ':') ? 2 : 0; + if (filePathCopy[i] == '/') { - return false; + ++i; } - if (f_mkdir(location.c_str()) != FR_OK) + + size_t limit = filePathCopy.strlen(); + while (limit != 0 && filePath[limit - 1] == '/') { - reprap.GetPlatform().MessageF(ErrorMessage, "Failed to create directory %s\n", location.c_str()); - return false; + --limit; + } + while (i < limit) + { + if (filePathCopy[i] == '/') + { + filePathCopy[i] = 0; + if (!MassStorage::DirectoryExists(filePathCopy.GetRef()) && f_mkdir(filePathCopy.c_str()) != FR_OK) + { + reprap.GetPlatform().MessageF(ErrorMessage, "Failed to create folder %s in path %s\n", filePathCopy.c_str(), filePath); + return false; + } + filePathCopy[i] = '/'; + } + ++i; } return true; } +// Create a new directory bool MassStorage::MakeDirectory(const char *directory) noexcept { + if (!EnsurePath(directory)) + { + return false; + } if (f_mkdir(directory) != FR_OK) { - reprap.GetPlatform().MessageF(ErrorMessage, "Failed to create directory %s\n", directory); + reprap.GetPlatform().MessageF(ErrorMessage, "Failed to create folder %s\n", directory); return false; } return true; @@ -405,11 +429,15 @@ bool MassStorage::Rename(const char *oldFilename, const char *newFilename) noexc { if (newFilename[0] >= '0' && newFilename[0] <= '9' && newFilename[1] == ':') { - // Workaround for DWC 1.13 which send a volume specification at the start of the new path. + // Workaround for DWC 1.13 which sends a volume specification at the start of the new path. // f_rename can't handle this, so skip past the volume specification. // We are assuming that the user isn't really trying to rename across volumes. This is a safe assumption when the client is DWC. newFilename += 2; } + if (!EnsurePath(newFilename)) + { + return false; + } if (f_rename(oldFilename, newFilename) != FR_OK) { reprap.GetPlatform().MessageF(ErrorMessage, "Failed to rename file or directory %s to %s\n", oldFilename, newFilename); diff --git a/src/Storage/MassStorage.h b/src/Storage/MassStorage.h index c18438f2..603b871c 100644 --- a/src/Storage/MassStorage.h +++ b/src/Storage/MassStorage.h @@ -33,7 +33,7 @@ namespace MassStorage bool FindNext(FileInfo &file_info) noexcept; void AbandonFindNext() noexcept; bool Delete(const char* filePath) noexcept; - bool MakeDirectory(const char *parentDir, const char *dirName) noexcept; + bool EnsurePath(const char* filePath) noexcept; bool MakeDirectory(const char *directory) noexcept; bool Rename(const char *oldFilePath, const char *newFilePath) noexcept; bool FileExists(const char *filePath) noexcept; |