X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-persister%2Fsrc%2Futil.rs;h=7d63a41565bac14edca10707f62499544caef96e;hb=7c44dcd682892f682bc5c98ed40c0fe1606de087;hp=f1a36e4597d000a00c77871f2b705e0616e6bdb7;hpb=08ae2f214c5c5246ccf61b3ff8a0bcd52a88adf9;p=rust-lightning diff --git a/lightning-persister/src/util.rs b/lightning-persister/src/util.rs index f1a36e45..7d63a415 100644 --- a/lightning-persister/src/util.rs +++ b/lightning-persister/src/util.rs @@ -1,6 +1,14 @@ + +#[cfg(target_os = "windows")] +extern crate winapi; + use std::fs; use std::path::{Path, PathBuf}; - +#[cfg(target_os = "windows")] + use { + std::ffi::OsStr, + std::os::windows::ffi::OsStrExt + }; #[cfg(not(target_os = "windows"))] use std::os::unix::io::AsRawFd; @@ -13,10 +21,34 @@ pub(crate) fn get_full_filepath(filepath: String, filename: String) -> String { path.push(filename); path.to_str().unwrap().to_string() } +#[cfg(target_os = "windows")] +macro_rules! call { + ($e: expr) => ( + if $e != 0 { + return Ok(()) + } else { + return Err(std::io::Error::last_os_error()) + } + ) +} + +#[cfg(target_os = "windows")] +fn path_to_windows_str>(x: T) -> Vec { + x.as_ref().encode_wide().chain(Some(0)).collect() +} #[allow(bare_trait_objects)] pub(crate) fn write_to_file(path: String, filename: String, data: &D) -> std::io::Result<()> { + println!("VMW: creating dir"); fs::create_dir_all(path.clone())?; + println!("VMW: created dir"); + + println!("VMW: entries in dir:"); + let dir = PathBuf::from(path.clone()); + for entry in fs::read_dir(dir).unwrap() { + let entry = entry.unwrap(); + println!("VMW: entry in dir: {:?}", entry.path()); + } // Do a crazy dance with lots of fsync()s to be overly cautious here... // We never want to end up in a state where we've lost the old data, or end up using the // old data on power loss after we've returned. @@ -28,18 +60,33 @@ pub(crate) fn write_to_file(path: String, filename: String, da { // Note that going by rust-lang/rust@d602a6b, on MacOS it is only safe to use // rust stdlib 1.36 or higher. + println!("VMW: about to create file"); let mut f = fs::File::create(&tmp_filename)?; + println!("VMW: created file"); data.write_to_file(&mut f)?; + println!("VMW: about to sync all"); f.sync_all()?; + println!("VMW: sync'd all"); } - fs::rename(&tmp_filename, &filename_with_path)?; // Fsync the parent directory on Unix. #[cfg(not(target_os = "windows"))] { + fs::rename(&tmp_filename, &filename_with_path)?; let path = Path::new(&filename_with_path).parent().unwrap(); let dir_file = fs::OpenOptions::new().read(true).open(path)?; unsafe { libc::fsync(dir_file.as_raw_fd()); } } + #[cfg(target_os = "windows")] + { + println!("VMW: about to rename"); + let src = PathBuf::from(tmp_filename); + let dst = PathBuf::from(filename_with_path); + call!(unsafe {winapi::um::winbase::MoveFileExW( + path_to_windows_str(src).as_ptr(), path_to_windows_str(dst).as_ptr(), + winapi::um::winbase::MOVEFILE_WRITE_THROUGH | winapi::um::winbase::MOVEFILE_REPLACE_EXISTING + )}); + println!("VMW: renamed"); + } Ok(()) }