Skip to content

Fixing file deployent (nanoff or Visual Studio) for ChibiOS targets#3494

Open
Ellerbach wants to merge 6 commits into
mainfrom
fix-rp2350-file
Open

Fixing file deployent (nanoff or Visual Studio) for ChibiOS targets#3494
Ellerbach wants to merge 6 commits into
mainfrom
fix-rp2350-file

Conversation

@Ellerbach

Copy link
Copy Markdown
Member

Description

Fixing file deployent (nanoff or Visual Studio) for ChibiOS targets

Motivation and Context

  • Fixing file deployent (nanoff or Visual Studio) for ChibiOS targets

How Has This Been Tested?

On real RP devices

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dev Containers (changes related with Dev Containers, has no impact on code or features)
  • Dependencies/declarations (update dependencies or assembly declarations and changes associated, has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Ellerbach, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 11 seconds

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 72ce9155-250a-43f4-9acc-e8aed1199d2b

📥 Commits

Reviewing files that changed from the base of the PR and between 0943811 and 8f93006.

📒 Files selected for processing (1)
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp
📝 Walkthrough

Walkthrough

LittleFS storage operations now normalize incoming paths, create intermediate directories safely, persist custom file attributes during writes and appends, and explicitly handle deletion results.

Changes

LittleFS storage operations

Layer / File(s) Summary
Path normalization and directory creation
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp
create_directories now mutates and validates path components without a fixed buffer. Storage paths strip drive prefixes and convert backslashes to forward slashes.
Write and append attribute persistence
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp
Write and append use normalized paths, create intermediate directories, open files with custom LittleFS attributes, sync after opening, and validate write results.
Delete path and result handling
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp
Delete uses the normalized path and accepts both successful removal and missing entries.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HAL_StorageOperation
  participant create_directories
  participant LittleFS
  HAL_StorageOperation->>HAL_StorageOperation: Normalize storageName into lfsPath
  HAL_StorageOperation->>create_directories: Create intermediate directories
  create_directories->>LittleFS: Create or validate directories
  HAL_StorageOperation->>LittleFS: Open file with NANO_LITTLEFS_ATTRIBUTE
  HAL_StorageOperation->>LittleFS: Sync attributes
  HAL_StorageOperation->>LittleFS: Write or append data
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is clearly related to the PR’s file deployment fix for ChibiOS targets.
Description check ✅ Passed The description matches the code changes and objective of fixing file deployment on ChibiOS targets.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp (1)

171-217: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Write and Append branches duplicate the mkdir/opencfg/attribute-sync logic almost verbatim.

The two ~40-line blocks (directory extraction, create_directories call, lfs_attr/lfs_file_config setup, lfs_file_opencfg, lfs_file_sync) differ only in the open flags. Any future fix (e.g. the ignored lfs_file_sync return above) has to be applied twice, and already risks drifting.

♻️ Sketch of a shared helper
static uint32_t OpenLittleFsFileWithAttributes(
    lfs_t *lfsDrive,
    lfs_file_t *lfsFile,
    const char *lfsPath,
    char *dirPath,
    int openFlags)
{
    if (dirPath[0] != '\0')
    {
        if (create_directories(lfsDrive, dirPath) != 0)
        {
            return StorageOperationErrorCode::WriteError;
        }
    }

    uint32_t nanoAttributes = kFileAttributesNormal;
    struct lfs_attr attr = {NANO_LITTLEFS_ATTRIBUTE, &nanoAttributes, NANO_LITTLEFS_ATTRIBUTE_SIZE};
    struct lfs_file_config fileConfig = {.buffer = NULL, .attrs = &attr, .attr_count = 1};

    if (lfs_file_opencfg(lfsDrive, lfsFile, lfsPath, openFlags, &fileConfig) != LFS_ERR_OK)
    {
        return StorageOperationErrorCode::WriteError;
    }

    if (lfs_file_sync(lfsDrive, lfsFile) != LFS_ERR_OK)
    {
        lfs_file_close(lfsDrive, lfsFile);
        return StorageOperationErrorCode::WriteError;
    }

    return StorageOperationErrorCode::NoError;
}

Call this once with LFS_O_RDWR | LFS_O_CREAT for Write and LFS_O_RDWR | LFS_O_APPEND | LFS_O_CREAT for Append.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp` around lines 171 -
217, Extract the duplicated directory creation, attribute configuration, file
opening, and synchronization logic from the Write and Append branches into a
shared helper such as OpenLittleFsFileWithAttributes. Have it return the
appropriate StorageOperationErrorCode, close the file and report WriteError when
lfs_file_sync fails, and invoke it with the existing Write and Append-specific
open flags.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp`:
- Around line 205-206: Handle the return value of lfs_file_sync in the
custom-attribute persistence path before the subsequent write. If syncing fails,
stop the operation, perform the same file-handle cleanup used by this function,
and propagate the failure so the write does not proceed with an unsaved
attribute.
- Around line 23-25: Rename the constant kFileAttributesNormal to follow the
repository’s c_ naming convention, and update both usage sites in the Write and
Append branches to reference the new name.
- Line 36: Change the file-scope helper create_directories to have internal
linkage by marking its definition static. Leave its signature and behavior
unchanged.
- Around line 126-149: The path normalization before create_directories() only
removes leading separators for drive-prefixed paths. Update the lfsPath
normalization in the storage operation flow so bare-rooted inputs such as \foo
and /foo also have all leading separators removed before directory creation,
while preserving drive-prefix stripping and separator conversion.

---

Outside diff comments:
In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp`:
- Around line 171-217: Extract the duplicated directory creation, attribute
configuration, file opening, and synchronization logic from the Write and Append
branches into a shared helper such as OpenLittleFsFileWithAttributes. Have it
return the appropriate StorageOperationErrorCode, close the file and report
WriteError when lfs_file_sync fails, and invoke it with the existing Write and
Append-specific open flags.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 17e7574c-5744-4e0f-9e7e-825efdd8566b

📥 Commits

Reviewing files that changed from the base of the PR and between 6836b4b and f7c698e.

📒 Files selected for processing (1)
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp

Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp Outdated
Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp Outdated
Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp
Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp Outdated
@josesimoes josesimoes added the Platform: ChibiOS Everything related specifically with ChibiOS platform label Jul 23, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp (2)

160-177: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

The fixed 256-byte directory buffer truncates paths in both branches.

  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp#L160-L177: derive the directory path in place from lfsPath, or allocate it based on the full path length.
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp#L227-L244: apply the same non-truncating directory extraction to Append.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp` around lines 160 -
177, Replace the fixed 256-byte directory buffers in the directory-extraction
logic for both the primary storage operation (lines 160-177) and Append (lines
227-244) with in-place extraction from lfsPath or storage sized to the complete
path length; preserve root-path handling and pass the full directory path to
create_directories without truncation.

215-223: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Check lfs_file_close() before reporting write success.

LittleFS performs pending sync operations in lfs_file_close(), so both the Write and Append paths can return NoError even when persistence fails. Map failed closes at both sites to WriteError and follow the failure path instead of falling through to success.

targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:223: map Write close failures to WriteError.
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:286: map Append close failures to WriteError.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp` around lines 215 -
223, Update the Write path around lfs_file_close() at
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:215-223 to check its
return value, map a failed close to StorageOperationErrorCode::WriteError, and
follow the existing failure path instead of reporting success; apply the same
close-result handling to the Append path at
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:276-284.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp`:
- Around line 160-177: Replace the fixed 256-byte directory buffers in the
directory-extraction logic for both the primary storage operation (lines
160-177) and Append (lines 227-244) with in-place extraction from lfsPath or
storage sized to the complete path length; preserve root-path handling and pass
the full directory path to create_directories without truncation.
- Around line 215-223: Update the Write path around lfs_file_close() at
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:215-223 to check its
return value, map a failed close to StorageOperationErrorCode::WriteError, and
follow the existing failure path instead of reporting success; apply the same
close-result handling to the Append path at
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp:276-284.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: afa08a5c-94e4-4171-a007-4bf725e6caaf

📥 Commits

Reviewing files that changed from the base of the PR and between f7c698e and 1033f06.

📒 Files selected for processing (1)
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp`:
- Around line 23-25: Rename the constant c_kFileAttributesNormal to
c_FileAttributesNormal and update both nanoAttributes assignments to use the new
name, preserving the existing value and behavior.
- Around line 227-277: Update the Append branch around lfs_file_opencfg to
zero-initialize lfsFile before opening it, matching the Write branch. Check the
lfs_file_sync result before writing and closing; on failure, set the same
WriteError and follow the existing cleanup path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2daa1601-ad19-4879-a471-9b2b8a74e5b9

📥 Commits

Reviewing files that changed from the base of the PR and between 1033f06 and 01a07bb.

📒 Files selected for processing (1)
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp

Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp Outdated
Comment thread targets/ChibiOS/_common/targetHAL_StorageOperation.cpp Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp (2)

160-177: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Avoid truncating paths before creating parent directories.

lfsPath can exceed 255 bytes, but both branches silently truncate it in dir_path; directories are created for the truncated prefix while LittleFS opens the original path, leaving its actual parent missing.

  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp#L160-L177: derive the directory buffer from the full lfsPath length or reject overlong paths.
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp#L229-L246: apply the same full-length handling in Append.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp` around lines 160 -
177, The directory extraction logic in the write path and the corresponding
Append path must not truncate lfsPath before creating parent directories. Update
both sites in targets/ChibiOS/_common/targetHAL_StorageOperation.cpp (lines
160-177 and 229-246) to use the full path length or reject overlong paths before
directory creation, preserving the existing root-directory handling and LittleFS
open behavior.

207-215: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Propagate failed final file closes in the LittleFS write paths.

lfs_file_sync before the write only persists the new attribute, not the payload. After the write, a failing lfs_file_close can leave the write uncommitted but the function still reports success. Update both write branches to check the final lfs_file_close return and set WriteError on failure at lines 223 and 291.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp` around lines 207 -
215, Update both LittleFS write branches in
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp at lines 207-215 and
274-280: check the final lfs_file_close return after writing, and set errorCode
to StorageOperationErrorCode::WriteError when it fails so the write operation
does not report success. The related close calls at lines 223 and 291 require
this handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@targets/ChibiOS/_common/targetHAL_StorageOperation.cpp`:
- Around line 160-177: The directory extraction logic in the write path and the
corresponding Append path must not truncate lfsPath before creating parent
directories. Update both sites in
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp (lines 160-177 and
229-246) to use the full path length or reject overlong paths before directory
creation, preserving the existing root-directory handling and LittleFS open
behavior.
- Around line 207-215: Update both LittleFS write branches in
targets/ChibiOS/_common/targetHAL_StorageOperation.cpp at lines 207-215 and
274-280: check the final lfs_file_close return after writing, and set errorCode
to StorageOperationErrorCode::WriteError when it fails so the write operation
does not report success. The related close calls at lines 223 and 291 require
this handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 6b9f1ea7-d534-4f51-ab42-3d624680e4c0

📥 Commits

Reviewing files that changed from the base of the PR and between 01a07bb and 0943811.

📒 Files selected for processing (1)
  • targets/ChibiOS/_common/targetHAL_StorageOperation.cpp

@josesimoes josesimoes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm all for verbose comments when something requires explanation or is not obvious, but most of these comments look like a bit too much... if we keep this pace, we'll end up with as many comment lines as code lines with the explanation of every code block. 😅

@Ellerbach

Copy link
Copy Markdown
Member Author

I'm all for verbose comments when something requires explanation or is not obvious, but most of these comments look like a bit too much... if we keep this pace, we'll end up with as many comment lines as code lines with the explanation of every code block. 😅

Well noticed for next time :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Platform: ChibiOS Everything related specifically with ChibiOS platform Type: bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants