@@ -741,32 +741,38 @@ extension FileManager {
741741 if rmdir ( fsRep) == 0 {
742742 return
743743 } 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
744749 let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
745750 ps. initialize ( to: UnsafeMutablePointer ( mutating: fsRep) )
746751 ps. advanced ( by: 1 ) . initialize ( to: nil )
752+ #endif
747753 let stream = fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil )
748754 ps. deinitialize ( count: 2 )
749755 ps. deallocate ( )
750756
751- if stream != nil {
757+ if let openStream = stream {
752758 defer {
753- fts_close ( stream )
759+ fts_close ( openStream )
754760 }
755761
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) )
758764 guard alreadyConfirmed || shouldRemoveItemAtPath ( itemPath, isURL: isURL) else {
759765 continue
760766 }
761767
762768 do {
763769 switch Int32 ( current. fts_info) {
764770 case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
765- if unlink ( current . fts_path ) == - 1 {
771+ if unlink ( current_path ) == - 1 {
766772 throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
767773 }
768774 case FTS_DP:
769- if rmdir ( current . fts_path ) == - 1 {
775+ if rmdir ( current_path ) == - 1 {
770776 throw _NSErrorWithErrno ( errno, reading: false , path: itemPath)
771777 }
772778 case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1085,10 +1091,18 @@ extension FileManager {
10851091 do {
10861092 guard fm. fileExists ( atPath: _url. path) else { throw _NSErrorWithErrno ( ENOENT, reading: true , url: url) }
10871093 _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
10881097 let ps = UnsafeMutablePointer< UnsafeMutablePointer< Int8>?> . allocate( capacity: 2 )
1098+ #endif
10891099 defer { ps. deallocate ( ) }
10901100 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
10911104 ps. advanced ( by: 1 ) . initialize ( to: nil )
1105+ #endif
10921106 return fts_open ( ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil )
10931107 }
10941108 if _stream == nil {
@@ -1135,14 +1149,14 @@ extension FileManager {
11351149 }
11361150
11371151 _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) )
11401154
11411155 switch Int32 ( current. pointee. fts_info) {
11421156 case FTS_D:
11431157 let ( showFile, skipDescendants) = match ( filename: filename, to: _options, isDir: true )
11441158 if skipDescendants {
1145- fts_set ( _stream , _current , FTS_SKIP)
1159+ fts_set ( stream , current , FTS_SKIP)
11461160 }
11471161 if showFile {
11481162 return URL ( fileURLWithPath: filename, isDirectory: true )
@@ -1315,7 +1329,7 @@ extension FileManager {
13151329 let finalErrno = originalItemURL. withUnsafeFileSystemRepresentation { ( originalFS) -> Int32 ? in
13161330 return newItemURL. withUnsafeFileSystemRepresentation { ( newItemFS) -> Int32 ? in
13171331 // 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 {
13191333 return nil
13201334 } else {
13211335 return errno
0 commit comments