Skip to content

Commit e36e819

Browse files
authored
Merge pull request #1662 from spevans/pr_sr_8519
2 parents b610bd5 + 84b6260 commit e36e819

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

Foundation/FileManager.swift

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// This source file is part of the Swift.org open source project
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
3+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
6-
// See http://swift.org/LICENSE.txt for license information
7-
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

1010
#if os(macOS) || os(iOS)
@@ -342,16 +342,7 @@ open class FileManager : NSObject {
342342
result[.groupOwnerAccountName] = name
343343
}
344344

345-
var type : FileAttributeType
346-
switch s.st_mode & S_IFMT {
347-
case S_IFCHR: type = .typeCharacterSpecial
348-
case S_IFDIR: type = .typeDirectory
349-
case S_IFBLK: type = .typeBlockSpecial
350-
case S_IFREG: type = .typeRegular
351-
case S_IFLNK: type = .typeSymbolicLink
352-
case S_IFSOCK: type = .typeSocket
353-
default: type = .typeUnknown
354-
}
345+
let type = FileAttributeType(statMode: s.st_mode)
355346
result[.type] = type
356347

357348
if type == .typeBlockSpecial || type == .typeCharacterSpecial {
@@ -426,7 +417,7 @@ open class FileManager : NSObject {
426417
})
427418
}
428419

429-
/* destinationOfSymbolicLinkAtPath:error: returns an NSString containing the path of the item pointed at by the symlink specified by 'path'. If this method returns 'nil', an NSError will be returned by reference in the 'error' parameter.
420+
/* destinationOfSymbolicLinkAtPath:error: returns a String containing the path of the item pointed at by the symlink specified by 'path'. If this method returns 'nil', an NSError will be thrown.
430421

431422
This method replaces pathContentOfSymbolicLinkAtPath:
432423
*/
@@ -535,13 +526,11 @@ open class FileManager : NSObject {
535526
}
536527

537528
private func _copyOrLinkDirectoryHelper(atPath srcPath: String, toPath dstPath: String, _ body: (String, String, FileAttributeType) throws -> ()) throws {
538-
guard
539-
let attrs = try? attributesOfItem(atPath: srcPath),
540-
let fileType = attrs[.type] as? FileAttributeType
541-
else {
529+
guard let stat = try? _lstatFile(atPath: srcPath) else {
542530
return
543531
}
544532

533+
let fileType = FileAttributeType(statMode: stat.st_mode)
545534
if fileType == .typeDirectory {
546535
try createDirectory(atPath: dstPath, withIntermediateDirectories: false, attributes: nil)
547536

@@ -552,7 +541,8 @@ open class FileManager : NSObject {
552541
while let item = enumerator.nextObject() as? String {
553542
let src = srcPath + "/" + item
554543
let dst = dstPath + "/" + item
555-
if let attrs = try? attributesOfItem(atPath: src), let fileType = attrs[.type] as? FileAttributeType {
544+
if let stat = try? _lstatFile(atPath: src) {
545+
let fileType = FileAttributeType(statMode: stat.st_mode)
556546
if fileType == .typeDirectory {
557547
try createDirectory(atPath: dst, withIntermediateDirectories: false, attributes: nil)
558548
} else {
@@ -1032,10 +1022,7 @@ open class FileManager : NSObject {
10321022
}
10331023

10341024
internal func _tryToResolveTrailingSymlinkInPath(_ path: String) -> String? {
1035-
guard _pathIsSymbolicLink(path) else {
1036-
return nil
1037-
}
1038-
1025+
// destinationOfSymbolicLink(atPath:) will fail if the path is not a symbolic link
10391026
guard let destination = try? FileManager.default.destinationOfSymbolicLink(atPath: path) else {
10401027
return nil
10411028
}
@@ -1051,16 +1038,6 @@ open class FileManager : NSObject {
10511038
return temp._bridgeToObjectiveC().appendingPathComponent(dest)
10521039
}
10531040
}
1054-
1055-
internal func _pathIsSymbolicLink(_ path: String) -> Bool {
1056-
guard
1057-
let attrs = try? attributesOfItem(atPath: path),
1058-
let fileType = attrs[.type] as? FileAttributeType
1059-
else {
1060-
return false
1061-
}
1062-
return fileType == .typeSymbolicLink
1063-
}
10641041
}
10651042

10661043
extension FileManager {
@@ -1199,6 +1176,18 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
11991176
return lhs.rawValue == rhs.rawValue
12001177
}
12011178

1179+
fileprivate init(statMode: mode_t) {
1180+
switch statMode & S_IFMT {
1181+
case S_IFCHR: self = .typeCharacterSpecial
1182+
case S_IFDIR: self = .typeDirectory
1183+
case S_IFBLK: self = .typeBlockSpecial
1184+
case S_IFREG: self = .typeRegular
1185+
case S_IFLNK: self = .typeSymbolicLink
1186+
case S_IFSOCK: self = .typeSocket
1187+
default: self = .typeUnknown
1188+
}
1189+
}
1190+
12021191
public static let typeDirectory = FileAttributeType(rawValue: "NSFileTypeDirectory")
12031192
public static let typeRegular = FileAttributeType(rawValue: "NSFileTypeRegular")
12041193
public static let typeSymbolicLink = FileAttributeType(rawValue: "NSFileTypeSymbolicLink")

0 commit comments

Comments
 (0)