merged from upstream

This commit is contained in:
Robert Foster 2018-07-11 06:40:49 +10:00
parent 4c1b445170
commit 8ac949d800
2 changed files with 28 additions and 35 deletions

View file

@ -5,7 +5,6 @@ import std.path;
import std.regex;
import std.stdio;
import util;
static import log;
final class SelectiveSync
{
@ -34,40 +33,16 @@ final class SelectiveSync
// Does the file match skip_file config entry?
// Returns true if the file matches a skip_file config entry
// Returns false if no match
log.dlog("Checking '", name, "' for exclusion...");
log.dlog(" Name matched in skip_file: ", !name.matchFirst(mask).empty);
return !name.matchFirst(mask).empty;
}
// config sync_list file handling
// also incorporates skip_file config parameter for expanded regex path matching
bool isPathExcluded(string path)
{
log.dlog("Checking '", path, "' for exclusion...");
log.dlog(" Path excluded in sync_list: ", .isPathExcluded(path, paths));
log.dlog(" Path matched in skip_file: ", .isPathMatched(path, mask));
return .isPathExcluded(path, paths) || .isPathMatched(path, mask);
}
}
// test if the given path is matched by the regex expression.
// recursively test up the tree.
private bool isPathMatched(string path, Regex!char mask) {
path = buildNormalizedPath(path);
auto paths = pathSplitter(path);
string prefix = "";
foreach(base; paths) {
prefix ~= base;
if (!path.matchFirst(mask).empty) {
log.dlog(" Path matched for '", prefix, "'");
return true;
}
prefix ~= dirSeparator;
}
return false;
}
// 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)
@ -92,6 +67,24 @@ private bool isPathExcluded(string path, string[] allowedPaths)
return true;
}
// test if the given path is matched by the regex expression.
// recursively test up the tree.
private bool isPathMatched(string path, Regex!char mask) {
path = buildNormalizedPath(path);
auto paths = pathSplitter(path);
string prefix = "";
foreach(base; paths) {
prefix ~= base;
if (!path.matchFirst(mask).empty) {
// the given path matches something which we should skip
return true;
}
prefix ~= dirSeparator;
}
return false;
}
unittest
{
assert(isPathExcluded("Documents2", ["Documents"]));

View file

@ -100,13 +100,13 @@ Regex!char wild2regex(const(char)[] pattern)
break;
case '+':
str ~= "\\+";
break;
break;
case ' ':
str ~= "\\s+";
break;
break;
case '/':
str ~= "\\/";
break;
break;
default:
str ~= c;
break;
@ -149,7 +149,7 @@ bool isValidName(string path)
// Restriction and limitations about windows naming files
// https://msdn.microsoft.com/en-us/library/aa365247
// https://support.microsoft.com/en-us/help/3125202/restrictions-and-limitations-when-you-sync-files-and-folders
// allow root item
if (path == ".") {
return true;
@ -169,14 +169,14 @@ bool isValidName(string path)
);
auto m = match(itemName, invalidNameReg);
matched = m.empty;
// Additional explicit validation checks
if (itemName == "Icon") {matched = false;}
if (itemName == ".lock") {matched = false;}
if (itemName == "desktop.ini") {matched = false;}
// _vti_ cannot appear anywhere in a file or folder name
if(canFind(itemName, "_vti_")){matched = false;}
// return response
return matched;
}
@ -187,7 +187,7 @@ bool containsBadWhiteSpace(string path)
if (path == ".") {
return true;
}
// https://github.com/abraunegg/onedrive/issues/35
// Issue #35 presented an interesting issue where the filename contained a newline item
// 'State-of-the-art, challenges, and open issues in the integration of Internet of'$'\n''Things and Cloud Computing.pdf'
@ -195,9 +195,9 @@ bool containsBadWhiteSpace(string path)
// /v1.0/me/drive/root:/.%2FState-of-the-art%2C%20challenges%2C%20and%20open%20issues%20in%20the%20integration%20of%20Internet%20of%0AThings%20and%20Cloud%20Computing.pdf
// The '$'\n'' is translated to %0A which causes the OneDrive query to fail
// Check for the presence of '%0A' via regex
string itemName = encodeComponent(baseName(path));
auto invalidWhitespaceReg =
ctRegex!(
// Check for \n which is %0A when encoded
@ -206,7 +206,7 @@ bool containsBadWhiteSpace(string path)
auto m = match(itemName, invalidWhitespaceReg);
return m.empty;
}