uploadDifferences skeleton

This commit is contained in:
skilion 2015-09-04 21:00:22 +02:00
parent 9dea1c1448
commit 3357a013f7

View file

@ -60,7 +60,7 @@ final class SyncEngine
} catch (FileException e) { } catch (FileException e) {
writeln("Welcome !"); writeln("Welcome !");
} }
writeln("Checking for changes..."); writeln("Applying differences ...");
string currDir = getcwd(); string currDir = getcwd();
string syncDir = cfg.get("sync_dir"); string syncDir = cfg.get("sync_dir");
@ -150,7 +150,7 @@ final class SyncEngine
private void applyDelete(Item item) private void applyDelete(Item item)
{ {
if (exists(item.path)) { if (exists(item.path)) {
if (isItemSynced(item, true)) { if (isItemSynced(item)) {
addFileToDelete(item.path); addFileToDelete(item.path);
} else { } else {
writeln("The local item is not synced, renaming ..."); writeln("The local item is not synced, renaming ...");
@ -164,7 +164,7 @@ final class SyncEngine
{ {
assert(item.id); assert(item.id);
if (exists(item.path)) { if (exists(item.path)) {
if (isItemSynced(item, true)) { if (isItemSynced(item)) {
writeln("The item is already present"); writeln("The item is already present");
// ensure the modified time is synced // ensure the modified time is synced
setTimes(item.path, item.mtime, item.mtime); setTimes(item.path, item.mtime, item.mtime);
@ -226,7 +226,7 @@ final class SyncEngine
} }
// returns true if the given item corresponds to the local one // returns true if the given item corresponds to the local one
private bool isItemSynced(Item item, bool checkHash = false) private bool isItemSynced(Item item)
{ {
final switch (item.type) { final switch (item.type) {
case ItemType.file: case ItemType.file:
@ -238,7 +238,7 @@ final class SyncEngine
else { else {
writeln("The local item has a different modified time ", localModifiedTime, " remote is ", item.mtime); writeln("The local item has a different modified time ", localModifiedTime, " remote is ", item.mtime);
} }
if (checkHash && item.crc32) { if (item.crc32) {
string localCrc32 = computeCrc32(item.path); string localCrc32 = computeCrc32(item.path);
if (localCrc32 == item.crc32) return true; if (localCrc32 == item.crc32) return true;
else { else {
@ -277,4 +277,31 @@ final class SyncEngine
assumeSafeAppend(itemToDelete); assumeSafeAppend(itemToDelete);
itemToDelete.length = 0; itemToDelete.length = 0;
} }
// scan the directory for unsynced files and upload them
public void uploadDifferences()
{
writeln("Uploading differences ...");
string currDir = getcwd();
string syncDir = cfg.get("sync_dir");
chdir(syncDir);
foreach (DirEntry entry; dirEntries(".", SpanMode.breadth, false)) {
uploadDifference(entry.name[2 .. $]);
}
// TODO: check deleted files
chdir(currDir);
}
private void uploadDifference(const(char)[] path)
{
assert(exists(path));
Item item;
if (itemCache.selectByPath(path, item)) {
if (!isItemSynced(item)) {
onedrive.simpleUpload(path.dup, path, item.eTag);
}
} else {
onedrive.simpleUpload(path.dup, path);
}
}
} }