Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Copy into itself: a link or other letter case no longer slips past (759)

og-orca2ac665447c0d
diff --git a/macos/Sources/RustyFiles/FileOps.swift b/macos/Sources/RustyFiles/FileOps.swift
index 4f7a2591..d0328f0e 100644
--- a/macos/Sources/RustyFiles/FileOps.swift
+++ b/macos/Sources/RustyFiles/FileOps.swift
@@ -29,6 +29,25 @@ enum FileOps {
return url
}
+ /// Whether `dir` is `src` or lies inside it. The paths are compared first, then the folders
+ /// up from `dir` by file identity, so a link or other letter case on a disk that ignores
+ /// case can't hide `src` (issue 759). A link as `src` is
+ /// copied as a link, so it is compared by its own path only.
+ static func isInside(_ dir: URL, _ src: URL) -> Bool {
+ let s = src.standardizedFileURL.path, d = dir.standardizedFileURL.path
+ if d == s || d.hasPrefix(s + "/") { return true }
+ let keys: Set<URLResourceKey> = [.fileResourceIdentifierKey, .isSymbolicLinkKey]
+ guard let values = try? src.resourceValues(forKeys: keys), values.isSymbolicLink != true,
+ let id = values.fileResourceIdentifier else { return false }
+ var u = dir.resolvingSymlinksInPath()
+ while true {
+ if let other = try? u.resourceValues(forKeys: [.fileResourceIdentifierKey]).fileResourceIdentifier,
+ other.isEqual(id) { return true }
+ if u.path == "/" { return false }
+ u = u.deletingLastPathComponent()
+ }
+ }
+
/// Copies (or moves) `urls` into `dir`. A copy into the folder it came from is named
/// "name - Copy"; anything else whose name is taken gets " (2)" and so on. Nothing is
/// overwritten. Returns the new items.
@@ -40,7 +59,7 @@ enum FileOps {
let stem = src.deletingPathExtension().lastPathComponent
var dest = dir.appendingPathComponent(src.lastPathComponent)
if move && src.deletingLastPathComponent().standardizedFileURL == dir.standardizedFileURL { continue }
- if dir.standardizedFileURL.path.hasPrefix(src.standardizedFileURL.path + "/") || dir.standardizedFileURL == src.standardizedFileURL {
+ if isInside(dir, src) {
throw Failure(message: String(localized: "“\(src.lastPathComponent)” can’t be put inside itself."))
}
if fm.fileExists(atPath: dest.path) {
diff --git a/macos/Sources/RustyFiles/SelfTest.swift b/macos/Sources/RustyFiles/SelfTest.swift
index 50ae144a..0953fafd 100644
--- a/macos/Sources/RustyFiles/SelfTest.swift
+++ b/macos/Sources/RustyFiles/SelfTest.swift
@@ -234,7 +234,7 @@ enum SelfTest {
ArchiveDrag.current = nil
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
check(fm.fileExists(atPath: d.appendingPathComponent("inner/note.txt").path), "a folder dragged out of an archive arrives whole")
- unreadable(root) { links(root) { finish(root) } }
+ unreadable(root) { links(root) { inside(root) { finish(root) } } }
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
@@ -336,4 +336,41 @@ enum SelfTest {
}
}
}
+
+ /// 14. a folder can't be put inside itself by another spelling of the way in (issue 759):
+ /// through a link to its own subfolder, or in other letter case on a disk that ignores it.
+ private static func inside(_ root: URL, then done: @escaping () -> Void) {
+ let fm = FileManager.default
+ let box = root.appendingPathComponent("Box"), sub = box.appendingPathComponent("sub")
+ let link = root.appendingPathComponent("to-sub"), other = root.appendingPathComponent("box/sub")
+ try? fm.createDirectory(at: sub, withIntermediateDirectories: true)
+ fm.createFile(atPath: box.appendingPathComponent("f.txt").path, contents: Data("f".utf8))
+ try? fm.createSymbolicLink(at: link, withDestinationURL: sub)
+ let caseless = fm.fileExists(atPath: other.path)
+ var done1 = false, done2 = false
+ Transfer(.copy, [box], into: link).start(window: nil) { _ in done1 = true }
+ if caseless { Transfer(.copy, [box], into: other).start(window: nil) { _ in done2 = true } }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
+ check(!done1 && !fm.fileExists(atPath: sub.appendingPathComponent("Box").path),
+ "a copy into a link to its own subfolder is refused")
+ if caseless {
+ check(!done2 && !fm.fileExists(atPath: sub.appendingPathComponent("Box").path),
+ "a copy into its own subfolder in other letter case is refused")
+ }
+ check((try? FileOps.paste([box], into: link, move: false)) == nil
+ && !fm.fileExists(atPath: sub.appendingPathComponent("Box").path),
+ "a paste into a link to its own subfolder is refused")
+ if caseless {
+ check((try? FileOps.paste([box], into: other, move: false)) == nil
+ && !fm.fileExists(atPath: sub.appendingPathComponent("Box").path),
+ "a paste into its own subfolder in other letter case is refused")
+ }
+ let toBox = root.appendingPathComponent("to-box")
+ try? fm.createSymbolicLink(at: toBox, withDestinationURL: box)
+ let pasted = (try? FileOps.paste([toBox], into: sub, move: false))?.first
+ check(pasted.flatMap { try? fm.destinationOfSymbolicLink(atPath: $0.path) } != nil,
+ "a link to a folder pastes into that folder as a link")
+ done()
+ }
+ }
}
diff --git a/macos/Sources/RustyFiles/Transfers.swift b/macos/Sources/RustyFiles/Transfers.swift
index d9f0201c..f3e52d9e 100644
--- a/macos/Sources/RustyFiles/Transfers.swift
+++ b/macos/Sources/RustyFiles/Transfers.swift
@@ -214,7 +214,7 @@ final class Transfer: @unchecked Sendable {
var steps: [Step] = []
for src in sources {
let s = src.standardizedFileURL, d = dest.standardizedFileURL
- if d.path == s.path || d.path.hasPrefix(s.path + "/") {
+ if FileOps.isInside(d, s) {
throw FileOps.Failure(message: String(localized: "“\(src.lastPathComponent)” can’t be put inside itself."))
}
// moving an item into the folder it is already in does nothing