跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos-diagnostics.swift

61 行 · 3.7 KB历史下载
1import Foundation
2import Darwin
3
4enum 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}