team-ljyzipelo

공개

임시 폴더 생성 직후 교체된 폴더를 원본으로 기록하지 않음

작성자
호시노 히나타
작성 시각
커밋
e6b4d62bb87e4b88ed14516c0e62c475a05bc1e3
부모
0739474
이 시점의 파일 보기

변경된 파일 3개

+105-2
수정experiments/rust-archive-worker/src/tree_remove.rs+14-0
@@ -13,6 +13,11 @@ pub fn remove_contents(dir: &File, path: &Path) -> io::Result<()> {
1313 platform::remove_contents(dir, path)
1414}
1515
16추가: /// `dir` 핸들이 가리키는 폴더가 비어 있는지 확인함. 새로 만든 폴더를 핸들로 잡은 직후 쓰임.
17추가: pub fn is_empty(dir: &File, path: &Path) -> io::Result<bool> {
18추가: platform::is_empty(dir, path)
19추가: }
20추가:
1621#[cfg(target_os = "linux")]
1722mod platform {
1823 use std::ffi::{CStr, CString};
@@ -59,6 +64,10 @@ mod platform {
5964 remove_children(dir, device, 0)
6065 }
6166
67추가: pub fn is_empty(dir: &File, _path: &Path) -> io::Result<bool> {
68추가: Ok(names(dir)?.is_empty())
69추가: }
70추가:
6271 fn names(dir: &File) -> io::Result<Vec<CString>> {
6372 let copy = dir.try_clone()?;
6473 let fd = std::os::unix::io::IntoRawFd::into_raw_fd(copy);
@@ -155,6 +164,11 @@ mod platform {
155164 use std::io;
156165 use std::path::Path;
157166
167추가: pub fn is_empty(_dir: &File, path: &Path) -> io::Result<bool> {
168추가: // 호출한 쪽의 핸들이 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴.
169추가: Ok(fs::read_dir(path)?.next().is_none())
170추가: }
171추가:
158172 pub fn remove_contents(_dir: &File, path: &Path) -> io::Result<()> {
159173 let mut failure = None;
160174 // 호출한 쪽의 핸들이 폴더 이름 변경을 막고 있으므로 경로가 같은 폴더를 가리킴.
수정experiments/rust-ui-memory-app/src/archive_present.rs+32-2
@@ -512,6 +512,11 @@ pub struct OpenRoot {
512512impl OpenRoot {
513513 fn capture(path: PathBuf) -> io::Result<Self> {
514514 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추가: }
515520 let identity = worker_protocol::filesystem::object(&handle)
516521 .map_err(|_| io::Error::other("임시 폴더 식별 실패"))?;
517522 Ok(Self {
@@ -570,6 +575,17 @@ fn open_root_handle(path: &Path) -> io::Result<fs::File> {
570575/// 숨김 폴더로 만들지 않음. snap으로 설치한 Firefox 등은 홈 바로 아래 숨김 폴더와
571576/// 시스템 임시 폴더의 파일을 읽지 못해 열기 요청을 조용히 무시함.
572577pub 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> {
573589 let name = archive.file_name().unwrap_or_default().to_string_lossy();
574590 let mut error = None;
575591 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> {
577593 for _ in 0..8 {
578594 let path = unique_folder(&base);
579595 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추가: }
581608 Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue,
582609 Err(e) => {
583610 error = Some(e);
@@ -589,7 +616,10 @@ pub fn create_open_root(archive: &Path) -> io::Result<OpenRoot> {
589616 let base = std::env::temp_dir().join(format!("zipelo-{}-{name}", std::process::id()));
590617 let path = unique_folder(&base);
591618 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추가: })
593623}
594624
595625/// 열기용 임시 폴더 이름. 예: `자료.zip (Zipelo 임시)`.
수정tests/rust-app-ui/presentation.rs+59-0
@@ -1284,3 +1284,62 @@ fn removal_of_original_folder_clears_nested_contents() {
12841284 remove_open_root(root);
12851285 assert!(!path.exists());
12861286}
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추가: }