mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-04 23:12:18 +02:00
better exception management
This commit is contained in:
parent
4498c03dec
commit
d2ec6c688f
43
src/main.d
43
src/main.d
|
@ -1,4 +1,4 @@
|
|||
import core.time, core.thread;
|
||||
import core.memory, core.time, core.thread;
|
||||
import std.getopt, std.file, std.path, std.process, std.stdio;
|
||||
import config, itemdb, monitor, onedrive, sync;
|
||||
|
||||
|
@ -70,15 +70,18 @@ void main(string[] args)
|
|||
|
||||
string syncDir = expandTilde(cfg.get("sync_dir"));
|
||||
chdir(syncDir);
|
||||
sync.applyDifferences();
|
||||
sync.scanForDifferences(".");
|
||||
performSync(sync);
|
||||
|
||||
if (monitor) {
|
||||
if (verbose) writeln("Initializing monitor ...");
|
||||
Monitor m;
|
||||
m.onDirCreated = delegate(string path) {
|
||||
if (verbose) writeln("[M] Directory created: ", path);
|
||||
sync.scanForDifferences(path);
|
||||
try {
|
||||
sync.scanForDifferences(path);
|
||||
} catch(SyncException e) {
|
||||
writeln(e.msg);
|
||||
}
|
||||
};
|
||||
m.onFileChanged = delegate(string path) {
|
||||
if (verbose) writeln("[M] File changed: ", path);
|
||||
|
@ -90,11 +93,19 @@ void main(string[] args)
|
|||
};
|
||||
m.onDelete = delegate(string path) {
|
||||
if (verbose) writeln("[M] Item deleted: ", path);
|
||||
sync.deleteByPath(path);
|
||||
try {
|
||||
sync.deleteByPath(path);
|
||||
} catch(SyncException e) {
|
||||
writeln(e.msg);
|
||||
}
|
||||
};
|
||||
m.onMove = delegate(string from, string to) {
|
||||
if (verbose) writeln("[M] Item moved: ", from, " -> ", to);
|
||||
sync.uploadMoveItem(from, to);
|
||||
try {
|
||||
sync.uploadMoveItem(from, to);
|
||||
} catch(SyncException e) {
|
||||
writeln(e.msg);
|
||||
}
|
||||
};
|
||||
m.init(cfg, verbose);
|
||||
// monitor loop
|
||||
|
@ -106,10 +117,8 @@ void main(string[] args)
|
|||
if (currTime - lastCheckTime > checkInterval) {
|
||||
lastCheckTime = currTime;
|
||||
m.shutdown();
|
||||
sync.applyDifferences();
|
||||
sync.scanForDifferences(".");
|
||||
performSync(sync);
|
||||
m.init(cfg, verbose);
|
||||
import core.memory;
|
||||
GC.collect();
|
||||
} else {
|
||||
Thread.sleep(dur!"msecs"(100));
|
||||
|
@ -117,3 +126,19 @@ void main(string[] args)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to synchronize the folder three times
|
||||
void performSync(SyncEngine sync)
|
||||
{
|
||||
int count;
|
||||
do {
|
||||
try {
|
||||
sync.applyDifferences();
|
||||
sync.scanForDifferences(".");
|
||||
count = -1;
|
||||
} catch (SyncException e) {
|
||||
if (++count == 3) throw e;
|
||||
else writeln(e.msg);
|
||||
}
|
||||
} while (count != -1);
|
||||
}
|
||||
|
|
44
src/sync.d
44
src/sync.d
|
@ -261,8 +261,6 @@ final class SyncEngine
|
|||
// returns true if the given item corresponds to the local one
|
||||
private bool isItemSynced(Item item, string path)
|
||||
{
|
||||
import std.stdio;
|
||||
writeln(path);
|
||||
if (!exists(path)) return false;
|
||||
final switch (item.type) {
|
||||
case ItemType.file:
|
||||
|
@ -320,16 +318,22 @@ final class SyncEngine
|
|||
// scan the given directory for differences
|
||||
public void scanForDifferences(string path)
|
||||
{
|
||||
if (verbose) writeln("Uploading differences ...");
|
||||
Item item;
|
||||
if (itemdb.selectByPath(path, item)) {
|
||||
uploadDifferences(item);
|
||||
try {
|
||||
if (verbose) writeln("Uploading differences ...");
|
||||
Item item;
|
||||
if (itemdb.selectByPath(path, item)) {
|
||||
uploadDifferences(item);
|
||||
}
|
||||
if (verbose) writeln("Uploading new items ...");
|
||||
uploadNewItems(path);
|
||||
} catch (FileException e) {
|
||||
throw new SyncException(e.msg, e);
|
||||
} catch (OneDriveException e) {
|
||||
throw new SyncException(e.msg, e);
|
||||
}
|
||||
if (verbose) writeln("Uploading new items ...");
|
||||
uploadNewItems(path);
|
||||
}
|
||||
|
||||
public void uploadDifferences(Item item)
|
||||
private void uploadDifferences(Item item)
|
||||
{
|
||||
if (verbose) writeln(item.id, " ", item.name);
|
||||
string path = itemdb.computePath(item.id);
|
||||
|
@ -442,23 +446,11 @@ final class SyncEngine
|
|||
private void uploadNewFile(string path)
|
||||
{
|
||||
writeln("Uploading: ", path);
|
||||
JSONValue res;
|
||||
try {
|
||||
res = onedrive.simpleUpload(path, path);
|
||||
} catch (OneDriveException e) {
|
||||
writeln(e.msg);
|
||||
return;
|
||||
}
|
||||
JSONValue res = onedrive.simpleUpload(path, path);
|
||||
saveItem(res);
|
||||
string id = res["id"].str;
|
||||
string eTag = res["eTag"].str;
|
||||
SysTime mtime;
|
||||
try {
|
||||
mtime = timeLastModified(path).toUTC();
|
||||
} catch (FileException e) {
|
||||
writeln(e.msg);
|
||||
return;
|
||||
}
|
||||
SysTime mtime = timeLastModified(path).toUTC();
|
||||
uploadLastModifiedTime(id, eTag, mtime);
|
||||
}
|
||||
|
||||
|
@ -514,16 +506,14 @@ final class SyncEngine
|
|||
writeln("Moving remote item: ", from, " -> ", to);
|
||||
Item fromItem, toItem, parentItem;
|
||||
if (!itemdb.selectByPath(from, fromItem)) {
|
||||
writeln("Can't move an unsynced item");
|
||||
return;
|
||||
throw new SyncException("Can't move an unsynced item");
|
||||
}
|
||||
if (itemdb.selectByPath(to, toItem)) {
|
||||
// the destination has been overridden
|
||||
uploadDeleteItem(toItem, to);
|
||||
}
|
||||
if (!itemdb.selectByPath(to.dirName, parentItem)) {
|
||||
writeln("Can't move an item to an unsynced directory");
|
||||
return;
|
||||
throw new SyncException("Can't move an item to an unsynced directory");
|
||||
}
|
||||
JSONValue diff = ["name": baseName(to)];
|
||||
diff["parentReference"] = JSONValue([
|
||||
|
|
Loading…
Reference in a new issue