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.

Judge a movie by the type Spotlight indexed it under

Țugui Dragoș728632e172e3
diff --git a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
index f05472b0..c314645f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
@@ -115,8 +115,15 @@ public enum FileSearch {
case skipped
}
- /// Runs a Spotlight query and returns the paths it found, or nil when it could not run or was stopped.
- typealias Gather = @concurrent @Sendable (sending MDQuery) async -> [String]?
+ /// What a Spotlight query found.
+ struct Found: Sendable {
+ var paths: [String] = []
+ /// Those Spotlight indexed as an MPEG-2 transport stream, read only for a search for movies.
+ var transportStreams: Set<String> = []
+ }
+
+ /// Runs a Spotlight query and returns what it found, or nil when it could not run or was stopped.
+ typealias Gather = @concurrent @Sendable (sending MDQuery) async -> Found?
/// Moves `files` to the Trash as one removal. A file written again under the same name after it was
/// listed is a different file, so it fails with `changedSinceScan` instead, checked right before its own move.
@@ -140,7 +147,10 @@ public enum FileSearch {
environment: SearchEnvironment = .current,
exclusions: Exclusions = .none
) async -> FileSearchResults {
- await run(criteria, environment: environment, exclusions: exclusions) { await SpotlightGathering($0).paths() }
+ let readsTypes = criteria.kind == .movies
+ return await run(criteria, environment: environment, exclusions: exclusions) { query in
+ await SpotlightGathering(query, readsTypes: readsTypes).found()
+ }
}
/// The same, with the query, the disks and their index asked of `gather`, `disks` and `isIndexing`, so a test can
@@ -158,8 +168,11 @@ public enum FileSearch {
}
let scope = criteria.scope == .home ? kMDQueryScopeHome : kMDQueryScopeComputer
MDQuerySetSearchScope(query, [scope] as CFArray, 0)
- guard let paths = await gather(query), !Task.isCancelled else { return FileSearchResults(didRun: false) }
- var results = results(from: paths, kind: criteria.kind, environment: environment, exclusions: exclusions)
+ guard let found = await gather(query), !Task.isCancelled else { return FileSearchResults(didRun: false) }
+ var results = results(
+ from: found.paths, transportStreams: found.transportStreams, environment: environment,
+ exclusions: exclusions
+ )
// Files found on one disk say nothing of another, while a search of the home that found any has read its disk.
guard results.didRun, results.files.isEmpty || criteria.scope == .computer else { return results }
let searched = disks(criteria.scope)
@@ -177,7 +190,7 @@ public enum FileSearch {
/// left out counted by why. Separate from the query, so a test can pass its own paths and answers.
static func results(
from paths: [String],
- kind: FileKind = .any,
+ transportStreams: Set<String> = [],
environment: SearchEnvironment = .current,
exclusions: Exclusions = .none,
refusal: ((URL) -> GuardRefusal?)? = nil,
@@ -212,7 +225,10 @@ public enum FileSearch {
guard !Task.isCancelled else { return FileSearchResults(didRun: false) }
let url = URL(filePath: candidate.path)
let hasRoom = results.files.count < maximumResults
- let judgment = judge(url, info: candidate.info, kind: kind, refusal: refusal, syncKeeper: syncKeeper)
+ let judgment = judge(
+ url, info: candidate.info, isTypedAsAStream: transportStreams.contains(candidate.path),
+ refusal: refusal, syncKeeper: syncKeeper
+ )
switch judgment {
case .skipped:
continue
@@ -267,14 +283,21 @@ public enum FileSearch {
}
static func judge(
- _ url: URL, info: stat, kind: FileKind, refusal: (URL) -> GuardRefusal?, syncKeeper: (URL) -> SyncKeeper?
+ _ url: URL,
+ info: stat,
+ isTypedAsAStream: Bool,
+ refusal: (URL) -> GuardRefusal?,
+ syncKeeper: (URL) -> SyncKeeper?
) -> Judgment {
guard holdsItsContentsHere(info) else {
// A transport stream is a movie only by its first bytes, and reading a file that lives only in the cloud
// downloads it.
- return kind == .movies && isTypedAsATransportStream(url) ? .skipped : .leftOut(.syncs)
+ return isTypedAsAStream ? .skipped : .leftOut(.syncs)
+ }
+ if isTypedAsAStream {
+ guard let isAStream = beginsAsATransportStream(url) else { return .leftOut(.couldNotBeRead) }
+ guard isAStream else { return .skipped }
}
- guard kind != .movies || isAMovie(url) else { return .skipped }
// A search of this Mac finds macOS's own files by the tens of thousands, and the guard opens each it judges.
if RemovalGuard.neverMovesAFile(at: url.path(percentEncoded: false)) { return .leftOut(.protected) }
guard let refused = refusal(url) else { return .listed }
@@ -286,18 +309,17 @@ public enum FileSearch {
/// macOS gives TypeScript's `.ts` and `.mts` the type of an MPEG-2 transport stream, a movie, so such a file is one
/// only when it begins as one: packets that each start with the byte 0x47, every 188 bytes, or every 192 after the
- /// 4-byte header Blu-ray adds (ISO/IEC 13818-1).
- static func isAMovie(_ url: URL) -> Bool {
- guard isTypedAsATransportStream(url) else { return true }
- guard let bytes = BoundedRead.prefix(of: url, count: 197).map(Array.init) else { return false }
+ /// 4-byte header Blu-ray adds (ISO/IEC 13818-1). Nil when it can't be read.
+ static func beginsAsATransportStream(_ url: URL) -> Bool? {
+ guard let bytes = BoundedRead.prefix(of: url, count: 197).map(Array.init) else { return nil }
func packets(from start: Int, every length: Int) -> Bool {
bytes.count > start + length && bytes[start] == 0x47 && bytes[start + length] == 0x47
}
return packets(from: 0, every: 188) || packets(from: 4, every: 192)
}
- static func isTypedAsATransportStream(_ url: URL) -> Bool {
- UTType(filenameExtension: url.pathExtension)?.conforms(to: .mpeg2TransportStream) == true
+ static func isATransportStream(type identifier: String) -> Bool {
+ UTType(identifier)?.conforms(to: .mpeg2TransportStream) == true
}
/// Whether a file's contents are on this Mac. One whose contents live only in the cloud (`SF_DATALESS`) frees
@@ -389,17 +411,19 @@ private final class SpotlightGathering: @unchecked Sendable {
private let query: MDQuery
private let queue = DispatchQueue(label: "com.tuguidragos.Peel.FileSearch")
// Touched only on `queue`.
- private var continuation: CheckedContinuation<[String]?, Never>?
+ private var continuation: CheckedContinuation<FileSearch.Found?, Never>?
private var observer: (any NSObjectProtocol)?
private var isOver = false
/// Set as soon as the task is canceled, not on `queue`, where the cancel would wait behind the reading of paths.
private let isCanceled = Atomic(false)
+ private let readsTypes: Bool
- init(_ query: MDQuery) {
+ init(_ query: MDQuery, readsTypes: Bool) {
self.query = query
+ self.readsTypes = readsTypes
}
- func paths() async -> [String]? {
+ func found() async -> FileSearch.Found? {
await withTaskCancellationHandler {
await withCheckedContinuation { continuation in
queue.async { self.start(continuation) }
@@ -410,7 +434,7 @@ private final class SpotlightGathering: @unchecked Sendable {
}
}
- private func start(_ continuation: CheckedContinuation<[String]?, Never>) {
+ private func start(_ continuation: CheckedContinuation<FileSearch.Found?, Never>) {
// Canceled before it started.
guard !isOver else { return continuation.resume(returning: nil) }
self.continuation = continuation
@@ -419,7 +443,7 @@ private final class SpotlightGathering: @unchecked Sendable {
forName: Notification.Name(kMDQueryDidFinishNotification as String), object: query, queue: nil
) { [weak self] _ in
guard let self else { return }
- end(with: foundPaths())
+ end(with: results())
}
if !MDQueryExecute(query, 0) {
end(with: nil)
@@ -428,29 +452,32 @@ private final class SpotlightGathering: @unchecked Sendable {
/// Reads each result's path from the result itself, since a query's value lists never carry `kMDItemPath`.
/// Nil once the task is canceled.
- private func foundPaths() -> [String]? {
+ private func results() -> FileSearch.Found? {
MDQueryDisableUpdates(query)
- var paths: [String] = []
+ var found = FileSearch.Found()
for index in 0..<MDQueryGetResultCount(query) {
if index.isMultiple(of: 256), isCanceled.load(ordering: .relaxed) { return nil }
guard let pointer = MDQueryGetResultAtIndex(query, index) else { continue }
let item = Unmanaged<MDItem>.fromOpaque(pointer).takeUnretainedValue()
- if let path = MDItemCopyAttribute(item, kMDItemPath) as? String {
- paths.append(path)
+ guard let path = MDItemCopyAttribute(item, kMDItemPath) as? String else { continue }
+ found.paths.append(path)
+ if readsTypes, let type = MDItemCopyAttribute(item, kMDItemContentType) as? String,
+ FileSearch.isATransportStream(type: type) {
+ found.transportStreams.insert(path)
}
}
- return paths
+ return found
}
/// Stops the query and answers the waiting task, once.
- private func end(with paths: [String]?) {
+ private func end(with found: FileSearch.Found?) {
guard !isOver else { return }
isOver = true
MDQueryStop(query)
if let observer {
NotificationCenter.default.removeObserver(observer)
}
- continuation?.resume(returning: paths)
+ continuation?.resume(returning: found)
continuation = nil
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
index e3738f16..f4c67c62 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
@@ -41,7 +41,7 @@ struct FileSearchTests {
let judged = Mutex(0)
let searching = Task {
- FileSearch.results(from: paths, kind: .any, environment: environment, refusal: { _ in
+ FileSearch.results(from: paths, environment: environment, refusal: { _ in
judged.withLock { $0 += 1 }
withUnsafeCurrentTask { $0?.cancel() }
return nil
@@ -62,7 +62,7 @@ struct FileSearchTests {
]
func search(indexing: Set<String>, finding paths: [String] = []) async -> FileSearchResults {
await FileSearch.run(
- criteria, gather: { _ in paths }, disks: { _ in disks },
+ criteria, gather: { _ in FileSearch.Found(paths: paths) }, disks: { _ in disks },
isIndexing: { indexing.contains($0.path) }
)
}
@@ -92,7 +92,8 @@ struct FileSearchTests {
]
func search(indexing: Set<String>) async -> FileSearchResults {
await FileSearch.run(
- criteria, gather: { _ in [file.path(percentEncoded: false)] }, disks: { _ in disks },
+ criteria, gather: { _ in FileSearch.Found(paths: [file.path(percentEncoded: false)]) },
+ disks: { _ in disks },
isIndexing: { indexing.contains($0.path) }
)
}
@@ -239,8 +240,10 @@ struct FileSearchTests {
let paths = [code, clip, recording].map { $0.path(percentEncoded: false) }
let environment = SearchEnvironment(homeDirectory: home, rootDirectory: directory.url)
- let movies = FileSearch.results(from: paths, kind: .movies, environment: environment, refusal: { _ in nil })
- let anything = FileSearch.results(from: paths, kind: .any, environment: environment, refusal: { _ in nil })
+ let movies = FileSearch.results(
+ from: paths, transportStreams: Set(paths), environment: environment, refusal: { _ in nil }
+ )
+ let anything = FileSearch.results(from: paths, environment: environment, refusal: { _ in nil })
#expect(Set(movies.files.map(\.url.lastPathComponent)) == ["clip.ts", "recording.ts"])
#expect(Set(anything.files.map(\.url.lastPathComponent)) == ["index.ts", "clip.ts", "recording.ts"])
@@ -258,9 +261,44 @@ struct FileSearchTests {
let paths = [module, types, camera, blueRay].map { $0.path(percentEncoded: false) }
let environment = SearchEnvironment(homeDirectory: home, rootDirectory: directory.url)
- let movies = FileSearch.results(from: paths, kind: .movies, environment: environment, refusal: { _ in nil })
+ let movies = FileSearch.results(
+ from: paths, transportStreams: Set(paths), environment: environment, refusal: { _ in nil }
+ )
#expect(Set(movies.files.map(\.url.lastPathComponent)) == ["00001.mts", "00002.m2ts"])
+ #expect(FileSearch.isATransportStream(type: "public.mpeg-2-transport-stream"))
+ #expect(FileSearch.isATransportStream(type: "public.avchd-mpeg-2-transport-stream"))
+ #expect(!FileSearch.isATransportStream(type: "public.mpeg-4"))
+ }
+
+ @Test func goesByTheTypeSpotlightIndexedAndNotTheExtension() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let indexedAsCode = try directory.file("home/Projects/app/index.ts", contents: Data("export {}\n".utf8))
+ let indexedAsAStream = try directory.file("home/Movies/notes.dat", contents: Data("export {}\n".utf8))
+ let paths = [indexedAsCode, indexedAsAStream].map { $0.path(percentEncoded: false) }
+
+ let movies = FileSearch.results(
+ from: paths, transportStreams: [indexedAsAStream.path(percentEncoded: false)],
+ environment: SearchEnvironment(homeDirectory: home, rootDirectory: directory.url), refusal: { _ in nil }
+ )
+
+ #expect(movies.files.map(\.url.lastPathComponent) == ["index.ts"])
+ }
+
+ @Test(.permissionsHold) func countsAFileIndexedAsAStreamThatCouldNotBeRead() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let clip = try directory.file("home/Movies/clip.ts", bytes: 2_000)
+ try directory.setPermissions(0, of: clip)
+
+ let movies = FileSearch.results(
+ from: [clip.path(percentEncoded: false)], transportStreams: [clip.path(percentEncoded: false)],
+ environment: SearchEnvironment(homeDirectory: home, rootDirectory: directory.url), refusal: { _ in nil }
+ )
+
+ #expect(movies.files.isEmpty)
+ #expect(movies.leftOut == [.couldNotBeRead: 1])
}
@Test func neverListsAFileThatSyncsToTheCloud() throws {
@@ -324,12 +362,14 @@ struct FileSearchTests {
var info = stat()
info.st_mode = S_IFREG
info.st_flags = UInt32(SF_DATALESS)
- func judged(_ kind: FileKind) -> FileSearch.Judgment {
- FileSearch.judge(clip, info: info, kind: kind, refusal: { _ in nil }, syncKeeper: { _ in nil })
+ func judged(isTypedAsAStream: Bool) -> FileSearch.Judgment {
+ FileSearch.judge(
+ clip, info: info, isTypedAsAStream: isTypedAsAStream, refusal: { _ in nil }, syncKeeper: { _ in nil }
+ )
}
- #expect(judged(.any) == .leftOut(.syncs))
- #expect(judged(.movies) == .skipped)
+ #expect(judged(isTypedAsAStream: false) == .leftOut(.syncs))
+ #expect(judged(isTypedAsAStream: true) == .skipped)
}
@Test func countsWhatItCannotCheckAgainstExclusionsNotYetRead() throws {