Update sync.d

* Add a check when using --download-only to test if the file already exists locally, and if it does, has the local file been modified since the file was last downloaded. If it has, then rename the existing file to preserve it to prevent local data loss
This commit is contained in:
abraunegg 2023-10-20 05:45:33 +11:00
parent 2257f0ba92
commit d7ea79773f

View file

@ -1962,6 +1962,41 @@ class SyncEngine {
log.vdebug("WARNING: onedriveJSONItem['file']['hashes'] is missing - unable to compare file hash after download");
}
// Is this a --download-only scenario?
if (appConfig.getValueBool("download_only")) {
if (exists(newItemPath)) {
// file exists locally already
Item databaseItem;
bool fileFoundInDB = false;
foreach (driveId; driveIDsArray) {
if (itemDB.selectByPath(newItemPath, driveId, databaseItem)) {
fileFoundInDB = true;
break;
}
}
// Log the DB details
log.vdebug("File to download exists locally and this is the DB record: ", databaseItem);
// Does the DB (what we think is in sync) hash match the existing local file hash?
if (!testFileHash(newItemPath, databaseItem)) {
// local file is different to what we know to be true
log.log("The local file to replace (", newItemPath,") has been modified locally since the last download. Renaming it to avoid potential local data loss.");
// do the rename if we are not in a --dry-run scenario
if (!dryRun) {
// If we need to rename the file, what do we rename it to?
auto ext = extension(newItemPath);
auto renamedNewItemPath = newItemPath.chomp(ext) ~ "-" ~ deviceName ~ ext;
// Perform the local rename of the existing local file
safeRename(newItemPath, renamedNewItemPath, dryRun);
}
}
}
}
// Is there enough free space locally to download the file
// - We can use '.' here as we change the current working directory to the configured 'sync_dir'
ulong localActualFreeSpace = to!ulong(getAvailableDiskSpace("."));
@ -2149,14 +2184,6 @@ class SyncEngine {
// Save this item into the database
saveItem(onedriveJSONItem);
/**
log.vdebug("Inserting new item details to local database");
// What was the item that was saved
log.vdebug("item details: ", newDatabaseItem);
itemDB.upsert(newDatabaseItem);
**/
// If we are in a --dry-run situation - if we are, we need to track that we faked the download
if (dryRun) {
// track that we 'faked it'