@@ -741,32 +741,38 @@ extension FileManager {
741
741
if rmdir ( fsRep) == 0 {
742
742
return
743
743
} else if errno == ENOTEMPTY {
744
+ #if os(Android)
745
+ let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>>. allocate( capacity: 2 )
746
+ ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
747
+ ps. advanced ( by: 1 ) . initialize ( to: unsafeBitCast ( 0 , to: UnsafeMutablePointer< Int8> . self ) )
748
+ #else
744
749
let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
745
750
ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
746
751
ps. advanced ( by: 1 ) . initialize ( to: nil )
752
+ #endif
747
753
let stream = fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil )
748
754
ps. deinitialize ( count: 2 )
749
755
ps. deallocate ( )
750
756
751
- if stream != nil {
757
+ if let openStream = stream {
752
758
defer {
753
- fts_close ( stream )
759
+ fts_close ( openStream )
754
760
}
755
761
756
- while let current = fts_read ( stream ) ? . pointee {
757
- let itemPath = string ( withFileSystemRepresentation: current . fts_path , length: Int ( current. fts_pathlen) )
762
+ while let current = fts_read ( openStream ) ? . pointee, let current_path = current . fts_path {
763
+ let itemPath = string ( withFileSystemRepresentation: current_path , length: Int ( current. fts_pathlen) )
758
764
guard alreadyConfirmed || shouldRemoveItemAtPath ( itemPath, isURL: isURL) else {
759
765
continue
760
766
}
761
767
762
768
do {
763
769
switch Int32 ( current. fts_info) {
764
770
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
765
- if unlink ( current . fts_path ) == - 1 {
771
+ if unlink ( current_path ) == - 1 {
766
772
throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
767
773
}
768
774
case FTS_DP:
769
- if rmdir ( current . fts_path ) == - 1 {
775
+ if rmdir ( current_path ) == - 1 {
770
776
throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
771
777
}
772
778
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1085,10 +1091,18 @@ extension FileManager {
1085
1091
do {
1086
1092
guard fm. fileExists ( atPath: _url. path) else { throw _NSErrorWithErrno ( ENOENT, reading: true , url: url) }
1087
1093
_stream = try FileManager . default. _fileSystemRepresentation ( withPath: _url. path) { fsRep in
1094
+ #if os(Android)
1095
+ let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>>. allocate( capacity: 2 )
1096
+ #else
1088
1097
let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
1098
+ #endif
1089
1099
defer { ps. deallocate ( ) }
1090
1100
ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
1101
+ #if os(Android)
1102
+ ps. advanced ( by: 1 ) . initialize ( to: unsafeBitCast ( 0 , to: UnsafeMutablePointer< Int8> . self ) )
1103
+ #else
1091
1104
ps. advanced ( by: 1 ) . initialize ( to: nil )
1105
+ #endif
1092
1106
return fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil )
1093
1107
}
1094
1108
if _stream == nil {
@@ -1135,14 +1149,14 @@ extension FileManager {
1135
1149
}
1136
1150
1137
1151
_current = fts_read ( stream)
1138
- while let current = _current {
1139
- let filename = FileManager . default. string ( withFileSystemRepresentation: current . pointee . fts_path , length: Int ( current. pointee. fts_pathlen) )
1152
+ while let current = _current, let current_path = current . pointee . fts_path {
1153
+ let filename = FileManager . default. string ( withFileSystemRepresentation: current_path , length: Int ( current. pointee. fts_pathlen) )
1140
1154
1141
1155
switch Int32 ( current. pointee. fts_info) {
1142
1156
case FTS_D:
1143
1157
let ( showFile, skipDescendants) = match ( filename: filename, to: _options, isDir: true )
1144
1158
if skipDescendants {
1145
- fts_set ( _stream , _current , FTS_SKIP)
1159
+ fts_set ( stream , current , FTS_SKIP)
1146
1160
}
1147
1161
if showFile {
1148
1162
return URL ( fileURLWithPath: filename, isDirectory: true )
@@ -1315,7 +1329,7 @@ extension FileManager {
1315
1329
let finalErrno = originalItemURL. withUnsafeFileSystemRepresentation { ( originalFS) -> Int32 ? in
1316
1330
return newItemURL. withUnsafeFileSystemRepresentation { ( newItemFS) -> Int32 ? in
1317
1331
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
1318
- if rename ( newItemFS, originalFS) == 0 {
1332
+ if let newFS = newItemFS, let origFS = originalFS, rename ( newFS , origFS ) == 0 {
1319
1333
return nil
1320
1334
} else {
1321
1335
return errno
0 commit comments