mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-27 04:35:27 +00:00
Expose mounted file timestamps to Win32 guests
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
b15226efe9
commit
da9f2c0035
4 files changed
+81
-8
No files matched your search
@@ -294,21 +294,21 @@ fn host_attributes(entry: &host_files::Entry, writable: bool) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
fn file_metadata(state: &ServicesState, raw: &str) -> Result<(u32, u64), u32> {
|
||||
fn file_metadata(state: &ServicesState, raw: &str) -> Result<(u32, u64, [u64; 3]), u32> {
|
||||
if let Some(resolved) = host_files::resolve(&state.mounts, raw, valid_component) {
|
||||
let resolved = resolved?;
|
||||
let (_, entry) = resolved.lookup()?;
|
||||
let entry = entry.ok_or(FILE_NOT_FOUND)?;
|
||||
return Ok((host_attributes(&entry, resolved.mount.writable()), entry.size));
|
||||
return Ok((host_attributes(&entry, resolved.mount.writable()), entry.size, entry.times));
|
||||
}
|
||||
|
||||
let objects = state.objects.lock().expect("guest object state");
|
||||
let path = resolve_file_path(raw, &objects.current_directory).ok_or(INVALID_NAME)?;
|
||||
if objects.directories.contains(&path) {
|
||||
return Ok((0x10, 0)); // FILE_ATTRIBUTE_DIRECTORY
|
||||
return Ok((0x10, 0, [0; 3])); // FILE_ATTRIBUTE_DIRECTORY
|
||||
}
|
||||
if let Some(file) = objects.files.get(&path) {
|
||||
return Ok((0x80, file.len() as u64)); // FILE_ATTRIBUTE_NORMAL
|
||||
return Ok((0x80, file.len() as u64, [0; 3])); // FILE_ATTRIBUTE_NORMAL
|
||||
}
|
||||
let parent = parent(&path);
|
||||
Err(if parent.is_empty() || objects.directories.contains(parent) {
|
||||
@@ -347,6 +347,7 @@ fn find_host_entries(state: &ServicesState, raw: &str) -> Option<Result<Vec<Sear
|
||||
.map(|entry| SearchEntry {
|
||||
attributes: host_attributes(&entry, writable),
|
||||
size: entry.size,
|
||||
times: entry.times,
|
||||
name: entry.name,
|
||||
})
|
||||
.collect();
|
||||
@@ -477,6 +478,7 @@ fn resolve_file_path(raw: &str, cwd: &str) -> Option<String> {
|
||||
}
|
||||
|
||||
struct SearchEntry {
|
||||
times: [u64; 3],
|
||||
name: String,
|
||||
attributes: u32,
|
||||
size: u64,
|
||||
@@ -487,6 +489,12 @@ pub(super) struct Search {
|
||||
next: usize,
|
||||
}
|
||||
|
||||
fn write_file_times(bytes: &mut [u8], times: [u64; 3]) {
|
||||
for (slot, time) in bytes.as_chunks_mut::<8>().0.iter_mut().zip(times) {
|
||||
slot.copy_from_slice(&time.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn write_find_data(
|
||||
memory: &mut GuestMemory,
|
||||
output: u64,
|
||||
@@ -499,6 +507,7 @@ fn write_find_data(
|
||||
// two reserved DWORDs, WCHAR[260], then WCHAR[14].
|
||||
let mut bytes = [0u8; 592];
|
||||
bytes[..4].copy_from_slice(&entry.attributes.to_le_bytes());
|
||||
write_file_times(&mut bytes[4..28], entry.times);
|
||||
bytes[28..32].copy_from_slice(&((entry.size >> 32) as u32).to_le_bytes());
|
||||
bytes[32..36].copy_from_slice(&(entry.size as u32).to_le_bytes());
|
||||
for (slot, unit) in bytes[44..564]
|
||||
@@ -1035,7 +1044,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
let Some(raw) = raw else {
|
||||
return attributes.fail(cpu.gs_base, INVALID_NAME, failure);
|
||||
};
|
||||
let (flags, size) = match file_metadata(&attributes, &raw) {
|
||||
let (flags, size, times) = match file_metadata(&attributes, &raw) {
|
||||
Ok(metadata) => metadata,
|
||||
Err(error) => return attributes.fail(cpu.gs_base, error, failure),
|
||||
};
|
||||
@@ -1046,6 +1055,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
// WIN32_FILE_ATTRIBUTE_DATA: flags, three FILETIMEs, then size high and low.
|
||||
let mut data = [0; 36];
|
||||
data[..4].copy_from_slice(&flags.to_le_bytes());
|
||||
write_file_times(&mut data[4..28], times);
|
||||
data[28..32].copy_from_slice(&((size >> 32) as u32).to_le_bytes());
|
||||
data[32..36].copy_from_slice(&(size as u32).to_le_bytes());
|
||||
if args.r8 == 0 || memory.write_bytes(args.r8, &data).is_err() {
|
||||
@@ -1145,6 +1155,7 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc<ServicesState>
|
||||
if wildcard_match(pattern, name) {
|
||||
entries.push(SearchEntry {
|
||||
name: name.to_owned(),
|
||||
times: [0; 3],
|
||||
attributes,
|
||||
size,
|
||||
});
|
||||
|
||||
@@ -169,6 +169,7 @@ pub(super) struct Entry {
|
||||
pub(super) name: String,
|
||||
pub(super) kind: Kind,
|
||||
pub(super) size: u64,
|
||||
pub(super) times: [u64; 3],
|
||||
pub(super) id: FileId,
|
||||
}
|
||||
|
||||
@@ -187,6 +188,27 @@ fn classify(status: &libc::stat) -> Option<(Kind, u64, FileId)> {
|
||||
Some((kind, status.st_size as u64, id))
|
||||
}
|
||||
|
||||
fn file_times(status: &libc::stat) -> [u64; 3] {
|
||||
fn filetime(seconds: i64, nanoseconds: i64) -> u64 {
|
||||
let ticks = (i128::from(seconds) + 11_644_473_600) * 10_000_000
|
||||
+ i128::from(nanoseconds) / 100;
|
||||
u64::try_from(ticks).unwrap_or(0)
|
||||
}
|
||||
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
||||
let created = if status.st_birthtime == 0 && status.st_birthtime_nsec == 0 {
|
||||
0
|
||||
} else {
|
||||
filetime(status.st_birthtime, status.st_birthtime_nsec)
|
||||
};
|
||||
#[cfg(target_os = "linux")]
|
||||
let created = 0;
|
||||
[
|
||||
created,
|
||||
filetime(status.st_atime, status.st_atime_nsec),
|
||||
filetime(status.st_mtime, status.st_mtime_nsec),
|
||||
]
|
||||
}
|
||||
|
||||
fn status(fd: BorrowedFd<'_>) -> io::Result<libc::stat> {
|
||||
let mut status = std::mem::MaybeUninit::<libc::stat>::uninit();
|
||||
// SAFETY: `fd` is open and `status` has room for a stat.
|
||||
@@ -318,7 +340,8 @@ pub(super) fn list(directory: &OwnedFd) -> io::Result<Vec<Entry>> {
|
||||
break Err(error);
|
||||
}
|
||||
// SAFETY: fstatat succeeded and initialized `status`.
|
||||
let Some((kind, size, id)) = classify(&unsafe { status.assume_init() }) else {
|
||||
let status = unsafe { status.assume_init() };
|
||||
let Some((kind, size, id)) = classify(&status) else {
|
||||
continue;
|
||||
};
|
||||
if entries.len() >= MAX_DIRECTORY_ENTRIES {
|
||||
@@ -328,6 +351,7 @@ pub(super) fn list(directory: &OwnedFd) -> io::Result<Vec<Entry>> {
|
||||
name: name.to_owned(),
|
||||
kind,
|
||||
size,
|
||||
times: file_times(&status),
|
||||
id,
|
||||
});
|
||||
};
|
||||
@@ -390,11 +414,15 @@ impl Resolved<'_> {
|
||||
pub(super) fn lookup(&self) -> Result<(OwnedFd, Option<Entry>), u32> {
|
||||
let parent = self.parent()?;
|
||||
let Some(name) = self.components.last() else {
|
||||
let id = verify(&parent, Kind::Directory, None).map_err(|error| win32_error(&error))?;
|
||||
let status = status(parent.as_fd()).map_err(|error| win32_error(&error))?;
|
||||
let Some((Kind::Directory, _, id)) = classify(&status) else {
|
||||
return Err(win32_error(&refused()));
|
||||
};
|
||||
let root = Entry {
|
||||
name: String::new(),
|
||||
kind: Kind::Directory,
|
||||
size: 0,
|
||||
times: file_times(&status),
|
||||
id,
|
||||
};
|
||||
return Ok((parent, Some(root)));
|
||||
|
||||
@@ -158,6 +158,37 @@ fn symlink(target: &Path, link: &Path) {
|
||||
std::os::unix::fs::symlink(target, link).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mounted_metadata_reports_windows_filetimes() {
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
|
||||
let tree = Tree::new("filetimes");
|
||||
let root = tree.path("root");
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
let file = std::fs::File::create(root.join("Times.dat")).unwrap();
|
||||
let accessed = UNIX_EPOCH + Duration::new(1_700_000_000, 123_456_700);
|
||||
let modified = UNIX_EPOCH + Duration::new(1_700_000_001, 234_567_800);
|
||||
file.set_times(std::fs::FileTimes::new().set_accessed(accessed).set_modified(modified))
|
||||
.unwrap();
|
||||
let mount = HostMount::new('D', &root, false).unwrap();
|
||||
let mut guest = guest_with(vec![mount]);
|
||||
put_path(&mut guest, PATH, r"D:\Times.dat");
|
||||
|
||||
assert_eq!(guest.call("GetFileAttributesExW", &[PATH, 0, BUFFER]), 1);
|
||||
let created = guest.u64_at(BUFFER + 4);
|
||||
#[cfg(target_os = "macos")]
|
||||
assert_ne!(created, 0);
|
||||
assert_eq!(guest.u64_at(BUFFER + 12), 133_444_736_001_234_567);
|
||||
assert_eq!(guest.u64_at(BUFFER + 20), 133_444_736_012_345_678);
|
||||
|
||||
let search = guest.call("FindFirstFileW", &[PATH, FIND_DATA]);
|
||||
assert_ne!(search, INVALID);
|
||||
assert_eq!(guest.u64_at(FIND_DATA + 4), created);
|
||||
assert_eq!(guest.u64_at(FIND_DATA + 12), guest.u64_at(BUFFER + 12));
|
||||
assert_eq!(guest.u64_at(FIND_DATA + 20), guest.u64_at(BUFFER + 20));
|
||||
assert_eq!(guest.call("FindClose", &[search]), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_file_size_splits_large_lengths_and_distinguishes_failure() {
|
||||
let tree = Tree::new("legacy-size");
|
||||
|
||||
+4
-1
@@ -355,7 +355,10 @@ a mount, nothing on the host is reachable through these services. Mounts are
|
||||
read-only unless created writable.
|
||||
|
||||
The partial `GetFileAttributesExA/W` services return attributes and 64-bit
|
||||
sizes for `GetFileExInfoStandard`. The three timestamp fields are zero.
|
||||
sizes for `GetFileExInfoStandard`. Host-mounted entries carry access and
|
||||
modification timestamps and creation time where the host supplies it.
|
||||
Synthetic entry timestamps are zero. Directory enumeration uses the same
|
||||
timestamp representation.
|
||||
The ANSI form accepts ASCII paths.
|
||||
|
||||
The partial `FlushFileBuffers` service requires a writable file handle.
|
||||
|
||||
Reference in new issue
Block a user