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
This commit is contained in:
abraunegg 2018-07-02 14:05:21 +10:00 committed by GitHub
parent 8d13d3e850
commit 871b3d72c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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