From 871b3d72c4903513c840a93b342597f2cb136bc9 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Mon, 2 Jul 2018 14:05:21 +1000 Subject: [PATCH] Fix: Name checking rule not exactly right (Issue #34) (#38) * Fix regex for name checks to be more explicit * Update name validation checks to be more compliant with Microsoft requirements --- src/util.d | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/util.d b/src/util.d index d77f5bc4..34dc8895 100644 --- a/src/util.d +++ b/src/util.d @@ -9,6 +9,7 @@ import std.regex; import std.socket; import std.stdio; import std.string; +import std.algorithm; import qxor; private string deviceName; @@ -134,28 +135,39 @@ bool multiGlobMatch(const(char)[] path, const(char)[] pattern) 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; } + bool matched = true; string itemName = baseName(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 auto invalidNameReg = ctRegex!( - // leading whitespace and trailing whitespace/dot + // Leading whitespace and trailing whitespace/dot `^\s.*|^.*[\s\.]$|` ~ - // invalid character + // Invalid characters `.*[<>:"\|\?*/\\].*|` ~ - // reserved device name and trailing .~ - `(?:CON|PRN|AUX|NUL|COM[0-9]|LPT[0-9])(?:[.].+)?$` + // Reserved device name and trailing .~ + `(?:^CON|^PRN|^AUX|^NUL|^COM[0-9]|^LPT[0-9])(?:[.].+)?$` ); auto m = match(itemName, invalidNameReg); - - return m.empty; + 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; } unittest