| 1 | use crate::{filesystem, protocol::*}; |
| 2 | use sha2::{Digest, Sha256}; |
| 3 | use std::{ |
| 4 | fs::{File, OpenOptions}, |
| 5 | io::{Read, Seek, SeekFrom, Write}, |
| 6 | path::Path, |
| 7 | }; |
| 8 | |
| 9 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 10 | |
| 11 | pub(crate) fn temporary() -> Result<File> { |
| 12 | #[cfg(target_os = "linux")] |
| 13 | { |
| 14 | use std::os::unix::fs::OpenOptionsExt; |
| 15 | // 이름 없는 임시 파일은 마지막 핸들을 닫으면 제거됨. |
| 16 | match OpenOptions::new() |
| 17 | .read(true) |
| 18 | .write(true) |
| 19 | .custom_flags(0x410000 | 0x80) |
| 20 | .mode(0o600) |
| 21 | .open(std::env::temp_dir()) |
| 22 | { |
| 23 | Ok(file) => return Ok(file), |
| 24 | Err(ref error) if error.kind() == std::io::ErrorKind::StorageFull => { |
| 25 | return Err(ErrorCode::DiskFull); |
| 26 | } |
| 27 | Err(_) => {} |
| 28 | } |
| 29 | let mut random = [0; 16]; |
| 30 | crate::crypto::random(&mut random)?; |
| 31 | let name = random |
| 32 | .iter() |
| 33 | .map(|b| format!("{b:02x}")) |
| 34 | .collect::<String>(); |
| 35 | let path = std::env::temp_dir().join(format!("zipelo-{name}.tmp")); |
| 36 | let file = OpenOptions::new() |
| 37 | .read(true) |
| 38 | .write(true) |
| 39 | .create_new(true) |
| 40 | .mode(0o600) |
| 41 | .custom_flags(0x20000) |
| 42 | .open(&path) |
| 43 | .map_err(crate::space::io_error)?; |
| 44 | std::fs::remove_file(path).map_err(|_| ErrorCode::Io)?; |
| 45 | use std::os::unix::fs::MetadataExt; |
| 46 | if file.metadata().map_err(|_| ErrorCode::Io)?.nlink() != 0 { |
| 47 | return Err(ErrorCode::Io); |
| 48 | } |
| 49 | Ok(file) |
| 50 | } |
| 51 | #[cfg(windows)] |
| 52 | { |
| 53 | use std::os::windows::fs::OpenOptionsExt; |
| 54 | let mut random = [0; 16]; |
| 55 | crate::crypto::random(&mut random)?; |
| 56 | let name = random |
| 57 | .iter() |
| 58 | .map(|b| format!("{b:02x}")) |
| 59 | .collect::<String>(); |
| 60 | OpenOptions::new() |
| 61 | .read(true) |
| 62 | .write(true) |
| 63 | .create_new(true) |
| 64 | .share_mode(0) |
| 65 | .access_mode(0xc0010000) |
| 66 | .custom_flags(0x04000000 | 0x00200000) |
| 67 | .attributes(0x100) |
| 68 | .open(std::env::temp_dir().join(format!("zipelo-{name}.tmp"))) |
| 69 | .map_err(crate::space::io_error) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub(crate) struct Snapshot { |
| 74 | pub file: File, |
| 75 | pub len: u64, |
| 76 | pub object: SourceObject, |
| 77 | pub hash: [u8; 32], |
| 78 | } |
| 79 | impl Snapshot { |
| 80 | pub fn open(path: &Path, limit: u64, check: &mut impl FnMut() -> Result<()>) -> Result<Self> { |
| 81 | let mut options = OpenOptions::new(); |
| 82 | options.read(true); |
| 83 | #[cfg(unix)] |
| 84 | { |
| 85 | use std::os::unix::fs::OpenOptionsExt; |
| 86 | options.custom_flags(0x20000 | 0x800); |
| 87 | } |
| 88 | #[cfg(windows)] |
| 89 | { |
| 90 | use std::os::windows::fs::OpenOptionsExt; |
| 91 | options.share_mode(1); |
| 92 | } |
| 93 | let mut source = options.open(path).map_err(|_| ErrorCode::Io)?; |
| 94 | let snapshot = Self::capture(&mut source, limit, check)?; |
| 95 | let named = options.open(path).map_err(|_| ErrorCode::SourceChanged)?; |
| 96 | if filesystem::object(&named)? != snapshot.object |
| 97 | || !named.metadata().map_err(|_| ErrorCode::Io)?.is_file() |
| 98 | { |
| 99 | return Err(ErrorCode::SourceChanged); |
| 100 | } |
| 101 | Ok(snapshot) |
| 102 | } |
| 103 | pub(crate) fn capture( |
| 104 | source: &mut File, |
| 105 | limit: u64, |
| 106 | check: &mut impl FnMut() -> Result<()>, |
| 107 | ) -> Result<Self> { |
| 108 | let before = source.metadata().map_err(|_| ErrorCode::Io)?; |
| 109 | if !before.is_file() { |
| 110 | return Err(ErrorCode::InvalidRequest); |
| 111 | } |
| 112 | if before.len() > limit { |
| 113 | return Err(ErrorCode::Limit); |
| 114 | } |
| 115 | source.rewind().map_err(|_| ErrorCode::Io)?; |
| 116 | let object = filesystem::object(&source)?; |
| 117 | let mut file = temporary()?; |
| 118 | crate::space::ensure(&file, before.len())?; |
| 119 | let mut buffer = zeroize::Zeroizing::new([0; 65536]); |
| 120 | let mut hash = Sha256::new(); |
| 121 | let mut len = 0u64; |
| 122 | loop { |
| 123 | check()?; |
| 124 | let n = source.read(&mut buffer[..]).map_err(|_| ErrorCode::Io)?; |
| 125 | if n == 0 { |
| 126 | break; |
| 127 | } |
| 128 | len = len |
| 129 | .checked_add(n as u64) |
| 130 | .filter(|n| *n <= limit) |
| 131 | .ok_or(ErrorCode::Limit)?; |
| 132 | file.write_all(&buffer[..n]) |
| 133 | .map_err(crate::space::io_error)?; |
| 134 | hash.update(&buffer[..n]); |
| 135 | } |
| 136 | let after = source.metadata().map_err(|_| ErrorCode::Io)?; |
| 137 | if filesystem::object(&source)? != object |
| 138 | || before.len() != len |
| 139 | || after.len() != len |
| 140 | || before.modified().ok() != after.modified().ok() |
| 141 | { |
| 142 | return Err(ErrorCode::SourceChanged); |
| 143 | } |
| 144 | file.seek(SeekFrom::Start(0)).map_err(|_| ErrorCode::Io)?; |
| 145 | Ok(Self { |
| 146 | file, |
| 147 | len, |
| 148 | object, |
| 149 | hash: hash.finalize().into(), |
| 150 | }) |
| 151 | } |
| 152 | } |