From 6d7a9b42799d0592f8af8111b99002953e9ef703 Mon Sep 17 00:00:00 2001 From: Thiago Holanda Date: Sat, 13 Apr 2019 19:30:56 +0200 Subject: [PATCH 1/3] Rename variables to give more context --- Sources/Basic/Lock.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/Basic/Lock.swift b/Sources/Basic/Lock.swift index 39d5c80b22e..fa014ccfd18 100644 --- a/Sources/Basic/Lock.swift +++ b/Sources/Basic/Lock.swift @@ -37,9 +37,9 @@ enum ProcessLockError: Swift.Error { public final class FileLock { /// File descriptor to the lock file. #if os(Windows) - private var h: HANDLE? + private var handle: HANDLE? #else - private var fd: CInt? + private var fileDescriptor: CInt? #endif /// Path to the lock file. @@ -58,8 +58,8 @@ public final class FileLock { /// Note: This method can throw if underlying POSIX methods fail. public func lock() throws { #if os(Windows) - if h == nil { - let h = lockFile.pathString.withCString(encodedAs: UTF16.self, { + if handle == nil { + let h = lockFile.pathString.withCString(encodedAs: UTF16.self, { CreateFileW( $0, UInt32(GENERIC_READ) | UInt32(GENERIC_WRITE), @@ -73,7 +73,7 @@ public final class FileLock { if h == INVALID_HANDLE_VALUE { throw FileSystemError(errno: Int32(GetLastError())) } - self.h = h + self.handle = h } var overlapped = OVERLAPPED() overlapped.Offset = 0 @@ -84,16 +84,16 @@ public final class FileLock { } #else // Open the lock file. - if fd == nil { + if fileDescriptor == nil { let fd = SPMLibc.open(lockFile.pathString, O_WRONLY | O_CREAT | O_CLOEXEC, 0o666) if fd == -1 { throw FileSystemError(errno: errno) } - self.fd = fd + self.fileDescriptor = fd } // Aquire lock on the file. while true { - if flock(fd!, LOCK_EX) == 0 { + if flock(fileDescriptor!, LOCK_EX) == 0 { break } // Retry if interrupted. @@ -112,7 +112,7 @@ public final class FileLock { overlapped.hEvent = nil UnlockFileEx(h, 0, DWORD(INT_MAX), DWORD(INT_MAX), &overlapped) #else - guard let fd = fd else { return } + guard let fd = fileDescriptor else { return } flock(fd, LOCK_UN) #endif } @@ -122,7 +122,7 @@ public final class FileLock { guard let h = h else { return } CloseHandle(h) #else - guard let fd = fd else { return } + guard let fd = fileDescriptor else { return } close(fd) #endif } From 22af928ffd9acb370b0a3bf04f2d63ec93d725b2 Mon Sep 17 00:00:00 2001 From: Thiago Holanda Date: Sat, 13 Apr 2019 21:39:05 +0200 Subject: [PATCH 2/3] Fix the indentation --- Sources/Basic/Lock.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Basic/Lock.swift b/Sources/Basic/Lock.swift index fa014ccfd18..b5d115ce2bd 100644 --- a/Sources/Basic/Lock.swift +++ b/Sources/Basic/Lock.swift @@ -59,7 +59,7 @@ public final class FileLock { public func lock() throws { #if os(Windows) if handle == nil { - let h = lockFile.pathString.withCString(encodedAs: UTF16.self, { + let h = lockFile.pathString.withCString(encodedAs: UTF16.self, { CreateFileW( $0, UInt32(GENERIC_READ) | UInt32(GENERIC_WRITE), @@ -69,11 +69,11 @@ public final class FileLock { DWORD(FILE_ATTRIBUTE_NORMAL), nil ) - }) - if h == INVALID_HANDLE_VALUE { - throw FileSystemError(errno: Int32(GetLastError())) - } - self.handle = h + }) + if h == INVALID_HANDLE_VALUE { + throw FileSystemError(errno: Int32(GetLastError())) + } + self.handle = h } var overlapped = OVERLAPPED() overlapped.Offset = 0 From ca5f2a3fc43ed052b4d1c811c9f2f3704fc7b48d Mon Sep 17 00:00:00 2001 From: Thiago Holanda Date: Sun, 14 Apr 2019 03:13:49 +0200 Subject: [PATCH 3/3] Fix property name on deinit --- Sources/Basic/Lock.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Basic/Lock.swift b/Sources/Basic/Lock.swift index b5d115ce2bd..2094cc2ec2b 100644 --- a/Sources/Basic/Lock.swift +++ b/Sources/Basic/Lock.swift @@ -119,8 +119,8 @@ public final class FileLock { deinit { #if os(Windows) - guard let h = h else { return } - CloseHandle(h) + guard let handle = handle else { return } + CloseHandle(handle) #else guard let fd = fileDescriptor else { return } close(fd)