blob: fdc66c20b646692fe1fcaed1531d562ba26d1fff (
plain)
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
|
//
// LogRotation.swift
// MullvadVPN
//
// Created by pronebird on 02/08/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
enum LogRotation {
enum Error: ChainedError {
case noSourceLogFile
case moveSourceLogFile(Swift.Error)
var errorDescription: String? {
switch self {
case .noSourceLogFile:
return "Source log file does not exist"
case .moveSourceLogFile:
return "Failure to move the source log file to backup"
}
}
}
static func rotateLog(logsDirectory: URL, logFileName: String) -> Result<(), Error> {
let fileManager = FileManager.default
let source = logsDirectory.appendingPathComponent(logFileName)
let backup = source.deletingPathExtension().appendingPathExtension("old.log")
return Result { _ = try fileManager.replaceItemAt(backup, withItemAt: source) }
.mapError { (error) -> Error in
// FileManager returns a very obscure error chain so we need to traverse it to find
// the root cause of the error.
var errorCursor: Swift.Error? = error
let cocoaErrorIterator = AnyIterator { () -> CocoaError? in
if let cocoaError = errorCursor as? CocoaError {
errorCursor = cocoaError.underlying
return cocoaError
} else {
errorCursor = nil
return nil
}
}
while let fileError = cocoaErrorIterator.next() {
// .fileNoSuchFile is returned when both backup and source log files do not exist
// .fileReadNoSuchFile is returned when backup exists but source log file does not
if fileError.code == .fileNoSuchFile || fileError.code == .fileReadNoSuchFile,
fileError.url == source {
return .noSourceLogFile
}
}
return .moveSourceLogFile(error)
}
}
}
|