Meshtastic-Apple/Meshtastic/Extensions/FileManager.swift

69 lines
2 KiB
Swift
Raw Permalink Normal View History

2023-05-05 17:13:35 -07:00
//
// FileManager.swift
// Meshtastic
//
// Copyright(c) Garth Vander Houwen 5/5/23.
//
import Foundation
2024-06-03 02:17:55 -07:00
import OSLog
2023-05-05 17:13:35 -07:00
let allocatedSizeResourceKeys: Set<URLResourceKey> = [
.isRegularFileKey,
.fileAllocatedSizeKey,
2023-08-26 23:17:30 -07:00
.totalFileAllocatedSizeKey
2023-05-05 17:13:35 -07:00
]
public extension FileManager {
/// Calculate the allocated size of a directory and all its contents on the volume.
///
/// As there's no simple way to get this information from the file system the method
/// has to crawl the entire hierarchy, accumulating the overall sum on the way.
/// The resulting value is roughly equivalent with the amount of bytes
/// that would become available on the volume if the directory would be deleted.
///
/// - note: There are a couple of oddities that are not taken into account (like symbolic links, meta data of
/// directories, hard links, ...).
func allocatedSizeOfDirectory(at directoryURL: URL) -> String {
// The error handler simply stores the error and stops traversal
2023-08-26 23:17:30 -07:00
var enumeratorError: Error?
2023-05-05 17:13:35 -07:00
func errorHandler(_: URL, error: Error) -> Bool {
enumeratorError = error
return false
}
// We have to enumerate all directory contents, including subdirectories.
let enumerator = self.enumerator(at: directoryURL,
includingPropertiesForKeys: Array(allocatedSizeResourceKeys),
options: [],
errorHandler: errorHandler)!
// We'll sum up content size here:
var accumulatedSize: UInt64 = 0
// Perform the traversal.
for item in enumerator {
// Bail out on errors from the errorHandler.
if enumeratorError != nil { break }
// Add up individual file sizes.
guard let contentItemURL = item as? URL else { continue }
do {
accumulatedSize += try contentItemURL.regularFileAllocatedSize()
} catch {
2024-06-23 16:11:02 -07:00
Logger.services.error("💥 File Manager Error: \(error.localizedDescription, privacy: .public)")
2023-05-05 17:13:35 -07:00
}
}
if let error = enumeratorError {
2024-06-23 16:11:02 -07:00
Logger.services.error("💥 AllocatedSizeOfDirectory enumeratorError = \(error.localizedDescription, privacy: .public)")
}
2023-05-05 17:13:35 -07:00
return Double(accumulatedSize).toBytes
}
}