refactor(pkg/utils): add unified atomic file write utility#706
Open
mosir wants to merge 11 commits intosipeed:mainfrom
Open
refactor(pkg/utils): add unified atomic file write utility#706mosir wants to merge 11 commits intosipeed:mainfrom
mosir wants to merge 11 commits intosipeed:mainfrom
Conversation
mengzhuo
reviewed
Feb 24, 2026
pkg/utils/file.go
Outdated
| // | ||
| // Copyright (c) 2026 PicoClaw contributors | ||
|
|
||
| package utils |
Collaborator
There was a problem hiding this comment.
I think we need a better package name instead of utils
| // | ||
| // // Public readable file | ||
| // err := utils.WriteFileAtomic("public.txt", data, 0o644) | ||
| func WriteFileAtomic(path string, data []byte, perm os.FileMode) error { |
Collaborator
There was a problem hiding this comment.
The way you implement "atomicwrite" just using tmpfile for the job, it will corrupt the file if write to tmp file failed too.
Author
There was a problem hiding this comment.
You're correct. I've fixed the issue。
Collaborator
|
Please run |
Author
Done — I ran golangci-lint with --fix and pushed the follow-up commit. The import-order/gci issues are resolved on |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a unified atomic file write utility (
WriteFileAtomic) that eliminates code duplicationacross 9 locations while improving data reliability on low-power edge devices.
Problem
The existing atomic write pattern (temp file + rename) was missing explicit
Sync()calls in multiplelocations. On flash storage with write caching (SD cards, eMMC, SPI flash), this could lead to data corruption
on sudden power loss:
Solution
Created
pkg/utils/file.gowithWriteFileAtomic()function that:Sync()before rename to ensure data is physically writtenChanges
New file:
pkg/utils/file.go- Unified atomic write utility (97 lines)Refactored files (all now use
utils.WriteFileAtomic):pkg/tools/filesystem.go- hostFs, sandboxFspkg/config/config.go- SaveConfigpkg/auth/store.go- SaveStorepkg/cron/service.go- saveStoreUnsafepkg/agent/memory.go- WriteLongTerm, AppendTodaypkg/heartbeat/service.go- HEARTBEAT.md creation (0o644, user-editable)pkg/skills/installer.go- skill installationpkg/tools/skills_install.go- skill metadatapkg/state/state.go- saveAtomicBenefits
Testing
All existing tests pass:
est ./pkg/tools/... ./pkg/config/... ./pkg/auth/... ./pkg/cron/... ./pkg/agent/... ./pkg/state/...
g/utils/...
Note: Some tests fail on Windows due to pre-existing platform-specific permission handling issues (unrelated
to this PR).
Security Notes
HEARTBEAT.mduses 0o644 intentionally as it's a user-editable config file without sensitivedata
Related Issues
Fixes potential data corruption on power loss for edge devices running PicoClaw on flash storage (SD cards,
eMMC, SPI flash).