Skip to main content

hide

Function hide 

Source
pub fn hide(path: impl AsRef<Path>) -> Result<()>
Expand description

Hides a file or directory.

§Platform-specific behavior

  • On Unix, this function renames the file to start with ..
  • On Windows, this function sets the hidden file attribute to the file.

§Errors

§On Unix

Returns Err if any of the following are true:

  • path terminates in ...
  • path is not valid UTF-8.
  • The file name starts with ..
  • std::fs::rename returns an error.

§On Windows

Returns Err if any of the following are true:

§Examples

§On Unix

let temp_dir = tempfile::tempdir().unwrap();
let temp_dir = temp_dir.path();
let file_path = temp_dir.join("foo.txt");
let hidden_file_path = hf::unix::hidden_file_name(&file_path).unwrap();
assert_eq!(hidden_file_path, temp_dir.join(".foo.txt"));
assert!(!file_path.exists());
assert!(!hidden_file_path.exists());

File::create(&file_path).unwrap();
assert!(file_path.exists());
assert!(!hidden_file_path.exists());

hf::hide(&file_path).unwrap();
assert!(!file_path.exists());
assert!(hidden_file_path.exists());

assert!(hf::hide(".bar.txt").is_err());
assert!(hf::hide("bar.txt/..").is_err());
assert!(hf::hide("bar.txt").is_err());

§On Windows

let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("foo.txt");
assert!(!file_path.exists());

File::create(&file_path).unwrap();
assert!(file_path.exists());
assert!(!hf::is_hidden(&file_path).unwrap());

hf::hide(&file_path).unwrap();
assert!(file_path.exists());
assert!(hf::is_hidden(file_path).unwrap());

assert!(hf::hide("bar.txt").is_err());