-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathLock.swift
258 lines (233 loc) · 8.67 KB
/
Lock.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
import TSCLibc
@available(*, deprecated, message: "Use NSLock directly instead. SPM has a withLock extension function" )
/// A simple lock wrapper.
public struct Lock {
private let _lock = NSLock()
/// Create a new lock.
public init() {
}
func lock() {
_lock.lock()
}
func unlock() {
_lock.unlock()
}
/// Execute the given block while holding the lock.
public func withLock<T> (_ body: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try body()
}
}
// for internal usage
extension NSLock {
internal func withLock<T> (_ body: () throws -> T) rethrows -> T {
self.lock()
defer { self.unlock() }
return try body()
}
}
public enum ProcessLockError: Error {
case unableToAquireLock(errno: Int32)
}
extension ProcessLockError: CustomNSError {
public var errorUserInfo: [String : Any] {
return [NSLocalizedDescriptionKey: "\(self)"]
}
}
/// Provides functionality to acquire a lock on a file via POSIX's flock() method.
/// It can be used for things like serializing concurrent mutations on a shared resource
/// by multiple instances of a process. The `FileLock` is not thread-safe.
public final class FileLock {
public enum LockType {
case exclusive
case shared
}
/// File descriptor to the lock file.
#if os(Windows)
private var handle: HANDLE?
#else
private var fileDescriptor: CInt?
#endif
/// Path to the lock file.
private let lockFile: AbsolutePath
/// Create an instance of FileLock at the path specified
///
/// Note: The parent directory path should be a valid directory.
public init(at lockFile: AbsolutePath) {
self.lockFile = lockFile
}
@available(*, deprecated, message: "use init(at:) instead")
public convenience init(name: String, cachePath: AbsolutePath) {
self.init(at: cachePath.appending(component: name + ".lock"))
}
/// Try to acquire a lock. This method will block until lock the already aquired by other process.
///
/// Note: This method can throw if underlying POSIX methods fail.
public func lock(type: LockType = .exclusive, blocking: Bool = true) throws {
#if os(Windows)
if handle == nil {
let h: HANDLE = lockFile.pathString.withCString(encodedAs: UTF16.self, {
CreateFileW(
$0,
UInt32(GENERIC_READ) | UInt32(GENERIC_WRITE),
UInt32(FILE_SHARE_READ) | UInt32(FILE_SHARE_WRITE),
nil,
DWORD(OPEN_ALWAYS),
DWORD(FILE_ATTRIBUTE_NORMAL),
nil
)
})
if h == INVALID_HANDLE_VALUE {
throw FileSystemError(errno: Int32(GetLastError()), lockFile)
}
self.handle = h
}
var overlapped = OVERLAPPED()
overlapped.Offset = 0
overlapped.OffsetHigh = 0
overlapped.hEvent = nil
var dwFlags = Int32(0)
switch type {
case .exclusive: dwFlags |= LOCKFILE_EXCLUSIVE_LOCK
case .shared: break
}
if !blocking {
dwFlags |= LOCKFILE_FAIL_IMMEDIATELY
}
if !LockFileEx(handle, DWORD(dwFlags), 0,
UInt32.max, UInt32.max, &overlapped) {
throw ProcessLockError.unableToAquireLock(errno: Int32(GetLastError()))
}
#else
// Open the lock file.
if fileDescriptor == nil {
let fd = TSCLibc.open(lockFile.pathString, O_WRONLY | O_CREAT | O_CLOEXEC, 0o666)
if fd == -1 {
throw FileSystemError(errno: errno, lockFile)
}
self.fileDescriptor = fd
}
var flags = Int32(0)
switch type {
case .exclusive: flags = LOCK_EX
case .shared: flags = LOCK_SH
}
if !blocking {
flags |= LOCK_NB
}
// Aquire lock on the file.
while true {
if flock(fileDescriptor!, flags) == 0 {
break
}
// Retry if interrupted.
if errno == EINTR { continue }
throw ProcessLockError.unableToAquireLock(errno: errno)
}
#endif
}
/// Unlock the held lock.
public func unlock() {
#if os(Windows)
var overlapped = OVERLAPPED()
overlapped.Offset = 0
overlapped.OffsetHigh = 0
overlapped.hEvent = nil
UnlockFileEx(handle, 0, UInt32.max, UInt32.max, &overlapped)
#else
guard let fd = fileDescriptor else { return }
flock(fd, LOCK_UN)
#endif
}
deinit {
#if os(Windows)
guard let handle = handle else { return }
CloseHandle(handle)
#else
guard let fd = fileDescriptor else { return }
close(fd)
#endif
}
/// Execute the given block while holding the lock.
public func withLock<T>(type: LockType = .exclusive, blocking: Bool = true, _ body: () throws -> T) throws -> T {
try lock(type: type, blocking: blocking)
defer { unlock() }
return try body()
}
/// Execute the given block while holding the lock.
public func withLock<T>(type: LockType = .exclusive, blocking: Bool = true, _ body: () async throws -> T) async throws -> T {
try lock(type: type, blocking: blocking)
defer { unlock() }
return try await body()
}
public static func prepareLock(
fileToLock: AbsolutePath,
at lockFilesDirectory: AbsolutePath? = nil
) throws -> FileLock {
// unless specified, we use the tempDirectory to store lock files
let lockFilesDirectory = try lockFilesDirectory ?? localFileSystem.tempDirectory
if !localFileSystem.exists(lockFilesDirectory) {
throw FileSystemError(.noEntry, lockFilesDirectory)
}
if !localFileSystem.isDirectory(lockFilesDirectory) {
throw FileSystemError(.notDirectory, lockFilesDirectory)
}
// use the parent path to generate unique filename in temp
var lockFileName = try (resolveSymlinks(fileToLock.parentDirectory)
.appending(component: fileToLock.basename))
.components.joined(separator: "_")
.replacingOccurrences(of: ":", with: "_") + ".lock"
#if os(Windows)
// NTFS has an ARC limit of 255 codepoints
var lockFileUTF16 = lockFileName.utf16.suffix(255)
while String(lockFileUTF16) == nil {
lockFileUTF16 = lockFileUTF16.dropFirst()
}
lockFileName = String(lockFileUTF16) ?? lockFileName
#else
if lockFileName.hasPrefix(AbsolutePath.root.pathString) {
lockFileName = String(lockFileName.dropFirst(AbsolutePath.root.pathString.count))
}
// back off until it occupies at most `NAME_MAX` UTF-8 bytes but without splitting scalars
// (we might split clusters but it's not worth the effort to keep them together as long as we get a valid file name)
var lockFileUTF8 = lockFileName.utf8.suffix(Int(NAME_MAX))
while String(lockFileUTF8) == nil {
// in practice this will only be a few iterations
lockFileUTF8 = lockFileUTF8.dropFirst()
}
// we will never end up with nil since we have ASCII characters at the end
lockFileName = String(lockFileUTF8) ?? lockFileName
#endif
let lockFilePath = lockFilesDirectory.appending(component: lockFileName)
return FileLock(at: lockFilePath)
}
public static func withLock<T>(
fileToLock: AbsolutePath,
lockFilesDirectory: AbsolutePath? = nil,
type: LockType = .exclusive,
blocking: Bool = true,
body: () throws -> T
) throws -> T {
let lock = try Self.prepareLock(fileToLock: fileToLock, at: lockFilesDirectory)
return try lock.withLock(type: type, blocking: blocking, body)
}
public static func withLock<T>(
fileToLock: AbsolutePath,
lockFilesDirectory: AbsolutePath? = nil,
type: LockType = .exclusive,
blocking: Bool = true,
body: () async throws -> T
) async throws -> T {
let lock = try Self.prepareLock(fileToLock: fileToLock, at: lockFilesDirectory)
return try await lock.withLock(type: type, blocking: blocking, body)
}
}