From 9aa1f221c7b54b3a4c5d75b7fc3bb6120c15ad0c Mon Sep 17 00:00:00 2001 From: skilion Date: Thu, 17 Sep 2015 17:34:58 +0200 Subject: [PATCH] skip filtered files in applyDiifferences --- src/main.d | 4 ++-- src/monitor.d | 13 ++++++++++--- src/sync.d | 22 ++++++++++++++++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main.d b/src/main.d index 7008d186..63defcc9 100644 --- a/src/main.d +++ b/src/main.d @@ -100,7 +100,7 @@ void main(string[] args) if (verbose) writeln("[M] Item moved: ", from, " -> ", to); sync.uploadMoveItem(from, to); }; - m.init(verbose); + m.init(cfg, verbose); // monitor loop immutable auto checkInterval = dur!"seconds"(45); auto lastCheckTime = MonoTime.currTime(); @@ -112,7 +112,7 @@ void main(string[] args) m.shutdown(); sync.applyDifferences(); sync.uploadDifferences(); - m.init(verbose); + m.init(cfg, verbose); } Thread.sleep(dur!"msecs"(100)); } diff --git a/src/monitor.d b/src/monitor.d index 3d5f3a9d..0c065e79 100644 --- a/src/monitor.d +++ b/src/monitor.d @@ -1,7 +1,8 @@ import core.sys.linux.sys.inotify; import core.sys.posix.poll; 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 private immutable uint32_t mask = IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE | @@ -18,6 +19,8 @@ class MonitorException: ErrnoException struct Monitor { bool verbose; + // regexes that match files/dirs to skip + private Regex!char skipDir, skipFile; // inotify file descriptor private int fd; // map every inotify watch descriptor to its directory @@ -34,9 +37,11 @@ struct Monitor @disable this(this); - void init(bool verbose) + void init(Config cfg, bool verbose) { this.verbose = verbose; + skipDir = regex(cfg.get("skip_dir", "")); + skipFile = regex(cfg.get("skip_file", "")); fd = inotify_init(); if (fd == -1) throw new MonitorException("inotify_init failed"); if (!buffer) buffer = new void[4096]; @@ -53,7 +58,9 @@ struct Monitor { add(dirname); foreach(DirEntry entry; dirEntries(dirname, SpanMode.breadth, false)) { - if (entry.isDir) add(entry.name); + if (entry.isDir) { + add(entry.name); + } } } diff --git a/src/sync.d b/src/sync.d index e9376fb5..9f89d91a 100644 --- a/src/sync.d +++ b/src/sync.d @@ -1,5 +1,5 @@ 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; private bool isItemFolder(const ref JSONValue item) @@ -50,6 +50,7 @@ final class SyncEngine private OneDriveApi onedrive; private ItemDatabase itemdb; private bool verbose; + private Regex!char skipDir, skipFile; private string statusToken; private string[] skippedItems; private string[] itemsToDelete; @@ -63,6 +64,8 @@ final class SyncEngine this.onedrive = onedrive; this.itemdb = itemdb; this.verbose = verbose; + skipDir = regex(cfg.get("skip_dir", "")); + skipFile = regex(cfg.get("skip_file", "")); } void setStatusToken(string statusToken) @@ -113,8 +116,18 @@ final class SyncEngine return; } else if (isItemFile(item)) { type = ItemType.file; + if (!matchFirst(name, skipFile).empty) { + if (verbose) writeln("Filtered out"); + skippedItems ~= id; + return; + } } else if (isItemFolder(item)) { type = ItemType.dir; + if (!matchFirst(name, skipDir).empty) { + if (verbose) writeln("Filtered out"); + skippedItems ~= id; + return; + } } else { if (verbose) writeln("The item is neither a file nor a directory, skipping"); 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. */ public void uploadDifferences(string dirname) { @@ -324,6 +337,11 @@ final class SyncEngine private void uploadDifference(Item item) { if (verbose) writeln(item.id, " ", item.name); + if (!matchFirst(name, skipFile).empty) { + if (verbose) writeln("Filtered out"); + skippedItems ~= id; + return; + } if (exists(item.path)) { final switch (item.type) { case ItemType.file: