kanidmd_core/
utils.rs
1use filetime::FileTime;
2use std::fs::File;
3use std::io::ErrorKind;
4use std::path::Path;
5use std::time::SystemTime;
6
7pub fn touch_file_or_quit<P: AsRef<Path>>(file_path: P) {
8 let file_path: &Path = file_path.as_ref();
15
16 if file_path.exists() {
17 let t = FileTime::from_system_time(SystemTime::now());
18 match filetime::set_file_times(file_path, t, t) {
19 Ok(_) => debug!(
20 "Successfully touched existing file {}, can continue",
21 file_path.display()
22 ),
23 Err(e) => {
24 match e.kind() {
25 ErrorKind::PermissionDenied => {
26 error!(
28 "Permission denied writing to {}, quitting.",
29 file_path.display()
30 )
31 }
32 _ => {
33 error!(
34 "Failed to write to {} due to error: {:?} ... quitting.",
35 file_path.display(),
36 e
37 )
38 }
39 }
40 std::process::exit(1);
41 }
42 }
43 } else {
44 match File::create(file_path) {
45 Ok(_) => debug!("Successfully touched new file {}", file_path.display()),
46 Err(e) => {
47 error!(
48 "Failed to write to {} due to error: {:?} ... quitting.",
49 file_path.display(),
50 e
51 );
52 std::process::exit(1);
53 }
54 };
55 }
56}