Skip to content

Commit 6b073bc

Browse files
committed
Using UnsafePointer instead [UInt16]
1 parent 5fc99e3 commit 6b073bc

File tree

1 file changed

+68
-58
lines changed

1 file changed

+68
-58
lines changed

Foundation/FileManager.swift

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,9 @@ open class FileManager : NSObject {
137137
let modeT = number.uint32Value
138138
#endif
139139
#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+
}) {
144143
fatalError("errno \(errno)")
145144
}
146145
#else
@@ -159,10 +158,6 @@ open class FileManager : NSObject {
159158
This method replaces createDirectoryAtPath:attributes:
160159
*/
161160
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
166161
if createIntermediates {
167162
var isDir: ObjCBool = false
168163
if !fileExists(atPath: path, isDirectory: &isDir) {
@@ -171,7 +166,9 @@ open class FileManager : NSObject {
171166
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
172167
}
173168
#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+
})
175172
#else
176173
let mkdir_failed = mkdir(pathh, S_IRWXU | S_IRWXG | S_IRWXO) != 0
177174
#endif
@@ -187,7 +184,9 @@ open class FileManager : NSObject {
187184
}
188185
} else {
189186
#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+
})
191190
#else
192191
let mkdir_failed = mkdir(pathh, S_IRWXU | S_IRWXG | S_IRWXO) != 0
193192
#endif
@@ -215,10 +214,9 @@ open class FileManager : NSObject {
215214
open func contentsOfDirectory(atPath path: String) throws -> [String] {
216215
var contents : [String] = [String]()
217216
#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+
})
222220
#else
223221
let dir = opendir(path)
224222
#endif
@@ -280,10 +278,10 @@ open class FileManager : NSObject {
280278
open func subpathsOfDirectory(atPath path: String) throws -> [String] {
281279
var contents : [String] = [String]()
282280
#if CAN_IMPORT_MINGWCRT
283-
var wcpath = [UInt16](path.utf16)
284-
wcpath.append(UInt16(0))
285281

286-
let dir = _wopendir(wcpath)
282+
let dir = path.withCString(encodedAs:UTF16.self, {pointer in
283+
return _wopendir(pointer)
284+
})
287285
#else
288286

289287
let dir = opendir(path)
@@ -328,13 +326,22 @@ open class FileManager : NSObject {
328326
contents.append(entryName)
329327

330328
#if CAN_IMPORT_MINGWCRT
331-
let subPath: String = path + "/" + entryName
332-
var isDir = false
329+
do {
333330

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+
}
338345
}
339346
#else
340347
let entryType = withUnsafePointer(to: &entry!.pointee.d_type) { (ptr) -> Int32 in
@@ -370,11 +377,11 @@ open class FileManager : NSObject {
370377
*/
371378
open func attributesOfItem(atPath path: String) throws -> [FileAttributeKey : Any] {
372379
#if CAN_IMPORT_MINGWCRT
373-
var wcpath = [UInt16](path.utf16)
374-
wcpath.append(UInt16(0))
375-
376380
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 {
378385
throw _NSErrorWithErrno(errno, reading: true, path: path)
379386
}
380387
#else
@@ -520,12 +527,12 @@ open class FileManager : NSObject {
520527
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteFileExists.rawValue, userInfo: [NSFilePathErrorKey : NSString(dstPath)])
521528
}
522529
#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))
527530

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+
}) {
529536
throw _NSErrorWithErrno(errno, reading: false, path: srcPath)
530537
}
531538
#else
@@ -561,28 +568,29 @@ open class FileManager : NSObject {
561568

562569
open func removeItem(atPath path: String) throws {
563570
#if CAN_IMPORT_MINGWCRT
564-
var wcpath = [UInt16](path.utf16)
565-
wcpath.append(UInt16(0))
566-
567571
var s = _stat64()
568-
guard _wstat64(wcpath, &s) == 0 else {
569-
throw _NSErrorWithErrno(errno, reading: true, path: path)
570-
}
571572

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+
}
577577

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+
}
581583

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)
586594
}
587595
#else
588596
if rmdir(path) == 0 {
@@ -696,10 +704,9 @@ open class FileManager : NSObject {
696704
@discardableResult
697705
open func changeCurrentDirectoryPath(_ path: String) -> Bool {
698706
#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+
})
703710
#else
704711
return chdir(path) == 0
705712
#endif
@@ -713,14 +720,17 @@ open class FileManager : NSObject {
713720

714721
open func fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool {
715722
#if CAN_IMPORT_MINGWCRT
716-
var wcpath = [UInt16](path.utf16)
717-
wcpath.append(UInt16(0))
718723
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+
720728
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
722730
}
731+
723732
} else {
733+
724734
return false
725735
}
726736

0 commit comments

Comments
 (0)