Merge pull request #84 from Team-LJY/fix/zip-folder-input
폴더 입력과 빈 폴더를 ZIP 생성에 포함
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
75dc876a5b5330be931f76bf7be9870dd7d35035
변경된 파일 10개
+835-64| @@ -48,6 +48,12 @@ revision 2의 기존 필드와 오류 tag 0~12 및 입력/출력 기본 한도 | ||
| 48 | 48 | |
| 49 | 49 | 7z의 Copy/LZMA/LZMA2 입력과 solid 출력은 고정 버퍼와 임시 파일로 처리합니다. 모든 폴더와 하위 스트림의 CRC와 크기 검증이 끝난 뒤 기존 게시 경로를 사용합니다. ZIP 생성의 압축 및 AES 처리도 64 KiB 버퍼를 사용하며 여유 공간 사전 검사와 실제 쓰기의 DiskFull 구분을 적용합니다. 공간 예약을 보장하지 않으므로 다른 프로세스와의 경쟁으로 쓰기가 실패할 수 있습니다. |
| 50 | 50 | |
| 51 | 추가: ## ZIP 생성 입력 | |
| 52 | 추가: | |
| 53 | 추가: 원본에 일반 파일과 폴더를 함께 지정할 수 있습니다. 각 archive_name을 최상위 이름으로 쓰고 폴더 안의 상대 경로와 빈 폴더를 보존합니다. 재귀 수집된 파일과 폴더 모두 항목 수 한도에 포함하며 파일 크기 합계에는 기존 16 GiB 기본 한도를 적용합니다. 이름 충돌과 한도는 암호 요청 전에 검사합니다. | |
| 54 | 추가: | |
| 55 | 추가: 심볼릭 링크, Windows reparse point와 특수 파일은 건너뛰거나 따라가지 않고 작업 전체를 거부합니다. 열린 디렉터리 기준으로 수집하고 파일은 검증한 핸들에서 캡처하며, 감지된 원본 교체나 변경은 게시 전 SourceChanged로 끝납니다. 전체 폴더 트리의 원자적 스냅샷을 보장하지는 않습니다. AES-256은 파일 내용에 적용하고 디렉터리 항목은 0바이트 비암호화 항목으로 기록합니다. | |
| 56 | 추가: | |
| 51 | 57 | ## 공유 codec |
| 52 | 58 | |
| 53 | 59 | `protocol.rs`는 요청과 이벤트 타입, `wire.rs`는 LE codec이며 비밀 값이 담긴 프레임 버퍼는 zeroize로 정리합니다. 앱은 이 라이브러리의 타입과 Reader/Writer를 사용할 수 있습니다. `contract.json`은 합의한 revision 2 원문입니다. |
| @@ -184,5 +184,13 @@ | ||
| 184 | 184 | "mapping": "Insufficient preflight space and StorageFull I/O failures return DiskFull 13. Windows ERROR_HANDLE_DISK_FULL 39 and ERROR_DISK_FULL 112 also map to DiskFull. Query failure, permission errors and other I/O failures return Io 8. No error classification by localized detail text.", |
| 185 | 185 | "detail": "디스크 여유 공간이 부족합니다. 공간을 확보한 뒤 다시 시도하세요", |
| 186 | 186 | "publication": "Keep existing publication, durability and cleanup semantics. DiskFull does not imply rollback or absence of previously published outputs." |
| 187 | 추가: }, | |
| 188 | 추가: "create_zip_sources": { | |
| 189 | 추가: "agreement": "Proposed semantic extension under manager task 82093f56-73b7-4be4-ba6d-2597ffc2c6f7; frontend joint agreement is pending. Revision 2 fields, encoding, numeric quotas and error tags are unchanged.", | |
| 190 | 추가: "roots": "Each source may be a regular file or directory. archive_name names that root inside the ZIP; directory descendants retain relative paths and every directory, including empty directories, gets a trailing-slash ZIP entry.", | |
| 191 | 추가: "links": "Reject the whole job with UnsafePath for symbolic links, Windows reparse points, special files or unsafe/non-UTF-8 descendant names. Do not skip entries or follow external links. Open each path component without following links; enumerate pinned directories.", | |
| 192 | 추가: "limits": "All expanded file and directory entries count toward entries (default 10000). Only file logical bytes count toward min(input,output), default output 16 GiB. Preserve 8 MiB metadata and 64-component name limits. Validate the complete plan and case-insensitive/path-prefix conflicts before requesting a password.", | |
| 193 | 추가: "consistency": "Capture each file from its validated open handle. Compare planned object identity, kind, size and modification time before capture and check planned objects again before publishing the completed ZIP; detected changes fail SourceChanged. This is not an atomic snapshot of an entire directory tree.", | |
| 194 | 추가: "encryption": "AES-256 protects file contents using the existing AE-2 encoding. Directory entries are stored as zero-byte unencrypted directory records; entry names remain visible, as with other ZIP entries." | |
| 187 | 195 | } |
| 188 | 196 | } |
| @@ -1,8 +1,4 @@ | ||
| 1 | 삭제: use crate::{ | |
| 2 | 삭제: archive::{self, Member, MetadataBudget}, | |
| 3 | 삭제: protocol::*, | |
| 4 | 삭제: spool, | |
| 5 | 삭제: }; | |
| 1 | 추가: use crate::{protocol::*, spool}; | |
| 6 | 2 | use std::{ |
| 7 | 3 | fs::File, |
| 8 | 4 | io::{Read, Seek, Write}, |
| @@ -110,34 +106,16 @@ fn pack( | ||
| 110 | 106 | } |
| 111 | 107 | } |
| 112 | 108 | } |
| 113 | 삭제: pub(crate) fn plan(sources: &[Source], limits: Limits) -> Result<Vec<Member>> { | |
| 114 | 삭제: if sources.len() > limits.entries as usize { | |
| 115 | 삭제: return Err(ErrorCode::Limit); | |
| 116 | 삭제: } | |
| 117 | 삭제: let mut metadata = MetadataBudget::default(); | |
| 118 | 삭제: let mut members = Vec::new(); | |
| 119 | 삭제: for source in sources { | |
| 120 | 삭제: source.path.to_path()?; | |
| 121 | 삭제: let name = archive::valid_name(&source.archive_name, false)?; | |
| 122 | 삭제: metadata.charge(&name)?; | |
| 123 | 삭제: members.push(Member { | |
| 124 | 삭제: entry: Entry { | |
| 125 | 삭제: entry_id: members.len() as u32, | |
| 126 | 삭제: name, | |
| 127 | 삭제: kind: EntryKind::File, | |
| 128 | 삭제: size: 0, | |
| 129 | 삭제: compressed_size: 0, | |
| 130 | 삭제: encrypted: false, | |
| 131 | 삭제: }, | |
| 132 | 삭제: data: Vec::new(), | |
| 133 | 삭제: }); | |
| 134 | 삭제: } | |
| 135 | 삭제: archive::validate_members(&members)?; | |
| 136 | 삭제: Ok(members) | |
| 109 | 추가: pub(crate) use crate::create_sources::Plan; | |
| 110 | 추가: pub(crate) fn plan( | |
| 111 | 추가: sources: &[Source], | |
| 112 | 추가: limits: Limits, | |
| 113 | 추가: check: &mut impl FnMut() -> Result<()>, | |
| 114 | 추가: ) -> Result<Plan> { | |
| 115 | 추가: Plan::new(sources, limits, check) | |
| 137 | 116 | } |
| 138 | 117 | pub(crate) fn zip( |
| 139 | 삭제: sources: &[Source], | |
| 140 | 삭제: members: &[Member], | |
| 118 | 추가: plan: &Plan, | |
| 141 | 119 | method: Method, |
| 142 | 120 | limits: Limits, |
| 143 | 121 | password: Option<&[u8]>, |
| @@ -147,36 +125,53 @@ pub(crate) fn zip( | ||
| 147 | 125 | let mut central = Vec::new(); |
| 148 | 126 | let mut total = 0u64; |
| 149 | 127 | let mut have_zip64 = false; |
| 150 | 삭제: for (source, member) in sources.iter().zip(members) { | |
| 128 | 추가: for source in &plan.items { | |
| 129 | 추가: let member = &source.member; | |
| 130 | 추가: let directory = member.entry.kind == EntryKind::Directory; | |
| 131 | 추가: let method = if directory { Method::Store } else { method }; | |
| 132 | 추가: let password = if directory { None } else { password }; | |
| 151 | 133 | check()?; |
| 152 | 134 | let remaining = limits |
| 153 | 135 | .input |
| 154 | 136 | .min(limits.output) |
| 155 | 137 | .checked_sub(total) |
| 156 | 138 | .ok_or(ErrorCode::Limit)?; |
| 157 | 삭제: let mut snapshot = spool::Snapshot::open(&source.path.to_path()?, remaining, check)?; | |
| 139 | 추가: let mut input = source.open()?; | |
| 140 | 추가: let mut snapshot = if directory { | |
| 141 | 추가: None | |
| 142 | 추가: } else { | |
| 143 | 추가: Some(spool::Snapshot::capture(&mut input.file, remaining, check)?) | |
| 144 | 추가: }; | |
| 145 | 추가: let length = snapshot.as_ref().map_or(0, |s| s.len); | |
| 158 | 146 | total = total |
| 159 | 삭제: .checked_add(snapshot.len) | |
| 147 | 추가: .checked_add(length) | |
| 160 | 148 | .filter(|n| *n <= limits.output) |
| 161 | 149 | .ok_or(ErrorCode::Limit)?; |
| 162 | 150 | // 압축 결과의 보수적 상한과 ZIP에 복사할 공간을 함께 확인함. |
| 163 | 삭제: let bound = snapshot | |
| 164 | 삭제: .len | |
| 165 | 삭제: .checked_add(snapshot.len / 16383 * 5) | |
| 151 | 추가: let bound = length | |
| 152 | 추가: .checked_add(length / 16383 * 5) | |
| 166 | 153 | .and_then(|n| n.checked_add(131072)) |
| 167 | 154 | .ok_or(ErrorCode::Limit)?; |
| 168 | 155 | crate::space::ensure(&output, bound.checked_mul(2).ok_or(ErrorCode::Limit)?)?; |
| 169 | 156 | let mut packed = spool::temporary()?; |
| 170 | 삭제: let (packed_size, crc) = | |
| 171 | 삭제: pack(&mut snapshot.file, snapshot.len, method, &mut packed, check)?; | |
| 157 | 추가: let (packed_size, crc) = if let Some(snapshot) = snapshot.as_mut() { | |
| 158 | 추가: pack(&mut snapshot.file, length, method, &mut packed, check)? | |
| 159 | 추가: } else { | |
| 160 | 추가: (0, 0) | |
| 161 | 추가: }; | |
| 172 | 162 | let packed_size = packed_size |
| 173 | 163 | .checked_add(if password.is_some() { 28 } else { 0 }) |
| 174 | 164 | .ok_or(ErrorCode::Limit)?; |
| 175 | 165 | let offset = io(output.stream_position())?; |
| 176 | 삭제: let name = member.entry.name.as_bytes(); | |
| 166 | 추가: let zip_name = if directory { | |
| 167 | 추가: format!("{}/", member.entry.name) | |
| 168 | 추가: } else { | |
| 169 | 추가: member.entry.name.clone() | |
| 170 | 추가: }; | |
| 171 | 추가: let name = zip_name.as_bytes(); | |
| 177 | 172 | let real_method = if method == Method::Store { 0 } else { 8 }; |
| 178 | 173 | let flags = if password.is_some() { 0x0801 } else { 0x0800 }; |
| 179 | 삭제: let wide_sizes = snapshot.len >= u32::MAX as u64 || packed_size >= u32::MAX as u64; | |
| 174 | 추가: let wide_sizes = length >= u32::MAX as u64 || packed_size >= u32::MAX as u64; | |
| 180 | 175 | let wide_offset = offset >= u32::MAX as u64; |
| 181 | 176 | have_zip64 |= wide_sizes || wide_offset; |
| 182 | 177 | let version = if password.is_some() { |
| @@ -197,7 +192,7 @@ pub(crate) fn zip( | ||
| 197 | 192 | aes.push(3); |
| 198 | 193 | put16(&mut aes, real_method); |
| 199 | 194 | } |
| 200 | 삭제: let mut extra = zip64_extra(snapshot.len, packed_size, None); | |
| 195 | 추가: let mut extra = zip64_extra(length, packed_size, None); | |
| 201 | 196 | extra.extend(&aes); |
| 202 | 197 | let mut header = Vec::new(); |
| 203 | 198 | header.extend(b"PK\x03\x04"); |
| @@ -215,11 +210,7 @@ pub(crate) fn zip( | ||
| 215 | 210 | ); |
| 216 | 211 | put32( |
| 217 | 212 | &mut header, |
| 218 | 삭제: if wide_sizes { | |
| 219 | 삭제: u32::MAX | |
| 220 | 삭제: } else { | |
| 221 | 삭제: snapshot.len as u32 | |
| 222 | 삭제: }, | |
| 213 | 추가: if wide_sizes { u32::MAX } else { length as u32 }, | |
| 223 | 214 | ); |
| 224 | 215 | put16(&mut header, name.len() as u16); |
| 225 | 216 | put16(&mut header, extra.len() as u16); |
| @@ -246,7 +237,7 @@ pub(crate) fn zip( | ||
| 246 | 237 | io(output.write_all(&buffer[..n]))?; |
| 247 | 238 | } |
| 248 | 239 | } |
| 249 | 삭제: let mut extra = zip64_extra(snapshot.len, packed_size, Some(offset)); | |
| 240 | 추가: let mut extra = zip64_extra(length, packed_size, Some(offset)); | |
| 250 | 241 | extra.extend(&aes); |
| 251 | 242 | central.extend(b"PK\x01\x02"); |
| 252 | 243 | put16(&mut central, 0x032d); |
| @@ -264,18 +255,21 @@ pub(crate) fn zip( | ||
| 264 | 255 | ); |
| 265 | 256 | put32( |
| 266 | 257 | &mut central, |
| 267 | 삭제: if wide_sizes { | |
| 268 | 삭제: u32::MAX | |
| 269 | 삭제: } else { | |
| 270 | 삭제: snapshot.len as u32 | |
| 271 | 삭제: }, | |
| 258 | 추가: if wide_sizes { u32::MAX } else { length as u32 }, | |
| 272 | 259 | ); |
| 273 | 260 | put16(&mut central, name.len() as u16); |
| 274 | 261 | put16(&mut central, extra.len() as u16); |
| 275 | 262 | for _ in 0..3 { |
| 276 | 263 | put16(&mut central, 0); |
| 277 | 264 | } |
| 278 | 삭제: put32(&mut central, 0o100600 << 16); | |
| 265 | 추가: put32( | |
| 266 | 추가: &mut central, | |
| 267 | 추가: if directory { | |
| 268 | 추가: (0o40700 << 16) | 0x10 | |
| 269 | 추가: } else { | |
| 270 | 추가: 0o100600 << 16 | |
| 271 | 추가: }, | |
| 272 | 추가: ); | |
| 279 | 273 | put32( |
| 280 | 274 | &mut central, |
| 281 | 275 | if wide_offset { u32::MAX } else { offset as u32 }, |
| @@ -296,8 +290,8 @@ pub(crate) fn zip( | ||
| 296 | 290 | put16(&mut end, 45); |
| 297 | 291 | put32(&mut end, 0); |
| 298 | 292 | put32(&mut end, 0); |
| 299 | 삭제: put64(&mut end, sources.len() as u64); | |
| 300 | 삭제: put64(&mut end, sources.len() as u64); | |
| 293 | 추가: put64(&mut end, plan.items.len() as u64); | |
| 294 | 추가: put64(&mut end, plan.items.len() as u64); | |
| 301 | 295 | put64(&mut end, central.len() as u64); |
| 302 | 296 | put64(&mut end, at); |
| 303 | 297 | end.extend(b"PK\x06\x07"); |
| @@ -316,13 +310,14 @@ pub(crate) fn zip( | ||
| 316 | 310 | end.extend(b"PK\x05\x06"); |
| 317 | 311 | put16(&mut end, 0); |
| 318 | 312 | put16(&mut end, 0); |
| 319 | 삭제: put16(&mut end, sources.len() as u16); | |
| 320 | 삭제: put16(&mut end, sources.len() as u16); | |
| 313 | 추가: put16(&mut end, plan.items.len() as u16); | |
| 314 | 추가: put16(&mut end, plan.items.len() as u16); | |
| 321 | 315 | put32(&mut end, central.len() as u32); |
| 322 | 316 | put32(&mut end, at as u32); |
| 323 | 317 | put16(&mut end, 0); |
| 324 | 318 | } |
| 325 | 319 | io(output.write_all(&end))?; |
| 320 | 추가: plan.verify(check)?; | |
| 326 | 321 | let size = io(output.stream_position())?; |
| 327 | 322 | io(output.rewind())?; |
| 328 | 323 | Ok((output, size)) |
| @@ -0,0 +1,315 @@ | ||
| 1 | 추가: use crate::{ | |
| 2 | 추가: archive::{self, Member, MetadataBudget}, | |
| 3 | 추가: filesystem, | |
| 4 | 추가: protocol::*, | |
| 5 | 추가: }; | |
| 6 | 추가: use std::{ | |
| 7 | 추가: fs::{self, File, Metadata}, | |
| 8 | 추가: path::{Component, Path, PathBuf}, | |
| 9 | 추가: time::SystemTime, | |
| 10 | 추가: }; | |
| 11 | 추가: | |
| 12 | 추가: type Result<T> = std::result::Result<T, ErrorCode>; | |
| 13 | 추가: | |
| 14 | 추가: pub(super) struct Input { | |
| 15 | 추가: pub file: File, | |
| 16 | 추가: #[cfg(windows)] | |
| 17 | 추가: _parents: Vec<File>, | |
| 18 | 추가: } | |
| 19 | 추가: impl Input { | |
| 20 | 추가: #[cfg(target_os = "linux")] | |
| 21 | 추가: fn child(file: &File, name: &std::ffi::OsStr) -> Result<File> { | |
| 22 | 추가: use std::os::{ | |
| 23 | 추가: fd::{AsRawFd, FromRawFd}, | |
| 24 | 추가: unix::ffi::OsStrExt, | |
| 25 | 추가: }; | |
| 26 | 추가: unsafe extern "C" { | |
| 27 | 추가: fn openat(fd: i32, name: *const i8, flags: i32, ...) -> i32; | |
| 28 | 추가: } | |
| 29 | 추가: let name = std::ffi::CString::new(name.as_bytes()).map_err(|_| ErrorCode::UnsafePath)?; | |
| 30 | 추가: // 링크를 따라가지 않는 경로 핸들로 객체 종류를 확인함. | |
| 31 | 추가: let fd = unsafe { | |
| 32 | 추가: openat( | |
| 33 | 추가: file.as_raw_fd(), | |
| 34 | 추가: name.as_ptr(), | |
| 35 | 추가: 0x80000 | 0x20000 | 0x200000, | |
| 36 | 추가: ) | |
| 37 | 추가: }; | |
| 38 | 추가: if fd < 0 { | |
| 39 | 추가: let error = std::io::Error::last_os_error(); | |
| 40 | 추가: return Err(if matches!(error.raw_os_error(), Some(20 | 40)) { | |
| 41 | 추가: ErrorCode::UnsafePath | |
| 42 | 추가: } else { | |
| 43 | 추가: crate::space::io_error(error) | |
| 44 | 추가: }); | |
| 45 | 추가: } | |
| 46 | 추가: let pinned = unsafe { File::from_raw_fd(fd) }; | |
| 47 | 추가: Self::validate(&pinned)?; | |
| 48 | 추가: Ok(pinned) | |
| 49 | 추가: } | |
| 50 | 추가: #[cfg(target_os = "linux")] | |
| 51 | 추가: fn read(pinned: File) -> Result<File> { | |
| 52 | 추가: use std::os::fd::AsRawFd; | |
| 53 | 추가: // 읽기 권한은 최종 입력에만 요청함. | |
| 54 | 추가: File::open(format!("/proc/self/fd/{}", pinned.as_raw_fd())).map_err(crate::space::io_error) | |
| 55 | 추가: } | |
| 56 | 추가: pub fn open(path: &Path) -> Result<Self> { | |
| 57 | 추가: if !path.is_absolute() { | |
| 58 | 추가: return Err(ErrorCode::InvalidRequest); | |
| 59 | 추가: } | |
| 60 | 추가: #[cfg(target_os = "linux")] | |
| 61 | 추가: { | |
| 62 | 추가: use std::os::unix::fs::OpenOptionsExt; | |
| 63 | 추가: let mut file = fs::OpenOptions::new() | |
| 64 | 추가: .read(true) | |
| 65 | 추가: .custom_flags(0x200000 | 0x20000 | 0x10000 | 0x80000) | |
| 66 | 추가: .open("/") | |
| 67 | 추가: .map_err(crate::space::io_error)?; | |
| 68 | 추가: for part in path.components() { | |
| 69 | 추가: match part { | |
| 70 | 추가: Component::RootDir => {} | |
| 71 | 추가: Component::Normal(name) => file = Self::child(&file, name)?, | |
| 72 | 추가: _ => return Err(ErrorCode::UnsafePath), | |
| 73 | 추가: } | |
| 74 | 추가: } | |
| 75 | 추가: let file = Self::read(file)?; | |
| 76 | 추가: Self::validate(&file)?; | |
| 77 | 추가: Ok(Self { file }) | |
| 78 | 추가: } | |
| 79 | 추가: #[cfg(windows)] | |
| 80 | 추가: { | |
| 81 | 추가: use std::os::windows::fs::OpenOptionsExt; | |
| 82 | 추가: let mut current = PathBuf::new(); | |
| 83 | 추가: let mut parents = Vec::new(); | |
| 84 | 추가: for part in path.components() { | |
| 85 | 추가: match part { | |
| 86 | 추가: Component::Prefix(_) | Component::RootDir => current.push(part.as_os_str()), | |
| 87 | 추가: Component::Normal(name) => { | |
| 88 | 추가: if parents.is_empty() { | |
| 89 | 추가: parents.push(Self::lock(¤t)?); | |
| 90 | 추가: } | |
| 91 | 추가: current.push(name); | |
| 92 | 추가: parents.push(Self::lock(¤t)?); | |
| 93 | 추가: } | |
| 94 | 추가: _ => return Err(ErrorCode::UnsafePath), | |
| 95 | 추가: } | |
| 96 | 추가: } | |
| 97 | 추가: let file = fs::OpenOptions::new() | |
| 98 | 추가: .read(true) | |
| 99 | 추가: .share_mode(1) | |
| 100 | 추가: .custom_flags(0x02000000 | 0x00200000) | |
| 101 | 추가: .open(¤t) | |
| 102 | 추가: .map_err(crate::space::io_error)?; | |
| 103 | 추가: Self::validate(&file)?; | |
| 104 | 추가: Ok(Self { | |
| 105 | 추가: file, | |
| 106 | 추가: _parents: parents, | |
| 107 | 추가: }) | |
| 108 | 추가: } | |
| 109 | 추가: } | |
| 110 | 추가: #[cfg(windows)] | |
| 111 | 추가: fn lock(path: &Path) -> Result<File> { | |
| 112 | 추가: use std::os::windows::fs::{MetadataExt, OpenOptionsExt}; | |
| 113 | 추가: let file = fs::OpenOptions::new() | |
| 114 | 추가: .access_mode(0) | |
| 115 | 추가: .share_mode(3) | |
| 116 | 추가: .custom_flags(0x02000000 | 0x00200000) | |
| 117 | 추가: .open(path) | |
| 118 | 추가: .map_err(crate::space::io_error)?; | |
| 119 | 추가: if file | |
| 120 | 추가: .metadata() | |
| 121 | 추가: .map_err(crate::space::io_error)? | |
| 122 | 추가: .file_attributes() | |
| 123 | 추가: & 0x400 | |
| 124 | 추가: != 0 | |
| 125 | 추가: { | |
| 126 | 추가: return Err(ErrorCode::UnsafePath); | |
| 127 | 추가: } | |
| 128 | 추가: Ok(file) | |
| 129 | 추가: } | |
| 130 | 추가: fn validate(file: &File) -> Result<()> { | |
| 131 | 추가: let metadata = file.metadata().map_err(crate::space::io_error)?; | |
| 132 | 추가: #[cfg(windows)] | |
| 133 | 추가: { | |
| 134 | 추가: use std::os::windows::fs::MetadataExt; | |
| 135 | 추가: if metadata.file_attributes() & 0x400 != 0 { | |
| 136 | 추가: return Err(ErrorCode::UnsafePath); | |
| 137 | 추가: } | |
| 138 | 추가: } | |
| 139 | 추가: if !metadata.is_dir() && !metadata.is_file() { | |
| 140 | 추가: return Err(ErrorCode::UnsafePath); | |
| 141 | 추가: } | |
| 142 | 추가: Ok(()) | |
| 143 | 추가: } | |
| 144 | 추가: fn open_child(&self, path: &Path, name: &str) -> Result<Self> { | |
| 145 | 추가: #[cfg(target_os = "linux")] | |
| 146 | 추가: { | |
| 147 | 추가: let _ = path; | |
| 148 | 추가: let file = Self::read(Self::child(&self.file, std::ffi::OsStr::new(name))?)?; | |
| 149 | 추가: Self::validate(&file)?; | |
| 150 | 추가: Ok(Self { file }) | |
| 151 | 추가: } | |
| 152 | 추가: #[cfg(windows)] | |
| 153 | 추가: { | |
| 154 | 추가: Self::open(&path.join(name)) | |
| 155 | 추가: } | |
| 156 | 추가: } | |
| 157 | 추가: fn children(&self, path: &Path) -> Result<fs::ReadDir> { | |
| 158 | 추가: #[cfg(target_os = "linux")] | |
| 159 | 추가: { | |
| 160 | 추가: use std::os::fd::AsRawFd; | |
| 161 | 추가: let _ = path; | |
| 162 | 추가: fs::read_dir(format!("/proc/self/fd/{}", self.file.as_raw_fd())) | |
| 163 | 추가: .map_err(crate::space::io_error) | |
| 164 | 추가: } | |
| 165 | 추가: #[cfg(windows)] | |
| 166 | 추가: { | |
| 167 | 추가: fs::read_dir(path).map_err(crate::space::io_error) | |
| 168 | 추가: } | |
| 169 | 추가: } | |
| 170 | 추가: } | |
| 171 | 추가: | |
| 172 | 추가: pub(super) struct Planned { | |
| 173 | 추가: pub member: Member, | |
| 174 | 추가: path: PathBuf, | |
| 175 | 추가: object: SourceObject, | |
| 176 | 추가: modified: Option<SystemTime>, | |
| 177 | 추가: } | |
| 178 | 추가: impl Planned { | |
| 179 | 추가: pub fn open(&self) -> Result<Input> { | |
| 180 | 추가: let input = Input::open(&self.path).map_err(|_| ErrorCode::SourceChanged)?; | |
| 181 | 추가: if !self.matches(&input.file)? { | |
| 182 | 추가: return Err(ErrorCode::SourceChanged); | |
| 183 | 추가: } | |
| 184 | 추가: Ok(input) | |
| 185 | 추가: } | |
| 186 | 추가: fn matches(&self, file: &File) -> Result<bool> { | |
| 187 | 추가: let metadata = file.metadata().map_err(crate::space::io_error)?; | |
| 188 | 추가: Ok(filesystem::object(file)? == self.object | |
| 189 | 추가: && metadata.modified().ok() == self.modified | |
| 190 | 추가: && metadata.is_dir() == (self.member.entry.kind == EntryKind::Directory) | |
| 191 | 추가: && (metadata.is_dir() || metadata.len() == self.member.entry.size)) | |
| 192 | 추가: } | |
| 193 | 추가: } | |
| 194 | 추가: pub(super) struct Plan { | |
| 195 | 추가: pub items: Vec<Planned>, | |
| 196 | 추가: limits: Limits, | |
| 197 | 추가: budget: MetadataBudget, | |
| 198 | 추가: total: u64, | |
| 199 | 추가: } | |
| 200 | 추가: impl Plan { | |
| 201 | 추가: pub fn new( | |
| 202 | 추가: sources: &[Source], | |
| 203 | 추가: limits: Limits, | |
| 204 | 추가: check: &mut impl FnMut() -> Result<()>, | |
| 205 | 추가: ) -> Result<Self> { | |
| 206 | 추가: if sources.len() > limits.entries as usize { | |
| 207 | 추가: return Err(ErrorCode::Limit); | |
| 208 | 추가: } | |
| 209 | 추가: let mut plan = Self { | |
| 210 | 추가: items: Vec::new(), | |
| 211 | 추가: limits, | |
| 212 | 추가: budget: MetadataBudget::default(), | |
| 213 | 추가: total: 0, | |
| 214 | 추가: }; | |
| 215 | 추가: let mut roots = std::collections::BTreeSet::new(); | |
| 216 | 추가: for source in sources { | |
| 217 | 추가: check()?; | |
| 218 | 추가: let name = archive::valid_name(&source.archive_name, false)?; | |
| 219 | 추가: if !roots.insert(name.to_lowercase()) { | |
| 220 | 추가: return Err(ErrorCode::Conflict); | |
| 221 | 추가: } | |
| 222 | 추가: } | |
| 223 | 추가: for source in sources { | |
| 224 | 추가: let path = source.path.to_path()?; | |
| 225 | 추가: let input = Input::open(&path)?; | |
| 226 | 추가: plan.visit(path, source.archive_name.clone(), input, check)?; | |
| 227 | 추가: } | |
| 228 | 추가: let members = plan | |
| 229 | 추가: .items | |
| 230 | 추가: .iter() | |
| 231 | 추가: .map(|p| Member { | |
| 232 | 추가: entry: p.member.entry.clone(), | |
| 233 | 추가: data: Vec::new(), | |
| 234 | 추가: }) | |
| 235 | 추가: .collect::<Vec<_>>(); | |
| 236 | 추가: archive::validate_members(&members)?; | |
| 237 | 추가: Ok(plan) | |
| 238 | 추가: } | |
| 239 | 추가: fn visit( | |
| 240 | 추가: &mut self, | |
| 241 | 추가: path: PathBuf, | |
| 242 | 추가: name: String, | |
| 243 | 추가: input: Input, | |
| 244 | 추가: check: &mut impl FnMut() -> Result<()>, | |
| 245 | 추가: ) -> Result<()> { | |
| 246 | 추가: check()?; | |
| 247 | 추가: if self.items.len() >= self.limits.entries as usize { | |
| 248 | 추가: return Err(ErrorCode::Limit); | |
| 249 | 추가: } | |
| 250 | 추가: let metadata: Metadata = input.file.metadata().map_err(crate::space::io_error)?; | |
| 251 | 추가: let directory = metadata.is_dir(); | |
| 252 | 추가: let name = archive::valid_name(&name, directory)?; | |
| 253 | 추가: self.budget.charge(&name)?; | |
| 254 | 추가: self.budget.charge(&path.to_string_lossy())?; | |
| 255 | 추가: let size = if directory { 0 } else { metadata.len() }; | |
| 256 | 추가: self.total = self | |
| 257 | 추가: .total | |
| 258 | 추가: .checked_add(size) | |
| 259 | 추가: .filter(|n| *n <= self.limits.input.min(self.limits.output)) | |
| 260 | 추가: .ok_or(ErrorCode::Limit)?; | |
| 261 | 추가: self.items.push(Planned { | |
| 262 | 추가: member: Member { | |
| 263 | 추가: entry: Entry { | |
| 264 | 추가: entry_id: self.items.len() as u32, | |
| 265 | 추가: name: name.clone(), | |
| 266 | 추가: kind: if directory { | |
| 267 | 추가: EntryKind::Directory | |
| 268 | 추가: } else { | |
| 269 | 추가: EntryKind::File | |
| 270 | 추가: }, | |
| 271 | 추가: size, | |
| 272 | 추가: compressed_size: 0, | |
| 273 | 추가: encrypted: false, | |
| 274 | 추가: }, | |
| 275 | 추가: data: Vec::new(), | |
| 276 | 추가: }, | |
| 277 | 추가: path: path.clone(), | |
| 278 | 추가: object: filesystem::object(&input.file)?, | |
| 279 | 추가: modified: metadata.modified().ok(), | |
| 280 | 추가: }); | |
| 281 | 추가: if directory { | |
| 282 | 추가: let mut children = Vec::new(); | |
| 283 | 추가: for entry in input.children(&path)? { | |
| 284 | 추가: check()?; | |
| 285 | 추가: if self.items.len() + children.len() >= self.limits.entries as usize { | |
| 286 | 추가: return Err(ErrorCode::Limit); | |
| 287 | 추가: } | |
| 288 | 추가: let leaf = entry | |
| 289 | 추가: .map_err(crate::space::io_error)? | |
| 290 | 추가: .file_name() | |
| 291 | 추가: .into_string() | |
| 292 | 추가: .map_err(|_| ErrorCode::UnsafePath)?; | |
| 293 | 추가: self.budget.charge(&leaf)?; | |
| 294 | 추가: children.push(leaf); | |
| 295 | 추가: } | |
| 296 | 추가: children.sort(); | |
| 297 | 추가: for leaf in children { | |
| 298 | 추가: let child = input.open_child(&path, &leaf)?; | |
| 299 | 추가: self.visit(path.join(&leaf), format!("{name}/{leaf}"), child, check)?; | |
| 300 | 추가: } | |
| 301 | 추가: } | |
| 302 | 추가: Ok(()) | |
| 303 | 추가: } | |
| 304 | 추가: pub fn verify(&self, check: &mut impl FnMut() -> Result<()>) -> Result<()> { | |
| 305 | 추가: for item in &self.items { | |
| 306 | 추가: check()?; | |
| 307 | 추가: item.open()?; | |
| 308 | 추가: } | |
| 309 | 추가: Ok(()) | |
| 310 | 추가: } | |
| 311 | 추가: } | |
| 312 | 추가: | |
| 313 | 추가: #[cfg(test)] | |
| 314 | 추가: #[path = "../../../tests/rust-archive-worker/create_sources.rs"] | |
| 315 | 추가: mod tests; |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | pub mod archive; |
| 2 | 2 | mod create; |
| 3 | 추가: mod create_sources; | |
| 3 | 4 | mod crypto; |
| 4 | 5 | pub mod filesystem; |
| 5 | 6 | mod lzma; |
| @@ -407,7 +407,7 @@ impl<W: Write> Job<W> { | ||
| 407 | 407 | let name = archive::valid_name(leaf, false)?; |
| 408 | 408 | let root = Dir::root(output.parent().ok_or(ErrorCode::InvalidRequest)?)?; |
| 409 | 409 | filesystem::preflight(&root, &[(name.clone(), EntryKind::File)])?; |
| 410 | 삭제: let members = crate::create::plan(&sources, start.limits)?; | |
| 410 | 추가: let members = crate::create::plan(&sources, start.limits, &mut || self.check())?; | |
| 411 | 411 | let secret = if encryption == Encryption::Aes256 { |
| 412 | 412 | let secret = self.password(PasswordPurpose::Encrypt, 1)?; |
| 413 | 413 | if secret.0.is_empty() { |
| @@ -418,7 +418,6 @@ impl<W: Write> Job<W> { | ||
| 418 | 418 | None |
| 419 | 419 | }; |
| 420 | 420 | let (mut data, length) = crate::create::zip( |
| 421 | 삭제: &sources, | |
| 422 | 421 | &members, |
| 423 | 422 | method, |
| 424 | 423 | start.limits, |
| @@ -91,6 +91,20 @@ impl Snapshot { | ||
| 91 | 91 | options.share_mode(1); |
| 92 | 92 | } |
| 93 | 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> { | |
| 94 | 108 | let before = source.metadata().map_err(|_| ErrorCode::Io)?; |
| 95 | 109 | if !before.is_file() { |
| 96 | 110 | return Err(ErrorCode::InvalidRequest); |
| @@ -120,12 +134,9 @@ impl Snapshot { | ||
| 120 | 134 | hash.update(&buffer[..n]); |
| 121 | 135 | } |
| 122 | 136 | let after = source.metadata().map_err(|_| ErrorCode::Io)?; |
| 123 | 삭제: let named = options.open(path).map_err(|_| ErrorCode::SourceChanged)?; | |
| 124 | 삭제: if filesystem::object(&named)? != object | |
| 125 | 삭제: || filesystem::object(&source)? != object | |
| 137 | 추가: if filesystem::object(&source)? != object | |
| 126 | 138 | || before.len() != len |
| 127 | 139 | || after.len() != len |
| 128 | 삭제: || !named.metadata().map_err(|_| ErrorCode::Io)?.is_file() | |
| 129 | 140 | || before.modified().ok() != after.modified().ok() |
| 130 | 141 | { |
| 131 | 142 | return Err(ErrorCode::SourceChanged); |
| @@ -0,0 +1,107 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: use std::sync::atomic::{AtomicU64, Ordering}; | |
| 3 | 추가: static NEXT: AtomicU64 = AtomicU64::new(0); | |
| 4 | 추가: struct Scratch(PathBuf); | |
| 5 | 추가: impl Scratch { | |
| 6 | 추가: fn new() -> Self { | |
| 7 | 추가: let root = | |
| 8 | 추가: Path::new(env!("CARGO_MANIFEST_DIR")).join("../../tests/rust-archive-worker/runs"); | |
| 9 | 추가: fs::create_dir_all(&root).unwrap(); | |
| 10 | 추가: let path = root.join(format!( | |
| 11 | 추가: "sources-{}-{}", | |
| 12 | 추가: std::process::id(), | |
| 13 | 추가: NEXT.fetch_add(1, Ordering::Relaxed) | |
| 14 | 추가: )); | |
| 15 | 추가: fs::create_dir(&path).unwrap(); | |
| 16 | 추가: Self(path.canonicalize().unwrap()) | |
| 17 | 추가: } | |
| 18 | 추가: fn source(&self, name: &str) -> Source { | |
| 19 | 추가: Source { | |
| 20 | 추가: path: NativePath::from_path(&self.0.join(name)), | |
| 21 | 추가: archive_name: name.into(), | |
| 22 | 추가: } | |
| 23 | 추가: } | |
| 24 | 추가: } | |
| 25 | 추가: impl Drop for Scratch { | |
| 26 | 추가: fn drop(&mut self) { | |
| 27 | 추가: fs::remove_dir_all(&self.0).unwrap(); | |
| 28 | 추가: } | |
| 29 | 추가: } | |
| 30 | 추가: #[test] | |
| 31 | 추가: fn recursive_counts_and_source_bytes_honor_exact_quotas() { | |
| 32 | 추가: let s = Scratch::new(); | |
| 33 | 추가: fs::create_dir(s.0.join("folder")).unwrap(); | |
| 34 | 추가: fs::create_dir(s.0.join("folder/empty")).unwrap(); | |
| 35 | 추가: fs::write(s.0.join("folder/file"), b"abc").unwrap(); | |
| 36 | 추가: for (entries, output, expected) in [ | |
| 37 | 추가: (3, 3, None), | |
| 38 | 추가: (2, 3, Some(ErrorCode::Limit)), | |
| 39 | 추가: (3, 2, Some(ErrorCode::Limit)), | |
| 40 | 추가: ] { | |
| 41 | 추가: let result = Plan::new( | |
| 42 | 추가: &[s.source("folder")], | |
| 43 | 추가: Limits { | |
| 44 | 추가: entries, | |
| 45 | 추가: output, | |
| 46 | 추가: ..Limits::default() | |
| 47 | 추가: }, | |
| 48 | 추가: &mut || Ok(()), | |
| 49 | 추가: ); | |
| 50 | 추가: assert_eq!(result.as_ref().err().copied(), expected); | |
| 51 | 추가: if let Ok(plan) = result { | |
| 52 | 추가: assert_eq!(plan.items.len(), 3); | |
| 53 | 추가: } | |
| 54 | 추가: } | |
| 55 | 추가: } | |
| 56 | 추가: #[test] | |
| 57 | 추가: fn replaced_source_is_rejected_before_capture() { | |
| 58 | 추가: let s = Scratch::new(); | |
| 59 | 추가: fs::write(s.0.join("file"), b"original").unwrap(); | |
| 60 | 추가: let plan = Plan::new(&[s.source("file")], Limits::default(), &mut || Ok(())).unwrap(); | |
| 61 | 추가: fs::rename(s.0.join("file"), s.0.join("previous")).unwrap(); | |
| 62 | 추가: fs::write(s.0.join("file"), b"different").unwrap(); | |
| 63 | 추가: assert!(matches!( | |
| 64 | 추가: plan.items[0].open(), | |
| 65 | 추가: Err(ErrorCode::SourceChanged) | |
| 66 | 추가: )); | |
| 67 | 추가: } | |
| 68 | 추가: #[test] | |
| 69 | 추가: fn cancellation_stops_directory_enumeration() { | |
| 70 | 추가: let s = Scratch::new(); | |
| 71 | 추가: fs::create_dir(s.0.join("folder")).unwrap(); | |
| 72 | 추가: for n in 0..20 { | |
| 73 | 추가: fs::write(s.0.join(format!("folder/{n}")), b"x").unwrap(); | |
| 74 | 추가: } | |
| 75 | 추가: let mut calls = 0; | |
| 76 | 추가: let result = Plan::new(&[s.source("folder")], Limits::default(), &mut || { | |
| 77 | 추가: calls += 1; | |
| 78 | 추가: if calls == 5 { | |
| 79 | 추가: Err(ErrorCode::None) | |
| 80 | 추가: } else { | |
| 81 | 추가: Ok(()) | |
| 82 | 추가: } | |
| 83 | 추가: }); | |
| 84 | 추가: assert!(matches!(result, Err(ErrorCode::None))); | |
| 85 | 추가: } | |
| 86 | 추가: #[cfg(target_os = "linux")] | |
| 87 | 추가: #[test] | |
| 88 | 추가: fn pinned_directory_enumeration_does_not_follow_replacement_link() { | |
| 89 | 추가: let s = Scratch::new(); | |
| 90 | 추가: fs::create_dir(s.0.join("folder")).unwrap(); | |
| 91 | 추가: fs::create_dir(s.0.join("outside")).unwrap(); | |
| 92 | 추가: fs::write(s.0.join("folder/original"), b"original").unwrap(); | |
| 93 | 추가: fs::write(s.0.join("outside/secret"), b"secret").unwrap(); | |
| 94 | 추가: let path = s.0.join("folder"); | |
| 95 | 추가: let input = Input::open(&path).unwrap(); | |
| 96 | 추가: fs::rename(&path, s.0.join("moved")).unwrap(); | |
| 97 | 추가: std::os::unix::fs::symlink(s.0.join("outside"), &path).unwrap(); | |
| 98 | 추가: let names = input | |
| 99 | 추가: .children(&path) | |
| 100 | 추가: .unwrap() | |
| 101 | 추가: .map(|v| v.unwrap().file_name()) | |
| 102 | 추가: .collect::<Vec<_>>(); | |
| 103 | 추가: assert_eq!(names, vec![std::ffi::OsString::from("original")]); | |
| 104 | 추가: assert!(input.open_child(&path, "original").is_ok()); | |
| 105 | 추가: assert!(input.open_child(&path, "secret").is_err()); | |
| 106 | 추가: assert!(matches!(Input::open(&path), Err(ErrorCode::UnsafePath))); | |
| 107 | 추가: } |
| @@ -0,0 +1,327 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: use sha2::{Digest, Sha256}; | |
| 3 | 추가: fn request(s: &Scratch, method: Method, encryption: Encryption) -> Start { | |
| 4 | 추가: Start { | |
| 5 | 추가: action: Action::CreateZip { | |
| 6 | 추가: sources: vec![ | |
| 7 | 추가: Source { | |
| 8 | 추가: path: s.path("tree"), | |
| 9 | 추가: archive_name: "documents".into(), | |
| 10 | 추가: }, | |
| 11 | 추가: Source { | |
| 12 | 추가: path: s.path("loose"), | |
| 13 | 추가: archive_name: "loose.bin".into(), | |
| 14 | 추가: }, | |
| 15 | 추가: ], | |
| 16 | 추가: output_file: s.path("created.zip"), | |
| 17 | 추가: method, | |
| 18 | 추가: encryption, | |
| 19 | 추가: }, | |
| 20 | 추가: limits: Limits::default(), | |
| 21 | 추가: } | |
| 22 | 추가: } | |
| 23 | 추가: fn seed(s: &Scratch) -> Vec<u8> { | |
| 24 | 추가: fs::create_dir_all(s.0.join("tree/nested")).unwrap(); | |
| 25 | 추가: fs::create_dir(s.0.join("tree/empty")).unwrap(); | |
| 26 | 추가: let bytes = (0..262157).map(|i| (i % 251) as u8).collect::<Vec<_>>(); | |
| 27 | 추가: fs::write(s.0.join("tree/nested/한글.bin"), &bytes).unwrap(); | |
| 28 | 추가: fs::write(s.0.join("tree/zero"), []).unwrap(); | |
| 29 | 추가: fs::write(s.0.join("loose"), b"loose content").unwrap(); | |
| 30 | 추가: bytes | |
| 31 | 추가: } | |
| 32 | 추가: #[test] | |
| 33 | 추가: fn mixed_folders_and_files_round_trip_plain_and_aes_with_sha256() { | |
| 34 | 추가: for method in [Method::Store, Method::Deflate] { | |
| 35 | 추가: for encryption in [Encryption::None, Encryption::Aes256] { | |
| 36 | 추가: let s = Scratch::new(); | |
| 37 | 추가: let original = seed(&s); | |
| 38 | 추가: let events = interactive(request(&s, method, encryption), |id, _, _| { | |
| 39 | 추가: vec![Message::Password { | |
| 40 | 추가: challenge_id: id, | |
| 41 | 추가: secret: Secret(b"folder-password".to_vec()), | |
| 42 | 추가: }] | |
| 43 | 추가: }); | |
| 44 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 45 | 추가: let listed = execute(Start { | |
| 46 | 추가: action: Action::List { | |
| 47 | 추가: archive: s.path("created.zip"), | |
| 48 | 추가: }, | |
| 49 | 추가: limits: Limits::default(), | |
| 50 | 추가: }); | |
| 51 | 추가: assert_eq!(terminal(&listed).outcome, Outcome::Completed, "{listed:?}"); | |
| 52 | 추가: let entries = listed | |
| 53 | 추가: .iter() | |
| 54 | 추가: .filter_map(|e| { | |
| 55 | 추가: if let Message::Entry(e) = e { | |
| 56 | 추가: Some(e) | |
| 57 | 추가: } else { | |
| 58 | 추가: None | |
| 59 | 추가: } | |
| 60 | 추가: }) | |
| 61 | 추가: .collect::<Vec<_>>(); | |
| 62 | 추가: assert_eq!(entries.len(), 6); | |
| 63 | 추가: assert_eq!( | |
| 64 | 추가: entries | |
| 65 | 추가: .iter() | |
| 66 | 추가: .filter(|e| e.kind == EntryKind::Directory) | |
| 67 | 추가: .count(), | |
| 68 | 추가: 3 | |
| 69 | 추가: ); | |
| 70 | 추가: for entry in entries { | |
| 71 | 추가: assert_eq!( | |
| 72 | 추가: entry.encrypted, | |
| 73 | 추가: encryption == Encryption::Aes256 && entry.kind == EntryKind::File | |
| 74 | 추가: ); | |
| 75 | 추가: } | |
| 76 | 추가: if encryption == Encryption::None { | |
| 77 | 추가: assert!(Command::new("python3").arg("-c").arg("import sys,zipfile; z=zipfile.ZipFile(sys.argv[1]); assert z.testzip() is None; assert z.getinfo('documents/empty/').is_dir(); assert z.read('loose.bin') == b'loose content'").arg(s.0.join("created.zip")).status().unwrap().success()); | |
| 78 | 추가: } | |
| 79 | 추가: fs::create_dir(s.0.join("output")).unwrap(); | |
| 80 | 추가: #[cfg(unix)] | |
| 81 | 추가: { | |
| 82 | 추가: use std::os::unix::fs::PermissionsExt; | |
| 83 | 추가: fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); | |
| 84 | 추가: } | |
| 85 | 추가: let extracted = interactive( | |
| 86 | 추가: Start { | |
| 87 | 추가: action: Action::Extract { | |
| 88 | 추가: archive: s.path("created.zip"), | |
| 89 | 추가: destination: s.path("output"), | |
| 90 | 추가: selection: Selection::All, | |
| 91 | 추가: list_id: None, | |
| 92 | 추가: gzip_output_name: None, | |
| 93 | 추가: }, | |
| 94 | 추가: limits: Limits::default(), | |
| 95 | 추가: }, | |
| 96 | 추가: |id, _, _| { | |
| 97 | 추가: vec![Message::Password { | |
| 98 | 추가: challenge_id: id, | |
| 99 | 추가: secret: Secret(b"folder-password".to_vec()), | |
| 100 | 추가: }] | |
| 101 | 추가: }, | |
| 102 | 추가: ); | |
| 103 | 추가: assert_eq!( | |
| 104 | 추가: terminal(&extracted).outcome, | |
| 105 | 추가: Outcome::Completed, | |
| 106 | 추가: "{extracted:?}" | |
| 107 | 추가: ); | |
| 108 | 추가: assert_eq!( | |
| 109 | 추가: Sha256::digest(fs::read(s.0.join("output/documents/nested/한글.bin")).unwrap()), | |
| 110 | 추가: Sha256::digest(original) | |
| 111 | 추가: ); | |
| 112 | 추가: assert_eq!( | |
| 113 | 추가: Sha256::digest(fs::read(s.0.join("output/loose.bin")).unwrap()), | |
| 114 | 추가: Sha256::digest(b"loose content") | |
| 115 | 추가: ); | |
| 116 | 추가: assert_eq!( | |
| 117 | 추가: fs::read(s.0.join("output/documents/zero")).unwrap(), | |
| 118 | 추가: Vec::<u8>::new() | |
| 119 | 추가: ); | |
| 120 | 추가: assert!(s.0.join("output/documents/empty").is_dir()); | |
| 121 | 추가: } | |
| 122 | 추가: } | |
| 123 | 추가: } | |
| 124 | 추가: #[test] | |
| 125 | 추가: fn descendant_name_conflict_is_rejected_before_password() { | |
| 126 | 추가: let s = Scratch::new(); | |
| 127 | 추가: seed(&s); | |
| 128 | 추가: let mut start = request(&s, Method::Store, Encryption::Aes256); | |
| 129 | 추가: if let Action::CreateZip { sources, .. } = &mut start.action { | |
| 130 | 추가: sources[1].archive_name = "documents/zero".into(); | |
| 131 | 추가: } | |
| 132 | 추가: let events = execute(start); | |
| 133 | 추가: assert_eq!(terminal(&events).error, ErrorCode::Conflict, "{events:?}"); | |
| 134 | 추가: assert!( | |
| 135 | 추가: !events | |
| 136 | 추가: .iter() | |
| 137 | 추가: .any(|e| matches!(e, Message::PasswordRequired { .. })) | |
| 138 | 추가: ); | |
| 139 | 추가: assert!(!s.0.join("created.zip").exists()); | |
| 140 | 추가: } | |
| 141 | 추가: #[test] | |
| 142 | 추가: fn empty_root_folder_is_preserved() { | |
| 143 | 추가: let s = Scratch::new(); | |
| 144 | 추가: fs::create_dir(s.0.join("tree")).unwrap(); | |
| 145 | 추가: let mut start = request(&s, Method::Deflate, Encryption::None); | |
| 146 | 추가: if let Action::CreateZip { sources, .. } = &mut start.action { | |
| 147 | 추가: sources.truncate(1); | |
| 148 | 추가: } | |
| 149 | 추가: let events = execute(start); | |
| 150 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 151 | 추가: let bytes = fs::read(s.0.join("created.zip")).unwrap(); | |
| 152 | 추가: let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 153 | 추가: assert_eq!(catalog.members.len(), 1); | |
| 154 | 추가: assert_eq!(catalog.members[0].entry.kind, EntryKind::Directory); | |
| 155 | 추가: assert_eq!(catalog.members[0].entry.name, "documents"); | |
| 156 | 추가: } | |
| 157 | 추가: #[cfg(target_os = "linux")] | |
| 158 | 추가: #[test] | |
| 159 | 추가: fn symlinks_and_special_files_are_not_followed() { | |
| 160 | 추가: let s = Scratch::new(); | |
| 161 | 추가: seed(&s); | |
| 162 | 추가: for target in ["loose", "tree", "missing"] { | |
| 163 | 추가: std::os::unix::fs::symlink(s.0.join(target), s.0.join("tree/link")).unwrap(); | |
| 164 | 추가: let events = execute(request(&s, Method::Store, Encryption::Aes256)); | |
| 165 | 추가: assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); | |
| 166 | 추가: assert!( | |
| 167 | 추가: !events | |
| 168 | 추가: .iter() | |
| 169 | 추가: .any(|e| matches!(e, Message::PasswordRequired { .. })) | |
| 170 | 추가: ); | |
| 171 | 추가: assert!(!s.0.join("created.zip").exists()); | |
| 172 | 추가: fs::remove_file(s.0.join("tree/link")).unwrap(); | |
| 173 | 추가: } | |
| 174 | 추가: assert!( | |
| 175 | 추가: Command::new("mkfifo") | |
| 176 | 추가: .arg(s.0.join("tree/pipe")) | |
| 177 | 추가: .status() | |
| 178 | 추가: .unwrap() | |
| 179 | 추가: .success() | |
| 180 | 추가: ); | |
| 181 | 추가: let events = execute(request(&s, Method::Store, Encryption::None)); | |
| 182 | 추가: assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); | |
| 183 | 추가: } | |
| 184 | 추가: #[test] | |
| 185 | 추가: fn source_replacement_while_waiting_for_password_leaves_no_zip() { | |
| 186 | 추가: let s = Scratch::new(); | |
| 187 | 추가: seed(&s); | |
| 188 | 추가: let events = interactive( | |
| 189 | 추가: request(&s, Method::Store, Encryption::Aes256), | |
| 190 | 추가: |id, _, _| { | |
| 191 | 추가: fs::rename(s.0.join("tree/nested/한글.bin"), s.0.join("previous")).unwrap(); | |
| 192 | 추가: fs::write(s.0.join("tree/nested/한글.bin"), b"replacement").unwrap(); | |
| 193 | 추가: vec![Message::Password { | |
| 194 | 추가: challenge_id: id, | |
| 195 | 추가: secret: Secret(b"folder-password".to_vec()), | |
| 196 | 추가: }] | |
| 197 | 추가: }, | |
| 198 | 추가: ); | |
| 199 | 추가: assert_eq!( | |
| 200 | 추가: terminal(&events).error, | |
| 201 | 추가: ErrorCode::SourceChanged, | |
| 202 | 추가: "{events:?}" | |
| 203 | 추가: ); | |
| 204 | 추가: assert!(!s.0.join("created.zip").exists()); | |
| 205 | 추가: } | |
| 206 | 추가: | |
| 207 | 추가: #[cfg(target_os = "linux")] | |
| 208 | 추가: #[test] | |
| 209 | 추가: fn invalid_descendant_names_and_case_collisions_fail_before_password() { | |
| 210 | 추가: let s = Scratch::new(); | |
| 211 | 추가: seed(&s); | |
| 212 | 추가: for (leaf, expected) in [ | |
| 213 | 추가: ("CON.txt", ErrorCode::UnsafePath), | |
| 214 | 추가: ("ZERO", ErrorCode::Conflict), | |
| 215 | 추가: ] { | |
| 216 | 추가: fs::write(s.0.join("tree").join(leaf), b"not exported").unwrap(); | |
| 217 | 추가: let events = execute(request(&s, Method::Store, Encryption::Aes256)); | |
| 218 | 추가: assert_eq!(terminal(&events).error, expected, "{events:?}"); | |
| 219 | 추가: assert!( | |
| 220 | 추가: !events | |
| 221 | 추가: .iter() | |
| 222 | 추가: .any(|e| matches!(e, Message::PasswordRequired { .. })) | |
| 223 | 추가: ); | |
| 224 | 추가: assert!(!s.0.join("created.zip").exists()); | |
| 225 | 추가: fs::remove_file(s.0.join("tree").join(leaf)).unwrap(); | |
| 226 | 추가: } | |
| 227 | 추가: } | |
| 228 | 추가: | |
| 229 | 추가: #[cfg(target_os = "linux")] | |
| 230 | 추가: fn readable_sources_under_parent(mode: u32) { | |
| 231 | 추가: use std::os::unix::fs::PermissionsExt; | |
| 232 | 추가: struct RestoreParent(PathBuf); | |
| 233 | 추가: impl Drop for RestoreParent { | |
| 234 | 추가: fn drop(&mut self) { | |
| 235 | 추가: fs::set_permissions(&self.0, fs::Permissions::from_mode(0o700)).unwrap(); | |
| 236 | 추가: } | |
| 237 | 추가: } | |
| 238 | 추가: let s = Scratch::new(); | |
| 239 | 추가: let parent = s.0.join("restricted"); | |
| 240 | 추가: fs::create_dir_all(parent.join("tree/empty")).unwrap(); | |
| 241 | 추가: fs::write(parent.join("loose"), b"readable file").unwrap(); | |
| 242 | 추가: fs::write(parent.join("tree/nested"), b"nested content").unwrap(); | |
| 243 | 추가: let _restore = RestoreParent(parent.clone()); | |
| 244 | 추가: fs::set_permissions(&parent, fs::Permissions::from_mode(mode)).unwrap(); | |
| 245 | 추가: if mode == 0o111 { | |
| 246 | 추가: assert_eq!( | |
| 247 | 추가: fs::read_dir(&parent).unwrap_err().kind(), | |
| 248 | 추가: std::io::ErrorKind::PermissionDenied | |
| 249 | 추가: ); | |
| 250 | 추가: } else { | |
| 251 | 추가: assert!(fs::read_dir(&parent).is_ok()); | |
| 252 | 추가: } | |
| 253 | 추가: assert_eq!(fs::read(parent.join("loose")).unwrap(), b"readable file"); | |
| 254 | 추가: for (name, encryption) in [("plain", Encryption::None), ("aes", Encryption::Aes256)] { | |
| 255 | 추가: let archive_name = format!("{name}.zip"); | |
| 256 | 추가: let events = interactive( | |
| 257 | 추가: Start { | |
| 258 | 추가: action: Action::CreateZip { | |
| 259 | 추가: sources: vec![ | |
| 260 | 추가: Source { | |
| 261 | 추가: path: NativePath::from_path(&parent.join("loose")), | |
| 262 | 추가: archive_name: "loose.txt".into(), | |
| 263 | 추가: }, | |
| 264 | 추가: Source { | |
| 265 | 추가: path: NativePath::from_path(&parent.join("tree")), | |
| 266 | 추가: archive_name: "tree".into(), | |
| 267 | 추가: }, | |
| 268 | 추가: ], | |
| 269 | 추가: output_file: s.path(&archive_name), | |
| 270 | 추가: method: Method::Deflate, | |
| 271 | 추가: encryption, | |
| 272 | 추가: }, | |
| 273 | 추가: limits: Limits::default(), | |
| 274 | 추가: }, | |
| 275 | 추가: |id, _, _| { | |
| 276 | 추가: vec![Message::Password { | |
| 277 | 추가: challenge_id: id, | |
| 278 | 추가: secret: Secret(b"parent-password".to_vec()), | |
| 279 | 추가: }] | |
| 280 | 추가: }, | |
| 281 | 추가: ); | |
| 282 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 283 | 추가: let output = format!("output-{name}"); | |
| 284 | 추가: fs::create_dir(s.0.join(&output)).unwrap(); | |
| 285 | 추가: fs::set_permissions(s.0.join(&output), fs::Permissions::from_mode(0o700)).unwrap(); | |
| 286 | 추가: let extracted = interactive( | |
| 287 | 추가: Start { | |
| 288 | 추가: action: Action::Extract { | |
| 289 | 추가: archive: s.path(&archive_name), | |
| 290 | 추가: destination: s.path(&output), | |
| 291 | 추가: selection: Selection::All, | |
| 292 | 추가: list_id: None, | |
| 293 | 추가: gzip_output_name: None, | |
| 294 | 추가: }, | |
| 295 | 추가: limits: Limits::default(), | |
| 296 | 추가: }, | |
| 297 | 추가: |id, _, _| { | |
| 298 | 추가: vec![Message::Password { | |
| 299 | 추가: challenge_id: id, | |
| 300 | 추가: secret: Secret(b"parent-password".to_vec()), | |
| 301 | 추가: }] | |
| 302 | 추가: }, | |
| 303 | 추가: ); | |
| 304 | 추가: assert_eq!( | |
| 305 | 추가: terminal(&extracted).outcome, | |
| 306 | 추가: Outcome::Completed, | |
| 307 | 추가: "{extracted:?}" | |
| 308 | 추가: ); | |
| 309 | 추가: for (source, destination) in [("loose", "loose.txt"), ("tree/nested", "tree/nested")] { | |
| 310 | 추가: assert_eq!( | |
| 311 | 추가: Sha256::digest(fs::read(parent.join(source)).unwrap()), | |
| 312 | 추가: Sha256::digest(fs::read(s.0.join(&output).join(destination)).unwrap()) | |
| 313 | 추가: ); | |
| 314 | 추가: } | |
| 315 | 추가: assert!(s.0.join(&output).join("tree/empty").is_dir()); | |
| 316 | 추가: } | |
| 317 | 추가: } | |
| 318 | 추가: #[cfg(target_os = "linux")] | |
| 319 | 추가: #[test] | |
| 320 | 추가: fn readable_sources_under_search_only_parent_0111() { | |
| 321 | 추가: readable_sources_under_parent(0o111); | |
| 322 | 추가: } | |
| 323 | 추가: #[cfg(target_os = "linux")] | |
| 324 | 추가: #[test] | |
| 325 | 추가: fn readable_sources_under_readable_parent_0711() { | |
| 326 | 추가: readable_sources_under_parent(0o711); | |
| 327 | 추가: } |
| @@ -1593,3 +1593,5 @@ fn disk_full_error_tag_preserves_previous_tags_and_rejects_unknown() { | ||
| 1593 | 1593 | assert!(Reader::new(bytes.as_slice()).read().is_err()); |
| 1594 | 1594 | } |
| 1595 | 1595 | } |
| 1596 | 추가: | |
| 1597 | 추가: mod folder_inputs; |