| 1 |
use crate::FileTime; |
| 2 |
use std::fs::{self, OpenOptions}; |
| 3 |
use std::io; |
| 4 |
use std::os::windows::fs::OpenOptionsExt; |
| 5 |
use std::os::windows::prelude::*; |
| 6 |
use std::path::Path; |
| 7 |
|
| 8 |
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x2000000; |
| 9 |
const FILE_FLAG_OPEN_REPARSE_POINT: u32 = 0x200000; |
| 10 |
|
| 11 |
pub fn open(p: &Path) -> io::Result<fs::File> { |
| 12 |
OpenOptions::new() |
| 13 |
.write(true) |
| 14 |
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS) |
| 15 |
.open(p) |
| 16 |
} |
| 17 |
|
| 18 |
pub fn set_symlink_file_times(p: &Path, atime: FileTime, mtime: FileTime) -> io::Result<()> { |
| 19 |
let f = OpenOptions::new() |
| 20 |
.write(true) |
| 21 |
.custom_flags(FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS) |
| 22 |
.open(p)?; |
| 23 |
crate::set_file_handle_times(&f, Some(atime), Some(mtime)) |
| 24 |
} |
| 25 |
|
| 26 |
pub fn from_last_modification_time(meta: &fs::Metadata) -> FileTime { |
| 27 |
from_intervals(meta.last_write_time()) |
| 28 |
} |
| 29 |
|
| 30 |
pub fn from_last_access_time(meta: &fs::Metadata) -> FileTime { |
| 31 |
from_intervals(meta.last_access_time()) |
| 32 |
} |
| 33 |
|
| 34 |
pub fn from_creation_time(meta: &fs::Metadata) -> Option<FileTime> { |
| 35 |
Some(from_intervals(meta.creation_time())) |
| 36 |
} |
| 37 |
|
| 38 |
fn from_intervals(ticks: u64) -> FileTime { |
| 39 |
// Windows write times are in 100ns intervals, so do a little math to |
| 40 |
// get it into the right representation. |
| 41 |
FileTime { |
| 42 |
seconds: (ticks / (1_000_000_000 / 100)) as i64, |
| 43 |
nanos: ((ticks % (1_000_000_000 / 100)) * 100) as u32, |
| 44 |
} |
| 45 |
} |