wireguard-apple/WireGuard/Shared/FileManager+Extension.swift

45 lines
1.5 KiB
Swift
Raw Normal View History

2018-11-29 10:16:21 +00:00
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
import Foundation
import os.log
extension FileManager {
2018-12-13 22:25:18 +00:00
private static var sharedFolderURL: URL? {
2018-11-29 10:16:21 +00:00
guard let appGroupId = Bundle.main.object(forInfoDictionaryKey: "com.wireguard.ios.app_group_id") as? String else {
2018-12-13 22:25:18 +00:00
os_log("Cannot obtain app group ID from bundle", log: OSLog.default, type: .error)
2018-11-29 10:16:21 +00:00
return nil
}
guard let sharedFolderURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId) else {
2018-12-13 22:25:18 +00:00
wg_log(.error, message: "Cannot obtain shared folder URL")
2018-11-29 10:16:21 +00:00
return nil
}
2018-12-13 22:25:18 +00:00
return sharedFolderURL
}
static var networkExtensionLogFileURL: URL? {
return sharedFolderURL?.appendingPathComponent("tunnel-log.bin")
}
static var networkExtensionLastErrorFileURL: URL? {
2018-12-13 22:25:18 +00:00
return sharedFolderURL?.appendingPathComponent("last-error.txt")
}
static var appLogFileURL: URL? {
2018-12-13 22:25:18 +00:00
guard let documentDirURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
wg_log(.error, message: "Cannot obtain app documents folder URL")
return nil
}
2018-12-13 22:25:18 +00:00
return documentDirURL.appendingPathComponent("app-log.bin")
2018-11-29 10:16:21 +00:00
}
static func deleteFile(at url: URL) -> Bool {
do {
try FileManager.default.removeItem(at: url)
2018-12-13 22:25:18 +00:00
} catch {
return false
}
return true
}
2018-11-29 10:16:21 +00:00
}