86f5dcba3b971f799e661d6234cb7db4992af234
[rust-lightning] / lightning-persister / src / util.rs
1
2 #[cfg(target_os = "windows")]
3 extern crate winapi;
4
5 use std::fs;
6 use std::path::{Path, PathBuf};
7 #[cfg(target_os = "windows")]
8         use {
9                 std::ffi::OsStr,
10                 std::os::windows::ffi::OsStrExt
11                 };
12 #[cfg(not(target_os = "windows"))]
13 use std::os::unix::io::AsRawFd;
14
15 pub(crate) trait DiskWriteable {
16         fn write_to_file(&self, writer: &mut fs::File) -> Result<(), std::io::Error>;
17 }
18
19 pub(crate) fn get_full_filepath(filepath: String, filename: String) -> String {
20         let mut path = PathBuf::from(filepath);
21         path.push(filename);
22         path.to_str().unwrap().to_string()
23 }
24 #[cfg(target_os = "windows")]
25 macro_rules! call {
26         ($e: expr) => (
27                 if $e != 0 {
28                         return Ok(())
29                 } else {
30                         return Err(std::io::Error::last_os_error())
31                 }
32         )
33 }
34
35 #[cfg(target_os = "windows")]
36 fn path_to_windows_str<T: AsRef<OsStr>>(x: T) -> Vec<winapi::shared::ntdef::WCHAR> {
37         x.as_ref().encode_wide().chain(Some(0)).collect()
38 }
39
40 #[allow(bare_trait_objects)]
41 pub(crate) fn write_to_file<D: DiskWriteable>(path: String, filename: String, data: &D) -> std::io::Result<()> {
42         println!("VMW: creating dir");
43         fs::create_dir_all(path.clone())?;
44         println!("VMW: created dir");
45
46         // Do a crazy dance with lots of fsync()s to be overly cautious here...
47         // We never want to end up in a state where we've lost the old data, or end up using the
48         // old data on power loss after we've returned.
49         // The way to atomically write a file on Unix platforms is:
50         // open(tmpname), write(tmpfile), fsync(tmpfile), close(tmpfile), rename(), fsync(dir)
51         let filename_with_path = get_full_filepath(path.clone(), filename);
52         let tmp_filename = format!("{}.tmp", filename_with_path);
53
54         {
55                 // Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use
56                 // rust stdlib 1.36 or higher.
57                 println!("VMW: about to create file");
58                 let mut f = fs::File::create(&tmp_filename)?;
59                 println!("VMW: created file");
60                 data.write_to_file(&mut f)?;
61                 println!("VMW: about to sync all");
62                 f.sync_all()?;
63                 println!("VMW: sync'd all");
64         }
65         // Fsync the parent directory on Unix.
66         #[cfg(not(target_os = "windows"))]
67         {
68                 fs::rename(&tmp_filename, &filename_with_path)?;
69                 let path = Path::new(&filename_with_path).parent().unwrap();
70                 let dir_file = fs::OpenOptions::new().read(true).open(path)?;
71                 unsafe { libc::fsync(dir_file.as_raw_fd()); }
72         }
73         // std::thread::sleep(std::time::Duration::new(60, 0));
74         #[cfg(target_os = "windows")]
75         {
76                 println!("VMW: entries in dir:");
77                 let dir_perms = fs::metadata(path.clone()).unwrap().permissions();
78                 println!("VMW: dir perms: {:?}, readonly: {}", dir_perms, dir_perms.readonly());
79                 let dir = PathBuf::from(path.clone());
80                 for entry in fs::read_dir(dir).unwrap() {
81                         let entry = entry.unwrap();
82                         let metadata = entry.metadata().unwrap();
83                         println!("VMW: entry in dir: {:?}, perms in entry: {:?}, readonly: {}", entry.path(), metadata.permissions(), metadata.permissions().readonly());
84                 }
85
86                 // let mut dir_perms = fs::metadata(path.clone()).unwrap().permissions();
87                 // dir_perms.set_readonly(false);
88                 // if let Ok(metadata) = fs::metadata(filename_with_path.clone()) {
89                 //      let mut perms = metadata.permissions();
90                 //      perms.set_readonly(false);
91                 // }
92     // // let mut perms = fs::metadata(filename_with_path.clone())?.permissions();
93     // let mut tmp_perms = fs::metadata(tmp_filename.clone())?.permissions();
94                 // tmp_perms.set_readonly(false);
95                 // println!("VMW: about to rename");
96                 let src = PathBuf::from(tmp_filename.clone());
97                 let dst = PathBuf::from(filename_with_path.clone());
98                 // fs::rename(&tmp_filename.clone(), &filename_with_path.clone())?;
99                 // call!(unsafe {winapi::um::winbase::MoveFileExW(
100                 //      path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(),
101                 //      winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING
102                 // )});
103                 let backup_filepath = PathBuf::from(format!("{}.backup", filename_with_path.clone()));
104
105                 if Path::new(&filename_with_path.clone()).exists() {
106                         unsafe {winapi::um::winbase::ReplaceFileW(
107                                 path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), path_to_windows_str(backup_filepath).as_ptr(),
108                                 winapi::um::winbase::REPLACEFILE_IGNORE_MERGE_ERRORS, std::ptr::null_mut() as *mut winapi::ctypes::c_void, std::ptr::null_mut() as *mut winapi::ctypes::c_void
109                         )};
110                 } else {
111                         call!(unsafe {winapi::um::winbase::MoveFileExW(
112                                 path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(),
113                                 winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING
114                         )});
115                 }
116                 println!("VMW: renamed");
117         }
118         Ok(())
119 }
120
121 #[cfg(test)]
122 mod tests {
123         use super::{DiskWriteable, get_full_filepath, write_to_file};
124         use std::fs;
125         use std::io;
126         use std::io::Write;
127
128         struct TestWriteable{}
129         impl DiskWriteable for TestWriteable {
130                 fn write_to_file(&self, writer: &mut fs::File) -> Result<(), io::Error> {
131                         writer.write_all(&[42; 1])
132                 }
133         }
134
135         // Test that if the persister's path to channel data is read-only, writing
136         // data to it fails. Windows ignores the read-only flag for folders, so this
137         // test is Unix-only.
138         #[cfg(not(target_os = "windows"))]
139         #[test]
140         fn test_readonly_dir() {
141                 let test_writeable = TestWriteable{};
142                 let filename = "test_readonly_dir_persister_filename".to_string();
143                 let path = "test_readonly_dir_persister_dir";
144                 fs::create_dir_all(path.to_string()).unwrap();
145                 let mut perms = fs::metadata(path.to_string()).unwrap().permissions();
146                 perms.set_readonly(true);
147                 fs::set_permissions(path.to_string(), perms).unwrap();
148                 match write_to_file(path.to_string(), filename, &test_writeable) {
149                         Err(e) => assert_eq!(e.kind(), io::ErrorKind::PermissionDenied),
150                         _ => panic!("Unexpected error message")
151                 }
152         }
153
154         // Test failure to rename in the process of atomically creating a channel
155         // monitor's file. We induce this failure by making the `tmp` file a
156         // directory.
157         // Explanation: given "from" = the file being renamed, "to" = the
158         // renamee that already exists: Windows should fail because it'll fail
159         // whenever "to" is a directory, and Unix should fail because if "from" is a
160         // file, then "to" is also required to be a file.
161         #[test]
162         fn test_rename_failure() {
163                 let test_writeable = TestWriteable{};
164                 let filename = "test_rename_failure_filename";
165                 let path = "test_rename_failure_dir";
166                 // Create the channel data file and make it a directory.
167                 fs::create_dir_all(get_full_filepath(path.to_string(), filename.to_string())).unwrap();
168                 match write_to_file(path.to_string(), filename.to_string(), &test_writeable) {
169                         Err(e) => {
170                                 #[cfg(not(target_os = "windows"))]
171                                 assert_eq!(e.kind(), io::ErrorKind::Other);
172                                 #[cfg(target_os = "windows")]
173                                 assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
174                         }
175                         _ => panic!("Unexpected error message")
176                 }
177                 fs::remove_dir_all(path).unwrap();
178         }
179
180         #[test]
181         fn test_diskwriteable_failure() {
182                 struct FailingWriteable {}
183                 impl DiskWriteable for FailingWriteable {
184                         fn write_to_file(&self, _writer: &mut fs::File) -> Result<(), std::io::Error> {
185                                 Err(std::io::Error::new(std::io::ErrorKind::Other, "expected failure"))
186                         }
187                 }
188
189                 let filename = "test_diskwriteable_failure";
190                 let path = "test_diskwriteable_failure_dir";
191                 let test_writeable = FailingWriteable{};
192                 match write_to_file(path.to_string(), filename.to_string(), &test_writeable) {
193                         Err(e) => {
194                                 assert_eq!(e.kind(), std::io::ErrorKind::Other);
195                                 assert_eq!(e.get_ref().unwrap().to_string(), "expected failure");
196                         },
197                         _ => panic!("unexpected result")
198                 }
199                 fs::remove_dir_all(path).unwrap();
200         }
201
202         // Test failure to create the temporary file in the persistence process.
203         // We induce this failure by having the temp file already exist and be a
204         // directory.
205         #[test]
206         fn test_tmp_file_creation_failure() {
207                 let test_writeable = TestWriteable{};
208                 let filename = "test_tmp_file_creation_failure_filename".to_string();
209                 let path = "test_tmp_file_creation_failure_dir".to_string();
210
211                 // Create the tmp file and make it a directory.
212                 let tmp_path = get_full_filepath(path.clone(), format!("{}.tmp", filename.clone()));
213                 fs::create_dir_all(tmp_path).unwrap();
214                 match write_to_file(path, filename, &test_writeable) {
215                         Err(e) => {
216                                 #[cfg(not(target_os = "windows"))]
217                                 assert_eq!(e.kind(), io::ErrorKind::Other);
218                                 #[cfg(target_os = "windows")]
219                                 assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);
220                         }
221                         _ => panic!("Unexpected error message")
222                 }
223         }
224 }