char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1use cap_fs_ext:: DirExt ; 2use cap_std:: fs:: Dir ; 3use std:: ffi:: OsString ; 4use std:: io; 5use std:: path::{ Component , Path , PathBuf }; 6 7pub fn parent ( path : & Path ) -> io:: Result <( Dir , OsString )> { 8let mut root =PathBuf :: new (); 9let mut names =Vec :: new (); 10for componentin path. components () { 11match component{ 12Component :: Prefix ( _) |Component :: RootDir => root. push ( component. as_os_str ()), 13Component :: Normal ( name) => names. push ( name), 14 _ =>{ 15return Err ( io:: Error :: new ( 16 io:: ErrorKind :: InvalidInput , 17"Expected a resolved filesystem path" , 18)); 19} 20} 21} 22let name = names. pop (). unwrap_or_else ( || std:: ffi:: OsStr :: new ( "." )); 23let mut directory =Dir :: open_ambient_dir ( root, cap_std:: ambient_authority ()) ?; 24for namein names{ 25 directory =match directory. open_dir_nofollow ( name) { 26Ok ( directory) => directory, 27Err ( _) 28if directory 29. symlink_metadata ( name) 30. is_ok_and ( |metadata| metadata. is_symlink ()) => 31{ 32return Err ( io:: Error :: new ( 33 io:: ErrorKind :: NotFound , 34"Symbolic link changed or has a missing target" , 35)); 36} 37Err ( error) =>return Err ( error), 38}; 39} 40Ok (( directory, name. to_owned ())) 41}