@@ -179,19 +179,10 @@ impl LocalBackend {
179
179
///
180
180
/// # Returns
181
181
///
182
- /// The parent path of the file.
183
- ///
184
- /// # Errors
185
- ///
186
- /// * [`LocalBackendErrorKind::FileDoesNotHaveParent`] - If the file does not have a parent.
187
- ///
188
- /// [`LocalBackendErrorKind::FileDoesNotHaveParent`]: LocalBackendErrorKind::FileDoesNotHaveParent
189
- fn parent_path ( & self , tpe : FileType , id : & Id ) -> Result < PathBuf > {
182
+ /// The parent path of the file or `None` if the file does not have a parent.
183
+ fn parent_path ( & self , tpe : FileType , id : & Id ) -> Option < PathBuf > {
190
184
let path = self . path ( tpe, id) ;
191
- path. parent ( ) . map_or (
192
- Err ( LocalBackendErrorKind :: FileDoesNotHaveParent ( path. clone ( ) ) . into ( ) ) ,
193
- |path| Ok ( path. to_path_buf ( ) ) ,
194
- )
185
+ path. parent ( ) . map ( Path :: to_path_buf)
195
186
}
196
187
}
197
188
@@ -380,7 +371,7 @@ impl ReadBackend for LocalBackend {
380
371
) -> RusticResult < Bytes > {
381
372
trace ! ( "reading tpe: {tpe:?}, id: {id}, offset: {offset}, length: {length}" ) ;
382
373
let filename = self . path ( tpe, id) ;
383
- let mut file = File :: open ( filename) . map_err ( |err| {
374
+ let mut file = File :: open ( filename. clone ( ) ) . map_err ( |err| {
384
375
RusticError :: with_source (
385
376
ErrorKind :: Backend ,
386
377
"Failed to open the file `{path}`. Please check the file and try again." ,
@@ -484,6 +475,10 @@ impl WriteBackend for LocalBackend {
484
475
/// * If the length of the file could not be set.
485
476
/// * If the bytes could not be written to the file.
486
477
/// * If the OS Metadata could not be synced to disk.
478
+ /// * If the file does not have a parent directory.
479
+ /// * If the parent directory could not be created.
480
+ /// * If the file cannot be opened, due to missing permissions.
481
+ /// * If the file cannot be written to, due to lack of space on the disk.
487
482
fn write_bytes (
488
483
& self ,
489
484
tpe : FileType ,
@@ -494,17 +489,27 @@ impl WriteBackend for LocalBackend {
494
489
trace ! ( "writing tpe: {:?}, id: {}" , & tpe, & id) ;
495
490
let filename = self . path ( tpe, id) ;
496
491
497
- // create parent directory if it does not exist
498
- if let Some ( parent) = filename. parent ( ) {
499
- fs:: create_dir_all ( parent) . map_err ( |err| {
500
- RusticError :: with_source (
501
- ErrorKind :: InputOutput ,
502
- "Failed to create directories `{path}`. Please check the file and try again." ,
503
- err,
492
+ let Some ( parent) = self . parent_path ( tpe, id) else {
493
+ return Err (
494
+ RusticError :: new (
495
+ ErrorKind :: Backend ,
496
+ "The file `{path}` does not have a parent directory. This may be empty or a root path. Please check the file and try again." ,
504
497
)
505
- . attach_context ( "path" , parent. to_string_lossy ( ) )
506
- } ) ?;
507
- }
498
+ . attach_context ( "path" , filename. display ( ) . to_string ( ) )
499
+ . ask_report ( )
500
+ ) ;
501
+ } ;
502
+
503
+ // create parent directory if it does not exist
504
+ fs:: create_dir_all ( parent. clone ( ) ) . map_err ( |err| {
505
+ RusticError :: with_source (
506
+ ErrorKind :: InputOutput ,
507
+ "Failed to create directories `{path}`. Does the directory already exist? Please check the file and try again." ,
508
+ err,
509
+ )
510
+ . attach_context ( "path" , parent. display ( ) . to_string ( ) )
511
+ . ask_report ( )
512
+ } ) ?;
508
513
509
514
let mut file = fs:: OpenOptions :: new ( )
510
515
. create ( true )
0 commit comments