Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

A folder that could not be listed is not read as empty

Țugui Dragoșf11fbb16b6d3
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift b/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
index 1f1e7c85..cc80f964 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/SlowRead.swift
@@ -1,3 +1,4 @@
+import Darwin
import Foundation
import Synchronization
@@ -5,22 +6,28 @@ import Synchronization
/// the way `FileSize` walks a folder. Nil means no answer, never an empty folder.
enum SlowRead {
/// The names in the folder at `url`: empty for anything that is not there or is not a folder, and nil when
- /// what it holds is not known: macOS refused to list it, the read did not come back in time, or the task was
- /// canceled.
+ /// what it holds is not known: macOS refused to list it, the listing failed any other way, the read did not
+ /// come back in time, or the task was canceled.
static func names(in url: URL, within budget: TimeInterval = FileSize.budget) async -> [String]? {
let path = url.path(percentEncoded: false)
let listed: [String]?? = await answer(within: budget) { _ in
do {
return try FileManager.default.contentsOfDirectory(atPath: path)
- } catch CocoaError.fileReadNoPermission {
- return nil
} catch {
- return []
+ return holdsNothing(path) ? [] : nil
}
}
return listed ?? nil
}
+ /// Whether `path` is not there or is not a folder, as `stat` tells. Foundation reports a link that leads back
+ /// to itself as a missing file too, and that one is there.
+ private static func holdsNothing(_ path: String) -> Bool {
+ var info = stat()
+ guard stat(path, &info) == 0 else { return errno == ENOENT || errno == ENOTDIR }
+ return info.st_mode & S_IFMT != S_IFDIR
+ }
+
/// Runs `read` on a thread of its own and returns its answer, or nil when it does not come within `budget`
/// or the task is canceled. A read that works in steps can check `isGivenUp` and stop once nobody waits.
static func answer<T: Sendable>(within budget: TimeInterval, _ read: @escaping @Sendable (_ isGivenUp: @escaping @Sendable () -> Bool) -> T) async -> T? {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SlowReadTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SlowReadTests.swift
index 804b28b0..672a365b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SlowReadTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SlowReadTests.swift
@@ -61,4 +61,15 @@ struct SlowReadTests {
#expect(await SlowRead.names(in: file) == [])
#expect(await SlowRead.names(in: directory.url.appending(path: "missing")) == [])
}
+
+ /// Only a folder that is not there, or is not a folder, holds nothing. Any other error leaves what it holds
+ /// unknown: a link that leads back to itself stands in for the errors a disk or a file provider can give.
+ @Test func aFolderThatCouldNotBeListedIsNotEmpty() async throws {
+ let directory = try TemporaryDirectory()
+ let loop = directory.url.appending(path: "loop")
+ let path = loop.path(percentEncoded: false)
+ try FileManager.default.createSymbolicLink(atPath: path, withDestinationPath: "loop")
+
+ #expect(await SlowRead.names(in: loop) == nil)
+ }
}