Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basics: extract darwinCacheDirectories from TSCUtility #6011

Merged
merged 1 commit into from Jan 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions Sources/Basics/Sandbox.swift
Expand Up @@ -13,10 +13,7 @@
import Foundation
import TSCBasic

import enum TSCUtility.Platform

public enum Sandbox {

/// Applies a sandbox invocation to the given command line (if the platform supports it),
/// and returns the modified command line. On platforms that don't support sandboxing, the
/// command line is returned unmodified.
Expand Down Expand Up @@ -55,6 +52,29 @@ public enum Sandbox {
// MARK: - macOS

#if os(macOS)
fileprivate let threadSafeDarwinCacheDirectories: [AbsolutePath] = {
func GetConfStr(_ name: CInt) -> AbsolutePath? {
let length: Int = confstr(name, nil, 0)

let buffer: UnsafeMutableBufferPointer<CChar> = .allocate(capacity: length)
defer { buffer.deallocate() }

guard confstr(name, buffer.baseAddress, length) == length else { return nil }

let value: String = String(cString: buffer.baseAddress!)
guard value.hasSuffix("/") else { return nil }

return try? resolveSymlinks(AbsolutePath(validating: value))
}

var directories: [AbsolutePath] = []
try? directories.append(AbsolutePath(validating: "/private/var/tmp"))
(try? TSCBasic.determineTempDirectory()).map { directories.append($0) }
GetConfStr(_CS_DARWIN_USER_TEMP_DIR).map { directories.append($0) }
GetConfStr(_CS_DARWIN_USER_CACHE_DIR).map { directories.append($0) }
return directories
}()

fileprivate func macOSSandboxProfile(
strictness: Sandbox.Strictness,
writableDirectories: [AbsolutePath],
Expand Down Expand Up @@ -86,7 +106,7 @@ fileprivate func macOSSandboxProfile(

// The following accesses are only needed when interpreting the manifest (versus running a compiled version).
if strictness == .manifest_pre_53 {
writableDirectoriesExpression += Platform.threadSafeDarwinCacheDirectories.get().map {
writableDirectoriesExpression += threadSafeDarwinCacheDirectories.map {
##"(regex #"^\##($0.pathString)/org\.llvm\.clang.*")"##
}
}
Expand Down Expand Up @@ -137,8 +157,4 @@ fileprivate extension AbsolutePath {
+ "\""
}
}

extension TSCUtility.Platform {
fileprivate static let threadSafeDarwinCacheDirectories = ThreadSafeArrayStore<AbsolutePath>((try? Self.darwinCacheDirectories()) ?? [])
}
#endif