Check tmp dir size

This commit is contained in:
Ilya Laktyushin 2023-02-04 02:32:09 +04:00
parent 524e8f1e81
commit b92a46ee58
2 changed files with 37 additions and 3 deletions

View File

@ -25,6 +25,7 @@ swift_library(
"//submodules/GZip:GZip",
"//third-party/ZipArchive:ZipArchive",
"//submodules/InAppPurchaseManager:InAppPurchaseManager",
"//submodules/Utils/DarwinDirStat",
],
visibility = [
"//visibility:public",

View File

@ -16,6 +16,7 @@ import AppBundle
import ZipArchive
import WebKit
import InAppPurchaseManager
import DarwinDirStat
@objc private final class DebugControllerMailComposeDelegate: NSObject, MFMailComposeViewControllerDelegate {
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
@ -947,10 +948,15 @@ private enum DebugControllerEntry: ItemListNodeEntry {
case .resetTranslationStates:
return ItemListActionItem(presentationData: presentationData, title: "Reset Translation States", kind: .generic, alignment: .natural, sectionId: self.section, style: .blocks, action: {
if let context = arguments.context {
let _ = context.engine.itemCache.clear(collectionIds: [
ApplicationSpecificItemCacheCollectionId.translationState
]).start()
let size = statForDirectory(path: NSTemporaryDirectory())
let controller = textAlertController(context: context, title: nil, text: "temp dir size \(size)", actions: [TextAlertAction(type: .genericAction, title: "OK", action: {})])
arguments.presentController(controller, nil)
}
// if let context = arguments.context {
// let _ = context.engine.itemCache.clear(collectionIds: [
// ApplicationSpecificItemCacheCollectionId.translationState
// ]).start()
// }
})
case .crash:
return ItemListActionItem(presentationData: presentationData, title: "Crash", kind: .generic, alignment: .natural, sectionId: self.section, style: .blocks, action: {
@ -1539,3 +1545,30 @@ public func triggerDebugSendLogsUI(context: AccountContext, additionalInfo: Stri
pushController(controller)
})
}
private func statForDirectory(path: String) -> Int64 {
if #available(macOS 10.13, *) {
var s = darwin_dirstat()
var result = dirstat_np(path, 1, &s, MemoryLayout<darwin_dirstat>.size)
if result != -1 {
return Int64(s.total_size)
} else {
result = dirstat_np(path, 0, &s, MemoryLayout<darwin_dirstat>.size)
if result != -1 {
return Int64(s.total_size)
} else {
return 0
}
}
} else {
let fileManager = FileManager.default
let folderURL = URL(fileURLWithPath: path)
var folderSize: Int64 = 0
if let files = try? fileManager.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: []) {
for file in files {
folderSize += (fileSize(file.path) ?? 0)
}
}
return folderSize
}
}