-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSMBFile.swift
78 lines (62 loc) · 2.35 KB
/
SMBFile.swift
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
//
// SMBFile.swift
// SMBClient
//
// Created by Seth Faxon on 9/1/17.
// Copyright © 2017 Filmic. All rights reserved.
//
import libdsm
public struct SMBFile {
public private(set) var path: SMBPath
public var name: String
public private(set) var fileSize: UInt64
public private(set) var allocationSize: UInt64
public private(set) var createdAt: Date?
public private(set) var accessedAt: Date?
public private(set) var writeAt: Date?
public private(set) var modifiedAt: Date?
init?(stat: OpaquePointer, parentPath: SMBPath) {
self.path = parentPath
guard let cName = smb_stat_name(stat) else { return nil }
let pathAndFile = String(cString: cName).split(separator: "\\")
guard let n = pathAndFile.last else { return nil }
self.name = n.decomposedStringWithCanonicalMapping
self.fileSize = smb_stat_get(stat, SMB_STAT_SIZE)
self.allocationSize = smb_stat_get(stat, SMB_STAT_ALLOC_SIZE)
self.createdAt = SMBFile.dateFrom(timestamp: smb_stat_get(stat, SMB_STAT_CTIME))
self.modifiedAt = SMBFile.dateFrom(timestamp: smb_stat_get(stat, SMB_STAT_MTIME))
self.accessedAt = SMBFile.dateFrom(timestamp: smb_stat_get(stat, SMB_STAT_ATIME))
self.writeAt = SMBFile.dateFrom(timestamp: smb_stat_get(stat, SMB_STAT_WTIME))
}
init?(path: SMBPath, name: String) {
self.path = path
self.name = name
self.fileSize = 0
self.allocationSize = 0
}
public var isHidden: Bool {
return self.name.first == "."
}
internal var uploadPath: String {
let slash = "\\"
let dirs: [String] = self.path.directories.map { $0.name }
let result = slash + dirs.joined(separator: slash) + slash + self.name
return result
}
internal var downloadPath: String {
let slash = "\\"
return slash + self.uploadPath
}
fileprivate static func dateFrom(timestamp: UInt64) -> Date? {
var base = DateComponents()
base.day = 1
base.month = 1
base.year = 1601
base.era = 1
let calendar = Calendar(identifier: .gregorian)
let baseDate = calendar.date(from: base)
let newTimestamp: TimeInterval = TimeInterval(timestamp) / 10000000
let result = baseDate?.addingTimeInterval(newTimestamp)
return result
}
}