team-ljyzipelo

공개
146줄4.7 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 if let Ok(file) = OpenOptions::new()
17 .read(true)
18 .write(true)
19 .custom_flags(0x410000 | 0x80)
20 .mode(0o600)
21 .open(std::env::temp_dir())
22 {
23 return Ok(file);
24 }
25 let mut random = [0; 16];
26 crate::crypto::random(&mut random)?;
27 let name = random
28 .iter()
29 .map(|b| format!("{b:02x}"))
30 .collect::<String>();
31 let path = std::env::temp_dir().join(format!("zipelo-{name}.tmp"));
32 let file = OpenOptions::new()
33 .read(true)
34 .write(true)
35 .create_new(true)
36 .mode(0o600)
37 .custom_flags(0x20000)
38 .open(&path)
39 .map_err(|_| ErrorCode::Io)?;
40 std::fs::remove_file(path).map_err(|_| ErrorCode::Io)?;
41 use std::os::unix::fs::MetadataExt;
42 if file.metadata().map_err(|_| ErrorCode::Io)?.nlink() != 0 {
43 return Err(ErrorCode::Io);
44 }
45 Ok(file)
46 }
47 #[cfg(windows)]
48 {
49 use std::os::windows::fs::OpenOptionsExt;
50 let mut random = [0; 16];
51 crate::crypto::random(&mut random)?;
52 let name = random
53 .iter()
54 .map(|b| format!("{b:02x}"))
55 .collect::<String>();
56 OpenOptions::new()
57 .read(true)
58 .write(true)
59 .create_new(true)
60 .share_mode(0)
61 .access_mode(0xc0010000)
62 .custom_flags(0x04000000 | 0x00200000)
63 .attributes(0x100)
64 .open(std::env::temp_dir().join(format!("zipelo-{name}.tmp")))
65 .map_err(|_| ErrorCode::Io)
66 }
67}
68
69pub(crate) struct Snapshot {
70 pub file: File,
71 pub len: u64,
72 pub object: SourceObject,
73 pub hash: [u8; 32],
74}
75impl Snapshot {
76 pub fn open(path: &Path, limit: u64, check: &mut impl FnMut() -> Result<()>) -> Result<Self> {
77 let mut options = OpenOptions::new();
78 options.read(true);
79 #[cfg(unix)]
80 {
81 use std::os::unix::fs::OpenOptionsExt;
82 options.custom_flags(0x20000 | 0x800);
83 }
84 #[cfg(windows)]
85 {
86 use std::os::windows::fs::OpenOptionsExt;
87 options.share_mode(1);
88 }
89 let mut source = options.open(path).map_err(|_| ErrorCode::Io)?;
90 let before = source.metadata().map_err(|_| ErrorCode::Io)?;
91 if !before.is_file() {
92 return Err(ErrorCode::InvalidRequest);
93 }
94 let mut prefix = [0; 6];
95 let prefix_len = before.len().min(prefix.len() as u64) as usize;
96 source
97 .read_exact(&mut prefix[..prefix_len])
98 .map_err(|_| ErrorCode::SourceChanged)?;
99 let limit = if prefix == *b"7z\xbc\xaf\x27\x1c" {
100 limit.min(64 * 1024 * 1024)
101 } else {
102 limit
103 };
104 if before.len() > limit {
105 return Err(ErrorCode::Limit);
106 }
107 source.rewind().map_err(|_| ErrorCode::Io)?;
108 let object = filesystem::object(&source)?;
109 let mut file = temporary()?;
110 crate::space::ensure(&file, before.len())?;
111 let mut buffer = [0; 65536];
112 let mut hash = Sha256::new();
113 let mut len = 0u64;
114 loop {
115 check()?;
116 let n = source.read(&mut buffer).map_err(|_| ErrorCode::Io)?;
117 if n == 0 {
118 break;
119 }
120 len = len
121 .checked_add(n as u64)
122 .filter(|n| *n <= limit)
123 .ok_or(ErrorCode::Limit)?;
124 file.write_all(&buffer[..n]).map_err(|_| ErrorCode::Io)?;
125 hash.update(&buffer[..n]);
126 }
127 let after = source.metadata().map_err(|_| ErrorCode::Io)?;
128 let named = options.open(path).map_err(|_| ErrorCode::SourceChanged)?;
129 if filesystem::object(&named)? != object
130 || filesystem::object(&source)? != object
131 || before.len() != len
132 || after.len() != len
133 || !named.metadata().map_err(|_| ErrorCode::Io)?.is_file()
134 || before.modified().ok() != after.modified().ok()
135 {
136 return Err(ErrorCode::SourceChanged);
137 }
138 file.seek(SeekFrom::Start(0)).map_err(|_| ErrorCode::Io)?;
139 Ok(Self {
140 file,
141 len,
142 object,
143 hash: hash.finalize().into(),
144 })
145 }
146}