| 1 | import Foundation |
| 2 | import Darwin |
| 3 | |
| 4 | enum GuestDiagnostic { |
| 5 | private static let limit = 1 << 20 |
| 6 | private static let sources = ["window", "clipboard"] |
| 7 | |
| 8 | static func record(_ source: String, _ reason: String) { |
| 9 | guard sources.contains(source), reason.range(of: "^[a-zA-Z][a-zA-Z0-9_]{0,95}$", options: .regularExpression) != nil else { return } |
| 10 | let directory = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Library/Logs/Cage") |
| 11 | try? append(source, reason, directory: directory) |
| 12 | } |
| 13 | |
| 14 | static func append(_ source: String, _ reason: String, directory: URL) throws { |
| 15 | guard sources.contains(source), reason.range(of: "^[a-zA-Z][a-zA-Z0-9_]{0,95}$", options: .regularExpression) != nil else { return } |
| 16 | try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700]) |
| 17 | let root = open(directory.path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW) |
| 18 | guard root >= 0 else { throw CocoaError(.fileReadNoPermission) } |
| 19 | defer { close(root) } |
| 20 | var info = stat() |
| 21 | guard fstat(root, &info) == 0, info.st_uid == getuid(), info.st_mode & 0o077 == 0 else { throw CocoaError(.fileReadNoPermission) } |
| 22 | let lock = openat(root, source + ".lock", O_CREAT | O_RDWR | O_NOFOLLOW, 0o600) |
| 23 | guard lock >= 0 else { throw CocoaError(.fileWriteNoPermission) } |
| 24 | defer { close(lock) } |
| 25 | guard fstat(lock, &info) == 0, info.st_mode & S_IFMT == S_IFREG, info.st_uid == getuid(), info.st_nlink == 1, |
| 26 | flock(lock, LOCK_EX) == 0 else { throw CocoaError(.fileWriteNoPermission) } |
| 27 | defer { flock(lock, LOCK_UN) } |
| 28 | let now = Date() |
| 29 | for index in 0...4 { |
| 30 | let name = index == 0 ? source + ".jsonl" : source + "-\(index).jsonl" |
| 31 | if fstatat(root, name, &info, AT_SYMLINK_NOFOLLOW) != 0 { continue } |
| 32 | guard info.st_mode & S_IFMT == S_IFREG, info.st_uid == getuid(), info.st_nlink == 1 else { throw CocoaError(.fileReadNoPermission) } |
| 33 | if Double(info.st_mtimespec.tv_sec) < now.addingTimeInterval(-14 * 86400).timeIntervalSince1970 { _ = unlinkat(root, name, 0) } |
| 34 | } |
| 35 | var data = try JSONSerialization.data(withJSONObject: ["time": ISO8601DateFormatter().string(from: now), "source": source, "reason": reason]) |
| 36 | data.append(10) |
| 37 | let current = source + ".jsonl" |
| 38 | if fstatat(root, current, &info, AT_SYMLINK_NOFOLLOW) == 0, |
| 39 | info.st_size + Int64(data.count) > limit || info.st_birthtimespec.tv_sec / 86400 != Int(now.timeIntervalSince1970) / 86400 { |
| 40 | _ = unlinkat(root, source + "-4.jsonl", 0) |
| 41 | for index in stride(from: 3, through: 0, by: -1) { |
| 42 | let previous = index == 0 ? current : source + "-\(index).jsonl" |
| 43 | let next = source + "-\(index + 1).jsonl" |
| 44 | if renameat(root, previous, root, next) != 0, errno != ENOENT { throw CocoaError(.fileWriteUnknown) } |
| 45 | } |
| 46 | } |
| 47 | let fd = openat(root, current, O_WRONLY | O_CREAT | O_APPEND | O_NOFOLLOW, 0o600) |
| 48 | guard fd >= 0 else { throw CocoaError(.fileWriteNoPermission) } |
| 49 | defer { close(fd) } |
| 50 | guard fstat(fd, &info) == 0, info.st_mode & S_IFMT == S_IFREG, info.st_uid == getuid(), info.st_nlink == 1 else { throw CocoaError(.fileWriteNoPermission) } |
| 51 | try data.withUnsafeBytes { bytes in |
| 52 | var offset = 0 |
| 53 | while offset < bytes.count { |
| 54 | let count = Darwin.write(fd, bytes.baseAddress!.advanced(by: offset), bytes.count - offset) |
| 55 | if count < 0 && errno == EINTR { continue } |
| 56 | guard count > 0 else { throw CocoaError(.fileWriteUnknown) } |
| 57 | offset += count |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |