-
Notifications
You must be signed in to change notification settings - Fork 6
fix: add Sync() calls to persist filesystem changes to storage #27
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
Open
marcelfarres
wants to merge
1
commit into
tinygo-org:dev
Choose a base branch
from
marcelfarres:release
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+144
−10
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,24 +186,48 @@ func (l *LFS) Mount() error { | |
| } | ||
|
|
||
| func (l *LFS) Format() error { | ||
| return errval(C.lfs_format(l.lfs, l.cfg)) | ||
| if err := errval(C.lfs_format(l.lfs, l.cfg)); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l *LFS) Unmount() error { | ||
| return errval(C.lfs_unmount(l.lfs)) | ||
| if err := errval(C.lfs_unmount(l.lfs)); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l *LFS) Remove(path string) error { | ||
| cs := cstring(path) | ||
| defer C.free(unsafe.Pointer(cs)) | ||
| return errval(C.lfs_remove(l.lfs, cs)) | ||
| if err := errval(C.lfs_remove(l.lfs, cs)); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l *LFS) Rename(oldPath string, newPath string) error { | ||
| cs1, cs2 := cstring(oldPath), cstring(newPath) | ||
| defer C.free(unsafe.Pointer(cs1)) | ||
| defer C.free(unsafe.Pointer(cs2)) | ||
| return errval(C.lfs_rename(l.lfs, cs1, cs2)) | ||
| if err := errval(C.lfs_rename(l.lfs, cs1, cs2)); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l *LFS) Stat(path string) (os.FileInfo, error) { | ||
|
|
@@ -223,7 +247,13 @@ func (l *LFS) Stat(path string) (os.FileInfo, error) { | |
| func (l *LFS) Mkdir(path string, _ os.FileMode) error { | ||
| cs := (*C.char)(cstring(path)) | ||
| defer C.free(unsafe.Pointer(cs)) | ||
| return errval(C.lfs_mkdir(l.lfs, cs)) | ||
| if err := errval(C.lfs_mkdir(l.lfs, cs)); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (l *LFS) Open(path string) (tinyfs.File, error) { | ||
|
|
@@ -261,6 +291,24 @@ func (l *LFS) OpenFile(path string, flags int) (tinyfs.File, error) { | |
| return nil, err | ||
| } | ||
|
|
||
| if flags&(os.O_CREATE|os.O_TRUNC) != 0 { | ||
| if flags&os.O_TRUNC != 0 { | ||
| if err := errval(C.lfs_file_sync(l.lfs, file.fileptr())); err != nil { | ||
| file.Close() | ||
| return nil, err | ||
| } | ||
| } | ||
| if syncer, ok := l.dev.(tinyfs.Syncer); ok { | ||
| if err := syncer.Sync(); err != nil { | ||
| // Should we close and return error? | ||
| // Maybe just log? But we can't log. | ||
| // For now let's just return error and close. | ||
| file.Close() | ||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return file, nil | ||
| } | ||
|
|
||
|
|
@@ -305,14 +353,21 @@ func (f *File) Close() error { | |
| C.free(f.hndl) | ||
| f.hndl = nil | ||
| }() | ||
| var err error | ||
| switch f.typ { | ||
| case fileTypeReg: | ||
| return errval(C.lfs_file_close(f.lfs.lfs, f.fileptr())) | ||
| err = errval(C.lfs_file_close(f.lfs.lfs, f.fileptr())) | ||
| case fileTypeDir: | ||
| return errval(C.lfs_dir_close(f.lfs.lfs, f.dirptr())) | ||
| err = errval(C.lfs_dir_close(f.lfs.lfs, f.dirptr())) | ||
| default: | ||
| panic("lfs: unknown typ for file handle") | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := f.lfs.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
@@ -373,12 +428,27 @@ func (f *File) Stat() (os.FileInfo, error) { | |
|
|
||
| // Sync synchronizes to storage so that any pending writes are written out. | ||
| func (f *File) Sync() error { | ||
| return errval(C.lfs_file_sync(f.lfs.lfs, f.fileptr())) | ||
| if err := errval(C.lfs_file_sync(f.lfs.lfs, f.fileptr())); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := f.lfs.dev.(tinyfs.Syncer); ok { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually |
||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Truncate the size of the file to the specified size | ||
| func (f *File) Truncate(size uint32) error { | ||
| return errval(C.lfs_file_truncate(f.lfs.lfs, f.fileptr(), C.lfs_off_t(size))) | ||
| if err := errval(C.lfs_file_truncate(f.lfs.lfs, f.fileptr(), C.lfs_off_t(size))); err != nil { | ||
| return err | ||
| } | ||
| if err := errval(C.lfs_file_sync(f.lfs.lfs, f.fileptr())); err != nil { | ||
| return err | ||
| } | ||
| if syncer, ok := f.lfs.dev.(tinyfs.Syncer); ok { | ||
| return syncer.Sync() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (f *File) Write(buf []byte) (n int, err error) { | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think it makes sense to call
Sync()on anOpen().