mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-15 20:25:18 +02:00
73 lines
1.7 KiB
D
73 lines
1.7 KiB
D
|
import std.algorithm;
|
||
|
import std.array;
|
||
|
import std.file;
|
||
|
import std.path;
|
||
|
import std.regex;
|
||
|
import std.stdio;
|
||
|
import util;
|
||
|
|
||
|
final class SelectiveSync
|
||
|
{
|
||
|
private string[] paths;
|
||
|
private Regex!char mask;
|
||
|
|
||
|
void load(string filepath)
|
||
|
{
|
||
|
if (exists(filepath)) {
|
||
|
paths = File(filepath)
|
||
|
.byLine()
|
||
|
.map!(a => buildNormalizedPath(a))
|
||
|
.filter!(a => a.length > 0)
|
||
|
.array;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void setMask(const(char)[] mask)
|
||
|
{
|
||
|
this.mask = wild2regex(mask);
|
||
|
}
|
||
|
|
||
|
bool isNameExcluded(string name)
|
||
|
{
|
||
|
return !name.matchFirst(mask).empty;
|
||
|
}
|
||
|
|
||
|
bool isPathExcluded(string path)
|
||
|
{
|
||
|
return .isPathExcluded(path, paths);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// test if the given path is not included in the allowed paths
|
||
|
// if there are no allowed paths always return false
|
||
|
private bool isPathExcluded(string path, string[] allowedPaths)
|
||
|
{
|
||
|
// always allow the root
|
||
|
if (path == ".") return false;
|
||
|
// if there are no allowed paths always return false
|
||
|
if (allowedPaths.empty) return false;
|
||
|
|
||
|
path = buildNormalizedPath(path);
|
||
|
foreach (allowed; allowedPaths) {
|
||
|
auto comm = commonPrefix(path, allowed);
|
||
|
if (comm.length == path.length) {
|
||
|
// the given path is contained in an allowed path
|
||
|
return false;
|
||
|
}
|
||
|
if (comm.length == allowed.length && path[comm.length] == '/') {
|
||
|
// the given path is a subitem of an allowed path
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
unittest
|
||
|
{
|
||
|
assert(isPathExcluded("Documents2", ["Documents"]));
|
||
|
assert(!isPathExcluded("Documents", ["Documents"]));
|
||
|
assert(!isPathExcluded("Documents/a.txt", ["Documents"]));
|
||
|
assert(isPathExcluded("Hello/World", ["Hello/John"]));
|
||
|
assert(!isPathExcluded(".", ["Documents"]));
|
||
|
}
|