skip filtered files in applyDiifferences

This commit is contained in:
skilion 2015-09-17 17:34:58 +02:00
parent 9867a7abea
commit 9aa1f221c7
3 changed files with 32 additions and 7 deletions

View file

@ -100,7 +100,7 @@ void main(string[] args)
if (verbose) writeln("[M] Item moved: ", from, " -> ", to); if (verbose) writeln("[M] Item moved: ", from, " -> ", to);
sync.uploadMoveItem(from, to); sync.uploadMoveItem(from, to);
}; };
m.init(verbose); m.init(cfg, verbose);
// monitor loop // monitor loop
immutable auto checkInterval = dur!"seconds"(45); immutable auto checkInterval = dur!"seconds"(45);
auto lastCheckTime = MonoTime.currTime(); auto lastCheckTime = MonoTime.currTime();
@ -112,7 +112,7 @@ void main(string[] args)
m.shutdown(); m.shutdown();
sync.applyDifferences(); sync.applyDifferences();
sync.uploadDifferences(); sync.uploadDifferences();
m.init(verbose); m.init(cfg, verbose);
} }
Thread.sleep(dur!"msecs"(100)); Thread.sleep(dur!"msecs"(100));
} }

View file

@ -1,7 +1,8 @@
import core.sys.linux.sys.inotify; import core.sys.linux.sys.inotify;
import core.sys.posix.poll; import core.sys.posix.poll;
import core.sys.posix.unistd; import core.sys.posix.unistd;
import std.exception, std.file, std.stdio, std.string; import std.exception, std.file, std.regex, std.stdio, std.string;
import config;
// relevant inotify events // relevant inotify events
private immutable uint32_t mask = IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE | private immutable uint32_t mask = IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE |
@ -18,6 +19,8 @@ class MonitorException: ErrnoException
struct Monitor struct Monitor
{ {
bool verbose; bool verbose;
// regexes that match files/dirs to skip
private Regex!char skipDir, skipFile;
// inotify file descriptor // inotify file descriptor
private int fd; private int fd;
// map every inotify watch descriptor to its directory // map every inotify watch descriptor to its directory
@ -34,9 +37,11 @@ struct Monitor
@disable this(this); @disable this(this);
void init(bool verbose) void init(Config cfg, bool verbose)
{ {
this.verbose = verbose; this.verbose = verbose;
skipDir = regex(cfg.get("skip_dir", ""));
skipFile = regex(cfg.get("skip_file", ""));
fd = inotify_init(); fd = inotify_init();
if (fd == -1) throw new MonitorException("inotify_init failed"); if (fd == -1) throw new MonitorException("inotify_init failed");
if (!buffer) buffer = new void[4096]; if (!buffer) buffer = new void[4096];
@ -53,7 +58,9 @@ struct Monitor
{ {
add(dirname); add(dirname);
foreach(DirEntry entry; dirEntries(dirname, SpanMode.breadth, false)) { foreach(DirEntry entry; dirEntries(dirname, SpanMode.breadth, false)) {
if (entry.isDir) add(entry.name); if (entry.isDir) {
add(entry.name);
}
} }
} }

View file

@ -1,5 +1,5 @@
import core.exception: RangeError; import core.exception: RangeError;
import std.algorithm, std.datetime, std.file, std.json, std.path, std.stdio; import std.algorithm, std.datetime, std.file, std.json, std.path, std.regex, std.stdio;
import config, itemdb, onedrive, util; import config, itemdb, onedrive, util;
private bool isItemFolder(const ref JSONValue item) private bool isItemFolder(const ref JSONValue item)
@ -50,6 +50,7 @@ final class SyncEngine
private OneDriveApi onedrive; private OneDriveApi onedrive;
private ItemDatabase itemdb; private ItemDatabase itemdb;
private bool verbose; private bool verbose;
private Regex!char skipDir, skipFile;
private string statusToken; private string statusToken;
private string[] skippedItems; private string[] skippedItems;
private string[] itemsToDelete; private string[] itemsToDelete;
@ -63,6 +64,8 @@ final class SyncEngine
this.onedrive = onedrive; this.onedrive = onedrive;
this.itemdb = itemdb; this.itemdb = itemdb;
this.verbose = verbose; this.verbose = verbose;
skipDir = regex(cfg.get("skip_dir", ""));
skipFile = regex(cfg.get("skip_file", ""));
} }
void setStatusToken(string statusToken) void setStatusToken(string statusToken)
@ -113,8 +116,18 @@ final class SyncEngine
return; return;
} else if (isItemFile(item)) { } else if (isItemFile(item)) {
type = ItemType.file; type = ItemType.file;
if (!matchFirst(name, skipFile).empty) {
if (verbose) writeln("Filtered out");
skippedItems ~= id;
return;
}
} else if (isItemFolder(item)) { } else if (isItemFolder(item)) {
type = ItemType.dir; type = ItemType.dir;
if (!matchFirst(name, skipDir).empty) {
if (verbose) writeln("Filtered out");
skippedItems ~= id;
return;
}
} else { } else {
if (verbose) writeln("The item is neither a file nor a directory, skipping"); if (verbose) writeln("The item is neither a file nor a directory, skipping");
skippedItems ~= id; skippedItems ~= id;
@ -312,7 +325,7 @@ final class SyncEngine
} }
} }
/* scan the specified directory for unsynced files and uplaod them /* scan the specified directory for unsynced files and upload them
NOTE: this function does not check for deleted files. */ NOTE: this function does not check for deleted files. */
public void uploadDifferences(string dirname) public void uploadDifferences(string dirname)
{ {
@ -324,6 +337,11 @@ final class SyncEngine
private void uploadDifference(Item item) private void uploadDifference(Item item)
{ {
if (verbose) writeln(item.id, " ", item.name); if (verbose) writeln(item.id, " ", item.name);
if (!matchFirst(name, skipFile).empty) {
if (verbose) writeln("Filtered out");
skippedItems ~= id;
return;
}
if (exists(item.path)) { if (exists(item.path)) {
final switch (item.type) { final switch (item.type) {
case ItemType.file: case ItemType.file: