@@ -18,30 +18,27 @@ public actor FileLock {
1818 self . filePath = path
1919 }
2020
21- public func tryLock( ) async -> Bool {
21+ public func tryLock( ) async throws -> Bool {
2222 do {
2323 guard !self . isLocked else { return true }
24-
25- guard !( try await FileSystem . exists ( atPath: self . filePath) ) else {
26- return false
27- }
28- // Create the lock file with exclusive permissions
29- try await FileSystem . create ( . mode( 0o600 ) , file: self . filePath, contents: nil )
24+ let fileURL = URL ( fileURLWithPath: self . filePath. string)
25+ let contents = Data ( ) // Empty data to create a lock file
26+ try contents. write ( to: fileURL, options: . withoutOverwriting)
3027 self . isLocked = true
3128 return true
32- } catch {
29+ } catch CocoaError . fileWriteFileExists {
3330 return false
3431 }
3532 }
3633
3734 public func waitForLock(
3835 timeout: TimeInterval = FileLock . defaultTimeout,
3936 pollingInterval: TimeInterval = FileLock . defaultPollingInterval
40- ) async -> Bool {
37+ ) async throws -> Bool {
4138 let start = Date ( )
4239
4340 while Date ( ) . timeIntervalSince ( start) < timeout {
44- if await self . tryLock ( ) {
41+ if try await self . tryLock ( ) {
4542 return true
4643 }
4744 try? await Task. sleep ( for: . seconds( pollingInterval) )
@@ -65,7 +62,7 @@ public func withLock<T>(
6562 action: @escaping ( ) async throws -> T
6663) async throws -> T {
6764 let lock = FileLock ( at: lockFile)
68- guard await lock. waitForLock ( timeout: timeout, pollingInterval: pollingInterval) else {
65+ guard try await lock. waitForLock ( timeout: timeout, pollingInterval: pollingInterval) else {
6966 throw SwiftlyError ( message: " Failed to acquire file lock at \( lock. filePath) " )
7067 }
7168
0 commit comments