압축 풀기 임시 파일을 풀 위치 폴더에 만들고 할당량 초과를 공간 부족으로 안내
압축 풀기는 원본 사본과 단계 임시 파일을 시스템 임시 폴더(/tmp)에 만들었다. /tmp가 사용자 할당량이 있는 tmpfs이면 큰 압축파일에서 쓰기가 EDQUOT로 실패했고, 이 오류가 Io로 분류되어 "접근 권한을 확인하세요"라는 잘못된 안내가 나왔다. - 압축 풀기 작업 동안 spool::temporary()가 풀 위치 폴더에 임시 파일을 만든다. Linux는 O_TMPFILE, 미지원 파일 시스템은 숨김 이름으로 만든 즉시 unlink, Windows는 숨김 delete-on-close 파일을 쓴다. 폴더를 미리 열 수 없으면 기존 위치를 쓰고 오류는 원래 지점에서 같은 순서로 보고한다. - EDQUOT/QuotaExceeded와 Windows 1295를 DiskFull로 변환한다. - 시스템 임시 폴더를 쓸 수 없어도 해제가 성공하고 출력 폴더에 임시 파일이 남지 않는지 시험한다. io_fault 주입기를 새 위치의 O_TMPFILE에도 적용한다. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
97cc56315c3418756383d7fbdba0b42fbf7167da- 부모
- 5d0afa5
변경된 파일 9개
+201-31| @@ -319,6 +319,29 @@ mod platform { | ||
| 319 | 319 | pub fn create(&self, n: &str) -> std::io::Result<File> { |
| 320 | 320 | open(&self.file, std::ffi::OsStr::new(n), 1 | 0x40 | 0x80) |
| 321 | 321 | } |
| 322 | 추가: /// 이 폴더 안에 이름 없는 읽기/쓰기 임시 파일을 만듦. 마지막 핸들을 닫으면 제거됨. | |
| 323 | 추가: pub(crate) fn temporary(&self) -> Result<File> { | |
| 324 | 추가: // O_TMPFILE | O_EXCL: 이름이 생기지 않고 나중에 링크할 수도 없음. | |
| 325 | 추가: match open(&self.file, std::ffi::OsStr::new("."), 2 | 0x410000 | 0x80) { | |
| 326 | 추가: Ok(file) => return Ok(file), | |
| 327 | 추가: Err(e) => { | |
| 328 | 추가: if crate::space::io_error(e) == ErrorCode::DiskFull { | |
| 329 | 추가: return Err(ErrorCode::DiskFull); | |
| 330 | 추가: } | |
| 331 | 추가: } | |
| 332 | 추가: } | |
| 333 | 추가: // O_TMPFILE을 지원하지 않는 파일 시스템은 숨김 이름으로 만든 즉시 이름을 지움. | |
| 334 | 추가: let n = format!(".zipelo-{}.tmp", crate::spool::random_name()?); | |
| 335 | 추가: let file = open(&self.file, std::ffi::OsStr::new(&n), 2 | 0x40 | 0x80) | |
| 336 | 추가: .map_err(crate::space::io_error)?; | |
| 337 | 추가: if !self.remove(&n) { | |
| 338 | 추가: return Err(ErrorCode::Io); | |
| 339 | 추가: } | |
| 340 | 추가: if file.metadata().map_err(|_| ErrorCode::Io)?.nlink() != 0 { | |
| 341 | 추가: return Err(ErrorCode::Io); | |
| 342 | 추가: } | |
| 343 | 추가: Ok(file) | |
| 344 | 추가: } | |
| 322 | 345 | pub fn link(&self, from: &str, to: &str) -> std::io::Result<()> { |
| 323 | 346 | let a = CString::new(from).unwrap(); |
| 324 | 347 | let b = CString::new(to).unwrap(); |
| @@ -478,6 +501,10 @@ mod platform { | ||
| 478 | 501 | .custom_flags(0x00200000) |
| 479 | 502 | .open(self.path.join(n)) |
| 480 | 503 | } |
| 504 | 추가: /// 이 폴더 안에 닫으면 지워지는 숨김 임시 파일을 만듦. | |
| 505 | 추가: pub(crate) fn temporary(&self) -> Result<File> { | |
| 506 | 추가: crate::spool::windows_temporary(&self.path, ".zipelo-", true) | |
| 507 | 추가: } | |
| 481 | 508 | pub fn link(&self, a: &str, b: &str) -> std::io::Result<()> { |
| 482 | 509 | std::fs::hard_link(self.path.join(a), self.path.join(b)) |
| 483 | 510 | } |
| @@ -586,6 +586,17 @@ impl<W: Write> Job<W> { | ||
| 586 | 586 | return Err(ErrorCode::InvalidRequest); |
| 587 | 587 | } |
| 588 | 588 | } |
| 589 | 추가: // 해제의 원본 사본과 단계 임시 파일은 출력 폴더에 둠. 폴더를 열 수 없으면 기존 위치를 | |
| 590 | 추가: // 쓰고, 오류는 아래의 원래 출력 폴더 확인 지점에서 같은 순서로 보고함. | |
| 591 | 추가: let _spool_location = match &start.action { | |
| 592 | 추가: Action::Extract { destination, .. } => destination | |
| 593 | 추가: .to_path() | |
| 594 | 추가: .ok() | |
| 595 | 추가: .and_then(|path| Dir::root(&path).ok()) | |
| 596 | 추가: .map(|root| crate::spool::place_in(&root)) | |
| 597 | 추가: .transpose()?, | |
| 598 | 추가: _ => None, | |
| 599 | 추가: }; | |
| 589 | 600 | if let Some(budget) = &self.budget { |
| 590 | 601 | let scope = ResourceScope { |
| 591 | 602 | output: budget.ceiling.output > budget.soft.output, |
| @@ -123,13 +123,17 @@ pub(crate) fn available(file: &File) -> Result<u128> { | ||
| 123 | 123 | Ok(free as u128) |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 삭제: // 공간 부족만 전용 오류로 변환함. | |
| 126 | 추가: // 공간 부족만 전용 오류로 변환함. 사용자 디스크 할당량 초과(EDQUOT, Windows 1295)도 | |
| 127 | 추가: // 쓸 수 있는 공간이 없다는 뜻이므로 권한 오류가 아니라 공간 부족으로 알림. | |
| 127 | 128 | pub(crate) fn io_error(error: std::io::Error) -> ErrorCode { |
| 128 | 129 | #[cfg(windows)] |
| 129 | 삭제: if matches!(error.raw_os_error(), Some(39 | 112)) { | |
| 130 | 추가: if matches!(error.raw_os_error(), Some(39 | 112 | 1295)) { | |
| 130 | 131 | return ErrorCode::DiskFull; |
| 131 | 132 | } |
| 132 | 삭제: if error.kind() == std::io::ErrorKind::StorageFull { | |
| 133 | 추가: if matches!( | |
| 134 | 추가: error.kind(), | |
| 135 | 추가: std::io::ErrorKind::StorageFull | std::io::ErrorKind::QuotaExceeded | |
| 136 | 추가: ) { | |
| 133 | 137 | ErrorCode::DiskFull |
| 134 | 138 | } else { |
| 135 | 139 | ErrorCode::Io |
| @@ -8,7 +8,40 @@ use std::{ | ||
| 8 | 8 | |
| 9 | 9 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 10 | 10 | |
| 11 | 추가: thread_local! { | |
| 12 | 추가: // 작업 스레드가 지정한 임시 파일 위치. 없으면 시스템 임시 폴더를 사용함. | |
| 13 | 추가: static LOCATION: std::cell::RefCell<Option<filesystem::Dir>> = | |
| 14 | 추가: const { std::cell::RefCell::new(None) }; | |
| 15 | 추가: } | |
| 16 | 추가: | |
| 17 | 추가: /// 해제 작업의 임시 파일을 출력 폴더에 두도록 지정함. 반환값이 사라지면 해제됨. | |
| 18 | 추가: /// 작업은 한 스레드에서 실행되므로 다른 작업이나 시험에 섞이지 않음. | |
| 19 | 추가: pub(crate) struct Location(()); | |
| 20 | 추가: impl Drop for Location { | |
| 21 | 추가: fn drop(&mut self) { | |
| 22 | 추가: LOCATION.with(|location| *location.borrow_mut() = None); | |
| 23 | 추가: } | |
| 24 | 추가: } | |
| 25 | 추가: pub(crate) fn place_in(dir: &filesystem::Dir) -> Result<Location> { | |
| 26 | 추가: let dir = dir.duplicate()?; | |
| 27 | 추가: LOCATION.with(|location| *location.borrow_mut() = Some(dir)); | |
| 28 | 추가: Ok(Location(())) | |
| 29 | 추가: } | |
| 30 | 추가: | |
| 31 | 추가: pub(crate) fn random_name() -> Result<String> { | |
| 32 | 추가: let mut random = [0; 16]; | |
| 33 | 추가: crate::crypto::random(&mut random)?; | |
| 34 | 추가: Ok(random.iter().map(|b| format!("{b:02x}")).collect()) | |
| 35 | 추가: } | |
| 36 | 추가: | |
| 37 | 추가: /// 이름 없는 임시 파일을 만듦. 해제 작업에서는 출력 폴더에, 그 외에는 시스템 임시 폴더에 둠. | |
| 38 | 추가: /// 출력 폴더에 두면 공간 판단이 실제 출력 디스크와 같아지고 /tmp 크기나 할당량에 묶이지 않음. | |
| 11 | 39 | pub(crate) fn temporary() -> Result<File> { |
| 40 | 추가: if let Some(file) = | |
| 41 | 추가: LOCATION.with(|location| location.borrow().as_ref().map(filesystem::Dir::temporary)) | |
| 42 | 추가: { | |
| 43 | 추가: return file; | |
| 44 | 추가: } | |
| 12 | 45 | #[cfg(target_os = "linux")] |
| 13 | 46 | { |
| 14 | 47 | use std::os::unix::fs::OpenOptionsExt; |
| @@ -21,17 +54,13 @@ pub(crate) fn temporary() -> Result<File> { | ||
| 21 | 54 | .open(std::env::temp_dir()) |
| 22 | 55 | { |
| 23 | 56 | Ok(file) => return Ok(file), |
| 24 | 삭제: Err(ref error) if error.kind() == std::io::ErrorKind::StorageFull => { | |
| 25 | 삭제: return Err(ErrorCode::DiskFull); | |
| 57 | 추가: Err(error) => { | |
| 58 | 추가: if crate::space::io_error(error) == ErrorCode::DiskFull { | |
| 59 | 추가: return Err(ErrorCode::DiskFull); | |
| 60 | 추가: } | |
| 26 | 61 | } |
| 27 | 삭제: Err(_) => {} | |
| 28 | 62 | } |
| 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>(); | |
| 63 | 추가: let name = random_name()?; | |
| 35 | 64 | let path = std::env::temp_dir().join(format!("zipelo-{name}.tmp")); |
| 36 | 65 | let file = OpenOptions::new() |
| 37 | 66 | .read(true) |
| @@ -50,26 +79,27 @@ pub(crate) fn temporary() -> Result<File> { | ||
| 50 | 79 | } |
| 51 | 80 | #[cfg(windows)] |
| 52 | 81 | { |
| 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) | |
| 82 | 추가: windows_temporary(&std::env::temp_dir(), "zipelo-", false) | |
| 70 | 83 | } |
| 71 | 84 | } |
| 72 | 85 | |
| 86 | 추가: /// 닫으면 지워지는 Windows 임시 파일을 지정한 폴더에 만듦. 출력 폴더에서는 숨김 속성을 붙임. | |
| 87 | 추가: #[cfg(windows)] | |
| 88 | 추가: pub(crate) fn windows_temporary(parent: &Path, prefix: &str, hidden: bool) -> Result<File> { | |
| 89 | 추가: use std::os::windows::fs::OpenOptionsExt; | |
| 90 | 추가: let name = random_name()?; | |
| 91 | 추가: OpenOptions::new() | |
| 92 | 추가: .read(true) | |
| 93 | 추가: .write(true) | |
| 94 | 추가: .create_new(true) | |
| 95 | 추가: .share_mode(0) | |
| 96 | 추가: .access_mode(0xc0010000) | |
| 97 | 추가: .custom_flags(0x04000000 | 0x00200000) | |
| 98 | 추가: .attributes(if hidden { 0x100 | 0x2 } else { 0x100 }) | |
| 99 | 추가: .open(parent.join(format!("{prefix}{name}.tmp"))) | |
| 100 | 추가: .map_err(crate::space::io_error) | |
| 101 | 추가: } | |
| 102 | 추가: | |
| 73 | 103 | #[derive(Clone, Copy, Debug)] |
| 74 | 104 | pub(crate) enum Step { |
| 75 | 105 | Check, |
| @@ -820,6 +820,8 @@ mod linux { | ||
| 820 | 820 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 821 | 821 | assert!(s.0.join("paused").exists()); |
| 822 | 822 | assert_eq!(fs::read_dir(s.0.join("staging")).unwrap().count(), 0); |
| 823 | 추가: // 출력 폴더의 이름 있는 대체 임시 파일도 사용 전에 이름이 지워져야 함. | |
| 824 | 추가: assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 1); | |
| 823 | 825 | assert_zero_file(&s.0.join("output/payload.bin"), 1024 * 1024); |
| 824 | 826 | } |
| 825 | 827 | struct Subreaper(i32); |
| @@ -39,7 +39,7 @@ static int output_file(int fd) { | ||
| 39 | 39 | |
| 40 | 40 | int openat(int fd, const char *path, int flags, ...) { |
| 41 | 41 | mode_t mode = 0; |
| 42 | 삭제: if (flags & O_CREAT) { | |
| 42 | 추가: if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) { | |
| 43 | 43 | va_list args; |
| 44 | 44 | va_start(args, flags); |
| 45 | 45 | mode = va_arg(args, unsigned int); |
| @@ -51,6 +51,19 @@ int openat(int fd, const char *path, int flags, ...) { | ||
| 51 | 51 | errno = !strcmp(fault, "create-full") ? ENOSPC : EACCES; |
| 52 | 52 | return -1; |
| 53 | 53 | } |
| 54 | 추가: /* 해제 작업의 원본 사본과 단계 임시 파일은 출력 폴더 핸들 기준 O_TMPFILE로 만듦. */ | |
| 55 | 추가: if ((flags & O_TMPFILE) == O_TMPFILE) { | |
| 56 | 추가: if (fault && !strcmp(fault, "temp-full")) { | |
| 57 | 추가: errno = ENOSPC; | |
| 58 | 추가: return -1; | |
| 59 | 추가: } | |
| 60 | 추가: if (fault && !strcmp(fault, "fallback")) { | |
| 61 | 추가: int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); | |
| 62 | 추가: if (marker >= 0) close(marker); | |
| 63 | 추가: errno = EOPNOTSUPP; | |
| 64 | 추가: return -1; | |
| 65 | 추가: } | |
| 66 | 추가: } | |
| 54 | 67 | return real_openat(fd, path, flags, mode); |
| 55 | 68 | } |
| 56 | 69 |
| @@ -1634,3 +1634,6 @@ mod recovery; | ||
| 1634 | 1634 | |
| 1635 | 1635 | #[cfg(target_os = "linux")] |
| 1636 | 1636 | mod partial_output; |
| 1637 | 추가: | |
| 1638 | 추가: #[cfg(target_os = "linux")] | |
| 1639 | 추가: mod spool_location; |
| @@ -4,6 +4,10 @@ use std::io::{Error, ErrorKind}; | ||
| 4 | 4 | #[test] |
| 5 | 5 | fn storage_full_is_distinct_from_other_io_errors() { |
| 6 | 6 | assert_eq!(io_error(ErrorKind::StorageFull.into()), ErrorCode::DiskFull); |
| 7 | 추가: assert_eq!( | |
| 8 | 추가: io_error(ErrorKind::QuotaExceeded.into()), | |
| 9 | 추가: ErrorCode::DiskFull | |
| 10 | 추가: ); | |
| 7 | 11 | for kind in [ |
| 8 | 12 | ErrorKind::PermissionDenied, |
| 9 | 13 | ErrorKind::Other, |
| @@ -17,6 +21,8 @@ fn storage_full_is_distinct_from_other_io_errors() { | ||
| 17 | 21 | #[test] |
| 18 | 22 | fn native_linux_enospc_is_disk_full() { |
| 19 | 23 | assert_eq!(io_error(Error::from_raw_os_error(28)), ErrorCode::DiskFull); |
| 24 | 추가: // EDQUOT: tmpfs usrquota 등 사용자 할당량 초과도 권한 오류로 안내하지 않음. | |
| 25 | 추가: assert_eq!(io_error(Error::from_raw_os_error(122)), ErrorCode::DiskFull); | |
| 20 | 26 | for code in [5, 13, 39, 112] { |
| 21 | 27 | assert_eq!(io_error(Error::from_raw_os_error(code)), ErrorCode::Io); |
| 22 | 28 | } |
| @@ -25,7 +31,7 @@ fn native_linux_enospc_is_disk_full() { | ||
| 25 | 31 | #[cfg(windows)] |
| 26 | 32 | #[test] |
| 27 | 33 | fn native_windows_disk_full_codes_are_distinct_from_access_denied() { |
| 28 | 삭제: for code in [39, 112] { | |
| 34 | 추가: for code in [39, 112, 1295] { | |
| 29 | 35 | assert_eq!( |
| 30 | 36 | io_error(Error::from_raw_os_error(code)), |
| 31 | 37 | ErrorCode::DiskFull |
| @@ -0,0 +1,74 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: | |
| 3 | 추가: // 해제 작업의 원본 사본과 단계 임시 파일은 시스템 임시 폴더가 아니라 출력 폴더에 둠. | |
| 4 | 추가: // 시스템 임시 폴더를 쓸 수 없게 만들어도(/tmp 할당량 초과와 같은 상황) 해제가 성공해야 함. | |
| 5 | 추가: fn extract_without_system_temp(bytes: &[u8]) -> (Scratch, Vec<Message>) { | |
| 6 | 추가: let s = Scratch::new(); | |
| 7 | 추가: let start = encrypted_start(&s, bytes); | |
| 8 | 추가: let mut child = s | |
| 9 | 추가: .worker() | |
| 10 | 추가: .env("TMPDIR", s.0.join("missing-temp")) | |
| 11 | 추가: .stdin(Stdio::piped()) | |
| 12 | 추가: .stdout(Stdio::piped()) | |
| 13 | 추가: .stderr(Stdio::piped()) | |
| 14 | 추가: .spawn() | |
| 15 | 추가: .unwrap(); | |
| 16 | 추가: let mut writer = Some(Writer::new(child.stdin.take().unwrap(), 17)); | |
| 17 | 추가: writer | |
| 18 | 추가: .as_mut() | |
| 19 | 추가: .unwrap() | |
| 20 | 추가: .send(&Message::StartInteractive(start)) | |
| 21 | 추가: .unwrap(); | |
| 22 | 추가: let mut reader = Reader::new(child.stdout.take().unwrap()); | |
| 23 | 추가: let mut events = Vec::new(); | |
| 24 | 추가: while let Some((job, _, event)) = reader.read().unwrap() { | |
| 25 | 추가: assert_eq!(job, 17); | |
| 26 | 추가: if let Message::PasswordRequired { challenge_id, .. } = &event { | |
| 27 | 추가: for reply in password_reply(*challenge_id, TEST_PASSWORD) { | |
| 28 | 추가: writer.as_mut().unwrap().send(&reply).unwrap(); | |
| 29 | 추가: } | |
| 30 | 추가: } | |
| 31 | 추가: if matches!(event, Message::Terminal(_)) { | |
| 32 | 추가: writer = None; | |
| 33 | 추가: } | |
| 34 | 추가: events.push(event); | |
| 35 | 추가: } | |
| 36 | 추가: drop(writer); | |
| 37 | 추가: let output = child.wait_with_output().unwrap(); | |
| 38 | 추가: assert!(output.status.success(), "{:?}", output.stderr); | |
| 39 | 추가: assert!(output.stderr.is_empty()); | |
| 40 | 추가: assert!(!s.0.join("missing-temp").exists()); | |
| 41 | 추가: (s, events) | |
| 42 | 추가: } | |
| 43 | 추가: | |
| 44 | 추가: fn assert_only_outputs(s: &Scratch, expected: &[&str]) { | |
| 45 | 추가: let mut names = fs::read_dir(s.0.join("output")) | |
| 46 | 추가: .unwrap() | |
| 47 | 추가: .map(|entry| entry.unwrap().file_name().into_string().unwrap()) | |
| 48 | 추가: .collect::<Vec<_>>(); | |
| 49 | 추가: names.sort(); | |
| 50 | 추가: assert_eq!(names, expected, "임시 파일이 출력 폴더에 남으면 안 됨"); | |
| 51 | 추가: } | |
| 52 | 추가: | |
| 53 | 추가: #[test] | |
| 54 | 추가: fn extract_spools_in_destination_when_system_temp_is_unusable() { | |
| 55 | 추가: let (s, events) = extract_without_system_temp(include_bytes!("fixtures/deflate.zip")); | |
| 56 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 57 | 추가: assert_eq!(terminal(&events).publication, Publication::Complete); | |
| 58 | 추가: assert_eq!( | |
| 59 | 추가: fs::read(s.0.join("output/folder/hello.txt")).unwrap(), | |
| 60 | 추가: PAYLOAD | |
| 61 | 추가: ); | |
| 62 | 추가: assert_only_outputs(&s, &["empty", "folder"]); | |
| 63 | 추가: } | |
| 64 | 추가: | |
| 65 | 추가: #[test] | |
| 66 | 추가: fn encrypted_extract_spools_in_destination_when_system_temp_is_unusable() { | |
| 67 | 추가: let (s, events) = extract_without_system_temp(AES_ZIP); | |
| 68 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 69 | 추가: assert_eq!( | |
| 70 | 추가: fs::read(s.0.join("output/payload.bin")).unwrap(), | |
| 71 | 추가: ENCRYPTED_PAYLOAD | |
| 72 | 추가: ); | |
| 73 | 추가: assert_only_outputs(&s, &["payload.bin"]); | |
| 74 | 추가: } |