@@ -137,10 +137,9 @@ open class FileManager : NSObject {
137
137
let modeT = number.uint32Value
138
138
#endif
139
139
#if CAN_IMPORT_MINGWCRT
140
- var wcpath = [UInt16](path.utf16)
141
- wcpath.append(UInt16(0))
142
-
143
- if _wchmod(wcpath, Int32(mode_t(modeT))) != 0 {
140
+ if path.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
141
+ return _wchmod(pointer, Int32(mode_t(modeT))) != 0
142
+ }) {
144
143
fatalError("errno \(errno)")
145
144
}
146
145
#else
@@ -159,10 +158,6 @@ open class FileManager : NSObject {
159
158
This method replaces createDirectoryAtPath:attributes:
160
159
*/
161
160
open func createDirectory(atPath path: String, withIntermediateDirectories createIntermediates: Bool, attributes: [FileAttributeKey : Any]? = [:]) throws {
162
- #if CAN_IMPORT_MINGWCRT
163
- var wcpath = [UInt16](path.utf16)
164
- wcpath.append(UInt16(0))
165
- #endif
166
161
if createIntermediates {
167
162
var isDir: ObjCBool = false
168
163
if !fileExists(atPath: path, isDirectory: &isDir) {
@@ -171,7 +166,9 @@ open class FileManager : NSObject {
171
166
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
172
167
}
173
168
#if CAN_IMPORT_MINGWCRT
174
- let mkdir_failed = _wmkdir(wcpath) != 0
169
+ let mkdir_failed = path.withCString(encodedAs:UTF16.self, {pointer in
170
+ return _wmkdir(pointer) != 0
171
+ })
175
172
#else
176
173
let mkdir_failed = mkdir(pathh, S_IRWXU | S_IRWXG | S_IRWXO) != 0
177
174
#endif
@@ -187,7 +184,9 @@ open class FileManager : NSObject {
187
184
}
188
185
} else {
189
186
#if CAN_IMPORT_MINGWCRT
190
- let mkdir_failed = _wmkdir(wcpath) != 0
187
+ let mkdir_failed = path.withCString(encodedAs:UTF16.self, {pointer in
188
+ return _wmkdir(pointer) != 0
189
+ })
191
190
#else
192
191
let mkdir_failed = mkdir(pathh, S_IRWXU | S_IRWXG | S_IRWXO) != 0
193
192
#endif
@@ -215,10 +214,9 @@ open class FileManager : NSObject {
215
214
open func contentsOfDirectory(atPath path: String) throws -> [String] {
216
215
var contents : [String] = [String]()
217
216
#if CAN_IMPORT_MINGWCRT
218
- var wcpath = [UInt16](path.utf16)
219
- wcpath.append(UInt16(0))
220
-
221
- let dir = _wopendir(wcpath)
217
+ let dir = path.withCString(encodedAs:UTF16.self, {pointer in
218
+ return _wopendir(pointer)
219
+ })
222
220
#else
223
221
let dir = opendir(path)
224
222
#endif
@@ -280,10 +278,10 @@ open class FileManager : NSObject {
280
278
open func subpathsOfDirectory(atPath path: String) throws -> [String] {
281
279
var contents : [String] = [String]()
282
280
#if CAN_IMPORT_MINGWCRT
283
- var wcpath = [UInt16](path.utf16)
284
- wcpath.append(UInt16(0))
285
281
286
- let dir = _wopendir(wcpath)
282
+ let dir = path.withCString(encodedAs:UTF16.self, {pointer in
283
+ return _wopendir(pointer)
284
+ })
287
285
#else
288
286
289
287
let dir = opendir(path)
@@ -328,13 +326,22 @@ open class FileManager : NSObject {
328
326
contents.append(entryName)
329
327
330
328
#if CAN_IMPORT_MINGWCRT
331
- let subPath: String = path + "/" + entryName
332
- var isDir = false
329
+ do {
333
330
334
- if fileExists(atPath:subPath, isDirectory:&isDir) && isDir {
335
- print("\(subPath) isDir")
336
- let entries = try subpathsOfDirectory(atPath: subPath)
337
- contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
331
+ let subPath: String = path + "/" + entryName
332
+ let isDir:Bool = subPath.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
333
+ var s = _stat64()
334
+ guard _wstat64(pointer, &s) >= 0 else {
335
+ return false
336
+ }
337
+
338
+ return (Int32(s.st_mode) & S_IFMT) == S_IFDIR
339
+ })
340
+
341
+ if isDir {
342
+ let entries = try subpathsOfDirectory(atPath: subPath)
343
+ contents.append(contentsOf: entries.map({file in "\(entryName)/\(file)"}))
344
+ }
338
345
}
339
346
#else
340
347
let entryType = withUnsafePointer(to: &entry!.pointee.d_type) { (ptr) -> Int32 in
@@ -370,11 +377,11 @@ open class FileManager : NSObject {
370
377
*/
371
378
open func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
372
379
#if CAN_IMPORT_MINGWCRT
373
- var wcpath = [UInt16](path.utf16)
374
- wcpath.append(UInt16(0))
375
-
376
380
var s = _stat64()
377
- guard _wstat64(wcpath, &s) == 0 else {
381
+
382
+ guard path.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
383
+ return _wstat64(pointer, &s) == 0
384
+ }) else {
378
385
throw _NSErrorWithErrno(errno, reading: true, path: path)
379
386
}
380
387
#else
@@ -520,12 +527,12 @@ open class FileManager : NSObject {
520
527
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteFileExists.rawValue, userInfo: [NSFilePathErrorKey : NSString(dstPath)])
521
528
}
522
529
#if CAN_IMPORT_MINGWCRT
523
- var wcSrcPath = [UInt16](srcPath.utf16)
524
- wcSrcPath.append(UInt16(0))
525
- var wcDstPath = [UInt16](dstPath.utf16)
526
- wcDstPath.append(UInt16(0))
527
530
528
- if _wrename(wcSrcPath, wcDstPath) != 0 {
531
+ if !srcPath.withCString(encodedAs:UTF16.self, {(srcPointer) -> Bool in
532
+ return dstPath.withCString(encodedAs:UTF16.self, {(dstPointer) -> Bool in
533
+ return _wrename(srcPointer, dstPointer) != 0
534
+ })
535
+ }) {
529
536
throw _NSErrorWithErrno(errno, reading: false, path: srcPath)
530
537
}
531
538
#else
@@ -561,28 +568,29 @@ open class FileManager : NSObject {
561
568
562
569
open func removeItem(atPath path: String) throws {
563
570
#if CAN_IMPORT_MINGWCRT
564
- var wcpath = [UInt16](path.utf16)
565
- wcpath.append(UInt16(0))
566
-
567
571
var s = _stat64()
568
- guard _wstat64(wcpath, &s) == 0 else {
569
- throw _NSErrorWithErrno(errno, reading: true, path: path)
570
- }
571
572
572
- if (Int32(s.st_mode) & S_IFMT) == S_IFDIR {
573
- let subpaths = try subpathsOfDirectory(atPath: path)
574
- for subpath in subpaths {
575
- try removeItem(atPath: path + "/" + subpath)
576
- }
573
+ guard try path.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
574
+ guard _wstat64(pointer, &s) >= 0 else {
575
+ return false
576
+ }
577
577
578
- if _wrmdir(wcpath) != 0 {
579
- throw _NSErrorWithErrno(errno, reading: false, path: path)
580
- }
578
+ if (Int32(s.st_mode) & S_IFMT) == S_IFDIR {
579
+ let subpaths = try subpathsOfDirectory(atPath: path)
580
+ for subpath in subpaths {
581
+ try removeItem(atPath: path + "/" + subpath)
582
+ }
581
583
582
- } else {
583
- if _wunlink(wcpath) != 0 {
584
- throw _NSErrorWithErrno(errno, reading: false, path: path)
585
- }
584
+ return _wrmdir(pointer) == 0
585
+
586
+ } else {
587
+
588
+ return _wunlink(pointer) == 0
589
+ }
590
+
591
+ }) else {
592
+
593
+ throw _NSErrorWithErrno(errno, reading: true, path: path)
586
594
}
587
595
#else
588
596
if rmdir(path) == 0 {
@@ -696,10 +704,9 @@ open class FileManager : NSObject {
696
704
@discardableResult
697
705
open func changeCurrentDirectoryPath(_ path: String) -> Bool {
698
706
#if CAN_IMPORT_MINGWCRT
699
- var wcpath = [UInt16](path.utf16)
700
- wcpath.append(UInt16(0))
701
-
702
- return _wchdir(wcpath) == 0
707
+ return path.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
708
+ return _wchdir(pointer) == 0
709
+ })
703
710
#else
704
711
return chdir(path) == 0
705
712
#endif
@@ -713,14 +720,17 @@ open class FileManager : NSObject {
713
720
714
721
open func fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
715
722
#if CAN_IMPORT_MINGWCRT
716
- var wcpath = [UInt16](path.utf16)
717
- wcpath.append(UInt16(0))
718
723
var s = _stat64()
719
- if _wstat64(wcpath, &s) >= 0 {
724
+ if path.withCString(encodedAs:UTF16.self, {(pointer) -> Bool in
725
+ return _wstat64(pointer, &s) >= 0
726
+ }) {
727
+
720
728
if let isDirectory = isDirectory {
721
- isDirectory.pointee = (Int32(s.st_mode) & S_IFMT) == S_IFDIR
729
+ isDirectory.pointee = (Int32(s.st_mode) & S_IFMT) == S_IFDIR
722
730
}
731
+
723
732
} else {
733
+
724
734
return false
725
735
}
726
736
0 commit comments