Merge pull request #147 from Team-LJY/fix/pr144-temp-cleanup-identity
종료 시 교체된 임시 폴더 보존
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
00db1f5c21506a9213d9a32103f332f6b3c7c595
변경된 파일 5개
+501-10| @@ -16,6 +16,7 @@ mod space; | ||
| 16 | 16 | mod split_seven_zip; |
| 17 | 17 | mod spool; |
| 18 | 18 | mod streaming; |
| 19 | 추가: pub mod tree_remove; | |
| 19 | 20 | pub mod wire; |
| 20 | 21 | mod zip_recovery; |
| 21 | 22 |
| @@ -0,0 +1,194 @@ | ||
| 1 | 추가: //! 열어 둔 폴더 핸들에 묶어서 폴더 안의 내용을 지움. | |
| 2 | 추가: //! 경로를 다시 해석하지 않으므로 확인한 뒤 경로가 바뀌어도 다른 폴더를 지우지 않음. | |
| 3 | 추가: | |
| 4 | 추가: use std::fs::File; | |
| 5 | 추가: use std::io; | |
| 6 | 추가: use std::path::Path; | |
| 7 | 추가: | |
| 8 | 추가: /// `dir` 핸들이 가리키는 폴더 안의 모든 항목을 지움. 폴더 자체는 지우지 않음. | |
| 9 | 추가: /// Linux는 핸들 기준 `unlinkat`으로 지우며 `path`는 쓰지 않음. | |
| 10 | 추가: /// Windows는 호출한 쪽이 공유 삭제를 허용하지 않고 `dir`을 열어 두어 폴더 이름이 | |
| 11 | 추가: /// 바뀌지 않게 한 상태에서만 `path`를 씀. | |
| 12 | 추가: pub fn remove_contents(dir: &File, path: &Path) -> io::Result<()> { | |
| 13 | 추가: platform::remove_contents(dir, path) | |
| 14 | 추가: } | |
| 15 | 추가: | |
| 16 | 추가: /// `dir` 핸들이 가리키는 폴더가 비어 있는지 확인함. 새로 만든 폴더를 핸들로 잡은 직후 쓰임. | |
| 17 | 추가: pub fn is_empty(dir: &File, path: &Path) -> io::Result<bool> { | |
| 18 | 추가: platform::is_empty(dir, path) | |
| 19 | 추가: } | |
| 20 | 추가: | |
| 21 | 추가: #[cfg(target_os = "linux")] | |
| 22 | 추가: mod platform { | |
| 23 | 추가: use std::ffi::{CStr, CString}; | |
| 24 | 추가: use std::fs::File; | |
| 25 | 추가: use std::io; | |
| 26 | 추가: use std::os::unix::{ | |
| 27 | 추가: fs::MetadataExt, | |
| 28 | 추가: io::{AsRawFd, FromRawFd}, | |
| 29 | 추가: }; | |
| 30 | 추가: use std::path::Path; | |
| 31 | 추가: | |
| 32 | 추가: #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] | |
| 33 | 추가: const NOFOLLOW_DIRECTORY: i32 = 0x8000 | 0x4000; | |
| 34 | 추가: #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] | |
| 35 | 추가: const NOFOLLOW_DIRECTORY: i32 = 0x20000 | 0x10000; | |
| 36 | 추가: const CLOEXEC: i32 = 0x80000; | |
| 37 | 추가: const AT_REMOVEDIR: i32 = 0x200; | |
| 38 | 추가: const ENOENT: i32 = 2; | |
| 39 | 추가: const EPERM: i32 = 1; | |
| 40 | 추가: const EISDIR: i32 = 21; | |
| 41 | 추가: /// 깊게 중첩된 폴더에서 파일 디스크립터가 바닥나지 않게 하는 한도. | |
| 42 | 추가: const MAX_DEPTH: usize = 256; | |
| 43 | 추가: | |
| 44 | 추가: #[repr(C)] | |
| 45 | 추가: struct Dirent64 { | |
| 46 | 추가: inode: u64, | |
| 47 | 추가: offset: i64, | |
| 48 | 추가: length: u16, | |
| 49 | 추가: kind: u8, | |
| 50 | 추가: name: [i8; 256], | |
| 51 | 추가: } | |
| 52 | 추가: | |
| 53 | 추가: unsafe extern "C" { | |
| 54 | 추가: fn openat(fd: i32, path: *const i8, flags: i32, ...) -> i32; | |
| 55 | 추가: fn unlinkat(fd: i32, path: *const i8, flags: i32) -> i32; | |
| 56 | 추가: fn fdopendir(fd: i32) -> *mut std::ffi::c_void; | |
| 57 | 추가: fn readdir64(stream: *mut std::ffi::c_void) -> *mut Dirent64; | |
| 58 | 추가: fn rewinddir(stream: *mut std::ffi::c_void); | |
| 59 | 추가: fn closedir(stream: *mut std::ffi::c_void) -> i32; | |
| 60 | 추가: } | |
| 61 | 추가: | |
| 62 | 추가: pub fn remove_contents(dir: &File, _path: &Path) -> io::Result<()> { | |
| 63 | 추가: let device = dir.metadata()?.dev(); | |
| 64 | 추가: remove_children(dir, device, 0) | |
| 65 | 추가: } | |
| 66 | 추가: | |
| 67 | 추가: pub fn is_empty(dir: &File, _path: &Path) -> io::Result<bool> { | |
| 68 | 추가: Ok(names(dir)?.is_empty()) | |
| 69 | 추가: } | |
| 70 | 추가: | |
| 71 | 추가: fn names(dir: &File) -> io::Result<Vec<CString>> { | |
| 72 | 추가: let copy = dir.try_clone()?; | |
| 73 | 추가: let fd = std::os::unix::io::IntoRawFd::into_raw_fd(copy); | |
| 74 | 추가: // 복제한 디스크립터의 소유권은 폴더 스트림이 가져가며 closedir이 닫음. | |
| 75 | 추가: let stream = unsafe { fdopendir(fd) }; | |
| 76 | 추가: if stream.is_null() { | |
| 77 | 추가: let error = io::Error::last_os_error(); | |
| 78 | 추가: // 스트림을 만들지 못했으므로 복제한 디스크립터를 직접 돌려받아 닫음. | |
| 79 | 추가: drop(unsafe { File::from_raw_fd(fd) }); | |
| 80 | 추가: return Err(error); | |
| 81 | 추가: } | |
| 82 | 추가: // 복제한 디스크립터는 원본과 읽기 위치를 공유하므로 처음부터 읽음. | |
| 83 | 추가: unsafe { rewinddir(stream) }; | |
| 84 | 추가: let mut found = Vec::new(); | |
| 85 | 추가: loop { | |
| 86 | 추가: let entry = unsafe { readdir64(stream) }; | |
| 87 | 추가: if entry.is_null() { | |
| 88 | 추가: break; | |
| 89 | 추가: } | |
| 90 | 추가: // 반환된 항목은 다음 readdir64 호출 전까지 유효하며 바로 복사함. | |
| 91 | 추가: let name = unsafe { CStr::from_ptr((*entry).name.as_ptr()) }; | |
| 92 | 추가: let bytes = name.to_bytes(); | |
| 93 | 추가: if bytes != b"." && bytes != b".." { | |
| 94 | 추가: found.push(name.to_owned()); | |
| 95 | 추가: } | |
| 96 | 추가: } | |
| 97 | 추가: unsafe { closedir(stream) }; | |
| 98 | 추가: Ok(found) | |
| 99 | 추가: } | |
| 100 | 추가: | |
| 101 | 추가: fn unlink(dir: &File, name: &CString, flags: i32) -> io::Result<()> { | |
| 102 | 추가: // 부모 핸들 기준으로 이름 하나만 지움. | |
| 103 | 추가: if unsafe { unlinkat(dir.as_raw_fd(), name.as_ptr(), flags) } == 0 { | |
| 104 | 추가: Ok(()) | |
| 105 | 추가: } else { | |
| 106 | 추가: Err(io::Error::last_os_error()) | |
| 107 | 추가: } | |
| 108 | 추가: } | |
| 109 | 추가: | |
| 110 | 추가: fn open_directory(dir: &File, name: &CString) -> io::Result<File> { | |
| 111 | 추가: // 심볼릭 링크를 따르지 않고 폴더만 부모 핸들 기준으로 엶. | |
| 112 | 추가: let fd = unsafe { | |
| 113 | 추가: openat( | |
| 114 | 추가: dir.as_raw_fd(), | |
| 115 | 추가: name.as_ptr(), | |
| 116 | 추가: NOFOLLOW_DIRECTORY | CLOEXEC, | |
| 117 | 추가: ) | |
| 118 | 추가: }; | |
| 119 | 추가: if fd < 0 { | |
| 120 | 추가: return Err(io::Error::last_os_error()); | |
| 121 | 추가: } | |
| 122 | 추가: Ok(unsafe { File::from_raw_fd(fd) }) | |
| 123 | 추가: } | |
| 124 | 추가: | |
| 125 | 추가: fn remove_children(dir: &File, device: u64, depth: usize) -> io::Result<()> { | |
| 126 | 추가: let mut failure = None; | |
| 127 | 추가: for name in names(dir)? { | |
| 128 | 추가: if let Err(error) = remove_entry(dir, &name, device, depth) { | |
| 129 | 추가: if error.raw_os_error() != Some(ENOENT) { | |
| 130 | 추가: failure.get_or_insert(error); | |
| 131 | 추가: } | |
| 132 | 추가: } | |
| 133 | 추가: } | |
| 134 | 추가: failure.map_or(Ok(()), Err) | |
| 135 | 추가: } | |
| 136 | 추가: | |
| 137 | 추가: fn remove_entry(dir: &File, name: &CString, device: u64, depth: usize) -> io::Result<()> { | |
| 138 | 추가: match unlink(dir, name, 0) { | |
| 139 | 추가: Ok(()) => return Ok(()), | |
| 140 | 추가: Err(error) if matches!(error.raw_os_error(), Some(EISDIR | EPERM)) => {} | |
| 141 | 추가: Err(error) => return Err(error), | |
| 142 | 추가: } | |
| 143 | 추가: if depth >= MAX_DEPTH { | |
| 144 | 추가: return Err(io::Error::other("폴더가 너무 깊음")); | |
| 145 | 추가: } | |
| 146 | 추가: let child = match open_directory(dir, name) { | |
| 147 | 추가: Ok(child) => child, | |
| 148 | 추가: // 그 사이 폴더가 아닌 항목으로 바뀌었으면 항목 자체를 지움. | |
| 149 | 추가: Err(_) => return unlink(dir, name, 0), | |
| 150 | 추가: }; | |
| 151 | 추가: // 다른 파일 시스템이 연결된 폴더 안은 지우지 않음. | |
| 152 | 추가: if child.metadata()?.dev() != device { | |
| 153 | 추가: return Err(io::Error::other("다른 파일 시스템의 폴더")); | |
| 154 | 추가: } | |
| 155 | 추가: remove_children(&child, device, depth + 1)?; | |
| 156 | 추가: drop(child); | |
| 157 | 추가: unlink(dir, name, AT_REMOVEDIR) | |
| 158 | 추가: } | |
| 159 | 추가: } | |
| 160 | 추가: | |
| 161 | 추가: #[cfg(windows)] | |
| 162 | 추가: mod platform { | |
| 163 | 추가: use std::fs::{self, File}; | |
| 164 | 추가: use std::io; | |
| 165 | 추가: use std::path::Path; | |
| 166 | 추가: | |
| 167 | 추가: pub fn is_empty(_dir: &File, path: &Path) -> io::Result<bool> { | |
| 168 | 추가: // 호출한 쪽의 핸들이 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴. | |
| 169 | 추가: Ok(fs::read_dir(path)?.next().is_none()) | |
| 170 | 추가: } | |
| 171 | 추가: | |
| 172 | 추가: pub fn remove_contents(_dir: &File, path: &Path) -> io::Result<()> { | |
| 173 | 추가: let mut failure = None; | |
| 174 | 추가: // 호출한 쪽의 핸들이 폴더 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴. | |
| 175 | 추가: for entry in fs::read_dir(path)? { | |
| 176 | 추가: let result = entry.and_then(|entry| { | |
| 177 | 추가: let path = entry.path(); | |
| 178 | 추가: let kind = fs::symlink_metadata(&path)?.file_type(); | |
| 179 | 추가: if kind.is_symlink() { | |
| 180 | 추가: // 링크는 따라가지 않고 링크 자체만 지움. | |
| 181 | 추가: fs::remove_file(&path).or_else(|_| fs::remove_dir(&path)) | |
| 182 | 추가: } else if kind.is_dir() { | |
| 183 | 추가: fs::remove_dir_all(&path) | |
| 184 | 추가: } else { | |
| 185 | 추가: fs::remove_file(&path) | |
| 186 | 추가: } | |
| 187 | 추가: }); | |
| 188 | 추가: if let Err(error) = result { | |
| 189 | 추가: failure.get_or_insert(error); | |
| 190 | 추가: } | |
| 191 | 추가: } | |
| 192 | 추가: failure.map_or(Ok(()), Err) | |
| 193 | 추가: } | |
| 194 | 추가: } |
| @@ -503,11 +503,89 @@ fn private_dir(path: &Path) -> io::Result<()> { | ||
| 503 | 503 | builder.create(path) |
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 추가: pub struct OpenRoot { | |
| 507 | 추가: path: PathBuf, | |
| 508 | 추가: handle: fs::File, | |
| 509 | 추가: identity: worker_protocol::protocol::SourceObject, | |
| 510 | 추가: } | |
| 511 | 추가: | |
| 512 | 추가: impl OpenRoot { | |
| 513 | 추가: fn capture(path: PathBuf) -> io::Result<Self> { | |
| 514 | 추가: let handle = open_root_handle(&path)?; | |
| 515 | 추가: // 만든 직후에는 비어 있어야 함. 만든 뒤 핸들을 잡기 전에 다른 폴더로 바뀌었다면 | |
| 516 | 추가: // 사용자 데이터가 들어 있을 수 있으므로 원본으로 기록하지 않음. | |
| 517 | 추가: if !worker_protocol::tree_remove::is_empty(&handle, &path)? { | |
| 518 | 추가: return Err(io::Error::other("만든 임시 폴더가 아님")); | |
| 519 | 추가: } | |
| 520 | 추가: let identity = worker_protocol::filesystem::object(&handle) | |
| 521 | 추가: .map_err(|_| io::Error::other("임시 폴더 식별 실패"))?; | |
| 522 | 추가: Ok(Self { | |
| 523 | 추가: path, | |
| 524 | 추가: handle, | |
| 525 | 추가: identity, | |
| 526 | 추가: }) | |
| 527 | 추가: } | |
| 528 | 추가: | |
| 529 | 추가: pub fn path(&self) -> &Path { | |
| 530 | 추가: &self.path | |
| 531 | 추가: } | |
| 532 | 추가: | |
| 533 | 추가: pub fn is_current(&self) -> bool { | |
| 534 | 추가: // 원본 핸들을 유지해 삭제된 inode가 재사용되어도 같다고 판정하지 않음. | |
| 535 | 추가: self.handle.metadata().is_ok() | |
| 536 | 추가: && open_root_handle(&self.path) | |
| 537 | 추가: .ok() | |
| 538 | 추가: .and_then(|file| worker_protocol::filesystem::object(&file).ok()) | |
| 539 | 추가: .is_some_and(|identity| identity == self.identity) | |
| 540 | 추가: } | |
| 541 | 추가: } | |
| 542 | 추가: | |
| 543 | 추가: fn open_root_handle(path: &Path) -> io::Result<fs::File> { | |
| 544 | 추가: let mut options = fs::OpenOptions::new(); | |
| 545 | 추가: options.read(true); | |
| 546 | 추가: #[cfg(target_os = "linux")] | |
| 547 | 추가: { | |
| 548 | 추가: use std::os::unix::fs::OpenOptionsExt; | |
| 549 | 추가: options.custom_flags(0x20000); // 심볼릭 링크를 따라가지 않음. | |
| 550 | 추가: } | |
| 551 | 추가: #[cfg(windows)] | |
| 552 | 추가: { | |
| 553 | 추가: use std::os::windows::fs::OpenOptionsExt; | |
| 554 | 추가: // 폴더 자체를 열며 재분석 지점을 따라가지 않음. | |
| 555 | 추가: // 삭제 공유를 허용하지 않아 열어 둔 동안 폴더 이름을 바꾸거나 지울 수 없게 함. | |
| 556 | 추가: options.share_mode(1 | 2).custom_flags(0x02000000 | 0x00200000); | |
| 557 | 추가: } | |
| 558 | 추가: let file = options.open(path)?; | |
| 559 | 추가: let metadata = file.metadata()?; | |
| 560 | 추가: if !metadata.is_dir() || fs::symlink_metadata(path)?.file_type().is_symlink() { | |
| 561 | 추가: return Err(io::Error::other("임시 폴더가 아님")); | |
| 562 | 추가: } | |
| 563 | 추가: #[cfg(windows)] | |
| 564 | 추가: { | |
| 565 | 추가: use std::os::windows::fs::MetadataExt; | |
| 566 | 추가: if metadata.file_attributes() & 0x400 != 0 { | |
| 567 | 추가: return Err(io::Error::other("재분석 지점은 임시 폴더로 쓰지 않음")); | |
| 568 | 추가: } | |
| 569 | 추가: } | |
| 570 | 추가: Ok(file) | |
| 571 | 추가: } | |
| 572 | 추가: | |
| 506 | 573 | /// 압축파일 안 파일을 기본 프로그램으로 열 때 쓸 임시 폴더를 만듦. |
| 507 | 574 | /// 압축파일과 같은 폴더에 만들고, 그곳에 만들 수 없으면 시스템 임시 폴더를 씀. |
| 508 | 575 | /// 숨김 폴더로 만들지 않음. snap으로 설치한 Firefox 등은 홈 바로 아래 숨김 폴더와 |
| 509 | 576 | /// 시스템 임시 폴더의 파일을 읽지 못해 열기 요청을 조용히 무시함. |
| 510 | 삭제: pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { | |
| 577 | 추가: pub fn create_open_root(archive: &Path) -> io::Result<OpenRoot> { | |
| 578 | 추가: create_open_root_with(archive, |_| {}) | |
| 579 | 추가: } | |
| 580 | 추가: | |
| 581 | 추가: /// `create_open_root`와 같고, 폴더를 만든 직후 핸들을 잡기 전에 `after_create`를 실행함. | |
| 582 | 추가: /// 만든 뒤 경로가 바뀌는 경우를 시험하려고 둠. | |
| 583 | 추가: /// 폴더를 만든 뒤 핸들을 잡기까지의 틈에 경로가 바뀌어도, 핸들로 확인한 폴더가 비어 있지 | |
| 584 | 추가: /// 않으면 원본으로 쓰지 않고 다른 이름으로 다시 만듦. 바뀐 폴더는 건드리지 않음. | |
| 585 | 추가: pub fn create_open_root_with( | |
| 586 | 추가: archive: &Path, | |
| 587 | 추가: mut after_create: impl FnMut(&Path), | |
| 588 | 추가: ) -> io::Result<OpenRoot> { | |
| 511 | 589 | let name = archive.file_name().unwrap_or_default().to_string_lossy(); |
| 512 | 590 | let mut error = None; |
| 513 | 591 | if let Some(parent) = archive.parent().filter(|p| !p.as_os_str().is_empty()) { |
| @@ -515,7 +593,18 @@ pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { | ||
| 515 | 593 | for _ in 0..8 { |
| 516 | 594 | let path = unique_folder(&base); |
| 517 | 595 | match private_dir(&path) { |
| 518 | 삭제: Ok(()) => return Ok(path), | |
| 596 | 추가: Ok(()) => { | |
| 597 | 추가: after_create(&path); | |
| 598 | 추가: match OpenRoot::capture(path.clone()) { | |
| 599 | 추가: Ok(root) => return Ok(root), | |
| 600 | 추가: Err(e) => { | |
| 601 | 추가: // 비어 있는 폴더만 지워지므로 바뀐 폴더의 내용은 남음. | |
| 602 | 추가: let _ = fs::remove_dir(&path); | |
| 603 | 추가: error = Some(e); | |
| 604 | 추가: continue; | |
| 605 | 추가: } | |
| 606 | 추가: } | |
| 607 | 추가: } | |
| 519 | 608 | Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, |
| 520 | 609 | Err(e) => { |
| 521 | 610 | error = Some(e); |
| @@ -527,7 +616,10 @@ pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { | ||
| 527 | 616 | let base = std::env::temp_dir().join(format!("zipelo-{}-{name}", std::process::id())); |
| 528 | 617 | let path = unique_folder(&base); |
| 529 | 618 | private_dir(&path).map_err(|e| error.unwrap_or(e))?; |
| 530 | 삭제: Ok(path) | |
| 619 | 추가: after_create(&path); | |
| 620 | 추가: OpenRoot::capture(path.clone()).inspect_err(|_| { | |
| 621 | 추가: let _ = fs::remove_dir(&path); | |
| 622 | 추가: }) | |
| 531 | 623 | } |
| 532 | 624 | |
| 533 | 625 | /// 열기용 임시 폴더 이름. 예: `자료.zip (Zipelo 임시)`. |
| @@ -536,8 +628,24 @@ pub fn open_root_name(archive_name: &str) -> String { | ||
| 536 | 628 | } |
| 537 | 629 | |
| 538 | 630 | /// 이 앱이 만든 열기용 폴더를 지움. 다른 프로그램이 파일을 쓰고 있으면 남을 수 있음. |
| 539 | 삭제: pub fn remove_open_root(path: &Path) { | |
| 540 | 삭제: let _ = fs::remove_dir_all(path); | |
| 631 | 추가: pub fn remove_open_root(root: OpenRoot) { | |
| 632 | 추가: remove_open_root_with(root, || {}); | |
| 633 | 추가: } | |
| 634 | 추가: | |
| 635 | 추가: /// `remove_open_root`와 같고, 정체를 확인한 직후 삭제 전에 `after_check`를 실행함. | |
| 636 | 추가: /// 확인과 삭제 사이에 경로가 바뀌는 경우를 시험하려고 둠. | |
| 637 | 추가: /// 내용은 만들 때 열어 둔 폴더 핸들에 묶어 지우므로 경로가 바뀌어도 다른 폴더는 건드리지 않음. | |
| 638 | 추가: pub fn remove_open_root_with(root: OpenRoot, after_check: impl FnOnce()) { | |
| 639 | 추가: if !root.is_current() { | |
| 640 | 추가: return; | |
| 641 | 추가: } | |
| 642 | 추가: after_check(); | |
| 643 | 추가: let OpenRoot { path, handle, .. } = root; | |
| 644 | 추가: let _ = worker_protocol::tree_remove::remove_contents(&handle, &path); | |
| 645 | 추가: // Windows는 핸들을 닫아야 폴더를 지울 수 있음. | |
| 646 | 추가: drop(handle); | |
| 647 | 추가: // 비어 있지 않거나 링크로 바뀐 경로는 지워지지 않으므로 다른 내용이 사라지지 않음. | |
| 648 | 추가: let _ = fs::remove_dir(&path); | |
| 541 | 649 | } |
| 542 | 650 | |
| 543 | 651 | /// 기본 프로그램으로 열면 실행될 수 있는 파일은 바로 열지 않음. |
| @@ -284,7 +284,7 @@ pub struct ArchiveApp { | ||
| 284 | 284 | /// 마지막 풀기가 기본 프로그램으로 열기였으면 그 항목. 다시 시도할 때 다시 엶. |
| 285 | 285 | last_open: Option<u32>, |
| 286 | 286 | /// 압축파일별 열기용 임시 폴더. 앱을 닫을 때 지움. |
| 287 | 삭제: open_roots: Vec<(PathBuf, PathBuf)>, | |
| 287 | 추가: open_roots: Vec<(PathBuf, present::OpenRoot)>, | |
| 288 | 288 | open_count: u64, |
| 289 | 289 | opener: Opener, |
| 290 | 290 | #[cfg(all(feature = "ui-fixture", debug_assertions))] |
| @@ -410,12 +410,13 @@ impl ArchiveApp { | ||
| 410 | 410 | return false; |
| 411 | 411 | }; |
| 412 | 412 | let root = match self.open_roots.iter().find(|(a, _)| *a == archive) { |
| 413 | 삭제: Some((_, root)) if root.is_dir() => root.clone(), | |
| 413 | 추가: Some((_, root)) if root.is_current() => root.path().to_path_buf(), | |
| 414 | 414 | _ => match present::create_open_root(&archive) { |
| 415 | 415 | Ok(root) => { |
| 416 | 416 | self.open_roots.retain(|(a, _)| *a != archive); |
| 417 | 삭제: self.open_roots.push((archive, root.clone())); | |
| 418 | 삭제: root | |
| 417 | 추가: let path = root.path().to_path_buf(); | |
| 418 | 추가: self.open_roots.push((archive, root)); | |
| 419 | 추가: path | |
| 419 | 420 | } |
| 420 | 421 | Err(_) => { |
| 421 | 422 | self.dismissed = false; |
| @@ -3658,7 +3659,7 @@ impl Drop for ArchiveApp { | ||
| 3658 | 3659 | fn drop(&mut self) { |
| 3659 | 3660 | self.clear_password(); |
| 3660 | 3661 | for (_, root) in self.open_roots.drain(..) { |
| 3661 | 삭제: present::remove_open_root(&root); | |
| 3662 | 추가: present::remove_open_root(root); | |
| 3662 | 3663 | } |
| 3663 | 3664 | } |
| 3664 | 3665 | } |
| @@ -1156,3 +1156,190 @@ fn client_protocol_errors_hide_internal_terms_but_preserve_exit_uncertainty() { | ||
| 1156 | 1156 | UNCONFIRMED_FINISH |
| 1157 | 1157 | ); |
| 1158 | 1158 | } |
| 1159 | 추가: | |
| 1160 | 추가: #[cfg(feature = "ui")] | |
| 1161 | 추가: #[cfg(unix)] | |
| 1162 | 추가: #[test] | |
| 1163 | 추가: fn app_exit_preserves_replaced_open_root() { | |
| 1164 | 추가: use zipelo_ui_memory_app::archive_screen::ArchiveApp; | |
| 1165 | 추가: let scratch = Folder::new(); | |
| 1166 | 추가: let mut app = ArchiveApp::new( | |
| 1167 | 추가: Box::new(FakeFactory::default()), | |
| 1168 | 추가: Some(scratch.0.join("sample.zip")), | |
| 1169 | 추가: false, | |
| 1170 | 추가: ); | |
| 1171 | 추가: pump(&mut app.model); | |
| 1172 | 추가: assert!(app.open_entry(0)); | |
| 1173 | 추가: let root = scratch.0.join(open_root_name("sample.zip")); | |
| 1174 | 추가: fs::rename(&root, scratch.0.join("original")).unwrap(); | |
| 1175 | 추가: fs::create_dir(&root).unwrap(); | |
| 1176 | 추가: let retained = root.join("keep.txt"); | |
| 1177 | 추가: fs::write(&retained, b"user data").unwrap(); | |
| 1178 | 추가: drop(app); | |
| 1179 | 추가: assert_eq!(fs::read(retained).unwrap(), b"user data"); | |
| 1180 | 추가: } | |
| 1181 | 추가: | |
| 1182 | 추가: #[cfg(feature = "ui")] | |
| 1183 | 추가: #[test] | |
| 1184 | 추가: fn app_exit_removes_original_open_root() { | |
| 1185 | 추가: use zipelo_ui_memory_app::archive_screen::ArchiveApp; | |
| 1186 | 추가: let scratch = Folder::new(); | |
| 1187 | 추가: let mut app = ArchiveApp::new( | |
| 1188 | 추가: Box::new(FakeFactory::default()), | |
| 1189 | 추가: Some(scratch.0.join("sample.zip")), | |
| 1190 | 추가: false, | |
| 1191 | 추가: ); | |
| 1192 | 추가: pump(&mut app.model); | |
| 1193 | 추가: assert!(app.open_entry(0)); | |
| 1194 | 추가: let root = scratch.0.join(open_root_name("sample.zip")); | |
| 1195 | 추가: fs::write(root.join("opened.txt"), b"temporary").unwrap(); | |
| 1196 | 추가: drop(app); | |
| 1197 | 추가: assert!(!root.exists()); | |
| 1198 | 추가: } | |
| 1199 | 추가: | |
| 1200 | 추가: #[cfg(feature = "ui")] | |
| 1201 | 추가: #[cfg(windows)] | |
| 1202 | 추가: #[test] | |
| 1203 | 추가: fn open_root_cannot_be_renamed_while_app_holds_it() { | |
| 1204 | 추가: use zipelo_ui_memory_app::archive_screen::ArchiveApp; | |
| 1205 | 추가: let scratch = Folder::new(); | |
| 1206 | 추가: let mut app = ArchiveApp::new( | |
| 1207 | 추가: Box::new(FakeFactory::default()), | |
| 1208 | 추가: Some(scratch.0.join("sample.zip")), | |
| 1209 | 추가: false, | |
| 1210 | 추가: ); | |
| 1211 | 추가: pump(&mut app.model); | |
| 1212 | 추가: assert!(app.open_entry(0)); | |
| 1213 | 추가: let root = scratch.0.join(open_root_name("sample.zip")); | |
| 1214 | 추가: // 열어 둔 핸들이 이름 변경을 막아 경로가 다른 폴더로 바뀌지 않음. | |
| 1215 | 추가: assert!(fs::rename(&root, scratch.0.join("original")).is_err()); | |
| 1216 | 추가: drop(app); | |
| 1217 | 추가: assert!(!root.exists()); | |
| 1218 | 추가: } | |
| 1219 | 추가: | |
| 1220 | 추가: #[cfg(unix)] | |
| 1221 | 추가: fn open_root_with_contents(scratch: &Folder) -> (OpenRoot, PathBuf) { | |
| 1222 | 추가: let root = create_open_root(&scratch.0.join("sample.zip")).unwrap(); | |
| 1223 | 추가: let path = root.path().to_path_buf(); | |
| 1224 | 추가: fs::create_dir_all(path.join("a/b")).unwrap(); | |
| 1225 | 추가: fs::write(path.join("a/b/deep.txt"), b"temporary").unwrap(); | |
| 1226 | 추가: fs::write(path.join("top.txt"), b"temporary").unwrap(); | |
| 1227 | 추가: (root, path) | |
| 1228 | 추가: } | |
| 1229 | 추가: | |
| 1230 | 추가: #[cfg(unix)] | |
| 1231 | 추가: #[test] | |
| 1232 | 추가: fn removal_is_bound_to_checked_folder_when_replaced_after_check() { | |
| 1233 | 추가: let scratch = Folder::new(); | |
| 1234 | 추가: let (root, path) = open_root_with_contents(&scratch); | |
| 1235 | 추가: let original = scratch.0.join("original"); | |
| 1236 | 추가: remove_open_root_with(root, || { | |
| 1237 | 추가: fs::rename(&path, &original).unwrap(); | |
| 1238 | 추가: fs::create_dir(&path).unwrap(); | |
| 1239 | 추가: fs::write(path.join("keep.txt"), b"user data").unwrap(); | |
| 1240 | 추가: }); | |
| 1241 | 추가: assert_eq!(fs::read(path.join("keep.txt")).unwrap(), b"user data"); | |
| 1242 | 추가: // 확인한 폴더의 내용만 지움. | |
| 1243 | 추가: assert_eq!(fs::read_dir(&original).unwrap().count(), 0); | |
| 1244 | 추가: } | |
| 1245 | 추가: | |
| 1246 | 추가: #[cfg(unix)] | |
| 1247 | 추가: #[test] | |
| 1248 | 추가: fn removal_does_not_follow_link_swapped_in_after_check() { | |
| 1249 | 추가: let scratch = Folder::new(); | |
| 1250 | 추가: let (root, path) = open_root_with_contents(&scratch); | |
| 1251 | 추가: let victim = scratch.0.join("victim"); | |
| 1252 | 추가: fs::create_dir(&victim).unwrap(); | |
| 1253 | 추가: fs::write(victim.join("keep.txt"), b"user data").unwrap(); | |
| 1254 | 추가: let original = scratch.0.join("original"); | |
| 1255 | 추가: remove_open_root_with(root, || { | |
| 1256 | 추가: fs::rename(&path, &original).unwrap(); | |
| 1257 | 추가: std::os::unix::fs::symlink(&victim, &path).unwrap(); | |
| 1258 | 추가: }); | |
| 1259 | 추가: assert_eq!(fs::read(victim.join("keep.txt")).unwrap(), b"user data"); | |
| 1260 | 추가: assert!(fs::symlink_metadata(&path).unwrap().file_type().is_symlink()); | |
| 1261 | 추가: assert_eq!(fs::read_dir(&original).unwrap().count(), 0); | |
| 1262 | 추가: } | |
| 1263 | 추가: | |
| 1264 | 추가: #[cfg(unix)] | |
| 1265 | 추가: #[test] | |
| 1266 | 추가: fn removal_does_not_follow_links_inside_folder() { | |
| 1267 | 추가: let scratch = Folder::new(); | |
| 1268 | 추가: let (root, path) = open_root_with_contents(&scratch); | |
| 1269 | 추가: let victim = scratch.0.join("victim"); | |
| 1270 | 추가: fs::create_dir(&victim).unwrap(); | |
| 1271 | 추가: fs::write(victim.join("keep.txt"), b"user data").unwrap(); | |
| 1272 | 추가: std::os::unix::fs::symlink(&victim, path.join("a/link")).unwrap(); | |
| 1273 | 추가: std::os::unix::fs::symlink(victim.join("keep.txt"), path.join("file-link")).unwrap(); | |
| 1274 | 추가: remove_open_root(root); | |
| 1275 | 추가: assert!(!path.exists()); | |
| 1276 | 추가: assert_eq!(fs::read(victim.join("keep.txt")).unwrap(), b"user data"); | |
| 1277 | 추가: } | |
| 1278 | 추가: | |
| 1279 | 추가: #[cfg(unix)] | |
| 1280 | 추가: #[test] | |
| 1281 | 추가: fn removal_of_original_folder_clears_nested_contents() { | |
| 1282 | 추가: let scratch = Folder::new(); | |
| 1283 | 추가: let (root, path) = open_root_with_contents(&scratch); | |
| 1284 | 추가: remove_open_root(root); | |
| 1285 | 추가: assert!(!path.exists()); | |
| 1286 | 추가: } | |
| 1287 | 추가: | |
| 1288 | 추가: #[cfg(unix)] | |
| 1289 | 추가: #[test] | |
| 1290 | 추가: fn creation_does_not_adopt_folder_replaced_before_handle_is_taken() { | |
| 1291 | 추가: let scratch = Folder::new(); | |
| 1292 | 추가: let archive = scratch.0.join("sample.zip"); | |
| 1293 | 추가: let swapped = scratch.0.join(open_root_name("sample.zip")); | |
| 1294 | 추가: let original = scratch.0.join("original"); | |
| 1295 | 추가: let mut swaps = 0; | |
| 1296 | 추가: let root = create_open_root_with(&archive, |path| { | |
| 1297 | 추가: swaps += 1; | |
| 1298 | 추가: if swaps == 1 { | |
| 1299 | 추가: fs::rename(path, &original).unwrap(); | |
| 1300 | 추가: fs::create_dir(path).unwrap(); | |
| 1301 | 추가: fs::write(path.join("keep.txt"), b"user data").unwrap(); | |
| 1302 | 추가: } | |
| 1303 | 추가: }) | |
| 1304 | 추가: .unwrap(); | |
| 1305 | 추가: // 바뀐 폴더는 원본으로 기록하지 않고 다른 이름의 새 폴더를 씀. | |
| 1306 | 추가: assert_ne!(root.path(), swapped); | |
| 1307 | 추가: assert!(root.is_current()); | |
| 1308 | 추가: remove_open_root(root); | |
| 1309 | 추가: assert_eq!(fs::read(swapped.join("keep.txt")).unwrap(), b"user data"); | |
| 1310 | 추가: } | |
| 1311 | 추가: | |
| 1312 | 추가: #[cfg(unix)] | |
| 1313 | 추가: #[test] | |
| 1314 | 추가: fn creation_does_not_adopt_link_replaced_before_handle_is_taken() { | |
| 1315 | 추가: let scratch = Folder::new(); | |
| 1316 | 추가: let archive = scratch.0.join("sample.zip"); | |
| 1317 | 추가: let link = scratch.0.join(open_root_name("sample.zip")); | |
| 1318 | 추가: let victim = scratch.0.join("victim"); | |
| 1319 | 추가: fs::create_dir(&victim).unwrap(); | |
| 1320 | 추가: fs::write(victim.join("keep.txt"), b"user data").unwrap(); | |
| 1321 | 추가: let mut swaps = 0; | |
| 1322 | 추가: let root = create_open_root_with(&archive, |path| { | |
| 1323 | 추가: swaps += 1; | |
| 1324 | 추가: if swaps == 1 { | |
| 1325 | 추가: fs::remove_dir(path).unwrap(); | |
| 1326 | 추가: std::os::unix::fs::symlink(&victim, path).unwrap(); | |
| 1327 | 추가: } | |
| 1328 | 추가: }) | |
| 1329 | 추가: .unwrap(); | |
| 1330 | 추가: assert_ne!(root.path(), link); | |
| 1331 | 추가: remove_open_root(root); | |
| 1332 | 추가: assert_eq!(fs::read(victim.join("keep.txt")).unwrap(), b"user data"); | |
| 1333 | 추가: assert!(fs::symlink_metadata(&link).unwrap().file_type().is_symlink()); | |
| 1334 | 추가: } | |
| 1335 | 추가: | |
| 1336 | 추가: #[test] | |
| 1337 | 추가: fn creation_of_unchanged_folder_is_empty_and_current() { | |
| 1338 | 추가: let scratch = Folder::new(); | |
| 1339 | 추가: let root = create_open_root(&scratch.0.join("sample.zip")).unwrap(); | |
| 1340 | 추가: let path = root.path().to_path_buf(); | |
| 1341 | 추가: assert!(root.is_current()); | |
| 1342 | 추가: assert_eq!(fs::read_dir(&path).unwrap().count(), 0); | |
| 1343 | 추가: remove_open_root(root); | |
| 1344 | 추가: assert!(!path.exists()); | |
| 1345 | 추가: } |