Skip to content

Commit 03d3fc1

Browse files
committed
add timeouts to helper functions too
1 parent 75282ad commit 03d3fc1

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/vfs/directory.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl Directory {
2020
let message = Request::new()
2121
.target(("our", "vfs", "distro", "sys"))
2222
.body(serde_json::to_vec(&request)?)
23-
.send_and_await_response(5)?;
23+
.send_and_await_response(self.timeout)?;
2424

2525
match message {
2626
Ok(Message::Response { body, .. }) => {
@@ -73,7 +73,9 @@ pub fn open_dir(path: &str, create: bool, timeout: Option<u64>) -> anyhow::Resul
7373
}
7474

7575
/// Removes a dir at path, errors if path not found or path is not a directory.
76-
pub fn remove_dir(path: &str) -> anyhow::Result<()> {
76+
pub fn remove_dir(path: &str, timeout: Option<u64>) -> anyhow::Result<()> {
77+
let timeout = timeout.unwrap_or(5);
78+
7779
let request = VfsRequest {
7880
path: path.to_string(),
7981
action: VfsAction::RemoveDir,
@@ -82,7 +84,7 @@ pub fn remove_dir(path: &str) -> anyhow::Result<()> {
8284
let message = Request::new()
8385
.target(("our", "vfs", "distro", "sys"))
8486
.body(serde_json::to_vec(&request)?)
85-
.send_and_await_response(5)?;
87+
.send_and_await_response(timeout)?;
8688

8789
match message {
8890
Ok(Message::Response { body, .. }) => {

src/vfs/file.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,15 +373,21 @@ impl File {
373373
/// Creates a drive with path "/package_id/drive", gives you read and write caps.
374374
/// Will only work on the same package_id as you're calling it from, unless you
375375
/// have root capabilities.
376-
pub fn create_drive(package_id: PackageId, drive: &str) -> anyhow::Result<String> {
376+
pub fn create_drive(
377+
package_id: PackageId,
378+
drive: &str,
379+
timeout: Option<u64>,
380+
) -> anyhow::Result<String> {
381+
let timeout = timeout.unwrap_or(5);
382+
377383
let path = format!("/{}/{}", package_id, drive);
378384
let res = Request::new()
379385
.target(("our", "vfs", "distro", "sys"))
380386
.body(serde_json::to_vec(&VfsRequest {
381387
path: path.clone(),
382388
action: VfsAction::CreateDrive,
383389
})?)
384-
.send_and_await_response(5)?;
390+
.send_and_await_response(timeout)?;
385391

386392
match res {
387393
Ok(Message::Response { body, .. }) => {
@@ -397,7 +403,9 @@ pub fn create_drive(package_id: PackageId, drive: &str) -> anyhow::Result<String
397403
}
398404

399405
/// Opens a file at path, if no file at path, creates one if boolean create is true.
400-
pub fn open_file(path: &str, create: bool) -> anyhow::Result<File> {
406+
pub fn open_file(path: &str, create: bool, timeout: Option<u64>) -> anyhow::Result<File> {
407+
let timeout = timeout.unwrap_or(5);
408+
401409
let request = VfsRequest {
402410
path: path.to_string(),
403411
action: VfsAction::OpenFile { create },
@@ -406,15 +414,15 @@ pub fn open_file(path: &str, create: bool) -> anyhow::Result<File> {
406414
let message = Request::new()
407415
.target(("our", "vfs", "distro", "sys"))
408416
.body(serde_json::to_vec(&request)?)
409-
.send_and_await_response(5)?;
417+
.send_and_await_response(timeout)?;
410418

411419
match message {
412420
Ok(Message::Response { body, .. }) => {
413421
let response = serde_json::from_slice::<VfsResponse>(&body)?;
414422
match response {
415423
VfsResponse::Ok => Ok(File {
416424
path: path.to_string(),
417-
timeout: 5,
425+
timeout,
418426
}),
419427
VfsResponse::Err(e) => Err(e.into()),
420428
_ => Err(anyhow::anyhow!("vfs: unexpected response: {:?}", response)),
@@ -454,7 +462,9 @@ pub fn create_file(path: &str, timeout: Option<u64>) -> anyhow::Result<File> {
454462
}
455463

456464
/// Removes a file at path, errors if path not found or path is not a file.
457-
pub fn remove_file(path: &str) -> anyhow::Result<()> {
465+
pub fn remove_file(path: &str, timeout: Option<u64>) -> anyhow::Result<()> {
466+
let timeout = timeout.unwrap_or(5);
467+
458468
let request = VfsRequest {
459469
path: path.to_string(),
460470
action: VfsAction::RemoveFile,
@@ -463,7 +473,7 @@ pub fn remove_file(path: &str) -> anyhow::Result<()> {
463473
let message = Request::new()
464474
.target(("our", "vfs", "distro", "sys"))
465475
.body(serde_json::to_vec(&request)?)
466-
.send_and_await_response(5)?;
476+
.send_and_await_response(timeout)?;
467477

468478
match message {
469479
Ok(Message::Response { body, .. }) => {

src/vfs/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,17 @@ impl VfsError {
127127
}
128128

129129
/// Metadata of a path, returns file type and length.
130-
pub fn metadata(path: &str) -> anyhow::Result<FileMetadata> {
130+
pub fn metadata(path: &str, timeout: Option<u64>) -> anyhow::Result<FileMetadata> {
131+
let timeout = timeout.unwrap_or(5);
132+
131133
let request = VfsRequest {
132134
path: path.to_string(),
133135
action: VfsAction::Metadata,
134136
};
135137
let message = Request::new()
136138
.target(("our", "vfs", "distro", "sys"))
137139
.body(serde_json::to_vec(&request)?)
138-
.send_and_await_response(5)?;
140+
.send_and_await_response(timeout)?;
139141

140142
match message {
141143
Ok(Message::Response { body, .. }) => {
@@ -151,11 +153,11 @@ pub fn metadata(path: &str) -> anyhow::Result<FileMetadata> {
151153
}
152154

153155
/// Removes a path, if it's either a directory or a file.
154-
pub fn remove_path(path: &str) -> anyhow::Result<()> {
155-
let meta = metadata(path)?;
156+
pub fn remove_path(path: &str, timeout: Option<u64>) -> anyhow::Result<()> {
157+
let meta = metadata(path, timeout)?;
156158
match meta.file_type {
157-
FileType::Directory => remove_dir(path),
158-
FileType::File => remove_file(path),
159+
FileType::Directory => remove_dir(path, timeout),
160+
FileType::File => remove_file(path, timeout),
159161
_ => Err(anyhow::anyhow!(
160162
"vfs: path is not a file or directory: {}",
161163
path

0 commit comments

Comments
 (0)