Handle .nosync directive when downloading new files (Issue #827) (#842)

* Handle .nosync directive when downloading new files to an existing directory that is (was) in sync
* Handle the situation where a .nosync is created after some sort of partial download, so that the file, whilst now unwanted, is not downloaded
This commit is contained in:
abraunegg 2020-04-02 07:39:02 +11:00 committed by GitHub
parent 0170884bd1
commit 9ae6f8e808
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1507,17 +1507,19 @@ final class SyncEngine
private void applyNewItem(Item item, string path)
{
if (exists(path)) {
// path exists locally
if (isItemSynced(item, path)) {
//log.vlog("The item is already present");
// file details from OneDrive and local file details in database are in-sync
log.vdebug("The item to sync is already present on the local file system and is in-sync with the local database");
return;
} else {
// TODO: force remote sync by deleting local item
// Is the local file technically 'newer' based on UTC timestamp?
// file is not in sync with the database
// is the local file technically 'newer' based on UTC timestamp?
SysTime localModifiedTime = timeLastModified(path).toUTC();
localModifiedTime.fracSecs = Duration.zero;
item.mtime.fracSecs = Duration.zero;
// is the local modified time greater than that from OneDrive?
if (localModifiedTime > item.mtime) {
// local file is newer than item on OneDrive based on file modified time
// Is this item id in the database?
@ -1528,6 +1530,21 @@ final class SyncEngine
log.vdebug("Skipping OneDrive change as this is determined to be unwanted due to local item modified time being newer than OneDrive item and present in the sqlite database");
return;
} else {
// Should this 'download' be skipped?
// Do we need to check for .nosync? Only if --check-for-nosync was passed in
if (cfg.getValueBool("check_nosync")) {
// need the parent path for this object
string parentPath = dirName(path);
if (exists(parentPath ~ "/.nosync")) {
log.vlog("Skipping downloading item - .nosync found in parent folder & --check-for-nosync is enabled: ", path);
// flag that this download failed, otherwise the 'item' is added to the database - then, as not present on the local disk, would get deleted from OneDrive
downloadFailed = true;
// clean up this partial file, otherwise every sync we will get theis warning
log.vlog("Removing previous partial file download due to .nosync found in parent folder & --check-for-nosync is enabled");
safeRemove(path);
return;
}
}
// file exists locally but is not in the sqlite database - maybe a failed download?
log.vlog("Local item does not exist in local database - replacing with file from OneDrive - failed download?");
}
@ -1538,6 +1555,7 @@ final class SyncEngine
auto newPath = path.chomp(ext) ~ "-" ~ deviceName ~ ext;
log.vlog("The local item is out-of-sync with OneDrive, renaming to preserve existing file: ", path, " -> ", newPath);
if (!dryRun) {
// rename the local file to prevent data loss incase the local file is actually needed
safeRename(path);
} else {
// Expectation here is that there is a new file locally (newPath) however as we don't create this, the "new file" will not be uploaded as it does not exist
@ -1545,7 +1563,23 @@ final class SyncEngine
}
}
}
} else {
// path does not exist locally - this will be a new file download or folder creation
// Should this 'download' be skipped?
// Do we need to check for .nosync? Only if --check-for-nosync was passed in
if (cfg.getValueBool("check_nosync")) {
// need the parent path for this object
string parentPath = dirName(path);
if (exists(parentPath ~ "/.nosync")) {
log.vlog("Skipping downloading item - .nosync found in parent folder & --check-for-nosync is enabled: ", path);
// flag that this download failed, otherwise the 'item' is added to the database - then, as not present on the local disk, would get deleted from OneDrive
downloadFailed = true;
return;
}
}
}
// how to handle this item?
final switch (item.type) {
case ItemType.file:
downloadFileItem(item, path);