-
-
Notifications
You must be signed in to change notification settings - Fork 193
Rework implementation of StorageOperation command #3502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josesimoes
merged 5 commits into
nanoframework:main
from
josesimoes:improve-storage-operation
Jul 27, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b2305e
Rework implementation of StorageOperation command
josesimoes e853667
Fixes from code review
josesimoes 6e4feb1
Discarding offset parameter
josesimoes bd13a77
Now checking offset against file size
josesimoes 6f1d59d
Remove unused var
josesimoes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // | ||
| // Copyright (c) .NET Foundation and Contributors | ||
| // See LICENSE file in the project root for full license information. | ||
| // | ||
|
|
||
| #include <nanoHAL.h> | ||
| #include <nanoHAL_v2.h> | ||
| #include <nanoWeak.h> | ||
| #include <nanoCLR_Runtime.h> | ||
| #include <nanoCLR_Types.h> | ||
| #include <nanoPAL_FileSystem.h> | ||
| #include <nanoCLR_FileStream.h> | ||
| #include <nanoHAL_StorageOperation.h> | ||
| #include <WireProtocol_MonitorCommands.h> | ||
|
|
||
| #if CONFIG_NF_FEATURE_HAS_ACCESSIBLE_STORAGE | ||
|
|
||
| uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t offset, uint8_t *data) | ||
| { | ||
| char *storageName = NULL; | ||
| char *rootName = NULL; | ||
| char *relativePath = NULL; | ||
| uint8_t *fileData = NULL; | ||
| uint32_t rootNameLength = 0; | ||
| FileSystemVolume *volume = NULL; | ||
| StorageOperationErrorCode errorCode = StorageOperationErrorCode::NoError; | ||
|
|
||
| // point to the storage name in the data buffer | ||
| storageName = (char *)data; | ||
|
|
||
| // split the path into root name and relative path | ||
| if (FAILED(CLR_RT_FileStream::SplitFilePath(storageName, rootName, rootNameLength, relativePath))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::PlatformError; | ||
| goto done; | ||
| } | ||
|
|
||
| // find the volume for this path | ||
| volume = FileSystemVolumeList::FindVolume(rootName, rootNameLength); | ||
|
|
||
| if (volume == NULL) | ||
| { | ||
| errorCode = StorageOperationErrorCode::PlatformError; | ||
| goto done; | ||
| } | ||
|
|
||
| // point to the file data in the data buffer (after the storage name and null terminator) | ||
| fileData = (uint8_t *)(data + hal_strlen_s(storageName) + 1); | ||
|
|
||
| if (operation == StorageOperation_Monitor::StorageOperation_Write) | ||
| { | ||
| void *fileHandle = NULL; | ||
| char dirPath[FS_MAX_DIRECTORY_LENGTH]; | ||
| char *lastSeparator; | ||
| int bytesWritten = 0; | ||
|
|
||
| // extract parent directory from relative path and create it if needed | ||
| snprintf(dirPath, sizeof(dirPath), "%s", relativePath); | ||
|
|
||
| lastSeparator = strrchr(dirPath, '\\'); | ||
| if (lastSeparator == NULL) | ||
| { | ||
| lastSeparator = strrchr(dirPath, '/'); | ||
| } | ||
|
|
||
| if (lastSeparator != NULL && lastSeparator != dirPath) | ||
| { | ||
| *lastSeparator = '\0'; | ||
|
|
||
| if (FAILED(volume->CreateDirectory(dirPath))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| goto done; | ||
| } | ||
| } | ||
|
|
||
| // open the file (creates it, if it doesn't exist) | ||
| if (FAILED(volume->Open(relativePath, fileHandle))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| goto done; | ||
| } | ||
|
|
||
| // write the data | ||
| if (FAILED(volume->Write(fileHandle, fileData, (int)dataLength, &bytesWritten))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| volume->Close(fileHandle); | ||
| goto done; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // close file | ||
| volume->Close(fileHandle); | ||
|
|
||
| if ((uint32_t)bytesWritten != dataLength) | ||
| { | ||
| // failed to write expected number of bytes | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| } | ||
| } | ||
| else if (operation == StorageOperation_Monitor::StorageOperation_Append) | ||
| { | ||
| void *fileHandle = NULL; | ||
| int64_t position; | ||
| int bytesWritten = 0; | ||
|
|
||
| // open the file | ||
| if (FAILED(volume->Open(relativePath, fileHandle))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| goto done; | ||
| } | ||
|
|
||
| // seek to the end, to append | ||
| if (FAILED(volume->Seek(fileHandle, 0, SEEKORIGIN_END, &position))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| volume->Close(fileHandle); | ||
| goto done; | ||
| } | ||
|
|
||
| // validate that the seek position matches the offset provided | ||
| if (position != offset) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| volume->Close(fileHandle); | ||
| goto done; | ||
| } | ||
|
|
||
| // append the data | ||
| if (FAILED(volume->Write(fileHandle, fileData, (int)dataLength, &bytesWritten))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| volume->Close(fileHandle); | ||
| goto done; | ||
| } | ||
|
|
||
| // close file | ||
| volume->Close(fileHandle); | ||
|
|
||
| if ((uint32_t)bytesWritten != dataLength) | ||
| { | ||
| // failed to write expected number of bytes | ||
| errorCode = StorageOperationErrorCode::WriteError; | ||
| } | ||
| } | ||
| else if (operation == StorageOperation_Monitor::StorageOperation_Delete) | ||
| { | ||
| // remove the file | ||
| if (FAILED(volume->Delete(relativePath, false))) | ||
| { | ||
| errorCode = StorageOperationErrorCode::DeleteError; | ||
| } | ||
| } | ||
|
|
||
| done: | ||
|
|
||
| return errorCode; | ||
| } | ||
|
|
||
| #endif // CONFIG_NF_FEATURE_HAS_ACCESSIBLE_STORAGE | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.