Initial
This commit is contained in:
1 parent
2cff35f215
commit
c8e6239d45
357 files changed
+253936
-74
No files matched your search
@@ -0,0 +1,192 @@
|
||||
|
||||
|
||||
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use super::xasync::{self, AsyncBlock, E_ABORT};
|
||||
use super::xgameruntime::{
|
||||
Guid, HRESULT, Interface, Methods, NOT_SUPPORTED, Object, S_OK, add_ref, missing, put,
|
||||
put_string, with_spare,
|
||||
};
|
||||
use crate::hle::runtime;
|
||||
|
||||
pub const PROVIDER: Guid = Guid::new(
|
||||
0xf4fa_f4d4,
|
||||
0x2d04,
|
||||
0x4fce,
|
||||
[0xb3, 0xe0, 0x47, 0x4a, 0x71, 0x3a, 0x3e, 0x84],
|
||||
);
|
||||
pub const IID: Guid = Guid::new(
|
||||
0xd294_11df,
|
||||
0x0794,
|
||||
0x4553,
|
||||
[0x8b, 0x27, 0x95, 0xfc, 0x02, 0xd0, 0xf7, 0x5d],
|
||||
);
|
||||
|
||||
pub const IID_2026: Guid = Guid::new(
|
||||
0x41a4_e10c,
|
||||
0x5a7e,
|
||||
0x41d9,
|
||||
[0x8c, 0x37, 0x37, 0xbd, 0xe6, 0x2a, 0x07, 0xd6],
|
||||
);
|
||||
|
||||
pub static OBJECT: Object = Object::new("persistent local storage", &VTABLE);
|
||||
pub const INTERFACES: &[Interface] = &[(PROVIDER, IID, &OBJECT), (PROVIDER, IID_2026, &OBJECT)];
|
||||
|
||||
const E_FAIL: HRESULT = 0x8000_4005_u32 as i32;
|
||||
|
||||
type This = *const Object;
|
||||
|
||||
fn folder() -> Option<String> {
|
||||
let storage = runtime::title().storage;
|
||||
if storage.as_os_str().is_empty() {
|
||||
return None;
|
||||
}
|
||||
#[cfg(windows)]
|
||||
return super::files::mount(&storage).map(|letter| format!("{letter}:\\"));
|
||||
#[cfg(not(windows))]
|
||||
None
|
||||
}
|
||||
|
||||
extern "system" fn path_size(_: This, out: *mut usize) -> HRESULT {
|
||||
match folder() {
|
||||
Some(path) => put(out, path.len() + 1),
|
||||
None => NOT_SUPPORTED,
|
||||
}
|
||||
}
|
||||
|
||||
extern "system" fn path(_: This, size: usize, buffer: *mut u8, used: *mut usize) -> HRESULT {
|
||||
match folder() {
|
||||
Some(path) if size <= path.len() => E_FAIL,
|
||||
Some(path) => put_string(&path, size, buffer, used),
|
||||
None => NOT_SUPPORTED,
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct SpaceInfo {
|
||||
|
||||
pub available_free: u64,
|
||||
|
||||
pub total_free: u64,
|
||||
pub used: u64,
|
||||
|
||||
pub total: u64,
|
||||
}
|
||||
|
||||
pub(crate) fn used_bytes(dir: &Path) -> u64 {
|
||||
let mut pending = vec![dir.to_owned()];
|
||||
let mut bytes = 0u64;
|
||||
while let Some(dir) = pending.pop() {
|
||||
let Ok(metadata) = std::fs::symlink_metadata(&dir) else {
|
||||
continue;
|
||||
};
|
||||
if !metadata.is_dir() || metadata.file_type().is_symlink() {
|
||||
continue;
|
||||
}
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
continue;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let path = entry.path();
|
||||
let Ok(metadata) = std::fs::symlink_metadata(&path) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if metadata.file_type().is_symlink() {
|
||||
continue;
|
||||
}
|
||||
if metadata.is_dir() {
|
||||
pending.push(path);
|
||||
} else {
|
||||
bytes = bytes.saturating_add(metadata.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
pub(crate) fn disk_free(dir: &Path) -> u64 {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
let wide: Vec<u16> = dir.as_os_str().encode_wide().chain([0]).collect();
|
||||
let mut free = 0;
|
||||
let null = std::ptr::null_mut();
|
||||
|
||||
let ok = unsafe {
|
||||
windows_sys::Win32::Storage::FileSystem::GetDiskFreeSpaceExW(
|
||||
wide.as_ptr(),
|
||||
&mut free,
|
||||
null,
|
||||
null,
|
||||
)
|
||||
};
|
||||
if ok != 0 {
|
||||
return free;
|
||||
}
|
||||
let error = std::io::Error::last_os_error();
|
||||
static WARNED: std::sync::Once = std::sync::Once::new();
|
||||
WARNED.call_once(|| {
|
||||
tracing::warn!(
|
||||
path = %dir.display(),
|
||||
%error,
|
||||
"could not query free storage space; treating it as unlimited"
|
||||
);
|
||||
});
|
||||
}
|
||||
let _ = dir;
|
||||
u64::MAX
|
||||
}
|
||||
|
||||
fn space() -> SpaceInfo {
|
||||
let title = runtime::title();
|
||||
let used = used_bytes(&title.storage);
|
||||
let free = title.storage_bytes.saturating_sub(used);
|
||||
SpaceInfo {
|
||||
|
||||
available_free: free.min(disk_free(&title.storage)),
|
||||
total_free: free,
|
||||
used,
|
||||
total: title.storage_bytes,
|
||||
}
|
||||
}
|
||||
|
||||
extern "system" fn space_info(_: This, out: *mut SpaceInfo) -> HRESULT {
|
||||
put(out, space())
|
||||
}
|
||||
|
||||
static PROMPT: u8 = 0;
|
||||
|
||||
fn prompt_status(bytes: u64, space: SpaceInfo) -> HRESULT {
|
||||
if bytes <= space.available_free {
|
||||
S_OK
|
||||
} else {
|
||||
E_ABORT
|
||||
}
|
||||
}
|
||||
|
||||
extern "system" fn prompt_for_space(_: This, bytes: u64, block: *mut AsyncBlock) -> HRESULT {
|
||||
let hr = prompt_status(bytes, space());
|
||||
xasync::finished(block, (&raw const PROMPT).cast(), hr)
|
||||
}
|
||||
|
||||
extern "system" fn prompt_result(_: This, block: *mut AsyncBlock) -> HRESULT {
|
||||
let none = std::ptr::null_mut();
|
||||
xasync::XAsyncGetResult(block, (&raw const PROMPT).cast(), 0, none, none.cast())
|
||||
}
|
||||
|
||||
static VTABLE: Methods<17> = with_spare([
|
||||
missing::<0> as *const (),
|
||||
add_ref as *const (),
|
||||
add_ref as *const (),
|
||||
path_size as *const (),
|
||||
path as *const (),
|
||||
space_info as *const (),
|
||||
prompt_for_space as *const (),
|
||||
prompt_result as *const (),
|
||||
missing::<8> as *const (),
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user