fs: unify Write and Replace(id) (#27)

This commit is contained in:
bitful-pannul 2023-10-19 02:51:20 +02:00 committed by GitHub
parent c9010147d6
commit 519e04f790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 35 deletions

View File

@ -535,15 +535,17 @@ async fn handle_request(
// println!("got action! {:?}", action);
let (ipc, bytes) = match action {
FsAction::Write => {
FsAction::Write(maybe_file_id) => {
let Some(ref payload) = payload else {
return Err(FsError::BadBytes {
action: "Write".into(),
});
};
// println!("fs: got write from {:?} with len {:?}", source.process, &payload.bytes.len());
let file_uuid = match maybe_file_id {
Some(id) => FileIdentifier::UUID(id),
None => FileIdentifier::new_uuid(),
};
let file_uuid = FileIdentifier::new_uuid();
match manifest.write(&file_uuid, &payload.bytes).await {
Ok(_) => (),
Err(e) => {
@ -606,26 +608,6 @@ async fn handle_request(
Ok(bytes) => (FsResponse::Read(req.file), Some(bytes)),
}
}
FsAction::Replace(old_file_uuid) => {
let Some(ref payload) = payload else {
return Err(FsError::BadBytes {
action: "Write".into(),
});
};
let file = FileIdentifier::UUID(old_file_uuid);
match manifest.write(&file, &payload.bytes).await {
Ok(_) => (),
Err(e) => {
return Err(FsError::WriteFailed {
file: old_file_uuid,
error: format!("replace error: {}", e),
})
}
}
(FsResponse::Write(old_file_uuid), None)
}
FsAction::Delete(del) => {
let file = FileIdentifier::UUID(del);
manifest.delete(&file).await?;

View File

@ -415,8 +415,7 @@ pub struct PackageManifestEntry {
#[derive(Serialize, Deserialize, Debug)]
pub enum FsAction {
Write,
Replace(u128),
Write(Option<u128>),
WriteOffset((u128, u64)),
Append(Option<u128>),
Read(u128),

View File

@ -782,9 +782,11 @@ async fn match_request(
panic!("empty path");
};
{
let hash = {
let mut vfs = vfs.lock().await;
if vfs.path_to_key.contains_key(&full_path) {
if !vfs.path_to_key.contains_key(&full_path) {
None
} else {
send_to_terminal
.send(Printout {
verbosity: 1,
@ -792,12 +794,18 @@ async fn match_request(
})
.await
.unwrap();
let Some(old_key) = vfs.path_to_key.remove(&full_path) else {
panic!("");
};
vfs.key_to_entry.remove(&old_key);
};
}
match vfs.path_to_key.remove(&full_path) {
None => None,
Some(key) => {
let Key::File { id: hash } = key else {
panic!("");
};
Some(hash)
}
}
// vfs.key_to_entry.remove(&old_key);
}
};
let _ = send_to_loop
.send(KernelMessage {
@ -814,7 +822,7 @@ async fn match_request(
message: Message::Request(Request {
inherit: true,
expects_response: Some(5), // TODO evaluate
ipc: Some(serde_json::to_string(&FsAction::Write).unwrap()),
ipc: Some(serde_json::to_string(&FsAction::Write(hash)).unwrap()),
metadata: None,
}),
payload,
@ -826,6 +834,7 @@ async fn match_request(
let Message::Response((Response { ipc, .. }, None)) = message else {
panic!("");
};
let Some(ipc) = ipc else {
panic!("");
};
@ -840,9 +849,11 @@ async fn match_request(
let Some(parent_key) = vfs.path_to_key.remove(&parent_path) else {
panic!("");
};
let Some(mut parent_entry) = vfs.key_to_entry.remove(&parent_key) else {
panic!("");
};
let EntryType::Dir {
children: ref mut parent_children,
..
@ -961,6 +972,30 @@ async fn match_request(
(is_file, is_dir, full_path, file_contents)
};
if is_file {
let hash = {
let vfs = vfs.lock().await;
if !vfs.path_to_key.contains_key(&full_path) {
None
} else {
send_to_terminal
.send(Printout {
verbosity: 1,
content: format!("vfs: overwriting file {}", full_path),
})
.await
.unwrap();
match vfs.path_to_key.get(&full_path) {
None => None,
Some(key) => {
let Key::File { id: hash } = key else {
panic!("");
};
Some(*hash)
}
}
// vfs.key_to_entry.remove(&old_key);
}
};
let _ = send_to_loop
.send(KernelMessage {
id,
@ -976,7 +1011,9 @@ async fn match_request(
message: Message::Request(Request {
inherit: true,
expects_response: Some(5), // TODO evaluate
ipc: Some(serde_json::to_string(&FsAction::Write).unwrap()),
ipc: Some(
serde_json::to_string(&FsAction::Write(hash)).unwrap(),
),
metadata: None,
}),
payload: Some(Payload {