임시 폴더 생성 직후 교체된 폴더를 원본으로 기록하지 않음
- 작성자
- 호시노 히나타
- 작성 시각
- 커밋
e6b4d62bb87e4b88ed14516c0e62c475a05bc1e3- 부모
- 0739474
변경된 파일 3개
+105-2| @@ -13,6 +13,11 @@ pub fn remove_contents(dir: &File, path: &Path) -> io::Result<()> { | ||
| 13 | 13 | platform::remove_contents(dir, path) |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 추가: /// `dir` 핸들이 가리키는 폴더가 비어 있는지 확인함. 새로 만든 폴더를 핸들로 잡은 직후 쓰임. | |
| 17 | 추가: pub fn is_empty(dir: &File, path: &Path) -> io::Result<bool> { | |
| 18 | 추가: platform::is_empty(dir, path) | |
| 19 | 추가: } | |
| 20 | 추가: | |
| 16 | 21 | #[cfg(target_os = "linux")] |
| 17 | 22 | mod platform { |
| 18 | 23 | use std::ffi::{CStr, CString}; |
| @@ -59,6 +64,10 @@ mod platform { | ||
| 59 | 64 | remove_children(dir, device, 0) |
| 60 | 65 | } |
| 61 | 66 | |
| 67 | 추가: pub fn is_empty(dir: &File, _path: &Path) -> io::Result<bool> { | |
| 68 | 추가: Ok(names(dir)?.is_empty()) | |
| 69 | 추가: } | |
| 70 | 추가: | |
| 62 | 71 | fn names(dir: &File) -> io::Result<Vec<CString>> { |
| 63 | 72 | let copy = dir.try_clone()?; |
| 64 | 73 | let fd = std::os::unix::io::IntoRawFd::into_raw_fd(copy); |
| @@ -155,6 +164,11 @@ mod platform { | ||
| 155 | 164 | use std::io; |
| 156 | 165 | use std::path::Path; |
| 157 | 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 | 추가: | |
| 158 | 172 | pub fn remove_contents(_dir: &File, path: &Path) -> io::Result<()> { |
| 159 | 173 | let mut failure = None; |
| 160 | 174 | // 호출한 쪽의 핸들이 폴더 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴. |
| @@ -512,6 +512,11 @@ pub struct OpenRoot { | ||
| 512 | 512 | impl OpenRoot { |
| 513 | 513 | fn capture(path: PathBuf) -> io::Result<Self> { |
| 514 | 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 | 추가: } | |
| 515 | 520 | let identity = worker_protocol::filesystem::object(&handle) |
| 516 | 521 | .map_err(|_| io::Error::other("임시 폴더 식별 실패"))?; |
| 517 | 522 | Ok(Self { |
| @@ -570,6 +575,17 @@ fn open_root_handle(path: &Path) -> io::Result<fs::File> { | ||
| 570 | 575 | /// 숨김 폴더로 만들지 않음. snap으로 설치한 Firefox 등은 홈 바로 아래 숨김 폴더와 |
| 571 | 576 | /// 시스템 임시 폴더의 파일을 읽지 못해 열기 요청을 조용히 무시함. |
| 572 | 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> { | |
| 573 | 589 | let name = archive.file_name().unwrap_or_default().to_string_lossy(); |
| 574 | 590 | let mut error = None; |
| 575 | 591 | if let Some(parent) = archive.parent().filter(|p| !p.as_os_str().is_empty()) { |
| @@ -577,7 +593,18 @@ pub fn create_open_root(archive: &Path) -> io::Result<OpenRoot> { | ||
| 577 | 593 | for _ in 0..8 { |
| 578 | 594 | let path = unique_folder(&base); |
| 579 | 595 | match private_dir(&path) { |
| 580 | 삭제: Ok(()) => return OpenRoot::capture(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 | 추가: } | |
| 581 | 608 | Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, |
| 582 | 609 | Err(e) => { |
| 583 | 610 | error = Some(e); |
| @@ -589,7 +616,10 @@ pub fn create_open_root(archive: &Path) -> io::Result<OpenRoot> { | ||
| 589 | 616 | let base = std::env::temp_dir().join(format!("zipelo-{}-{name}", std::process::id())); |
| 590 | 617 | let path = unique_folder(&base); |
| 591 | 618 | private_dir(&path).map_err(|e| error.unwrap_or(e))?; |
| 592 | 삭제: OpenRoot::capture(path) | |
| 619 | 추가: after_create(&path); | |
| 620 | 추가: OpenRoot::capture(path.clone()).inspect_err(|_| { | |
| 621 | 추가: let _ = fs::remove_dir(&path); | |
| 622 | 추가: }) | |
| 593 | 623 | } |
| 594 | 624 | |
| 595 | 625 | /// 열기용 임시 폴더 이름. 예: `자료.zip (Zipelo 임시)`. |
| @@ -1284,3 +1284,62 @@ fn removal_of_original_folder_clears_nested_contents() { | ||
| 1284 | 1284 | remove_open_root(root); |
| 1285 | 1285 | assert!(!path.exists()); |
| 1286 | 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 | 추가: } |