| | @@ -503,11 +503,72 @@ 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 | +추가: let identity = worker_protocol::filesystem::object(&handle) |
| 516 | +추가: .map_err(|_| io::Error::other("임시 폴더 식별 실패"))?; |
| 517 | +추가: Ok(Self { |
| 518 | +추가: path, |
| 519 | +추가: handle, |
| 520 | +추가: identity, |
| 521 | +추가: }) |
| 522 | +추가: } |
| 523 | +추가: |
| 524 | +추가: pub fn path(&self) -> &Path { |
| 525 | +추가: &self.path |
| 526 | +추가: } |
| 527 | +추가: |
| 528 | +추가: pub fn is_current(&self) -> bool { |
| 529 | +추가: // 원본 핸들을 유지해 삭제된 inode가 재사용되어도 같다고 판정하지 않음. |
| 530 | +추가: self.handle.metadata().is_ok() |
| 531 | +추가: && open_root_handle(&self.path) |
| 532 | +추가: .ok() |
| 533 | +추가: .and_then(|file| worker_protocol::filesystem::object(&file).ok()) |
| 534 | +추가: .is_some_and(|identity| identity == self.identity) |
| 535 | +추가: } |
| 536 | +추가: } |
| 537 | +추가: |
| 538 | +추가: fn open_root_handle(path: &Path) -> io::Result<fs::File> { |
| 539 | +추가: let mut options = fs::OpenOptions::new(); |
| 540 | +추가: options.read(true); |
| 541 | +추가: #[cfg(target_os = "linux")] |
| 542 | +추가: { |
| 543 | +추가: use std::os::unix::fs::OpenOptionsExt; |
| 544 | +추가: options.custom_flags(0x20000); // 심볼릭 링크를 따라가지 않음. |
| 545 | +추가: } |
| 546 | +추가: #[cfg(windows)] |
| 547 | +추가: { |
| 548 | +추가: use std::os::windows::fs::OpenOptionsExt; |
| 549 | +추가: // 폴더 자체를 열며 재분석 지점을 따라가지 않음. |
| 550 | +추가: options.custom_flags(0x02000000 | 0x00200000); |
| 551 | +추가: } |
| 552 | +추가: let file = options.open(path)?; |
| 553 | +추가: let metadata = file.metadata()?; |
| 554 | +추가: if !metadata.is_dir() || fs::symlink_metadata(path)?.file_type().is_symlink() { |
| 555 | +추가: return Err(io::Error::other("임시 폴더가 아님")); |
| 556 | +추가: } |
| 557 | +추가: #[cfg(windows)] |
| 558 | +추가: { |
| 559 | +추가: use std::os::windows::fs::MetadataExt; |
| 560 | +추가: if metadata.file_attributes() & 0x400 != 0 { |
| 561 | +추가: return Err(io::Error::other("재분석 지점은 임시 폴더로 쓰지 않음")); |
| 562 | +추가: } |
| 563 | +추가: } |
| 564 | +추가: Ok(file) |
| 565 | +추가: } |
| 566 | +추가: |
| 506 | 567 | /// 압축파일 안 파일을 기본 프로그램으로 열 때 쓸 임시 폴더를 만듦. |
| 507 | 568 | /// 압축파일과 같은 폴더에 만들고, 그곳에 만들 수 없으면 시스템 임시 폴더를 씀. |
| 508 | 569 | /// 숨김 폴더로 만들지 않음. snap으로 설치한 Firefox 등은 홈 바로 아래 숨김 폴더와 |
| 509 | 570 | /// 시스템 임시 폴더의 파일을 읽지 못해 열기 요청을 조용히 무시함. |
| 510 | | -삭제: pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { |
| 571 | +추가: pub fn create_open_root(archive: &Path) -> io::Result<OpenRoot> { |
| 511 | 572 | let name = archive.file_name().unwrap_or_default().to_string_lossy(); |
| 512 | 573 | let mut error = None; |
| 513 | 574 | if let Some(parent) = archive.parent().filter(|p| !p.as_os_str().is_empty()) { |
| | @@ -515,7 +576,7 @@ pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { |
| 515 | 576 | for _ in 0..8 { |
| 516 | 577 | let path = unique_folder(&base); |
| 517 | 578 | match private_dir(&path) { |
| 518 | | -삭제: Ok(()) => return Ok(path), |
| 579 | +추가: Ok(()) => return OpenRoot::capture(path), |
| 519 | 580 | Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, |
| 520 | 581 | Err(e) => { |
| 521 | 582 | error = Some(e); |
| | @@ -527,7 +588,7 @@ pub fn create_open_root(archive: &Path) -> io::Result<PathBuf> { |
| 527 | 588 | let base = std::env::temp_dir().join(format!("zipelo-{}-{name}", std::process::id())); |
| 528 | 589 | let path = unique_folder(&base); |
| 529 | 590 | private_dir(&path).map_err(|e| error.unwrap_or(e))?; |
| 530 | | -삭제: Ok(path) |
| 591 | +추가: OpenRoot::capture(path) |
| 531 | 592 | } |
| 532 | 593 | |
| 533 | 594 | /// 열기용 임시 폴더 이름. 예: `자료.zip (Zipelo 임시)`. |
| | @@ -536,8 +597,10 @@ pub fn open_root_name(archive_name: &str) -> String { |
| 536 | 597 | } |
| 537 | 598 | |
| 538 | 599 | /// 이 앱이 만든 열기용 폴더를 지움. 다른 프로그램이 파일을 쓰고 있으면 남을 수 있음. |
| 539 | | -삭제: pub fn remove_open_root(path: &Path) { |
| 540 | | -삭제: let _ = fs::remove_dir_all(path); |
| 600 | +추가: pub fn remove_open_root(root: &OpenRoot) { |
| 601 | +추가: if root.is_current() { |
| 602 | +추가: let _ = fs::remove_dir_all(root.path()); |
| 603 | +추가: } |
| 541 | 604 | } |
| 542 | 605 | |
| 543 | 606 | /// 기본 프로그램으로 열면 실행될 수 있는 파일은 바로 열지 않음. |