| 1 | use crate::protocol::*; |
| 2 | use std::fs::File; |
| 3 | |
| 4 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 5 | |
| 6 | pub(crate) fn volume(file: &File) -> Result<u64> { |
| 7 | Ok(match crate::filesystem::object(file)? { |
| 8 | SourceObject::Unix { device, .. } => device, |
| 9 | SourceObject::Windows { volume_serial, .. } => volume_serial, |
| 10 | SourceObject::SplitSevenZip { .. } => return Err(ErrorCode::Protocol), |
| 11 | }) |
| 12 | } |
| 13 | |
| 14 | pub(crate) fn ensure(file: &File, bytes: u64) -> Result<()> { |
| 15 | if bytes == 0 || bytes as u128 <= available(file)? { |
| 16 | Ok(()) |
| 17 | } else { |
| 18 | Err(ErrorCode::DiskFull) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | #[cfg(all(target_os = "linux", target_pointer_width = "64"))] |
| 23 | pub(crate) fn available(file: &File) -> Result<u128> { |
| 24 | use std::os::fd::AsRawFd; |
| 25 | #[repr(C)] |
| 26 | #[derive(Default)] |
| 27 | struct Vfs { |
| 28 | block_size: u64, |
| 29 | fragment_size: u64, |
| 30 | blocks: u64, |
| 31 | free_blocks: u64, |
| 32 | available_blocks: u64, |
| 33 | files: u64, |
| 34 | free_files: u64, |
| 35 | available_files: u64, |
| 36 | filesystem_id: u64, |
| 37 | flags: u64, |
| 38 | name_max: u64, |
| 39 | reserved: [i32; 6], |
| 40 | } |
| 41 | unsafe extern "C" { |
| 42 | fn fstatvfs(fd: i32, value: *mut Vfs) -> i32; |
| 43 | } |
| 44 | let mut value = Vfs::default(); |
| 45 | if unsafe { fstatvfs(file.as_raw_fd(), &mut value) } != 0 || value.fragment_size == 0 { |
| 46 | return Err(ErrorCode::Io); |
| 47 | } |
| 48 | Ok(value.available_blocks as u128 * value.fragment_size as u128) |
| 49 | } |
| 50 | |
| 51 | #[cfg(all(target_os = "linux", not(target_pointer_width = "64")))] |
| 52 | pub(crate) fn available(_: &File) -> Result<u128> { |
| 53 | Err(ErrorCode::UnsupportedFeature) |
| 54 | } |
| 55 | |
| 56 | #[cfg(windows)] |
| 57 | pub(crate) fn available(file: &File) -> Result<u128> { |
| 58 | use std::{ |
| 59 | ffi::OsString, |
| 60 | os::windows::{ |
| 61 | ffi::{OsStrExt, OsStringExt}, |
| 62 | io::AsRawHandle, |
| 63 | }, |
| 64 | path::PathBuf, |
| 65 | }; |
| 66 | unsafe extern "system" { |
| 67 | fn GetFinalPathNameByHandleW( |
| 68 | handle: *mut std::ffi::c_void, |
| 69 | path: *mut u16, |
| 70 | capacity: u32, |
| 71 | flags: u32, |
| 72 | ) -> u32; |
| 73 | fn GetDiskFreeSpaceExW( |
| 74 | path: *const u16, |
| 75 | available: *mut u64, |
| 76 | total: *mut u64, |
| 77 | free: *mut u64, |
| 78 | ) -> i32; |
| 79 | } |
| 80 | let mut buffer = vec![0u16; 32768]; |
| 81 | let mut length = unsafe { |
| 82 | GetFinalPathNameByHandleW( |
| 83 | file.as_raw_handle(), |
| 84 | buffer.as_mut_ptr(), |
| 85 | buffer.len() as u32, |
| 86 | 0, |
| 87 | ) |
| 88 | }; |
| 89 | if length == 0 { |
| 90 | length = unsafe { |
| 91 | GetFinalPathNameByHandleW( |
| 92 | file.as_raw_handle(), |
| 93 | buffer.as_mut_ptr(), |
| 94 | buffer.len() as u32, |
| 95 | 1, |
| 96 | ) |
| 97 | }; |
| 98 | } |
| 99 | if length == 0 || length as usize >= buffer.len() { |
| 100 | return Err(ErrorCode::Io); |
| 101 | } |
| 102 | let mut path = PathBuf::from(OsString::from_wide(&buffer[..length as usize])); |
| 103 | if !file.metadata().map_err(|_| ErrorCode::Io)?.is_dir() && !path.pop() { |
| 104 | return Err(ErrorCode::Io); |
| 105 | } |
| 106 | let mut wide: Vec<_> = path.as_os_str().encode_wide().collect(); |
| 107 | if wide.last() != Some(&92) { |
| 108 | wide.push(92); |
| 109 | } |
| 110 | wide.push(0); |
| 111 | let mut free = 0; |
| 112 | if unsafe { |
| 113 | GetDiskFreeSpaceExW( |
| 114 | wide.as_ptr(), |
| 115 | &mut free, |
| 116 | std::ptr::null_mut(), |
| 117 | std::ptr::null_mut(), |
| 118 | ) |
| 119 | } == 0 |
| 120 | { |
| 121 | return Err(ErrorCode::Io); |
| 122 | } |
| 123 | Ok(free as u128) |
| 124 | } |
| 125 | |
| 126 | // 공간 부족만 전용 오류로 변환함. 사용자 디스크 할당량 초과(EDQUOT, Windows 1295)도 |
| 127 | // 쓸 수 있는 공간이 없다는 뜻이므로 권한 오류가 아니라 공간 부족으로 알림. |
| 128 | pub(crate) fn io_error(error: std::io::Error) -> ErrorCode { |
| 129 | #[cfg(windows)] |
| 130 | if matches!(error.raw_os_error(), Some(39 | 112 | 1295)) { |
| 131 | return ErrorCode::DiskFull; |
| 132 | } |
| 133 | if matches!( |
| 134 | error.kind(), |
| 135 | std::io::ErrorKind::StorageFull | std::io::ErrorKind::QuotaExceeded |
| 136 | ) { |
| 137 | ErrorCode::DiskFull |
| 138 | } else { |
| 139 | ErrorCode::Io |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[cfg(test)] |
| 144 | #[path = "../../../tests/rust-archive-worker/space_errors.rs"] |
| 145 | mod tests; |