-
Notifications
You must be signed in to change notification settings - Fork 104
Support directory deletion #210
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
estebank
wants to merge
1
commit into
rust-embedded-community:develop
Choose a base branch
from
osdyne:esteban/directory-deletion
base: develop
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
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
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 |
|---|---|---|
|
|
@@ -665,26 +665,64 @@ where | |
| let data = data.deref_mut(); | ||
|
|
||
| let dir_idx = data.get_dir_by_id(directory)?; | ||
| let dir_info = &data.open_dirs[dir_idx]; | ||
| let volume_idx = data.get_volume_by_id(dir_info.raw_volume)?; | ||
| let parent_dir_info = data.open_dirs[dir_idx].clone(); | ||
| let volume_idx = data.get_volume_by_id(parent_dir_info.raw_volume)?; | ||
| let sfn = name.to_short_filename().map_err(Error::FilenameError)?; | ||
|
|
||
| let dir_entry = match &data.open_volumes[volume_idx].volume_type { | ||
| VolumeType::Fat(fat) => fat.find_directory_entry(&mut data.block_cache, dir_info, &sfn), | ||
| VolumeType::Fat(fat) => { | ||
| fat.find_directory_entry(&mut data.block_cache, &parent_dir_info, &sfn) | ||
| } | ||
| }?; | ||
|
|
||
| if dir_entry.attributes.is_directory() { | ||
| return Err(Error::DeleteDirAsFile); | ||
| } | ||
|
|
||
| if data.file_is_open(dir_info.raw_volume, &dir_entry) { | ||
| // Find the directory to be deleted, so that we can check its contents. | ||
| let dir_info = if let Some(dir_info) = data | ||
| .open_dirs | ||
| .iter() | ||
| .find(|dir_info| { | ||
| // Subdirectory is already open. | ||
| dir_info.cluster == dir_entry.cluster | ||
|
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. I think that if the directory you are deleting is already open, you shouldn't be able to delete it because it would leave a dangling entry. |
||
| }) | ||
| .cloned() | ||
| { | ||
| dir_info | ||
| } else { | ||
| // The subdirectory isn't yet open. Open it in order to be able to list it. | ||
| let raw_directory = RawDirectory(data.id_generator.generate()); | ||
| DirectoryInfo { | ||
| raw_directory, | ||
| raw_volume: data.open_volumes[volume_idx].raw_volume, | ||
| cluster: dir_entry.cluster, | ||
| } | ||
| }; | ||
| // Can only delete directories that are already empty. | ||
| let mut count = 0; | ||
| // Equivalent to `self.iterate_dir(raw_dir, |_| count += 1)?;`, without locking again. | ||
| match &data.open_volumes[volume_idx].volume_type { | ||
| VolumeType::Fat(fat) => { | ||
| fat.iterate_dir(&mut data.block_cache, &dir_info, |de| { | ||
| // Hide all the LFN directory entries | ||
| if !de.attributes.is_lfn() | ||
| && de.name != ShortFileName::this_dir() | ||
| && de.name != ShortFileName::parent_dir() | ||
| { | ||
| count += 1; | ||
| } | ||
| })?; | ||
| } | ||
| } | ||
| if count != 0 { | ||
| return Err(Error::DeleteNonEmptyDir); | ||
| } | ||
| } else if data.file_is_open(parent_dir_info.raw_volume, &dir_entry) { | ||
| return Err(Error::FileAlreadyOpen); | ||
| } | ||
|
|
||
| let volume_idx = data.get_volume_by_id(dir_info.raw_volume)?; | ||
| let volume_idx = data.get_volume_by_id(parent_dir_info.raw_volume)?; | ||
| match &data.open_volumes[volume_idx].volume_type { | ||
| VolumeType::Fat(fat) => { | ||
| fat.delete_directory_entry(&mut data.block_cache, dir_info, &sfn)? | ||
| fat.delete_directory_entry(&mut data.block_cache, &parent_dir_info, &sfn)? | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
I don't recall needing to clone an entry for an open directory before (unless I'm forgetting about it). What's the reason for the clone as opposed to borrowing the entry, or referring to it by index? This is probably fine, it just gave me pause as I read it.
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.
This is only needed if we want to allow deletion of currently open directories, because I'm creating a new
DirectoryInfoin a block and making it a borrow would require using some of the lifetime extension tricks, while the struct looked tiny enough that cloning seemed cleaner to do.