Skip to content

Commit

Permalink
chore: improve 'delete_folder' API
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Oct 23, 2024
1 parent 3cf65e4 commit cfeed3a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ strip = true
opt-level = 's'

[workspace.package]
version = "0.9.7"
version = "0.9.8"
edition = "2021"
repository = "https://github.com/ldclabs/ic-oss"
keywords = ["file", "storage", "oss", "s3", "icp"]
Expand Down
2 changes: 1 addition & 1 deletion src/ic_oss_bucket/src/api_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn delete_folder(id: u32, access_token: Option<ByteBuf>) -> Result<bool, String>
};

store::fs::delete_folder(id, now_ms, |folder| {
match permission::check_file_delete(&ctx.ps, &canister, folder.parent) {
match permission::check_folder_delete(&ctx.ps, &canister, folder.parent) {
true => Ok(()),
false => Err("permission denied".to_string()),
}
Expand Down
59 changes: 45 additions & 14 deletions src/ic_oss_bucket/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,20 +722,14 @@ impl FoldersTree {
});
}

fn delete_folder(
&mut self,
id: u32,
now_ms: u64,
checker: impl FnOnce(&FolderMetadata) -> Result<(), String>,
) -> Result<bool, String> {
fn delete_folder(&mut self, id: u32, now_ms: u64) -> Result<bool, String> {
if id == 0 {
Err("root folder cannot be deleted".to_string())?;
}

let parent_id = match self.get(&id) {
None => return Ok(false),
Some(folder) => {
checker(folder)?;
if folder.status > 0 {
Err("folder is readonly".to_string())?;
}
Expand Down Expand Up @@ -1349,7 +1343,44 @@ pub mod fs {
now_ms: u64,
checker: impl FnOnce(&FolderMetadata) -> Result<(), String>,
) -> Result<bool, String> {
FOLDERS.with(|r| r.borrow_mut().delete_folder(id, now_ms, checker))
if id == 0 {
Err("root folder cannot be deleted".to_string())?;
}

FOLDERS.with(|r| {
let mut folders = r.borrow_mut();
let folder = folders.parent_to_update(id)?;
let files = folder.files.clone();
checker(&folder)?;

FS_METADATA_STORE.with(|r| {
let mut fs_metadata = r.borrow_mut();

FS_CHUNKS_STORE.with(|r| {
let mut fs_data = r.borrow_mut();
for id in files {
match fs_metadata.get(&id) {
Some(file) => {
if file.status < 1 && fs_metadata.remove(&id).is_some() {
folder.files.remove(&id);
if let Some(hash) = file.hash {
HASHS.with(|r| r.borrow_mut().remove(&hash));
}

for i in 0..file.chunks {
fs_data.remove(&FileId(id, i));
}
}
}
None => {
folder.files.remove(&id);
}
}
}
});
});
folders.delete_folder(id, now_ms)
})
}

pub fn delete_file(
Expand Down Expand Up @@ -2139,11 +2170,11 @@ mod test {
fn test_folders_delete_folder() {
let mut tree = FoldersTree::new();
assert!(tree
.delete_folder(0, 99, |_| Ok(()))
.delete_folder(0, 99)
.err()
.unwrap()
.contains("root folder cannot be deleted"));
assert!(!tree.delete_folder(1, 99, |_| Ok(())).unwrap());
assert!(!tree.delete_folder(1, 99).unwrap());
tree.add_folder(
FolderMetadata {
parent: 0,
Expand All @@ -2158,25 +2189,25 @@ mod test {
)
.unwrap();
assert!(tree
.delete_folder(1, 99, |_| Ok(()))
.delete_folder(1, 99)
.err()
.unwrap()
.contains("folder is readonly"));
tree.get_mut(&1).unwrap().status = 0;
assert!(tree
.delete_folder(1, 99, |_| Ok(()))
.delete_folder(1, 99)
.err()
.unwrap()
.contains("folder is not empty"));
tree.get_mut(&1).unwrap().files.clear();
tree.get_mut(&0).unwrap().status = 1;
assert!(tree
.delete_folder(1, 99, |_| Ok(()))
.delete_folder(1, 99)
.err()
.unwrap()
.contains("parent folder is not writable"));
tree.get_mut(&0).unwrap().status = 0;
assert!(tree.delete_folder(1, 99, |_| Ok(())).unwrap());
assert!(tree.delete_folder(1, 99).unwrap());
assert_eq!(tree.len(), 1);
assert_eq!(tree.get_mut(&0).unwrap().folders, BTreeSet::new());
assert_eq!(tree.get_mut(&0).unwrap().updated_at, 99);
Expand Down

0 comments on commit cfeed3a

Please sign in to comment.