forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSPathUtilities.swift
727 lines (610 loc) · 26 KB
/
NSPathUtilities.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_implementationOnly import CoreFoundation
#if os(Windows)
import WinSDK
#elseif canImport(Android)
import Android
#elseif os(WASI)
import WASILibc
#endif
#if os(Windows)
let validPathSeps: [Character] = ["\\", "/"]
#else
let validPathSeps: [Character] = ["/"]
#endif
public func NSTemporaryDirectory() -> String {
FileManager.default.temporaryDirectory.withUnsafeFileSystemRepresentation {
String(cString: $0!)
} + String(validPathSeps[0])
}
extension String {
internal var _startOfLastPathComponent : String.Index {
precondition(!validPathSeps.contains(where: { hasSuffix(String($0)) }) && length > 1)
let startPos = startIndex
var curPos = endIndex
// Find the beginning of the component
while curPos > startPos {
let prevPos = index(before: curPos)
if validPathSeps.contains(self[prevPos]) {
break
}
curPos = prevPos
}
return curPos
}
internal var _startOfPathExtension : String.Index? {
precondition(!validPathSeps.contains(where: { hasSuffix(String($0)) }))
var currentPosition = endIndex
let startOfLastPathComponent = _startOfLastPathComponent
// Find the beginning of the extension
while currentPosition > startOfLastPathComponent {
let previousPosition = index(before: currentPosition)
let character = self[previousPosition]
if validPathSeps.contains(character) {
return nil
} else if character == "." {
if startOfLastPathComponent == previousPosition {
return nil
} else if case let previous2Position = index(before: previousPosition),
previousPosition == index(before: endIndex) &&
previous2Position == startOfLastPathComponent &&
self[previous2Position] == "."
{
return nil
} else {
return currentPosition
}
}
currentPosition = previousPosition
}
return nil
}
internal var isAbsolutePath: Bool {
if hasPrefix("~") { return true }
#if os(Windows)
guard let value = try? FileManager.default._fileSystemRepresentation(withPath: self, {
!PathIsRelativeW($0)
}) else { return false }
return value
#else
return hasPrefix("/")
#endif
}
internal func _stringByAppendingPathComponent(_ str: String, doneAppending : Bool = true) -> String {
if str.isEmpty {
return self
}
if isEmpty {
return str
}
if validPathSeps.contains(where: { hasSuffix(String($0)) }) {
return self + str
}
return self + "/" + str
}
internal func _stringByFixingSlashes(compress : Bool = true, stripTrailing: Bool = true) -> String {
var result = self
if compress {
let startPos = result.startIndex
var endPos = result.endIndex
var curPos = startPos
while curPos < endPos {
if validPathSeps.contains(result[curPos]) {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && validPathSeps.contains(result[afterLastSlashPos]) {
afterLastSlashPos = result.index(after: afterLastSlashPos)
}
if afterLastSlashPos != result.index(after: curPos) {
result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = result.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = result.index(after: curPos)
}
}
}
if stripTrailing && result.length > 1 && validPathSeps.contains(where: {result.hasSuffix(String($0))}) {
result.remove(at: result.index(before: result.endIndex))
}
return result
}
internal func _stringByRemovingPrefix(_ prefix: String) -> String {
guard hasPrefix(prefix) else {
return self
}
var temp = self
temp.removeSubrange(startIndex..<prefix.endIndex)
return temp
}
internal func _tryToRemovePathPrefix(_ prefix: String) -> String? {
guard self != prefix else {
return nil
}
let temp = _stringByRemovingPrefix(prefix)
if FileManager.default.fileExists(atPath: temp) {
return temp
}
return nil
}
}
extension NSString {
public var isAbsolutePath: Bool {
return (self as String).isAbsolutePath
}
public static func path(withComponents components: [String]) -> String {
var result = ""
for comp in components.prefix(components.count - 1) {
result = result._stringByAppendingPathComponent(comp._stringByFixingSlashes(), doneAppending: false)
}
if let last = components.last {
result = result._stringByAppendingPathComponent(last._stringByFixingSlashes(), doneAppending: true)
}
return result
}
public var pathComponents : [String] {
return _pathComponents(self._swiftObject)!
}
public var lastPathComponent : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return fixedSelf
}
return String(fixedSelf.suffix(from: fixedSelf._startOfLastPathComponent))
}
public var deletingLastPathComponent : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf == "/" || fixedSelf == "" {
return fixedSelf
}
switch fixedSelf._startOfLastPathComponent {
// relative path, single component
case fixedSelf.startIndex:
return ""
// absolute path, single component
case fixedSelf.index(after: fixedSelf.startIndex):
return "/"
// all common cases
case let startOfLast:
return String(fixedSelf.prefix(upTo: fixedSelf.index(before: startOfLast)))
}
}
internal func _stringByFixingSlashes(compress : Bool = true, stripTrailing: Bool = true) -> String {
if validPathSeps.contains(where: { String($0) == _swiftObject }) {
return _swiftObject
}
var result = _swiftObject
if compress {
let startPos = result.startIndex
var endPos = result.endIndex
var curPos = startPos
while curPos < endPos {
if validPathSeps.contains(result[curPos]) {
var afterLastSlashPos = curPos
while afterLastSlashPos < endPos && validPathSeps.contains(result[afterLastSlashPos]) {
afterLastSlashPos = result.index(after: afterLastSlashPos)
}
if afterLastSlashPos != result.index(after: curPos) {
result.replaceSubrange(curPos ..< afterLastSlashPos, with: ["/"])
endPos = result.endIndex
}
curPos = afterLastSlashPos
} else {
curPos = result.index(after: curPos)
}
}
}
if stripTrailing && validPathSeps.contains(where: { result.hasSuffix(String($0)) }) {
result.remove(at: result.index(before: result.endIndex))
}
return result
}
internal func _stringByAppendingPathComponent(_ str: String, doneAppending : Bool = true) -> String {
return _swiftObject._stringByAppendingPathComponent(str, doneAppending: doneAppending)
}
public func appendingPathComponent(_ str: String) -> String {
return _stringByAppendingPathComponent(str)
}
public var pathExtension : String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return ""
}
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.suffix(from: extensionPos))
} else {
return ""
}
}
public var deletingPathExtension: String {
let fixedSelf = _stringByFixingSlashes()
if fixedSelf.length <= 1 {
return fixedSelf
}
if let extensionPos = fixedSelf._startOfPathExtension {
return String(fixedSelf.prefix(upTo: fixedSelf.index(before: extensionPos)))
} else {
return fixedSelf
}
}
public func appendingPathExtension(_ str: String) -> String? {
if validPathSeps.contains(where: { str.hasPrefix(String($0)) }) || self == "" || validPathSeps.contains(where: { String($0)._nsObject == self }) {
print("Cannot append extension \(str) to path \(self)")
return nil
}
let result = _swiftObject._stringByFixingSlashes(compress: false, stripTrailing: true) + "." + str
return result._stringByFixingSlashes()
}
public var expandingTildeInPath: String {
guard hasPrefix("~") else {
return _swiftObject
}
let endOfUserName = _swiftObject.firstIndex(where : { validPathSeps.contains($0) }) ?? _swiftObject.endIndex
let startOfUserName = _swiftObject.index(after: _swiftObject.startIndex)
let userName = String(_swiftObject[startOfUserName..<endOfUserName])
let optUserName: String? = userName.isEmpty ? nil : userName
guard let homeDir = NSHomeDirectoryForUser(optUserName) else {
return _swiftObject._stringByFixingSlashes(compress: false, stripTrailing: true)
}
var result = _swiftObject
result.replaceSubrange(_swiftObject.startIndex..<endOfUserName, with: homeDir)
result = result._stringByFixingSlashes(compress: false, stripTrailing: true)
return result
}
#if os(Windows)
public var unixPath: String {
var unprefixed = self as String
// If there is anything before the drive letter, e.g. "\\?\", "\\host\",
// "\??\", etc, remove it.
if isAbsolutePath, let index = unprefixed.firstIndex(of: ":") {
unprefixed.removeSubrange(..<unprefixed.index(before: index))
}
let converted = String(unprefixed.map({ $0 == "\\" ? "/" : $0 }))
return converted._stringByFixingSlashes(stripTrailing: false)
}
#endif
public var standardizingPath: String {
#if os(Windows)
let expanded = unixPath.expandingTildeInPath
#else
let expanded = expandingTildeInPath
#endif
var resolved = expanded._bridgeToObjectiveC().resolvingSymlinksInPath
let automount = "/var/automount"
resolved = resolved._tryToRemovePathPrefix(automount) ?? resolved
return resolved
}
public var resolvingSymlinksInPath: String {
var components = pathComponents
guard !components.isEmpty else {
return _swiftObject
}
// TODO: pathComponents keeps final path separator if any. Check that logic.
if validPathSeps.contains(where: { String($0) == components.last }) && components.count > 1 {
components.removeLast()
}
var resolvedPath = components.removeFirst()
for component in components {
switch component {
case "", ".":
break
case ".." where isAbsolutePath:
resolvedPath = resolvedPath._bridgeToObjectiveC().deletingLastPathComponent
default:
resolvedPath = resolvedPath._bridgeToObjectiveC().appendingPathComponent(component)
if let destination = FileManager.default._tryToResolveTrailingSymlinkInPath(resolvedPath) {
resolvedPath = destination
}
}
}
let privatePrefix = "/private"
resolvedPath = resolvedPath._tryToRemovePathPrefix(privatePrefix) ?? resolvedPath
return resolvedPath
}
public func stringsByAppendingPaths(_ paths: [String]) -> [String] {
if self == "" {
return paths
}
return paths.map(appendingPathComponent)
}
/// - Experiment: This is a draft API currently under consideration for official import into Foundation
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
public func completePath(into outputName: inout String?, caseSensitive flag: Bool, matchesInto outputArray: inout [String], filterTypes: [String]?) -> Int {
let path = _swiftObject
guard !path.isEmpty else {
return 0
}
let url = URL(fileURLWithPath: path)
let searchAllFilesInDirectory = _stringIsPathToDirectory(path)
let namePrefix = searchAllFilesInDirectory ? "" : url.lastPathComponent
let checkFileName = _getFileNamePredicate(namePrefix, caseSensitive: flag)
let checkExtension = _getExtensionPredicate(filterTypes, caseSensitive: flag)
let resolvedURL: URL = url.resolvingSymlinksInPath()
let urlWhereToSearch: URL = searchAllFilesInDirectory ? resolvedURL : resolvedURL.deletingLastPathComponent()
var matches = _getNamesAtURL(urlWhereToSearch, prependWith: "", namePredicate: checkFileName, typePredicate: checkExtension)
if matches.count == 1 {
let theOnlyFoundItem = URL(fileURLWithPath: matches[0], relativeTo: urlWhereToSearch)
if theOnlyFoundItem.hasDirectoryPath {
matches = _getNamesAtURL(theOnlyFoundItem, prependWith: matches[0], namePredicate: nil, typePredicate: checkExtension)
}
}
let commonPath = searchAllFilesInDirectory ? path : _ensureLastPathSeparator(deletingLastPathComponent)
if searchAllFilesInDirectory {
outputName = "/"
} else {
if let lcp = _longestCommonPrefix(matches, caseSensitive: flag) {
outputName = (commonPath + lcp)
}
}
outputArray = matches.map({ (commonPath + $0) })
return matches.count
}
internal func _stringIsPathToDirectory(_ path: String) -> Bool {
if !validPathSeps.contains(where: { path.hasSuffix(String($0)) }) {
return false
}
var isDirectory: ObjCBool = false
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory.boolValue
}
fileprivate typealias _FileNamePredicate = (String) -> Bool
fileprivate func _getNamesAtURL(_ filePathURL: URL, prependWith: String, namePredicate: _FileNamePredicate?, typePredicate: _FileNamePredicate?) -> [String] {
var result: [String] = []
if let enumerator = FileManager.default.enumerator(at: filePathURL, includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants, errorHandler: nil) {
for item in enumerator.lazy.map({ $0 as! URL }) {
let itemName = item.lastPathComponent
if let predicate = namePredicate, !predicate(itemName) { continue }
if let predicate = typePredicate, !predicate(item.pathExtension) { continue }
if prependWith.isEmpty {
result.append(itemName)
} else {
result.append(prependWith._bridgeToObjectiveC().appendingPathComponent(itemName))
}
}
}
return result
}
fileprivate func _getExtensionPredicate(_ extensions: [String]?, caseSensitive: Bool) -> _FileNamePredicate? {
guard let exts = extensions else {
return nil
}
if caseSensitive {
let set = Set(exts)
return { set.contains($0) }
} else {
let set = Set(exts.map { $0.lowercased() })
return {set.contains($0.lowercased()) }
}
}
fileprivate func _getFileNamePredicate(_ prefix: String, caseSensitive: Bool) -> _FileNamePredicate? {
guard !prefix.isEmpty else {
return nil
}
if caseSensitive {
return { $0.hasPrefix(prefix) }
} else {
let prefix = prefix.lowercased()
return { $0.lowercased().hasPrefix(prefix) }
}
}
internal func _longestCommonPrefix(_ strings: [String], caseSensitive: Bool) -> String? {
guard !strings.isEmpty else {
return nil
}
guard strings.count > 1 else {
return strings.first
}
var sequences = strings.map({ $0.makeIterator() })
var prefix: [Character] = []
loop: while true {
var char: Character? = nil
for (idx, s) in sequences.enumerated() {
var seq = s
guard let c = seq.next() else {
break loop
}
if let char = char {
let lhs = caseSensitive ? char : String(char).lowercased().first!
let rhs = caseSensitive ? c : String(c).lowercased().first!
if lhs != rhs {
break loop
}
} else {
char = c
}
sequences[idx] = seq
}
prefix.append(char!)
}
return String(prefix)
}
internal func _ensureLastPathSeparator(_ path: String) -> String {
if validPathSeps.contains(where: { path.hasSuffix(String($0)) }) || path.isEmpty {
return path
}
return path + "/"
}
public var fileSystemRepresentation: UnsafePointer<Int8> {
return FileManager.default.fileSystemRepresentation(withPath: self._swiftObject)
}
public func getFileSystemRepresentation(_ cname: UnsafeMutablePointer<Int8>, maxLength max: Int) -> Bool {
#if os(Windows)
let fsr = UnsafeMutablePointer<WCHAR>.allocate(capacity: max)
defer { fsr.deallocate() }
guard _getFileSystemRepresentation(fsr, maxLength: max) else { return false }
return String(decodingCString: fsr, as: UTF16.self).withCString() {
let chars = strnlen_s($0, max)
guard chars < max else { return false }
cname.update(from: $0, count: chars + 1)
return true
}
#else
return _getFileSystemRepresentation(cname, maxLength: max)
#endif
}
internal func _getFileSystemRepresentation(_ cname: UnsafeMutablePointer<NativeFSRCharType>, maxLength max: Int) -> Bool {
guard self.length > 0 else {
return false
}
#if os(Windows)
var fsr = self._swiftObject
// If we have a RFC8089 style path, e.g. `/[drive-letter]:/...`, drop
// the leading '/', otherwise, a leading slash indicates a rooted path
// on the drive for the current working directory.
if fsr.count >= 3 {
let index0 = fsr.startIndex
let index1 = fsr.index(after: index0)
let index2 = fsr.index(after: index1)
if fsr[index0] == "/" && fsr[index1].isLetter && fsr[index2] == ":" {
fsr.removeFirst()
}
}
// Windows APIs that go through the path parser can handle forward
// slashes in paths. However, symlinks created with forward slashes
// do not resolve properly, so we normalize the path separators anyways.
fsr = fsr.replacingOccurrences(of: "/", with: "\\")
// Drop trailing slashes unless it follows a drive letter. On Windows,
// the path `C:\` indicates the root directory of the `C:` drive. The
// path `C:` indicates the current working directory on the `C:` drive.
while fsr.count > 1 &&
fsr[fsr.index(before: fsr.endIndex)] == "\\" &&
!(fsr.count == 3 &&
fsr[fsr.index(fsr.endIndex, offsetBy: -2)] == ":" &&
fsr[fsr.index(fsr.endIndex, offsetBy: -3)].isLetter) {
fsr.removeLast()
}
return fsr.withCString(encodedAs: UTF16.self) {
let wchars = wcsnlen_s($0, max)
guard wchars < max else { return false }
cname.update(from: $0, count: wchars + 1)
return true
}
#else
return CFStringGetFileSystemRepresentation(self._cfObject, cname, max)
#endif
}
}
public func NSSearchPathForDirectoriesInDomains(_ directory: FileManager.SearchPathDirectory, _ domainMask: FileManager.SearchPathDomainMask, _ expandTilde: Bool) -> [String] {
let knownDomains: [FileManager.SearchPathDomainMask] = [
.userDomainMask,
.networkDomainMask,
.localDomainMask,
.systemDomainMask,
]
var result: [URL] = []
for domain in knownDomains {
if domainMask.contains(domain) {
result.append(contentsOf: FileManager.default.urls(for: directory, in: domain))
}
}
return result.map { (url) in
var path = url.absoluteURL.path
if expandTilde {
path = NSString(string: path).expandingTildeInPath
}
return path
}
}
public func NSHomeDirectory() -> String {
FileManager.default.homeDirectoryForCurrentUser.withUnsafeFileSystemRepresentation {
String(cString: $0!)
}
}
public func NSHomeDirectoryForUser(_ user: String?) -> String? {
guard let user else { return NSHomeDirectory() }
return FileManager.default.homeDirectory(forUser: user)?.withUnsafeFileSystemRepresentation {
String(cString: $0!)
}
}
public func NSUserName() -> String {
let userName = CFCopyUserName().takeRetainedValue()
return userName._swiftObject
}
public func NSFullUserName() -> String {
let userName = CFCopyFullUserName()
return userName._swiftObject
}
internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, String) {
#if os(Windows)
let maxLength: Int = Int(MAX_PATH + 1)
var buf: [UInt16] = Array<UInt16>(repeating: 0, count: maxLength)
let length = GetTempPathW(DWORD(MAX_PATH), &buf)
precondition(length <= MAX_PATH - 14, "temp path too long")
guard "SCF".withCString(encodedAs: UTF16.self, {
return GetTempFileNameW(buf, $0, 0, &buf) != 0
}) else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
let pathResult = FileManager.default.string(withFileSystemRepresentation: String(decoding: buf, as: UTF16.self), length: wcslen(buf))
guard let h = CreateFileW(buf, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nil),
h != INVALID_HANDLE_VALUE else {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
}
// Don't close h, fd is transferred ownership
let fd = _open_osfhandle(intptr_t(bitPattern: h), 0)
return (fd, pathResult)
#elseif os(WASI)
// WASI does not have temp directories
throw NSError(domain: NSPOSIXErrorDomain, code: Int(ENOTSUP))
#else
var template = URL(fileURLWithPath: filePath)
let filename = template.lastPathComponent
let hashed = String(format: "%llx", Int64(filename.hashValue))
template.deleteLastPathComponent()
template.appendPathComponent("SCF.\(hashed).tmp.XXXXXX")
let (fd, errorCode, pathResult) = template.withUnsafeFileSystemRepresentation { ptr -> (Int32, Int32, String) in
let length = strlen(ptr!)
// buffer is updated with the temp file name on success.
let buffer = UnsafeMutableBufferPointer<CChar>.allocate(capacity: length + 1 /* the null character */)
UnsafeRawBufferPointer(start: ptr!, count: length + 1 /* the null character */)
.copyBytes(to: UnsafeMutableRawBufferPointer(buffer))
defer { buffer.deallocate() }
let fd = mkstemp(buffer.baseAddress!)
let errorCode = errno
return (fd,
errorCode,
FileManager.default.string(withFileSystemRepresentation: buffer.baseAddress!, length: strlen(buffer.baseAddress!)))
}
if fd == -1 {
throw _NSErrorWithErrno(errorCode, reading: false, path: pathResult)
}
// Set the file mode to match macOS
guard fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != -1 else {
let _errno = errno
close(fd)
throw _NSErrorWithErrno(_errno, reading: false, path: pathResult)
}
return (fd, pathResult)
#endif
}
internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String) throws {
#if os(Windows)
try withNTPathRepresentation(of: auxFilePath) { pwszSource in
try withNTPathRepresentation(of: filePath) { pwszDestination in
guard MoveFileExW(pwszSource, pwszDestination,
MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) else {
let dwErrorCode = GetLastError()
try? FileManager.default.removeItem(atPath: auxFilePath)
throw _NSErrorWithWindowsError(dwErrorCode, reading: false)
}
}
}
#else
try FileManager.default._fileSystemRepresentation(withPath: auxFilePath, andPath: filePath, {
if rename($0, $1) != 0 {
let errorCode = errno
try? FileManager.default.removeItem(atPath: auxFilePath)
throw _NSErrorWithErrno(errorCode, reading: false, path: filePath)
}
})
#endif
}