Skip to content

Commit 931a6db

Browse files
committed
unftp-sbe-fs: don't panic during Filesystem::new
Filesystem::new will now return an error if it can't open the root directory. However, ServerExt::with_fs will still panic because the ServerBuilder is required to be infalliable.
1 parent e0e7b33 commit 931a6db

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

crates/unftp-sbe-fs/examples/cap-ftpd-worker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ async fn main() {
241241
let auth = Arc::new(JsonFileAuthenticator::from_file(args[1].clone()).unwrap());
242242
// XXX This would be a lot easier if the libunftp API allowed creating the
243243
// storage just before calling service.
244-
let storage = Mutex::new(Some(Filesystem::new(std::env::temp_dir())));
244+
let storage = Mutex::new(Some(Filesystem::new(std::env::temp_dir()).unwrap()));
245245
let sgen = Box::new(move || storage.lock().unwrap().take().unwrap());
246246

247247
let mut sb = libunftp::ServerBuilder::with_authenticator(sgen, auth);

crates/unftp-sbe-fs/src/ext.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ pub trait ServerExt {
1919
let p = path.into();
2020
libunftp::ServerBuilder::new(Box::new(move || {
2121
let p = &p.clone();
22-
Filesystem::new(p)
22+
match Filesystem::new(p) {
23+
Ok(fs) => fs,
24+
Err(e) => panic!("Cannot open file system root {}: {}", p.display(), e),
25+
}
2326
}))
2427
}
2528
}

crates/unftp-sbe-fs/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ impl Filesystem {
8080
/// Create a new Filesystem backend, with the given root. No operations can take place outside
8181
/// of the root. For example, when the `Filesystem` root is set to `/srv/ftp`, and a client
8282
/// asks for `hello.txt`, the server will send it `/srv/ftp/hello.txt`.
83-
pub fn new<P: Into<PathBuf>>(root: P) -> Self {
83+
pub fn new<P: Into<PathBuf>>(root: P) -> io::Result<Self> {
8484
let path = root.into();
8585
let aa = cap_std::ambient_authority();
86-
let root_fd = Arc::new(cap_std::fs::Dir::open_ambient_dir(&path, aa).unwrap());
87-
Filesystem { root_fd, root: path }
86+
let root_fd = Arc::new(cap_std::fs::Dir::open_ambient_dir(&path, aa)?);
87+
Ok(Filesystem { root_fd, root: path })
8888
}
8989
}
9090

crates/unftp-sbe-fs/src/tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn fs_stat() {
2323
let meta = file.metadata().unwrap();
2424

2525
// Create a filesystem StorageBackend with the directory containing our temp file as root
26-
let fs = Filesystem::new(&root);
26+
let fs = Filesystem::new(&root).unwrap();
2727

2828
// Since the filesystem backend is based on futures, we need a runtime to run it
2929
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
@@ -48,7 +48,7 @@ fn fs_list() {
4848
let meta = file.metadata().unwrap();
4949

5050
// Create a filesystem StorageBackend with our root dir
51-
let fs = Filesystem::new(root.path());
51+
let fs = Filesystem::new(root.path()).unwrap();
5252

5353
// Since the filesystem backend is based on futures, we need a runtime to run it
5454
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
@@ -74,7 +74,7 @@ fn fs_list_fmt() {
7474
let relpath = path.strip_prefix(root.path()).unwrap();
7575

7676
// Create a filesystem StorageBackend with our root dir
77-
let fs = Filesystem::new(root.path());
77+
let fs = Filesystem::new(root.path()).unwrap();
7878

7979
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
8080
let my_list = rt.block_on(fs.list_fmt(&DefaultUser {}, "/")).unwrap();
@@ -96,7 +96,7 @@ fn fs_get() {
9696
file.write_all(data).unwrap();
9797

9898
let filename = path.file_name().unwrap();
99-
let fs = Filesystem::new(&root);
99+
let fs = Filesystem::new(&root).unwrap();
100100

101101
// Since the filesystem backend is based on futures, we need a runtime to run it
102102
let rt = Runtime::new().unwrap();
@@ -117,7 +117,7 @@ fn fs_get() {
117117
fn fs_put() {
118118
let root = std::env::temp_dir();
119119
let orig_content = b"hallo";
120-
let fs = Filesystem::new(&root);
120+
let fs = Filesystem::new(&root).unwrap();
121121

122122
// Since the Filesystem StorageBackend is based on futures, we need a runtime to run them
123123
// to completion
@@ -178,7 +178,7 @@ fn fileinfo_fmt() {
178178
#[test]
179179
fn fs_mkd() {
180180
let root = tempfile::TempDir::new().unwrap().into_path();
181-
let fs = Filesystem::new(&root);
181+
let fs = Filesystem::new(&root).unwrap();
182182
let new_dir_name = "bla";
183183

184184
// Since the Filesystem StorageBackend is based on futures, we need a runtime to run them
@@ -203,7 +203,7 @@ fn fs_rename_file() {
203203
// to completion
204204
let rt = Runtime::new().unwrap();
205205

206-
let fs = Filesystem::new(&root);
206+
let fs = Filesystem::new(&root).unwrap();
207207
let r = rt.block_on(fs.rename(&DefaultUser {}, &old_filename, &new_filename));
208208
assert!(r.is_ok());
209209

@@ -225,7 +225,7 @@ fn fs_rename_dir() {
225225
// to completion
226226
let rt = Runtime::new().unwrap();
227227

228-
let fs = Filesystem::new(&root);
228+
let fs = Filesystem::new(&root).unwrap();
229229
let r = rt.block_on(fs.rename(&DefaultUser {}, &old_dir, &new_dir));
230230
assert!(r.is_ok());
231231

@@ -247,7 +247,7 @@ fn fs_md5() {
247247
let mut file = file.as_file();
248248

249249
// Create a filesystem StorageBackend with the directory containing our temp file as root
250-
let fs = Filesystem::new(&root);
250+
let fs = Filesystem::new(&root).unwrap();
251251
file.write_all(DATA.as_bytes()).unwrap();
252252

253253
// Since the filesystem backend is based on futures, we need a runtime to run it

0 commit comments

Comments
 (0)