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.

Write a measurement report through descriptors alone

Țugui Dragoș15c15b475679
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift b/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift
index 01954abe..1af0ed41 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/MeasureFile.swift
@@ -4,21 +4,36 @@ public import Foundation
/// Peel chooses the name, and Peel may hold rights its launcher lacks, such as Full Disk Access.
public struct MeasureFile: Sendable {
public let url: URL
+ private let home: URL
+ private let name: String
+ private static let folders = ["Library", "Logs", "Peel"]
/// Nil for anything but a plain file name: a path, or a hidden name.
public init?(named name: String, home: URL = .homeDirectory) {
guard !name.isEmpty, !name.contains("/"), !name.hasPrefix(".") else { return nil }
- url = home.appending(path: "Library/Logs/Peel", directoryHint: .isDirectory).appending(path: name)
+ self.home = home
+ self.name = name
+ url = home.appending(path: Self.folders.joined(separator: "/"), directoryHint: .isDirectory)
+ .appending(path: name)
}
- /// Appends `lines`, making the folder and the file when needed, and never through a link put at that name.
+ /// Appends `lines`, making the folders and the file when needed, through descriptors from the home down, so
+ /// nothing is made or written through a link put at any name on the way.
public func append(_ lines: [String]) {
- try? FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
- let descriptor = open(
- url.path(percentEncoded: false),
- O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC,
- 0o600
- )
+ var directory = open(home.path(percentEncoded: false), O_RDONLY | O_DIRECTORY | O_CLOEXEC)
+ guard directory >= 0 else { return }
+ for folder in Self.folders {
+ if mkdirat(directory, folder, 0o700) != 0, errno != EEXIST {
+ close(directory)
+ return
+ }
+ let next = openat(directory, folder, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)
+ close(directory)
+ guard next >= 0 else { return }
+ directory = next
+ }
+ defer { close(directory) }
+ let descriptor = openat(directory, name, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0o600)
guard descriptor >= 0 else { return }
defer { close(descriptor) }
let data = Data(lines.map { $0 + "\n" }.joined().utf8)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift
index e982b9b7..03ba0c6a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/MeasureFileTests.swift
@@ -34,4 +34,22 @@ struct MeasureFileTests {
#expect(try String(contentsOf: target, encoding: .utf8) == "kept")
}
+
+ /// Nor is a link put at a folder on the way: nothing is made or written through it.
+ @Test func neverWritesThroughALinkedFolder() throws {
+ for linked in ["Library/Logs", "Library/Logs/Peel"] {
+ let directory = try TemporaryDirectory()
+ let elsewhere = try directory.directory("Elsewhere")
+ try directory.directory((linked as NSString).deletingLastPathComponent)
+ try FileManager.default.createSymbolicLink(
+ at: directory.url.appending(path: linked), withDestinationURL: elsewhere
+ )
+ let file = try #require(MeasureFile(named: "report.txt", home: directory.url))
+
+ file.append(["run loop: 3 turns"])
+
+ let left = try FileManager.default.contentsOfDirectory(atPath: elsewhere.path(percentEncoded: false))
+ #expect(left.isEmpty, "\(linked) was written through: \(left)")
+ }
+ }
}