Skip to content

Commit fcb547b

Browse files
committed
Added retry for SysProcess EINTR. Added Dir.workingDir
1 parent f064099 commit fcb547b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Sources/PerfectLib/Dir.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,21 @@ public struct Dir {
4444
public var exists: Bool {
4545
return exists(realPath)
4646
}
47+
48+
public func setAsWorkingDir() throws {
49+
let res = chdir(self.internalPath)
50+
guard res == 0 else {
51+
try ThrowFileError()
52+
}
53+
}
4754

55+
public static var workingDir: Dir {
56+
let buffer = Array(repeating: 0 as UInt8, count: 2049)
57+
let _ = getcwd(UnsafeMutablePointer<Int8>(buffer), 2048)
58+
let path = String(validatingUTF8: UnsafeMutablePointer<Int8>(buffer)) ?? "."
59+
return Dir(path)
60+
}
61+
4862
func exists(_ path: String) -> Bool {
4963
return access(path, F_OK) != -1
5064
}

Sources/PerfectLib/SysProcess.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,16 @@ public class SysProcess {
177177
/// Determine if the process has completed running and retrieve its result code.
178178
public func wait(hang: Bool = true) throws -> Int32 {
179179
var code = Int32(0)
180-
let status = waitpid(self.pid, &code, WUNTRACED | (hang ? 0 : WNOHANG))
181-
guard status != -1 else {
182-
try ThrowSystemError()
183-
}
180+
while true {
181+
let status = waitpid(self.pid, &code, WUNTRACED | (hang ? 0 : WNOHANG))
182+
if status == -1 && errno == EINTR {
183+
continue
184+
}
185+
guard status != -1 else {
186+
try ThrowSystemError()
187+
}
188+
break
189+
}
184190
self.pid = -1
185191
close()
186192
return code

0 commit comments

Comments
 (0)