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.

Leave TypeScript out of File Search's Movies

Țugui Dragoș935c8f56b252
diff --git a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
index f72c9162..263ad3d1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Search/FileSearch.swift
@@ -97,13 +97,14 @@ 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) }
- return results(from: paths, environment: environment, exclusions: exclusions)
+ return results(from: paths, kind: criteria.kind, environment: environment, exclusions: exclusions)
}
/// Turns the paths Spotlight found into results: checked on disk, sorted, and cut to `maximumResults`. It is
/// separate from the query so a test can pass its own paths, and its own answer in place of the guard's.
static func results(
from paths: [String],
+ kind: FileKind = .any,
environment: SearchEnvironment = .current,
exclusions: Exclusions = .none,
allows: ((URL) -> Bool)? = nil
@@ -128,7 +129,7 @@ public enum FileSearch {
var results = FileSearchResults()
for candidate in candidates {
let url = URL(filePath: candidate.path)
- guard allows(url) else { continue }
+ guard kind != .movies || isAMovie(url), allows(url) else { continue }
guard results.files.count < maximumResults else {
results.isTruncated = true
break
@@ -145,6 +146,18 @@ public enum FileSearch {
return results
}
+ /// macOS gives TypeScript's `.ts` 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 UTType(filenameExtension: url.pathExtension) == .mpeg2TransportStream else { return true }
+ guard let bytes = BoundedRead.prefix(of: url, count: 197).map(Array.init) else { return false }
+ 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)
+ }
+
/// Whether a file's contents are on this Mac. One whose contents live only in the cloud (`SF_DATALESS`) frees
/// nothing here when moved.
static func holdsItsContentsHere(_ info: stat) -> Bool {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
index 35578cca..ac587df1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FileSearchTests.swift
@@ -123,6 +123,26 @@ struct FileSearchTests {
#expect(!results.isTruncated)
}
+ @Test func moviesLeaveOutTypeScriptThatMacOSTypesAsATransportStream() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let code = try directory.file("home/Projects/app/index.ts", contents: Data("export const answer = 42\n".utf8))
+ var packets = [UInt8](repeating: 0xFF, count: 188 * 3)
+ for start in stride(from: 0, to: packets.count, by: 188) { packets[start] = 0x47 }
+ let clip = try directory.file("home/Movies/clip.ts", contents: Data(packets))
+ var blueRay = [UInt8](repeating: 0xFF, count: 192 * 3)
+ for start in stride(from: 4, to: blueRay.count, by: 192) { blueRay[start] = 0x47 }
+ let recording = try directory.file("home/Movies/recording.ts", contents: Data(blueRay))
+ 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, allows: { _ in true })
+ let anything = FileSearch.results(from: paths, kind: .any, environment: environment, allows: { _ in true })
+
+ #expect(Set(movies.files.map(\.url.lastPathComponent)) == ["clip.ts", "recording.ts"])
+ #expect(Set(anything.files.map(\.url.lastPathComponent)) == ["index.ts", "clip.ts", "recording.ts"])
+ }
+
@Test func neverListsAFileThatSyncsToTheCloud() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)