handle deleting remote folders

This commit is contained in:
skilion 2017-12-31 12:36:14 +01:00
parent b7adc4d0cc
commit 95c952fe62

View file

@ -103,7 +103,6 @@ class SyncException: Exception
} }
} }
// This class handle the synchronization of one folder (ex. the root)
final class SyncEngine final class SyncEngine
{ {
private Config cfg; private Config cfg;
@ -151,12 +150,12 @@ final class SyncEngine
} }
// download all new changes from OneDrive // download the new changes of a specific item
void applyDifferences(string driveId, const(char)[] id) private void applyDifferences(string driveId, const(char)[] id)
{ {
JSONValue changes; JSONValue changes;
string deltaLink = itemdb.getDeltaLink(driveId, id); string deltaLink = itemdb.getDeltaLink(driveId, id);
log.vlog("Downloading changes of " ~ id); log.vlog("Applying changes of " ~ id);
do { do {
try { try {
changes = onedrive.viewChangesById(driveId, id, deltaLink); changes = onedrive.viewChangesById(driveId, id, deltaLink);
@ -320,7 +319,7 @@ final class SyncEngine
if (oldItem.eTag != newItem.eTag) { if (oldItem.eTag != newItem.eTag) {
// handle changed name/path // handle changed name/path
if (oldPath != newPath) { if (oldPath != newPath) {
log.log("Moving: ", oldPath, " -> ", newPath); log.log("Moving ", oldPath, " to ", newPath);
if (exists(newPath)) { if (exists(newPath)) {
// TODO: force remote sync by deleting local item // TODO: force remote sync by deleting local item
log.vlog("The destination is occupied, renaming the conflicting file..."); log.vlog("The destination is occupied, renaming the conflicting file...");
@ -394,23 +393,27 @@ final class SyncEngine
private void deleteItems() private void deleteItems()
{ {
log.vlog("Deleting files ...");
foreach_reverse (i; idsToDelete) { foreach_reverse (i; idsToDelete) {
Item item; Item item;
if (!itemdb.selectById(i[0], i[1], item)) continue; // check if the item is in the db if (!itemdb.selectById(i[0], i[1], item)) continue; // check if the item is in the db
string path = itemdb.computePath(i[0], i[1]); string path = itemdb.computePath(i[0], i[1]);
itemdb.deleteById(i[0], i[1]); log.log("Deleting ", path);
// TODO CHECK REMOTE ITEM itemdb.deleteById(item.driveId, item.id);
if (exists(path)) { if (exists(path)) {
if (isFile(path)) { if (isFile(path)) {
remove(path); remove(path);
log.log("Deleted file: ", path);
} else { } else {
try { try {
rmdir(path); if (item.remoteDriveId == null) {
log.log("Deleted directory: ", path); rmdir(path);
} else {
// delete the linked remote folder
itemdb.deleteById(item.remoteDriveId, item.remoteId);
// remote items do not delete all the children first
rmdirRecurse(path);
}
} catch (FileException e) { } catch (FileException e) {
// directory not empty log.log(e.msg);
} }
} }
} }
@ -419,30 +422,32 @@ final class SyncEngine
assumeSafeAppend(idsToDelete); assumeSafeAppend(idsToDelete);
} }
// scan the given directory for differences // scan the given directory for differences and new items
void scanForDifferences(string path = ".") void scanForDifferences(string path = ".")
{ {
log.vlog("Uploading differences ..."); log.vlog("Uploading differences of ", path);
Item item; Item item;
if (itemdb.selectByPath(path, item)) { if (itemdb.selectByPath(path, item)) {
uploadDifferences(item); uploadDifferences(item);
} }
log.vlog("Uploading new items ..."); log.vlog("Uploading new items of ", path);
uploadNewItems(path); uploadNewItems(path);
} }
private void uploadDifferences(Item item) private void uploadDifferences(Item item)
{ {
log.vlog(item.id, " ", item.name); log.vlog("Processing ", item.name);
// skip filtered items string path;
if (selectiveSync.isNameExcluded(item.name)) { bool unwanted = selectiveSync.isNameExcluded(item.name);
log.vlog("Filtered out"); if (!unwanted) {
return; path = itemdb.computePath(item.driveId, item.id);
unwanted = selectiveSync.isPathExcluded(path);
} }
string path = itemdb.computePath(item.driveId, item.id);
if (selectiveSync.isPathExcluded(path)) { // skip unwanted items
log.vlog("Filtered out: ", path); if (unwanted) {
log.vlog("Filtered out");
return; return;
} }
@ -454,10 +459,12 @@ final class SyncEngine
uploadFileDifferences(item, path); uploadFileDifferences(item, path);
break; break;
case ItemType.remote: case ItemType.remote:
assert(item.remoteDriveId && item.remoteId);
Item remoteItem; Item remoteItem;
bool found = itemdb.selectById(item.remoteDriveId, item.remoteId, remoteItem); bool found = itemdb.selectById(item.remoteDriveId, item.remoteId, remoteItem);
assert(found && remoteItem.type == ItemType.dir, "The remote item is not linked to a shared folder"); assert(found);
uploadDifferences(remoteItem); uploadDifferences(remoteItem);
break;
} }
} }
@ -501,9 +508,9 @@ final class SyncEngine
log.log("Uploading: ", path); log.log("Uploading: ", path);
JSONValue response; JSONValue response;
if (getSize(path) <= thresholdFileSize) { if (getSize(path) <= thresholdFileSize) {
writeln("upload by id");
response = onedrive.simpleUploadById(path, item.driveId, item.id, item.eTag); response = onedrive.simpleUploadById(path, item.driveId, item.id, item.eTag);
} else { } else {
// TODO: upload by id
response = session.upload(path, path, eTag); response = session.upload(path, path, eTag);
} }
/*saveItem(response); /*saveItem(response);
@ -529,7 +536,6 @@ final class SyncEngine
private void uploadNewItems(string path) private void uploadNewItems(string path)
{ {
writeln("uploadNewItems " ~ path);
// skip unexisting symbolic links // skip unexisting symbolic links
if (isSymlink(path) && !exists(readLink(path))) { if (isSymlink(path) && !exists(readLink(path))) {
return; return;