1
+ //===----------------------------------------------------------------------===//
1
2
//
2
- // PID.swift
3
- // SwiftPM
3
+ // This source file is part of the Swift open source project
4
4
//
5
- // Created by John Bute on 2025-04-24.
5
+ // Copyright (c) 2023 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
6
7
//
8
+ // See http://swift.org/LICENSE.txt for license information
9
+ // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
7
12
8
13
import Foundation
9
14
10
15
public protocol PIDFileHandler {
11
- var scratchDirectory : AbsolutePath { get set }
16
+ var scratchDirectory : AbsolutePath { get set }
12
17
13
18
init ( scratchDirectory: AbsolutePath )
14
-
19
+
15
20
func readPID( ) -> Int32 ?
16
21
func deletePIDFile( ) throws
17
22
func writePID( pid: pid_t ) throws
18
23
func getCurrentPID( ) -> Int32
19
24
}
20
25
21
-
22
-
23
26
public struct PIDFile : PIDFileHandler {
24
-
25
27
public var scratchDirectory : AbsolutePath
26
-
28
+
27
29
public init ( scratchDirectory: AbsolutePath ) {
28
30
self . scratchDirectory = scratchDirectory
29
31
}
30
-
32
+
31
33
/// Return the path of the PackageManager.lock.pid file where the PID is located
32
34
private var lockFilePath : AbsolutePath {
33
- return self . scratchDirectory. appending ( component: " PackageManager.lock.pid " )
35
+ self . scratchDirectory. appending ( component: " PackageManager.lock.pid " )
34
36
}
35
37
36
38
/// Read the pid file
37
39
public func readPID( ) -> Int32 ? {
38
40
// Check if the file exists
39
- let filePath = lockFilePath. pathString
40
- guard FileManager . default. fileExists ( atPath: filePath) else {
41
+ let filePath = self . lockFilePath. pathString
42
+ guard FileManager . default. fileExists ( atPath: filePath) else {
41
43
print ( " File does not exist at path: \( filePath) " )
42
44
return nil
43
45
}
44
46
45
47
do {
46
48
// Read the contents of the file
47
- let pidString = try String ( contentsOf: lockFilePath. asURL, encoding: . utf8) . trimmingCharacters ( in: . whitespacesAndNewlines)
49
+ let pidString = try String ( contentsOf: lockFilePath. asURL, encoding: . utf8)
50
+ . trimmingCharacters ( in: . whitespacesAndNewlines)
48
51
49
52
// Check if the PID string can be converted to an Int32
50
53
if let pid = Int32 ( pidString) {
@@ -60,17 +63,16 @@ public struct PIDFile: PIDFileHandler {
60
63
61
64
/// Get the current PID of the process
62
65
public func getCurrentPID( ) -> Int32 {
63
- return getpid ( )
66
+ getpid ( )
64
67
}
65
68
66
69
/// Write .pid file containing PID of process currently using .build directory
67
70
public func writePID( pid: pid_t ) throws {
68
- try " \( pid) " . write ( to: lockFilePath. asURL, atomically: true , encoding: . utf8)
71
+ try " \( pid) " . write ( to: self . lockFilePath. asURL, atomically: true , encoding: . utf8)
69
72
}
70
-
73
+
71
74
/// Delete PID file at URL
72
75
public func deletePIDFile( ) throws {
73
- try FileManager . default. removeItem ( at: lockFilePath. asURL)
76
+ try FileManager . default. removeItem ( at: self . lockFilePath. asURL)
74
77
}
75
-
76
78
}
0 commit comments