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