Skip to content

Commit

Permalink
Added file creation time to returned attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
amosavian committed Apr 28, 2019
1 parent 8beedae commit 21e9678
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
28 changes: 15 additions & 13 deletions AMSMB2/AMSMB2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,15 @@ public class AMSMB2: NSObject, NSSecureCoding, Codable, NSCopying, CustomReflect
// This block will only delete children of directory, the path itself will removed after if block.
let list = try self.listDirectory(path: path, recursive: true)
let sortedContents = list.sorted(by: {
guard let firstPath = $0.filepath, let secPath = $1.filepath else {
guard let firstPath = $0.filePath, let secPath = $1.filePath else {
return false
}
return firstPath.localizedStandardCompare(secPath) == .orderedDescending
})

for item in sortedContents {
guard let itemPath = item.filepath else { continue }
if item.filetype == URLFileResourceType.directory {
guard let itemPath = item.filePath else { continue }
if item.fileType == URLFileResourceType.directory {
try context.rmdir(itemPath)
} else {
try context.unlink(itemPath)
Expand Down Expand Up @@ -845,7 +845,7 @@ extension AMSMB2 {
fileprivate func populateResourceValue(_ dic: inout [URLResourceKey: Any], stat: smb2_stat_64) {

func convertDate(unixTime: UInt64, nsec: UInt64) -> NSDate {
let time = TimeInterval(unixTime) + TimeInterval(nsec) / TimeInterval(NSEC_PER_SEC)
let time = TimeInterval(unixTime) + TimeInterval(nsec / 1000) / TimeInterval(USEC_PER_SEC)
return NSDate(timeIntervalSince1970: time)
}

Expand All @@ -866,10 +866,12 @@ extension AMSMB2 {

dic[.contentModificationDateKey] = convertDate(unixTime: stat.smb2_mtime,
nsec: stat.smb2_mtime_nsec)
dic[.creationDateKey] = convertDate(unixTime: stat.smb2_ctime,
nsec: stat.smb2_ctime_nsec)
dic[.attributeModificationDateKey] = convertDate(unixTime: stat.smb2_ctime,
nsec: stat.smb2_ctime_nsec)
dic[.contentAccessDateKey] = convertDate(unixTime: stat.smb2_atime,
nsec: stat.smb2_atime_nsec)
dic[.creationDateKey] = convertDate(unixTime: stat.smb2_btime,
nsec: stat.smb2_btime_nsec)
}

fileprivate func listDirectory(path: String, recursive: Bool) throws -> [[URLResourceKey: Any]] {
Expand All @@ -887,10 +889,10 @@ extension AMSMB2 {
}

if recursive {
let subDirectories = contents.filter { $0.filetype == .directory }
let subDirectories = contents.filter { $0.fileType == .directory }

for subDir in subDirectories {
guard let path = subDir.filepath else { continue }
guard let path = subDir.filePath else { continue }
contents.append(contentsOf: try listDirectory(path: path, recursive: true))
}
}
Expand All @@ -907,25 +909,25 @@ extension AMSMB2 {

let list = try self.listDirectory(path: path, recursive: recursive)
let sortedContents = list.sorted(by: {
guard let firstPath = $0.filepath, let secPath = $1.filepath else {
guard let firstPath = $0.filePath, let secPath = $1.filePath else {
return false
}
return firstPath.localizedStandardCompare(secPath) == .orderedAscending
})

let overallSize = list.reduce(0, { (result, value) -> Int64 in
if value.filetype == URLFileResourceType.regular {
return result + (value.filesize ?? 0)
if value.fileType == URLFileResourceType.regular {
return result + (value.fileSize ?? 0)
} else {
return result
}
})

var totalCopied: Int64 = 0
for item in sortedContents {
guard let itemPath = item.filepath else { continue }
guard let itemPath = item.filePath else { continue }
let destPath = itemPath.replacingOccurrences(of: path, with: toPath, options: .anchored)
if item.filetype == URLFileResourceType.directory {
if item.fileType == URLFileResourceType.directory {
try context.mkdir(destPath)
} else {
let shouldContinue = try handle(itemPath, destPath, {
Expand Down
20 changes: 16 additions & 4 deletions AMSMB2/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ extension POSIXError {
}

extension Dictionary where Key == URLResourceKey, Value == Any {
var filename: String? {
var fileName: String? {
return self[.nameKey] as? String
}

var filepath: String? {
var filePath: String? {
return self[.pathKey] as? String
}

var filetype: URLFileResourceType? {
var fileType: URLFileResourceType? {
return self[.fileResourceTypeKey] as? URLFileResourceType
}

var filesize: Int64? {
var fileSize: Int64? {
return self[.fileSizeKey] as? Int64
}

var fileModificationDate: Date? {
return self[.contentModificationDateKey] as? Date
}

var fileAccessDate: Date? {
return self[.contentAccessDateKey] as? Date
}

var fileCreationDate: Date? {
return self[.creationDateKey] as? Date
}
}

extension Data {
Expand Down
9 changes: 7 additions & 2 deletions AMSMB2Tests/AMSMB2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ class AMSMB2Tests: XCTestCase {
switch result {
case .success(let value):
XCTAssertFalse(value.isEmpty)
XCTAssertNotNil(value.first?.filename)
XCTAssertNotNil(value.first)
guard let file = value.first else { break }
XCTAssertNotNil(file.fileName)
XCTAssertNotNil(file.fileModificationDate)
XCTAssertNotNil(file.fileCreationDate)
XCTAssertGreaterThanOrEqual(file.fileModificationDate!, file.fileCreationDate!)
case .failure:
XCTAssert(false)
}
Expand Down Expand Up @@ -319,7 +324,7 @@ class AMSMB2Tests: XCTestCase {
smb.attributesOfItem(atPath: "copyTestDest.dat", completionHandler: { result in
switch result {
case .success(let value):
XCTAssertEqual(value.filesize, Int64(data.count))
XCTAssertEqual(value.fileSize, Int64(data.count))
case .failure:
XCTAssert(false)
}
Expand Down
2 changes: 2 additions & 0 deletions libsmb2/include/smb2/libsmb2.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct smb2_stat_64 {
uint64_t smb2_mtime_nsec;
uint64_t smb2_ctime;
uint64_t smb2_ctime_nsec;
uint64_t smb2_btime;
uint64_t smb2_btime_nsec;
};

struct smb2_statvfs {
Expand Down
Binary file modified libsmb2/lib/libsmb2-ios.a
Binary file not shown.
Binary file modified libsmb2/lib/libsmb2-macos.a
Binary file not shown.
Binary file modified libsmb2/lib/libsmb2-tvos.a
Binary file not shown.

0 comments on commit 21e9678

Please sign in to comment.