Don't perform directory mapping if the total size does not exceed the limit

This commit is contained in:
Ali 2023-01-09 00:35:14 +04:00
parent c7422c1a5a
commit 1249733d95
2 changed files with 58 additions and 6 deletions

View File

@ -18,6 +18,7 @@ swift_library(
"//submodules/ManagedFile:ManagedFile",
"//submodules/Utils/RangeSet:RangeSet",
"//submodules/CryptoUtils",
"//submodules/Utils/DarwinDirStat",
],
visibility = [
"//visibility:public",

View File

@ -1,5 +1,7 @@
import Foundation
import SwiftSignalKit
import DarwinDirStat
private typealias SignalKitTimer = SwiftSignalKit.Timer
struct InodeInfo {
@ -127,7 +129,7 @@ private final class TempScanDatabase {
}
}
private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSubdirectories: Bool, tempDatabase: TempScanDatabase) -> ScanFilesResult {
private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSubdirectories: Bool, performSizeMapping: Bool, tempDatabase: TempScanDatabase) -> ScanFilesResult {
var result = ScanFilesResult()
if let dp = opendir(path) {
@ -156,7 +158,7 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
if (((value.st_mode) & S_IFMT) == S_IFDIR) {
if includeSubdirectories {
if let subPath = String(data: Data(bytes: pathBuffer, count: strnlen(pathBuffer, 1024)), encoding: .utf8) {
let subResult = scanFiles(at: subPath, olderThan: minTimestamp, includeSubdirectories: true, tempDatabase: tempDatabase)
let subResult = scanFiles(at: subPath, olderThan: minTimestamp, includeSubdirectories: true, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase)
result.totalSize += subResult.totalSize
result.unlinkedCount += subResult.unlinkedCount
}
@ -167,11 +169,13 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
result.unlinkedCount += 1
} else {
result.totalSize += UInt64(value.st_size)
if performSizeMapping {
tempDatabase.add(pathBuffer: pathBuffer, pathSize: strnlen(pathBuffer, 1024), size: Int64(value.st_size), timestamp: Int32(value.st_mtimespec.tv_sec))
}
}
}
}
}
closedir(dp)
}
@ -259,6 +263,21 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
}
}*/
private func statForDirectory(path: String) -> Int64 {
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
}
}
}
private final class TimeBasedCleanupImpl {
private let queue: Queue
private let storageBox: StorageBox
@ -329,10 +348,42 @@ private final class TimeBasedCleanupImpl {
let bytesLimit = UInt64(gigabytesLimit) * 1024 * 1024 * 1024
//#endif
var totalApproximateSize: Int64 = 0
for path in shortLivedPaths {
totalApproximateSize += statForDirectory(path: path)
}
for path in generalPaths {
totalApproximateSize += statForDirectory(path: path)
}
if let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: totalSizeBasedPath), includingPropertiesForKeys: [.fileSizeKey, .fileResourceIdentifierKey], options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants], errorHandler: nil) {
var fileIds = Set<Data>()
loop: for url in enumerator {
guard let url = url as? URL else {
continue
}
if let fileId = (try? url.resourceValues(forKeys: Set([.fileResourceIdentifierKey])))?.fileResourceIdentifier as? Data {
if fileIds.contains(fileId) {
continue loop
}
if let value = (try? url.resourceValues(forKeys: Set([.fileSizeKey])))?.fileSize, value != 0 {
fileIds.insert(fileId)
totalApproximateSize += Int64(value)
}
}
}
}
var performSizeMapping = true
if totalApproximateSize <= gigabytesLimit {
performSizeMapping = false
}
let oldestShortLivedTimestamp = timestamp - shortLived
let oldestGeneralTimestamp = timestamp - general
for path in shortLivedPaths {
let scanResult = scanFiles(at: path, olderThan: oldestShortLivedTimestamp, includeSubdirectories: true, tempDatabase: tempDatabase)
let scanResult = scanFiles(at: path, olderThan: oldestShortLivedTimestamp, includeSubdirectories: true, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase)
if !paths.contains(path) {
paths.append(path)
}
@ -342,7 +393,7 @@ private final class TimeBasedCleanupImpl {
var totalLimitSize: UInt64 = 0
for path in generalPaths {
let scanResult = scanFiles(at: path, olderThan: oldestGeneralTimestamp, includeSubdirectories: true, tempDatabase: tempDatabase)
let scanResult = scanFiles(at: path, olderThan: oldestGeneralTimestamp, includeSubdirectories: true, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase)
if !paths.contains(path) {
paths.append(path)
}
@ -350,7 +401,7 @@ private final class TimeBasedCleanupImpl {
totalLimitSize += scanResult.totalSize
}
do {
let scanResult = scanFiles(at: totalSizeBasedPath, olderThan: 0, includeSubdirectories: false, tempDatabase: tempDatabase)
let scanResult = scanFiles(at: totalSizeBasedPath, olderThan: 0, includeSubdirectories: false, performSizeMapping: performSizeMapping, tempDatabase: tempDatabase)
if !paths.contains(totalSizeBasedPath) {
paths.append(totalSizeBasedPath)
}