1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if FOUNDATION_FRAMEWORK
internal import _ForSwiftFoundation
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Bionic)
@preconcurrency import Bionic
#elseif canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
@preconcurrency import Musl
#elseif os(Windows)
import CRT
import WinSDK
#elseif os(WASI)
@preconcurrency import WASILibc
#endif
// MARK: - Error Creation with CocoaError.Code
extension CocoaError {
static func errorWithFilePath(_ code: CocoaError.Code, _ path: String, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
CocoaError(code, path: path, variant: variant, source: source, destination: destination)
}
static func errorWithFilePath(_ code: CocoaError.Code, _ url: URL, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
CocoaError(code, url: url, variant: variant, source: source, destination: destination)
}
}
// MARK: - POSIX Errors
extension CocoaError.Code {
fileprivate init(fileErrno: Int32, reading: Bool) {
self = if reading {
switch fileErrno {
case EFBIG: .fileReadTooLarge
case ENOENT: .fileReadNoSuchFile
case EPERM, EACCES: .fileReadNoPermission
case ENAMETOOLONG: .fileReadInvalidFileName
default: .fileReadUnknown
}
} else {
switch fileErrno {
case ENOENT: .fileNoSuchFile
case EPERM, EACCES: .fileWriteNoPermission
case ENAMETOOLONG: .fileWriteInvalidFileName
#if !os(Windows)
case EDQUOT: .fileWriteOutOfSpace
#endif
case ENOSPC: .fileWriteOutOfSpace
case EROFS: .fileWriteVolumeReadOnly
case EEXIST: .fileWriteFileExists
default: .fileWriteUnknown
}
}
}
}
extension POSIXError {
fileprivate init?(errno: Int32) {
// (130280235) POSIXError.Code does not have a case for EOPNOTSUPP
guard errno != EOPNOTSUPP else { return nil }
guard let code = POSIXError.Code(rawValue: errno) else {
fatalError("Invalid posix errno \(errno)")
}
self.init(code)
}
}
extension CocoaError {
static func errorWithFilePath(_ pathOrURL: PathOrURL, errno: Int32, reading: Bool, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
switch pathOrURL {
case .path(let path):
return Self.errorWithFilePath(path, errno: errno, reading: reading, variant: variant, source: source, destination: destination)
case .url(let url):
return Self.errorWithFilePath(url, errno: errno, reading: reading, variant: variant, source: source, destination: destination)
}
}
static func errorWithFilePath(_ path: String, errno: Int32, reading: Bool, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
CocoaError(Code(fileErrno: errno, reading: reading), path: path, underlying: POSIXError(errno: errno), variant: variant, source: source, destination: destination)
}
static func errorWithFilePath(_ url: URL, errno: Int32, reading: Bool, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
CocoaError(Code(fileErrno: errno, reading: reading), url: url, underlying: POSIXError(errno: errno), variant: variant, source: source, destination: destination)
}
}
// MARK: - Windows Errors
#if os(Windows)
extension CocoaError.Code {
fileprivate init(win32: DWORD, reading: Bool, emptyPath: Bool? = nil) {
self = switch (reading, win32) {
case (true, ERROR_FILE_NOT_FOUND), (true, ERROR_PATH_NOT_FOUND):
// Windows will return ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND
// for empty paths.
(emptyPath ?? false) ? .fileReadInvalidFileName : .fileReadNoSuchFile
case (true, ERROR_ACCESS_DENIED): .fileReadNoPermission
case (true, ERROR_INVALID_ACCESS): .fileReadNoPermission
case (true, ERROR_INVALID_DRIVE): .fileReadNoSuchFile
case (true, ERROR_SHARING_VIOLATION): .fileReadNoPermission
case (true, ERROR_INVALID_NAME): .fileReadInvalidFileName
case (true, ERROR_LABEL_TOO_LONG): .fileReadInvalidFileName
case (true, ERROR_BAD_PATHNAME): .fileReadInvalidFileName
case (true, ERROR_FILENAME_EXCED_RANGE): .fileReadInvalidFileName
case (true, ERROR_DIRECTORY): .fileReadInvalidFileName
case (true, _): .fileReadUnknown
case (false, ERROR_FILE_NOT_FOUND), (false, ERROR_PATH_NOT_FOUND):
// Windows will return ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND
// for empty paths.
(emptyPath ?? false) ? .fileWriteInvalidFileName : .fileNoSuchFile
case (false, ERROR_ACCESS_DENIED): .fileWriteNoPermission
case (false, ERROR_INVALID_ACCESS): .fileWriteNoPermission
case (false, ERROR_INVALID_DRIVE): .fileNoSuchFile
case (false, ERROR_WRITE_FAULT): .fileWriteVolumeReadOnly
case (false, ERROR_SHARING_VIOLATION): .fileWriteNoPermission
case (false, ERROR_FILE_EXISTS): .fileWriteFileExists
case (false, ERROR_DISK_FULL): .fileWriteOutOfSpace
case (false, ERROR_INVALID_NAME): .fileWriteInvalidFileName
case (false, ERROR_LABEL_TOO_LONG): .fileWriteInvalidFileName
case (false, ERROR_BAD_PATHNAME): .fileWriteInvalidFileName
case (false, ERROR_ALREADY_EXISTS): .fileWriteFileExists
case (false, ERROR_FILENAME_EXCED_RANGE): .fileWriteInvalidFileName
case (false, ERROR_DIRECTORY): .fileWriteInvalidFileName
case (false, ERROR_DISK_RESOURCES_EXHAUSTED): .fileWriteOutOfSpace
case (false, _): .fileWriteUnknown
}
}
}
extension CocoaError {
static func errorWithFilePath(_ path: PathOrURL, win32 dwError: DWORD, reading: Bool) -> CocoaError {
switch path {
case let .path(path):
return CocoaError(.init(win32: dwError, reading: reading, emptyPath: path.isEmpty), path: path, underlying: Win32Error(dwError))
case let .url(url):
let pathStr = url.withUnsafeFileSystemRepresentation { String(cString: $0!) }
return CocoaError(.init(win32: dwError, reading: reading, emptyPath: pathStr.isEmpty), path: pathStr, url: url, underlying: Win32Error(dwError))
}
}
static func errorWithFilePath(_ path: String? = nil, win32 dwError: DWORD, reading: Bool, variant: String? = nil, source: String? = nil, destination: String? = nil) -> CocoaError {
return CocoaError(.init(win32: dwError, reading: reading, emptyPath: path?.isEmpty), path: path, underlying: Win32Error(dwError), source: source, destination: destination)
}
}
#endif
// MARK: - OSStatus Errors
extension CocoaError {
static func errorWithFilePath(_ path: String? = nil, osStatus: Int, reading: Bool, variant: String? = nil) -> CocoaError {
// Do more or less what _NSErrorWithFilePathAndErrno() does, except for OSStatus values
let errorCode: CocoaError.Code = switch (reading, osStatus) {
case (true, -43 /*fnfErr*/), (true, -120 /*dirNFErr*/): .fileReadNoSuchFile
case (true, -5000 /*afpAccessDenied*/): .fileReadNoPermission
case (true, _): .fileReadUnknown
case (false, -34 /*dskFulErr*/), (false, -1425 /*errFSQuotaExceeded*/): .fileWriteOutOfSpace
case (false, -45 /*fLckdErr*/), (false, -5000 /*afpAccessDenied*/): .fileWriteNoPermission
case (false, _): .fileWriteUnknown
}
#if FOUNDATION_FRAMEWORK
return CocoaError(errorCode, path: path, underlying: NSError(domain: NSOSStatusErrorDomain, code: osStatus), variant: variant)
#else
return CocoaError(errorCode, path: path, variant: variant)
#endif
}
}
// MARK: - Error creation funnel points
extension CocoaError {
fileprivate init(
_ code: CocoaError.Code,
path: String? = nil,
underlying: (some Error)? = Optional<CocoaError>.none,
variant: String? = nil,
source: String? = nil,
destination: String? = nil
) {
self.init(
code,
path: path,
url: path.flatMap(URL.init(_fileManagerFailableFileURLWithPath:)),
underlying: underlying,
variant: variant,
source: source,
destination: destination
)
}
fileprivate init(
_ code: CocoaError.Code,
url: URL,
underlying: (some Error)? = Optional<CocoaError>.none,
variant: String? = nil,
source: String? = nil,
destination: String? = nil
) {
self.init(
code,
path: url.path,
url: url,
underlying: underlying,
variant: variant,
source: source,
destination: destination
)
}
fileprivate init(
_ code: CocoaError.Code,
path: String?,
url: URL?,
underlying: (some Error)? = Optional<CocoaError>.none,
variant: String? = nil,
source: String? = nil,
destination: String? = nil
) {
#if FOUNDATION_FRAMEWORK
self.init(_uncheckedNSError: NSError._cocoaError(withCode: code.rawValue, path: path, url: url, underlying: underlying, variant: variant, source: source, destination: destination) as NSError)
#else
var userInfo: [String : Any] = [:]
if let path {
userInfo[NSFilePathErrorKey] = path
}
if let url {
userInfo[NSURLErrorKey] = url
}
if let underlying {
userInfo[NSUnderlyingErrorKey] = underlying
}
if let source {
userInfo[NSSourceFilePathErrorKey] = source
}
if let destination {
userInfo[NSDestinationFilePathErrorKey] = destination
}
if let variant {
userInfo[NSUserStringVariantErrorKey] = [variant]
}
self.init(code, userInfo: userInfo)
#endif
}
}
|