Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect statistical calculation #1252

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions Mixin/Service/Job/BackupJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class BackupJob: BaseJob {
return
}

var uploadPaths: [String] = []
var backupPaths: [String] = []
monitors = SafeDictionary<String, Int64>()
totalFileSize = 0
Expand All @@ -122,31 +121,39 @@ class BackupJob: BaseJob {
for filename in localPaths {
let localURL = AttachmentContainer.url.appendingPathComponent(filename)
let cloudURL = backupUrl.appendingPathComponent(filename)
let localFileSize = FileManager.default.fileSize(localURL.path)
let cloudExists = FileManager.default.fileExists(atPath: cloudURL.path)

if !cloudExists || FileManager.default.fileSize(cloudURL.path) != localFileSize {
let cloudFileSize = FileManager.default.fileSize(cloudURL.path)
let localFileSize = FileManager.default.fileSize(localURL.path)
let isFileUploading = cloudURL.isUploaded || cloudURL.isUploading
// 1. Cloud file doesn't exist.
// 2. Cloud file size is different from the local file size.
// 3. File has not been uploaded and is currently not uploading.
if !cloudExists || cloudFileSize != localFileSize || !isFileUploading {
backupPaths.append(filename)
backupTotalSize += localFileSize

uploadPaths.append(filename)
} else if cloudExists {
if cloudURL.isUploaded {
withoutUploadSize += localFileSize
} else {
uploadPaths.append(filename)
}
}
if cloudURL.isUploading {
monitors[cloudURL.lastPathComponent] = 0
}
if cloudURL.isUploaded {
withoutUploadSize += localFileSize
}
totalFileSize += localFileSize
}

let databaseFileSize = getDatabaseFileSize()
let localDatabaseSize = getDatabaseFileSize()
let databaseCloudURL = backupUrl.appendingPathComponent(backupDatabaseName)
let isBackupDatabase = !FileManager.default.fileExists(atPath: databaseCloudURL.path) || FileManager.default.fileSize(databaseCloudURL.path) != databaseFileSize
let cloudDatabaseExists = FileManager.default.fileExists(atPath: databaseCloudURL.path)
let cloudDatabaseSize = FileManager.default.fileSize(databaseCloudURL.path)
let isDatabaseUploading = databaseCloudURL.isUploaded || databaseCloudURL.isUploading
let isBackupDatabase = !cloudDatabaseExists || cloudDatabaseSize != localDatabaseSize || !isDatabaseUploading

if !isBackupDatabase {
withoutUploadSize += databaseFileSize
totalFileSize += databaseFileSize
withoutUploadSize += localDatabaseSize
totalFileSize += localDatabaseSize
}
if databaseCloudURL.isUploading {
monitors[databaseCloudURL.lastPathComponent] = 0
}

guard isContinueBackup else {
Expand Down