Skip to content

Commit 44143b2

Browse files
committed
Improve error when file not found, and avoid unnecessary call to try_exists, and fix unnecessary recreation of matched pattern
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 94512d8 commit 44143b2

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ impl UninitializedSandbox {
143143
log_build_details();
144144

145145
// If the guest binary is a file make sure it exists
146-
147146
let guest_binary = match guest_binary {
148147
GuestBinary::FilePath(binary_path) => {
149-
let path = Path::new(&binary_path).canonicalize()?;
150-
path.try_exists()?;
151-
GuestBinary::FilePath(path.to_str().unwrap().to_string())
148+
let path = Path::new(&binary_path)
149+
.canonicalize()
150+
.map_err(|e| new_error!("GuestBinary not found: '{}': {}", binary_path, e))?;
151+
GuestBinary::FilePath(
152+
path.into_os_string()
153+
.into_string()
154+
.map_err(|e| new_error!("Error converting OsString to String: {:?}", e))?,
155+
)
152156
}
153-
GuestBinary::Buffer(buffer) => GuestBinary::Buffer(buffer),
157+
buffer @ GuestBinary::Buffer(_) => buffer,
154158
};
155159

156160
let run_opts = sandbox_run_options.unwrap_or_default();
@@ -1088,4 +1092,18 @@ mod tests {
10881092
assert_eq!(0, num_calls);
10891093
}
10901094
}
1095+
1096+
#[test]
1097+
fn test_invalid_path() {
1098+
let invalid_path = "some/path/that/does/not/exist";
1099+
let sbox = UninitializedSandbox::new(
1100+
GuestBinary::FilePath(invalid_path.to_string()),
1101+
None,
1102+
None,
1103+
None,
1104+
);
1105+
assert!(
1106+
matches!(sbox, Err(e) if e.to_string().contains("GuestBinary not found: 'some/path/that/does/not/exist': No such file or directory (os error 2)"))
1107+
);
1108+
}
10911109
}

0 commit comments

Comments
 (0)