team-ljyzipelo

공개
145줄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 let mut buffer = [0; 65536];
111 let mut hash = Sha256::new();
112 let mut len = 0u64;
113 loop {
114 check()?;
115 let n = source.read(&mut buffer).map_err(|_| ErrorCode::Io)?;
116 if n == 0 {
117 break;
118 }
119 len = len
120 .checked_add(n as u64)
121 .filter(|n| *n <= limit)
122 .ok_or(ErrorCode::Limit)?;
123 file.write_all(&buffer[..n]).map_err(|_| ErrorCode::Io)?;
124 hash.update(&buffer[..n]);
125 }
126 let after = source.metadata().map_err(|_| ErrorCode::Io)?;
127 let named = options.open(path).map_err(|_| ErrorCode::SourceChanged)?;
128 if filesystem::object(&named)? != object
129 || filesystem::object(&source)? != object
130 || before.len() != len
131 || after.len() != len
132 || !named.metadata().map_err(|_| ErrorCode::Io)?.is_file()
133 || before.modified().ok() != after.modified().ok()
134 {
135 return Err(ErrorCode::SourceChanged);
136 }
137 file.seek(SeekFrom::Start(0)).map_err(|_| ErrorCode::Io)?;
138 Ok(Self {
139 file,
140 len,
141 object,
142 hash: hash.finalize().into(),
143 })
144 }
145}