mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Don't perform directory mapping if the total size does not exceed the limit
This commit is contained in:
parent
c7422c1a5a
commit
1249733d95
@ -18,6 +18,7 @@ swift_library(
|
|||||||
"//submodules/ManagedFile:ManagedFile",
|
"//submodules/ManagedFile:ManagedFile",
|
||||||
"//submodules/Utils/RangeSet:RangeSet",
|
"//submodules/Utils/RangeSet:RangeSet",
|
||||||
"//submodules/CryptoUtils",
|
"//submodules/CryptoUtils",
|
||||||
|
"//submodules/Utils/DarwinDirStat",
|
||||||
],
|
],
|
||||||
visibility = [
|
visibility = [
|
||||||
"//visibility:public",
|
"//visibility:public",
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import SwiftSignalKit
|
import SwiftSignalKit
|
||||||
|
import DarwinDirStat
|
||||||
|
|
||||||
private typealias SignalKitTimer = SwiftSignalKit.Timer
|
private typealias SignalKitTimer = SwiftSignalKit.Timer
|
||||||
|
|
||||||
struct InodeInfo {
|
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()
|
var result = ScanFilesResult()
|
||||||
|
|
||||||
if let dp = opendir(path) {
|
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 (((value.st_mode) & S_IFMT) == S_IFDIR) {
|
||||||
if includeSubdirectories {
|
if includeSubdirectories {
|
||||||
if let subPath = String(data: Data(bytes: pathBuffer, count: strnlen(pathBuffer, 1024)), encoding: .utf8) {
|
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.totalSize += subResult.totalSize
|
||||||
result.unlinkedCount += subResult.unlinkedCount
|
result.unlinkedCount += subResult.unlinkedCount
|
||||||
}
|
}
|
||||||
@ -167,7 +169,9 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, includeSu
|
|||||||
result.unlinkedCount += 1
|
result.unlinkedCount += 1
|
||||||
} else {
|
} else {
|
||||||
result.totalSize += UInt64(value.st_size)
|
result.totalSize += UInt64(value.st_size)
|
||||||
tempDatabase.add(pathBuffer: pathBuffer, pathSize: strnlen(pathBuffer, 1024), size: Int64(value.st_size), timestamp: Int32(value.st_mtimespec.tv_sec))
|
if performSizeMapping {
|
||||||
|
tempDatabase.add(pathBuffer: pathBuffer, pathSize: strnlen(pathBuffer, 1024), size: Int64(value.st_size), timestamp: Int32(value.st_mtimespec.tv_sec))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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 final class TimeBasedCleanupImpl {
|
||||||
private let queue: Queue
|
private let queue: Queue
|
||||||
private let storageBox: StorageBox
|
private let storageBox: StorageBox
|
||||||
@ -329,10 +348,42 @@ private final class TimeBasedCleanupImpl {
|
|||||||
let bytesLimit = UInt64(gigabytesLimit) * 1024 * 1024 * 1024
|
let bytesLimit = UInt64(gigabytesLimit) * 1024 * 1024 * 1024
|
||||||
//#endif
|
//#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 oldestShortLivedTimestamp = timestamp - shortLived
|
||||||
let oldestGeneralTimestamp = timestamp - general
|
let oldestGeneralTimestamp = timestamp - general
|
||||||
for path in shortLivedPaths {
|
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) {
|
if !paths.contains(path) {
|
||||||
paths.append(path)
|
paths.append(path)
|
||||||
}
|
}
|
||||||
@ -342,7 +393,7 @@ private final class TimeBasedCleanupImpl {
|
|||||||
var totalLimitSize: UInt64 = 0
|
var totalLimitSize: UInt64 = 0
|
||||||
|
|
||||||
for path in generalPaths {
|
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) {
|
if !paths.contains(path) {
|
||||||
paths.append(path)
|
paths.append(path)
|
||||||
}
|
}
|
||||||
@ -350,7 +401,7 @@ private final class TimeBasedCleanupImpl {
|
|||||||
totalLimitSize += scanResult.totalSize
|
totalLimitSize += scanResult.totalSize
|
||||||
}
|
}
|
||||||
do {
|
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) {
|
if !paths.contains(totalSizeBasedPath) {
|
||||||
paths.append(totalSizeBasedPath)
|
paths.append(totalSizeBasedPath)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user