From b92a46ee5814ef1a1b5e95f302cfbb98f05b6d68 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 4 Feb 2023 02:32:09 +0400 Subject: [PATCH] Check tmp dir size --- submodules/DebugSettingsUI/BUILD | 1 + .../Sources/DebugController.swift | 39 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/submodules/DebugSettingsUI/BUILD b/submodules/DebugSettingsUI/BUILD index 60710882b0..a8ea3aa7b3 100644 --- a/submodules/DebugSettingsUI/BUILD +++ b/submodules/DebugSettingsUI/BUILD @@ -25,6 +25,7 @@ swift_library( "//submodules/GZip:GZip", "//third-party/ZipArchive:ZipArchive", "//submodules/InAppPurchaseManager:InAppPurchaseManager", + "//submodules/Utils/DarwinDirStat", ], visibility = [ "//visibility:public", diff --git a/submodules/DebugSettingsUI/Sources/DebugController.swift b/submodules/DebugSettingsUI/Sources/DebugController.swift index 8f2a768db3..46e8ae0508 100644 --- a/submodules/DebugSettingsUI/Sources/DebugController.swift +++ b/submodules/DebugSettingsUI/Sources/DebugController.swift @@ -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.size) + if result != -1 { + return Int64(s.total_size) + } else { + result = dirstat_np(path, 0, &s, MemoryLayout.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 + } +}