임시 폴더 내용을 열어 둔 핸들에 묶어 지움
- 작성자
- 호시노 히나타
- 작성 시각
- 커밋
554fee1fce15748035d7292de6d12963b88a76c7- 부모
- b332764
변경된 파일 5개
+291-6| @@ -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,180 @@ | ||
| 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 | 추가: #[cfg(target_os = "linux")] | |
| 17 | 추가: mod platform { | |
| 18 | 추가: use std::ffi::{CStr, CString}; | |
| 19 | 추가: use std::fs::File; | |
| 20 | 추가: use std::io; | |
| 21 | 추가: use std::os::unix::{ | |
| 22 | 추가: fs::MetadataExt, | |
| 23 | 추가: io::{AsRawFd, FromRawFd}, | |
| 24 | 추가: }; | |
| 25 | 추가: use std::path::Path; | |
| 26 | 추가: | |
| 27 | 추가: #[cfg(any(target_arch = "aarch64", target_arch = "arm"))] | |
| 28 | 추가: const NOFOLLOW_DIRECTORY: i32 = 0x8000 | 0x4000; | |
| 29 | 추가: #[cfg(not(any(target_arch = "aarch64", target_arch = "arm")))] | |
| 30 | 추가: const NOFOLLOW_DIRECTORY: i32 = 0x20000 | 0x10000; | |
| 31 | 추가: const CLOEXEC: i32 = 0x80000; | |
| 32 | 추가: const AT_REMOVEDIR: i32 = 0x200; | |
| 33 | 추가: const ENOENT: i32 = 2; | |
| 34 | 추가: const EPERM: i32 = 1; | |
| 35 | 추가: const EISDIR: i32 = 21; | |
| 36 | 추가: /// 깊게 중첩된 폴더에서 파일 디스크립터가 바닥나지 않게 하는 한도. | |
| 37 | 추가: const MAX_DEPTH: usize = 256; | |
| 38 | 추가: | |
| 39 | 추가: #[repr(C)] | |
| 40 | 추가: struct Dirent64 { | |
| 41 | 추가: inode: u64, | |
| 42 | 추가: offset: i64, | |
| 43 | 추가: length: u16, | |
| 44 | 추가: kind: u8, | |
| 45 | 추가: name: [i8; 256], | |
| 46 | 추가: } | |
| 47 | 추가: | |
| 48 | 추가: unsafe extern "C" { | |
| 49 | 추가: fn openat(fd: i32, path: *const i8, flags: i32, ...) -> i32; | |
| 50 | 추가: fn unlinkat(fd: i32, path: *const i8, flags: i32) -> i32; | |
| 51 | 추가: fn fdopendir(fd: i32) -> *mut std::ffi::c_void; | |
| 52 | 추가: fn readdir64(stream: *mut std::ffi::c_void) -> *mut Dirent64; | |
| 53 | 추가: fn rewinddir(stream: *mut std::ffi::c_void); | |
| 54 | 추가: fn closedir(stream: *mut std::ffi::c_void) -> i32; | |
| 55 | 추가: } | |
| 56 | 추가: | |
| 57 | 추가: pub fn remove_contents(dir: &File, _path: &Path) -> io::Result<()> { | |
| 58 | 추가: let device = dir.metadata()?.dev(); | |
| 59 | 추가: remove_children(dir, device, 0) | |
| 60 | 추가: } | |
| 61 | 추가: | |
| 62 | 추가: fn names(dir: &File) -> io::Result<Vec<CString>> { | |
| 63 | 추가: let copy = dir.try_clone()?; | |
| 64 | 추가: let fd = std::os::unix::io::IntoRawFd::into_raw_fd(copy); | |
| 65 | 추가: // 복제한 디스크립터의 소유권은 폴더 스트림이 가져가며 closedir이 닫음. | |
| 66 | 추가: let stream = unsafe { fdopendir(fd) }; | |
| 67 | 추가: if stream.is_null() { | |
| 68 | 추가: let error = io::Error::last_os_error(); | |
| 69 | 추가: // 스트림을 만들지 못했으므로 복제한 디스크립터를 직접 돌려받아 닫음. | |
| 70 | 추가: drop(unsafe { File::from_raw_fd(fd) }); | |
| 71 | 추가: return Err(error); | |
| 72 | 추가: } | |
| 73 | 추가: // 복제한 디스크립터는 원본과 읽기 위치를 공유하므로 처음부터 읽음. | |
| 74 | 추가: unsafe { rewinddir(stream) }; | |
| 75 | 추가: let mut found = Vec::new(); | |
| 76 | 추가: loop { | |
| 77 | 추가: let entry = unsafe { readdir64(stream) }; | |
| 78 | 추가: if entry.is_null() { | |
| 79 | 추가: break; | |
| 80 | 추가: } | |
| 81 | 추가: // 반환된 항목은 다음 readdir64 호출 전까지 유효하며 바로 복사함. | |
| 82 | 추가: let name = unsafe { CStr::from_ptr((*entry).name.as_ptr()) }; | |
| 83 | 추가: let bytes = name.to_bytes(); | |
| 84 | 추가: if bytes != b"." && bytes != b".." { | |
| 85 | 추가: found.push(name.to_owned()); | |
| 86 | 추가: } | |
| 87 | 추가: } | |
| 88 | 추가: unsafe { closedir(stream) }; | |
| 89 | 추가: Ok(found) | |
| 90 | 추가: } | |
| 91 | 추가: | |
| 92 | 추가: fn unlink(dir: &File, name: &CString, flags: i32) -> io::Result<()> { | |
| 93 | 추가: // 부모 핸들 기준으로 이름 하나만 지움. | |
| 94 | 추가: if unsafe { unlinkat(dir.as_raw_fd(), name.as_ptr(), flags) } == 0 { | |
| 95 | 추가: Ok(()) | |
| 96 | 추가: } else { | |
| 97 | 추가: Err(io::Error::last_os_error()) | |
| 98 | 추가: } | |
| 99 | 추가: } | |
| 100 | 추가: | |
| 101 | 추가: fn open_directory(dir: &File, name: &CString) -> io::Result<File> { | |
| 102 | 추가: // 심볼릭 링크를 따르지 않고 폴더만 부모 핸들 기준으로 엶. | |
| 103 | 추가: let fd = unsafe { | |
| 104 | 추가: openat( | |
| 105 | 추가: dir.as_raw_fd(), | |
| 106 | 추가: name.as_ptr(), | |
| 107 | 추가: NOFOLLOW_DIRECTORY | CLOEXEC, | |
| 108 | 추가: ) | |
| 109 | 추가: }; | |
| 110 | 추가: if fd < 0 { | |
| 111 | 추가: return Err(io::Error::last_os_error()); | |
| 112 | 추가: } | |
| 113 | 추가: Ok(unsafe { File::from_raw_fd(fd) }) | |
| 114 | 추가: } | |
| 115 | 추가: | |
| 116 | 추가: fn remove_children(dir: &File, device: u64, depth: usize) -> io::Result<()> { | |
| 117 | 추가: let mut failure = None; | |
| 118 | 추가: for name in names(dir)? { | |
| 119 | 추가: if let Err(error) = remove_entry(dir, &name, device, depth) { | |
| 120 | 추가: if error.raw_os_error() != Some(ENOENT) { | |
| 121 | 추가: failure.get_or_insert(error); | |
| 122 | 추가: } | |
| 123 | 추가: } | |
| 124 | 추가: } | |
| 125 | 추가: failure.map_or(Ok(()), Err) | |
| 126 | 추가: } | |
| 127 | 추가: | |
| 128 | 추가: fn remove_entry(dir: &File, name: &CString, device: u64, depth: usize) -> io::Result<()> { | |
| 129 | 추가: match unlink(dir, name, 0) { | |
| 130 | 추가: Ok(()) => return Ok(()), | |
| 131 | 추가: Err(error) if matches!(error.raw_os_error(), Some(EISDIR | EPERM)) => {} | |
| 132 | 추가: Err(error) => return Err(error), | |
| 133 | 추가: } | |
| 134 | 추가: if depth >= MAX_DEPTH { | |
| 135 | 추가: return Err(io::Error::other("폴더가 너무 깊음")); | |
| 136 | 추가: } | |
| 137 | 추가: let child = match open_directory(dir, name) { | |
| 138 | 추가: Ok(child) => child, | |
| 139 | 추가: // 그 사이 폴더가 아닌 항목으로 바뀌었으면 항목 자체를 지움. | |
| 140 | 추가: Err(_) => return unlink(dir, name, 0), | |
| 141 | 추가: }; | |
| 142 | 추가: // 다른 파일 시스템이 연결된 폴더 안은 지우지 않음. | |
| 143 | 추가: if child.metadata()?.dev() != device { | |
| 144 | 추가: return Err(io::Error::other("다른 파일 시스템의 폴더")); | |
| 145 | 추가: } | |
| 146 | 추가: remove_children(&child, device, depth + 1)?; | |
| 147 | 추가: drop(child); | |
| 148 | 추가: unlink(dir, name, AT_REMOVEDIR) | |
| 149 | 추가: } | |
| 150 | 추가: } | |
| 151 | 추가: | |
| 152 | 추가: #[cfg(windows)] | |
| 153 | 추가: mod platform { | |
| 154 | 추가: use std::fs::{self, File}; | |
| 155 | 추가: use std::io; | |
| 156 | 추가: use std::path::Path; | |
| 157 | 추가: | |
| 158 | 추가: pub fn remove_contents(_dir: &File, path: &Path) -> io::Result<()> { | |
| 159 | 추가: let mut failure = None; | |
| 160 | 추가: // 호출한 쪽의 핸들이 폴더 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴. | |
| 161 | 추가: for entry in fs::read_dir(path)? { | |
| 162 | 추가: let result = entry.and_then(|entry| { | |
| 163 | 추가: let path = entry.path(); | |
| 164 | 추가: let kind = fs::symlink_metadata(&path)?.file_type(); | |
| 165 | 추가: if kind.is_symlink() { | |
| 166 | 추가: // 링크는 따라가지 않고 링크 자체만 지움. | |
| 167 | 추가: fs::remove_file(&path).or_else(|_| fs::remove_dir(&path)) | |
| 168 | 추가: } else if kind.is_dir() { | |
| 169 | 추가: fs::remove_dir_all(&path) | |
| 170 | 추가: } else { | |
| 171 | 추가: fs::remove_file(&path) | |
| 172 | 추가: } | |
| 173 | 추가: }); | |
| 174 | 추가: if let Err(error) = result { | |
| 175 | 추가: failure.get_or_insert(error); | |
| 176 | 추가: } | |
| 177 | 추가: } | |
| 178 | 추가: failure.map_or(Ok(()), Err) | |
| 179 | 추가: } | |
| 180 | 추가: } |
| @@ -547,7 +547,8 @@ fn open_root_handle(path: &Path) -> io::Result<fs::File> { | ||
| 547 | 547 | { |
| 548 | 548 | use std::os::windows::fs::OpenOptionsExt; |
| 549 | 549 | // 폴더 자체를 열며 재분석 지점을 따라가지 않음. |
| 550 | 삭제: options.custom_flags(0x02000000 | 0x00200000); | |
| 550 | 추가: // 삭제 공유를 허용하지 않아 열어 둔 동안 폴더 이름을 바꾸거나 지울 수 없게 함. | |
| 551 | 추가: options.share_mode(1 | 2).custom_flags(0x02000000 | 0x00200000); | |
| 551 | 552 | } |
| 552 | 553 | let file = options.open(path)?; |
| 553 | 554 | let metadata = file.metadata()?; |
| @@ -597,10 +598,24 @@ pub fn open_root_name(archive_name: &str) -> String { | ||
| 597 | 598 | } |
| 598 | 599 | |
| 599 | 600 | /// 이 앱이 만든 열기용 폴더를 지움. 다른 프로그램이 파일을 쓰고 있으면 남을 수 있음. |
| 600 | 삭제: pub fn remove_open_root(root: &OpenRoot) { | |
| 601 | 삭제: if root.is_current() { | |
| 602 | 삭제: let _ = fs::remove_dir_all(root.path()); | |
| 603 | 삭제: } | |
| 601 | 추가: pub fn remove_open_root(root: OpenRoot) { | |
| 602 | 추가: remove_open_root_with(root, || {}); | |
| 603 | 추가: } | |
| 604 | 추가: | |
| 605 | 추가: /// `remove_open_root`와 같고, 정체를 확인한 직후 삭제 전에 `after_check`를 실행함. | |
| 606 | 추가: /// 확인과 삭제 사이에 경로가 바뀌는 경우를 시험하려고 둠. | |
| 607 | 추가: /// 내용은 만들 때 열어 둔 폴더 핸들에 묶어 지우므로 경로가 바뀌어도 다른 폴더는 건드리지 않음. | |
| 608 | 추가: pub fn remove_open_root_with(root: OpenRoot, after_check: impl FnOnce()) { | |
| 609 | 추가: if !root.is_current() { | |
| 610 | 추가: return; | |
| 611 | 추가: } | |
| 612 | 추가: after_check(); | |
| 613 | 추가: let OpenRoot { path, handle, .. } = root; | |
| 614 | 추가: let _ = worker_protocol::tree_remove::remove_contents(&handle, &path); | |
| 615 | 추가: // Windows는 핸들을 닫아야 폴더를 지울 수 있음. | |
| 616 | 추가: drop(handle); | |
| 617 | 추가: // 비어 있지 않거나 링크로 바뀐 경로는 지워지지 않으므로 다른 내용이 사라지지 않음. | |
| 618 | 추가: let _ = fs::remove_dir(&path); | |
| 604 | 619 | } |
| 605 | 620 | |
| 606 | 621 | /// 기본 프로그램으로 열면 실행될 수 있는 파일은 바로 열지 않음. |
| @@ -3659,7 +3659,7 @@ impl Drop for ArchiveApp { | ||
| 3659 | 3659 | fn drop(&mut self) { |
| 3660 | 3660 | self.clear_password(); |
| 3661 | 3661 | for (_, root) in self.open_roots.drain(..) { |
| 3662 | 삭제: present::remove_open_root(&root); | |
| 3662 | 추가: present::remove_open_root(root); | |
| 3663 | 3663 | } |
| 3664 | 3664 | } |
| 3665 | 3665 | } |
| @@ -1158,6 +1158,7 @@ fn client_protocol_errors_hide_internal_terms_but_preserve_exit_uncertainty() { | ||
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | #[cfg(feature = "ui")] |
| 1161 | 추가: #[cfg(unix)] | |
| 1161 | 1162 | #[test] |
| 1162 | 1163 | fn app_exit_preserves_replaced_open_root() { |
| 1163 | 1164 | use zipelo_ui_memory_app::archive_screen::ArchiveApp; |
| @@ -1195,3 +1196,91 @@ fn app_exit_removes_original_open_root() { | ||
| 1195 | 1196 | drop(app); |
| 1196 | 1197 | assert!(!root.exists()); |
| 1197 | 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 | 추가: } |