team-ljyzipelo

공개
151줄4.9 KB
1use crate::{filesystem, protocol::*};
2use sha2::{Digest, Sha256};
3use std::{
4 fs::{File, OpenOptions},
5 io::{Read, Seek, SeekFrom, Write},
6 path::Path,
7};
8
9type Result<T> = std::result::Result<T, ErrorCode>;
10
11pub(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
73pub(crate) struct Snapshot {
74 pub file: File,
75 pub len: u64,
76 pub object: SourceObject,
77 pub hash: [u8; 32],
78}
79impl 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 before = source.metadata().map_err(|_| ErrorCode::Io)?;
95 if !before.is_file() {
96 return Err(ErrorCode::InvalidRequest);
97 }
98 let mut prefix = [0; 6];
99 let prefix_len = before.len().min(prefix.len() as u64) as usize;
100 source
101 .read_exact(&mut prefix[..prefix_len])
102 .map_err(|_| ErrorCode::SourceChanged)?;
103 let limit = if prefix == *b"7z\xbc\xaf\x27\x1c" {
104 limit.min(64 * 1024 * 1024)
105 } else {
106 limit
107 };
108 if before.len() > limit {
109 return Err(ErrorCode::Limit);
110 }
111 source.rewind().map_err(|_| ErrorCode::Io)?;
112 let object = filesystem::object(&source)?;
113 let mut file = temporary()?;
114 crate::space::ensure(&file, before.len())?;
115 let mut buffer = [0; 65536];
116 let mut hash = Sha256::new();
117 let mut len = 0u64;
118 loop {
119 check()?;
120 let n = source.read(&mut buffer).map_err(|_| ErrorCode::Io)?;
121 if n == 0 {
122 break;
123 }
124 len = len
125 .checked_add(n as u64)
126 .filter(|n| *n <= limit)
127 .ok_or(ErrorCode::Limit)?;
128 file.write_all(&buffer[..n])
129 .map_err(crate::space::io_error)?;
130 hash.update(&buffer[..n]);
131 }
132 let after = source.metadata().map_err(|_| ErrorCode::Io)?;
133 let named = options.open(path).map_err(|_| ErrorCode::SourceChanged)?;
134 if filesystem::object(&named)? != object
135 || filesystem::object(&source)? != object
136 || before.len() != len
137 || after.len() != len
138 || !named.metadata().map_err(|_| ErrorCode::Io)?.is_file()
139 || before.modified().ok() != after.modified().ok()
140 {
141 return Err(ErrorCode::SourceChanged);
142 }
143 file.seek(SeekFrom::Start(0)).map_err(|_| ErrorCode::Io)?;
144 Ok(Self {
145 file,
146 len,
147 object,
148 hash: hash.finalize().into(),
149 })
150 }
151}