-
Notifications
You must be signed in to change notification settings - Fork 89
fix lack of rename #445
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
jinwjinl
wants to merge
2
commits into
vivoblueos:main
Choose a base branch
from
jinwjinl:fix_lack_of_rename
base: main
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.
Open
fix lack of rename #445
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -90,6 +90,7 @@ pub mod syscalls { | |
| TimerGetTime, | ||
| TimerSetTime, | ||
| TimerGetOverrun, | ||
| Rename, | ||
| LastNR, | ||
| } | ||
| } | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -433,6 +433,36 @@ pub fn link(old_path: *const c_char, new_path: *const c_char) -> c_int { | |
| } | ||
| } | ||
|
|
||
| pub fn rename(old_path: *const c_char, new_path: *const c_char) -> c_int { | ||
| if old_path.is_null() || new_path.is_null() { | ||
| return -libc::EINVAL; | ||
| } | ||
|
|
||
| let old_path = match unsafe { CStr::from_ptr(old_path).to_str() } { | ||
| Ok(path) => path, | ||
| Err(_) => return -libc::EINVAL, | ||
| }; | ||
| let new_path = match unsafe { CStr::from_ptr(new_path).to_str() } { | ||
| Ok(path) => path, | ||
| Err(_) => return -libc::EINVAL, | ||
| }; | ||
|
|
||
| let (old_dir, old_name) = match path::find_parent_and_name(old_path) { | ||
| Some(result) => result, | ||
| None => return -libc::ENOENT, | ||
| }; | ||
| let (new_dir, new_name) = match path::find_parent_and_name(new_path) { | ||
| Some(result) => result, | ||
| None => return -libc::ENOENT, | ||
| }; | ||
|
Collaborator
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. If there is a file with new_path, how to handle this case |
||
|
|
||
| debug!("[rename] {} -> {}", old_path, new_path); | ||
| match old_dir.rename(old_name, &new_dir, new_name) { | ||
| Ok(()) => 0, | ||
| Err(error) => error.to_errno(), | ||
| } | ||
| } | ||
|
|
||
| pub fn unlink(path: *const c_char) -> c_int { | ||
| if path.is_null() { | ||
| return -libc::EINVAL; | ||
|
|
@@ -930,6 +960,24 @@ mod tests { | |
| assert_eq!(result, code::ENOENT.to_errno()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_rename_invalid_path() { | ||
| assert_eq!( | ||
| rename(core::ptr::null(), TEST_PATH), | ||
| code::EINVAL.to_errno() | ||
| ); | ||
| assert_eq!( | ||
| rename(TEST_PATH, core::ptr::null()), | ||
| code::EINVAL.to_errno() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_rename_missing_source() { | ||
| let new_path = c"/test/new.txt".as_ptr() as *const c_char; | ||
| assert_eq!(rename(TEST_PATH, new_path), code::ENOENT.to_errno()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_dir() { | ||
| let result = open(TEST_DIR, libc::O_RDONLY, 0o755); | ||
|
|
||
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.
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.
Why is
Optionthere?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.
rust-fatfs::Dir::renamerequires that no liveFileinstance references the entry being renamed; otherwise the oldFilemay later update the original directory entry and corrupt the filesystem.BlueOS keeps a
fatfs::Filepersistently insideFatInode, soOption<File>allows the rename path totake()and drop that handle before callingDir::rename, then reopen the file under its new name and store it back afterward. If rename fails, the original file is reopened.The
Nonestate exists only during the rename critical section while the inode write lock is held. Other file operations returnEIOif this invariant is unexpectedly violated.Using
mem::replacewould still require a dummy liveFile, so it would not satisfy the library’s rename requirement.You can see it in https://github.com/rafalh/rust-fatfs/blob/c4b88477b22ca7e5131fbd8891f62a5deaa88e6e/src/dir.rs#L388