Skip to content

Commit df8f085

Browse files
committed
wip: cleanup
1 parent 1fa72c3 commit df8f085

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

Sources/System/FileLock.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ extension FileDescriptor.FileLock {
7070
///
7171
/// The corresponding C field is `l_pid`
7272
@_alwaysEmitIntoClient
73-
public var pid: CInterop.PID {
74-
get { rawValue.l_pid }
75-
set { rawValue.l_pid = newValue }
73+
public var pid: ProcessID {
74+
get { ProcessID(rawValue: rawValue.l_pid) }
75+
set { rawValue.l_pid = newValue.rawValue }
7676
}
7777
}
7878

7979
// MARK: - Convenience for `struct flock`
8080
extension FileDescriptor.FileLock {
81-
82-
// For whole-file OFD locks
81+
// For OFD locks
8382
internal init(
8483
ofdType: Kind,
8584
start: Int64,
@@ -89,12 +88,8 @@ extension FileDescriptor.FileLock {
8988
self.type = ofdType
9089
self.start = start
9190
self.length = length
92-
self.pid = 0
91+
self.pid = ProcessID(rawValue: 0)
9392
}
94-
95-
// TOOO: convenience initializers or static constructors
96-
97-
9893
}
9994

10095
extension FileDescriptor.FileLock {

Sources/System/ProcessID.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
/// TODO: docs
3+
@frozen
4+
public struct ProcessID: RawRepresentable, Hashable {
5+
@_alwaysEmitIntoClient
6+
public var rawValue: CInterop.PID
7+
8+
@_alwaysEmitIntoClient
9+
public init(rawValue: CInterop.PID) { self.rawValue = rawValue }
10+
}
11+

Tests/SystemTests/FileLockTest.swift

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import XCTest
1515
@testable import System
1616
#endif
1717

18+
func _range(_ r: some RangeExpression<Int64>) -> Range<Int64> {
19+
r.relative(to: Int64.min..<Int64.max)
20+
}
21+
1822
extension FileOperationsTest {
1923

2024
func testFileLocks() throws {
@@ -28,12 +32,24 @@ extension FileOperationsTest {
2832
path, .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite)
2933
let dup_2 = try ofd_2.duplicate()
3034

31-
func testOFDs(one: FileDescriptor.FileLock.Kind, two: FileDescriptor.FileLock.Kind) {
32-
XCTAssertEqual(one, try ofd_1.getConflictingLock())
33-
XCTAssertEqual(one, try dup_1.getConflictingLock())
34-
35-
XCTAssertEqual(two, try ofd_2.getConflictingLock())
36-
XCTAssertEqual(two, try dup_2.getConflictingLock())
35+
func testOFDs(
36+
one: FileDescriptor.FileLock.Kind,
37+
two: FileDescriptor.FileLock.Kind,
38+
byteRange: Range<Int64>? = nil
39+
) {
40+
if let br = byteRange {
41+
XCTAssertEqual(one, try ofd_1.getConflictingLock(byteRange: br))
42+
XCTAssertEqual(one, try dup_1.getConflictingLock(byteRange: br))
43+
44+
XCTAssertEqual(two, try ofd_2.getConflictingLock(byteRange: br))
45+
XCTAssertEqual(two, try dup_2.getConflictingLock(byteRange: br))
46+
} else {
47+
XCTAssertEqual(one, try ofd_1.getConflictingLock())
48+
XCTAssertEqual(one, try dup_1.getConflictingLock())
49+
50+
XCTAssertEqual(two, try ofd_2.getConflictingLock())
51+
XCTAssertEqual(two, try dup_2.getConflictingLock())
52+
}
3753
}
3854

3955
testOFDs(one: .none, two: .none)
@@ -65,42 +81,32 @@ extension FileOperationsTest {
6581
}
6682

6783
try ofd_1.unlock()
68-
XCTAssertEqual(.read, try ofd_1.getConflictingLock())
69-
XCTAssertEqual(.read, try dup_1.getConflictingLock())
70-
XCTAssertEqual(.none, try ofd_2.getConflictingLock())
71-
XCTAssertEqual(.none, try dup_2.getConflictingLock())
72-
73-
try dup_2.lock(.write, nonBlocking: true)
74-
XCTAssertEqual(.write, try ofd_1.getConflictingLock())
75-
XCTAssertEqual(.write, try dup_1.getConflictingLock())
76-
XCTAssertEqual(.none, try ofd_2.getConflictingLock())
77-
XCTAssertEqual(.none, try dup_2.getConflictingLock())
78-
}
79-
80-
func testFileLockByteRanges() throws {
81-
let path = FilePath("/tmp/\(UUID().uuidString).txt")
84+
try ofd_2.unlock()
85+
testOFDs(one: .none, two: .none)
8286

83-
let ofd_1 = try FileDescriptor.open(
84-
path, .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite)
85-
let dup_1 = try ofd_1.duplicate()
87+
/// Byte ranges
8688

87-
let ofd_2 = try FileDescriptor.open(
88-
path, .readWrite, options: [.create, .truncate], permissions: .ownerReadWrite)
89-
let dup_2 = try ofd_2.duplicate()
89+
try dup_1.lock(byteRange: ..<50)
90+
testOFDs(one: .none, two: .read)
91+
testOFDs(one: .none, two: .none, byteRange: _range(51...))
92+
testOFDs(one: .none, two: .read, byteRange: _range(1..<2))
9093

91-
func testOFDs(
92-
one: FileDescriptor.FileLock.Kind,
93-
two: FileDescriptor.FileLock.Kind,
94-
byteRange range: Range<Int64>
95-
) {
96-
XCTAssertEqual(one, try ofd_1.getConflictingLock(byteRange: range))
97-
XCTAssertEqual(one, try dup_1.getConflictingLock(byteRange: range))
94+
try dup_1.lock(.write, byteRange: 100..<150)
95+
testOFDs(one: .none, two: .write)
96+
testOFDs(one: .none, two: .read, byteRange: 49..<50)
97+
testOFDs(one: .none, two: .none, byteRange: 98..<99)
98+
testOFDs(one: .none, two: .write, byteRange: _range(100...))
9899

99-
XCTAssertEqual(two, try ofd_2.getConflictingLock(byteRange: range))
100-
XCTAssertEqual(two, try dup_2.getConflictingLock(byteRange: range))
101-
}
100+
try dup_1.unlock(byteRange: ..<49)
101+
testOFDs(one: .none, two: .read, byteRange: 49..<50)
102102

103-
// TODO: tests
103+
try dup_1.unlock(byteRange: ..<149)
104+
testOFDs(one: .none, two: .write)
105+
testOFDs(one: .none, two: .none, byteRange: _range(..<149))
106+
testOFDs(one: .none, two: .write, byteRange: 149..<150)
104107

108+
try dup_1.unlock(byteRange: 149..<150)
109+
testOFDs(one: .none, two: .none)
105110
}
106111
}
112+

0 commit comments

Comments
 (0)