Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/HAL/nanoHAL_StorageOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t o
char dirPath[FS_MAX_DIRECTORY_LENGTH];
char *lastSeparator;
int bytesWritten = 0;
HRESULT deleteResult;

// extract parent directory from relative path and create it if needed
snprintf(dirPath, sizeof(dirPath), "%s", relativePath);
Expand All @@ -74,6 +75,17 @@ uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t o
}
}

// Open() below doesn't truncate, so remove an existing file to start the write from an empty one
deleteResult = volume->Delete(relativePath, false);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// a missing file, or a volume without Delete, is the normal case; any other failure would leave
// the previous content in place and the write would produce a file with a stale tail
if (FAILED(deleteResult) && deleteResult != CLR_E_FILE_NOT_FOUND && deleteResult != CLR_E_NOT_SUPPORTED)
{
errorCode = StorageOperationErrorCode::WriteError;
goto done;
}

// open the file (creates it, if it doesn't exist)
if (FAILED(volume->Open(relativePath, fileHandle)))
{
Expand Down
Loading