/
Utils.swift
157 lines (135 loc) · 3.92 KB
/
Utils.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//===--- Utils.swift - Utility functions ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Utility functions that are used in the swift-backtrace program.
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin.C
#elseif canImport(Glibc)
import Glibc
#elseif canImport(CRT)
import CRT
#endif
import Swift
internal func hex<T: FixedWidthInteger>(_ value: T,
withPrefix: Bool = true) -> String {
let digits = String(value, radix: 16)
let padTo = value.bitWidth / 4
let padding = digits.count >= padTo ? "" : String(repeating: "0",
count: padTo - digits.count)
let prefix = withPrefix ? "0x" : ""
return "\(prefix)\(padding)\(digits)"
}
internal func hex(_ bytes: [UInt8]) -> String {
return bytes.map{ hex($0, withPrefix: false) }.joined(separator: "")
}
internal func parseUInt64<S: StringProtocol>(_ s: S) -> UInt64? {
if s.hasPrefix("0x") {
return UInt64(s.dropFirst(2), radix: 16)
} else if s.hasPrefix("0b") {
return UInt64(s.dropFirst(2), radix: 2)
} else if s.hasPrefix("0o") {
return UInt64(s.dropFirst(2), radix: 8)
} else {
return UInt64(s, radix: 10)
}
}
#if os(macOS) || os(Linux)
struct PosixError: Error {
var errno: Int32
var desription: String {
return String(cString: strerror(self.errno))
}
}
internal func recursiveRemoveContents(_ dir: String) throws {
guard let dirp = opendir(dir) else {
throw PosixError(errno: errno)
}
defer {
closedir(dirp)
}
while let dp = readdir(dirp) {
let name: String =
withUnsafePointer(to: &dp.pointee.d_name) {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
let len = Int(dp.pointee.d_namlen)
#else
let len = Int(strlen($0))
#endif
return String(decoding: UnsafeRawBufferPointer(start: $0,
count: len),
as: UTF8.self)
}
if name == "." || name == ".." {
continue
}
let fullPath = "\(dir)/\(name)"
if dp.pointee.d_type == DT_DIR {
try recursiveRemove(fullPath)
} else {
if unlink(fullPath) != 0 {
throw PosixError(errno: errno)
}
}
}
}
internal func recursiveRemove(_ dir: String) throws {
try recursiveRemoveContents(dir)
if rmdir(dir) != 0 {
throw PosixError(errno: errno)
}
}
internal func withTemporaryDirectory(pattern: String, shouldDelete: Bool = true,
body: (String) throws -> ()) throws {
var buf = Array<UInt8>(pattern.utf8)
buf.append(0)
guard let dir = buf.withUnsafeMutableBufferPointer({
if let ptr = mkdtemp($0.baseAddress!) {
return String(cString: ptr)
}
return nil
}) else {
throw PosixError(errno: errno)
}
defer {
if shouldDelete {
try? recursiveRemove(dir)
}
}
try body(dir)
}
internal func spawn(_ path: String, args: [String]) throws {
var cargs = args.map{ strdup($0) }
cargs.append(nil)
let result = cargs.withUnsafeBufferPointer{
posix_spawn(nil, path, nil, nil, $0.baseAddress!, nil)
}
for arg in cargs {
free(arg)
}
if result != 0 {
throw PosixError(errno: errno)
}
}
#endif // os(macOS)
struct CFileStream: TextOutputStream {
var fp: UnsafeMutablePointer<FILE>
public func write(_ string: String) {
fputs(string, fp)
}
public func flush() {
fflush(fp)
}
}
var standardOutput = CFileStream(fp: stdout)
var standardError = CFileStream(fp: stderr)