Fixed normalizePath() to strip out single dot dirs

Now removing "/./" and trailing "/." from the paths when normalizing.
This commit is contained in:
Vincent Petry 2013-11-14 13:15:03 +01:00
parent 8d0d0836ba
commit c06d8bb007
2 changed files with 71 additions and 4 deletions

View File

@ -689,18 +689,32 @@ class Filesystem {
}
//no windows style slashes
$path = str_replace('\\', '/', $path);
//add leading slash
if ($path[0] !== '/') {
$path = '/' . $path;
}
//remove duplicate slashes
while (strpos($path, '//') !== false) {
$path = str_replace('//', '/', $path);
// remove '/./'
// ugly, but str_replace() can't replace them all in one go
// as the replacement itself is part of the search string
// which will only be found during the next iteration
while (strpos($path, '/./') !== false) {
$path = str_replace('/./', '/', $path);
}
// remove sequences of slashes
$path = preg_replace('#/{2,}#', '/', $path);
//remove trailing slash
if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
$path = substr($path, 0, -1);
}
// remove trailing '/.'
if (substr($path, -2) == '/.') {
$path = substr($path, 0, -2);
}
//normalize unicode if possible
$path = \OC_Util::normalizeUnicode($path);

View File

@ -69,17 +69,70 @@ class Filesystem extends \PHPUnit_Framework_TestCase {
}
public function testNormalize() {
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/'));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('/', false));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//'));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('//', false));
$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('/path/'));
$this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('/path/', false));
$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('path'));
$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\path'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo//bar/'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo//bar/', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo////bar'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/////bar'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/.'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/./', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/./.'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/bar/././'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/bar/././', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('/foo/./bar/'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('/foo/./bar/', false));
$this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('/foo/.bar/'));
$this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('/foo/.bar/', false));
$this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('/foo/.bar/tee'));
// normalize does not resolve '..' (by design)
$this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('/foo/../'));
if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
}
}
public function testNormalizeWindowsPaths() {
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath(''));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\'));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\', false));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\'));
$this->assertEquals('/', \OC\Files\Filesystem::normalizePath('\\\\', false));
$this->assertEquals('/path', \OC\Files\Filesystem::normalizePath('\\path\\'));
$this->assertEquals('/path/', \OC\Files\Filesystem::normalizePath('\\path\\', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\\\bar\\', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\bar'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\\\\\\\\\bar'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.'));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\bar\\.\\.\\', false));
$this->assertEquals('/foo/bar', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\'));
$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.\\bar\\', false));
$this->assertEquals('/foo/.bar', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\'));
$this->assertEquals('/foo/.bar/', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\', false));
$this->assertEquals('/foo/.bar/tee', \OC\Files\Filesystem::normalizePath('\\foo\\.bar\\tee'));
// normalize does not resolve '..' (by design)
$this->assertEquals('/foo/..', \OC\Files\Filesystem::normalizePath('\\foo\\..\\'));
if (class_exists('Patchwork\PHP\Shim\Normalizer')) {
$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88"));
}
}
public function testHooks() {
if(\OC\Files\Filesystem::getView()){
$user = \OC_User::getUser();