Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.96 KB | None | 0 0
  1. let BLS_ENTRIES = "/some/prefix/";
  2. let entry = String::new("../../../some/prefix/foobar.conf");
  3. let path = std::path::Path::new(&entry);
  4. let abs_path = path.canonicalize();
  5.  
  6. // We try to ensure that the path exists, that it's a file in BLS_ENTRIES and that it's suffix is .conf
  7. match abs_path {
  8.   Ok(abs_path) => {
  9.     if abs_path.exists() {
  10.       if abs_path.parent() != Some(Path::new(BLS_ENTRIES)) {
  11.         return Err(format!("entry argument {} is not in bootloader entry directory {}", entry, BLS_ENTRIES));
  12.       }
  13.       if !abs_path.ends_with(".conf") {
  14.         return Err(format!("entry argument {:?} does not have .conf suffix", abs_path));
  15.       }
  16.       match abs_path.file_stem() {
  17.         Some(file_stem) => {
  18.           match file_stem.to_str() {
  19.             Some(file_stem) => {
  20.               return Ok(BLSEntry::new(String::from(file_stem)));
  21.             }
  22.             _ => {}
  23.           }
  24.         }
  25.         _ => {}
  26.       }
  27.     }
  28.   },
  29.   _ => {}
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement