Skip to content

Commit

Permalink
Merge pull request #232 from jrose-apple/optional-pointers
Browse files Browse the repository at this point in the history
Update for SE-0055: Making pointer nullability explicit. See swiftlang/swift#1878.
  • Loading branch information
jrose-apple committed Apr 12, 2016
2 parents 903309e + 442f62a commit e8cd7d7
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
5 changes: 3 additions & 2 deletions Sources/POSIX/fopen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public enum FopenMode: String {
}

public func fopen(_ path: String, mode: FopenMode = .Read) throws -> UnsafeMutablePointer<FILE> {
let f = libc.fopen(path, mode.rawValue)
guard f != nil else { throw SystemError.fopen(errno, path) }
guard let f = libc.fopen(path, mode.rawValue) else {
throw SystemError.fopen(errno, path)
}
return f
}
5 changes: 3 additions & 2 deletions Sources/POSIX/opendir.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import var libc.errno
import func libc.opendir

public func opendir(_ path: String) throws -> DirHandle {
let d = libc.opendir(path)
guard d != nil else { throw SystemError.opendir(errno, path) }
guard let d = libc.opendir(path) else {
throw SystemError.opendir(errno, path)
}
return d
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/POSIX/popen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public func popen(_ arguments: [String], redirectStandardError: Bool = false, en

// Create the file actions to use for spawning.
#if os(OSX)
var fileActions: posix_spawn_file_actions_t = nil
var fileActions: posix_spawn_file_actions_t? = nil
#else
var fileActions = posix_spawn_file_actions_t()
#endif
Expand Down
15 changes: 10 additions & 5 deletions Sources/POSIX/system.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ public func system(_ arguments: [String], environment: [String:String] = [:]) th
public func system() {}


#if os(OSX)
typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t?
#else
typealias swiftpm_posix_spawn_file_actions_t = posix_spawn_file_actions_t
#endif

/// Convenience wrapper for posix_spawn.
func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: posix_spawn_file_actions_t? = nil) throws -> pid_t {
let argv = args.map{ $0.withCString(strdup) }
defer { for arg in argv { free(arg) } }
func posix_spawnp(_ path: String, args: [String], environment: [String: String] = [:], fileActions: swiftpm_posix_spawn_file_actions_t? = nil) throws -> pid_t {
let argv: [UnsafeMutablePointer<CChar>?] = args.map{ $0.withCString(strdup) }
defer { for case let arg? in argv { free(arg) } }

var environment = environment
#if Xcode
Expand All @@ -60,8 +65,8 @@ func posix_spawnp(_ path: String, args: [String], environment: [String: String]
}
}

let env = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) }
defer { env.forEach{ free($0) } }
let env: [UnsafeMutablePointer<CChar>?] = environment.map{ "\($0.0)=\($0.1)".withCString(strdup) }
defer { for case let arg? in env { free(arg) } }

var pid = pid_t()
let rv: Int32
Expand Down
11 changes: 8 additions & 3 deletions Sources/PackageDescription/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ public final class Package {
// to get the interpreter to dump the package when attempting to load a
// manifest.

if let fileNoOptIndex = Process.arguments.index(of: "-fileno"),
fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) {
dumpPackageAtExit(self, fileNo: fileNo)
// FIXME: Additional hackery here to avoid accessing 'arguments' in a
// process whose 'main' isn't generated by Swift.
// See https://bugs.swift.org/browse/SR-1119.
if Process.argc > 0 {
if let fileNoOptIndex = Process.arguments.index(of: "-fileno"),
fileNo = Int32(Process.arguments[fileNoOptIndex + 1]) {
dumpPackageAtExit(self, fileNo: fileNo)
}
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/Utility/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public class FileLineGenerator: IteratorProtocol, Sequence {
}

deinit {
if fp != nil {
fclose(fp)
}
fclose(fp)
}

public func next() -> String? {
Expand Down
6 changes: 2 additions & 4 deletions Sources/Utility/POSIXConvenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import POSIX

public func fopen(_ path: String..., mode: FopenMode = .Read, body: (UnsafeMutablePointer<FILE>) throws -> Void) throws {
var fp = try POSIX.fopen(Path.join(path), mode: mode)
defer { if fp != nil { fclose(fp) } }
let fp = try POSIX.fopen(Path.join(path), mode: mode)
defer { fclose(fp) }
try body(fp)
fclose(fp) // defer is not necessarily immediate
fp = nil
}

@_exported import func POSIX.fputs
Expand Down
10 changes: 5 additions & 5 deletions Sources/Utility/walk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public func walk(_ paths: String..., recursing: (String) -> Bool) -> RecursibleD
A generator for a single directory’s contents
*/
private class DirectoryContentsGenerator: IteratorProtocol {
private let dirptr: DirHandle
private let dirptr: DirHandle?
private let path: String

private init(path: String) {
Expand All @@ -72,16 +72,16 @@ private class DirectoryContentsGenerator: IteratorProtocol {
}

deinit {
if dirptr != nil { closedir(dirptr) }
if let openeddir = dirptr { closedir(openeddir) }
}

func next() -> dirent? {
if dirptr == nil { return nil } // yuck, silently ignoring the error to maintain this pattern
guard let validdir = dirptr else { return nil } // yuck, silently ignoring the error to maintain this pattern

while true {
var entry = dirent()
var result: UnsafeMutablePointer<dirent> = nil
guard readdir_r(dirptr, &entry, &result) == 0 else { continue }
var result: UnsafeMutablePointer<dirent>? = nil
guard readdir_r(validdir, &entry, &result) == 0 else { continue }
guard result != nil else { return nil }

switch (Int32(entry.d_type), entry.d_name.0, entry.d_name.1, entry.d_name.2) {
Expand Down

0 comments on commit e8cd7d7

Please sign in to comment.