Skip to content

Commit 5b04418

Browse files
author
Ken Carroll
committed
Set attributes of files created
1 parent dff9930 commit 5b04418

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Source/Shared/Storage/DiskStorage.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ final public class DiskStorage<Key: Hashable, Value> {
1717

1818
private let transformer: Transformer<Value>
1919
private let hasher = Hasher.constantAccrossExecutions()
20+
private var defaultFileAttributes: [FileAttributeKey : Any]?
2021

2122
// MARK: - Initialization
22-
public convenience init(config: DiskConfig, fileManager: FileManager = FileManager.default, transformer: Transformer<Value>) throws {
23+
public convenience init(config: DiskConfig, fileManager: FileManager = FileManager.default, transformer: Transformer<Value>,
24+
fileAttributes: [FileAttributeKey : Any] = [:]) throws {
2325
let url: URL
2426
if let directory = config.directory {
2527
url = directory
@@ -44,6 +46,9 @@ final public class DiskStorage<Key: Hashable, Value> {
4446
FileAttributeKey.protectionKey: protectionType
4547
])
4648
}
49+
50+
defaultFileAttributes = fileAttributes
51+
4752
}
4853

4954
public required init(config: DiskConfig, fileManager: FileManager = FileManager.default, path: String, transformer: Transformer<Value>) {
@@ -80,8 +85,11 @@ extension DiskStorage: StorageAware {
8085
let expiry = expiry ?? config.expiry
8186
let data = try transformer.toData(object)
8287
let filePath = makeFilePath(for: key)
83-
_ = fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
84-
try fileManager.setAttributes([.modificationDate: expiry.date], ofItemAtPath: filePath)
88+
var attributes = defaultFileAttributes!
89+
attributes[.modificationDate] = expiry.date
90+
91+
_ = fileManager.createFile(atPath: filePath, contents: data, attributes: attributes)
92+
// try fileManager.setAttributes([.modificationDate: expiry.date], ofItemAtPath: filePath)
8593
}
8694

8795
public func removeObject(forKey key: Key) throws {

0 commit comments

Comments
 (0)