mirror of
https://github.com/abraunegg/onedrive
synced 2024-06-13 03:12:37 +02:00
570d42269e
* Fix the handling of hidden filenames & directories (.hello, .git etc) so that these are uploaded / downloaded without issue. * Add verbose logging for uploaded files as to why they were skipped
80 lines
1.8 KiB
D
80 lines
1.8 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)
|
|
{
|
|
auto validName = isValidFilename(name);
|
|
if (validName){
|
|
// This is a valid filename - do NOT exclude
|
|
return false;
|
|
} else {
|
|
// Invalid file name - exclude this name
|
|
return true;
|
|
}
|
|
}
|
|
|
|
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"]));
|
|
}
|