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

52 lines
1.7 KiB
Swift
Raw Normal View History

2018-11-29 10:16:21 +00:00
// SPDX-License-Identifier: MIT
2019-01-02 00:56:33 +00:00
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
2018-11-29 10:16:21 +00:00
import Foundation
import os.log
extension FileManager {
2018-12-13 22:25:18 +00:00
private static var sharedFolderURL: URL? {
#if os(iOS)
let appGroupIdInfoDictionaryKey = "com.wireguard.ios.app_group_id"
2019-01-21 22:36:27 +00:00
#elseif os(macOS)
let appGroupIdInfoDictionaryKey = "com.wireguard.macos.app_group_id"
2019-01-21 22:36:27 +00:00
#else
#error("Unimplemented")
#endif
guard let appGroupId = Bundle.main.object(forInfoDictionaryKey: appGroupIdInfoDictionaryKey) 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
}