Merge pull request #12462 from owncloud/issue/12460-localstorage-buildpath

Introduce buildPath() in Storage\Local to reduce the difference to MappedLocal
This commit is contained in:
Vincent Petry 2014-11-27 14:57:08 +01:00
commit e733d32eec
2 changed files with 79 additions and 63 deletions

View File

@ -35,7 +35,7 @@ if (\OC_Util::runningOnWindows()) {
}
public function mkdir($path) {
return @mkdir($this->datadir . $path, 0777, true);
return @mkdir($this->getSourcePath($path), 0777, true);
}
public function rmdir($path) {
@ -44,7 +44,7 @@ if (\OC_Util::runningOnWindows()) {
}
try {
$it = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->datadir . $path),
new \RecursiveDirectoryIterator($this->getSourcePath($path)),
\RecursiveIteratorIterator::CHILD_FIRST
);
/**
@ -68,30 +68,30 @@ if (\OC_Util::runningOnWindows()) {
}
$it->next();
}
return rmdir($this->datadir . $path);
return rmdir($this->getSourcePath($path));
} catch (\UnexpectedValueException $e) {
return false;
}
}
public function opendir($path) {
return opendir($this->datadir . $path);
return opendir($this->getSourcePath($path));
}
public function is_dir($path) {
if (substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
return is_dir($this->datadir . $path);
return is_dir($this->getSourcePath($path));
}
public function is_file($path) {
return is_file($this->datadir . $path);
return is_file($this->getSourcePath($path));
}
public function stat($path) {
clearstatcache();
$fullPath = $this->datadir . $path;
$fullPath = $this->getSourcePath($path);
$statResult = stat($fullPath);
if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
$filesize = $this->filesize($path);
@ -102,9 +102,9 @@ if (\OC_Util::runningOnWindows()) {
}
public function filetype($path) {
$filetype = filetype($this->datadir . $path);
$filetype = filetype($this->getSourcePath($path));
if ($filetype == 'link') {
$filetype = filetype(realpath($this->datadir . $path));
$filetype = filetype(realpath($this->getSourcePath($path)));
}
return $filetype;
}
@ -113,7 +113,7 @@ if (\OC_Util::runningOnWindows()) {
if ($this->is_dir($path)) {
return 0;
}
$fullPath = $this->datadir . $path;
$fullPath = $this->getSourcePath($path);
if (PHP_INT_SIZE === 4) {
$helper = new \OC\LargeFileHelper;
return $helper->getFilesize($fullPath);
@ -122,19 +122,19 @@ if (\OC_Util::runningOnWindows()) {
}
public function isReadable($path) {
return is_readable($this->datadir . $path);
return is_readable($this->getSourcePath($path));
}
public function isUpdatable($path) {
return is_writable($this->datadir . $path);
return is_writable($this->getSourcePath($path));
}
public function file_exists($path) {
return file_exists($this->datadir . $path);
return file_exists($this->getSourcePath($path));
}
public function filemtime($path) {
return filemtime($this->datadir . $path);
return filemtime($this->getSourcePath($path));
}
public function touch($path, $mtime = null) {
@ -145,30 +145,30 @@ if (\OC_Util::runningOnWindows()) {
return false;
}
if (!is_null($mtime)) {
$result = touch($this->datadir . $path, $mtime);
$result = touch($this->getSourcePath($path), $mtime);
} else {
$result = touch($this->datadir . $path);
$result = touch($this->getSourcePath($path));
}
if ($result) {
clearstatcache(true, $this->datadir . $path);
clearstatcache(true, $this->getSourcePath($path));
}
return $result;
}
public function file_get_contents($path) {
return file_get_contents($this->datadir . $path);
return file_get_contents($this->getSourcePath($path));
}
public function file_put_contents($path, $data) { //trigger_error("$path = ".var_export($path, 1));
return file_put_contents($this->datadir . $path, $data);
public function file_put_contents($path, $data) {
return file_put_contents($this->getSourcePath($path), $data);
}
public function unlink($path) {
if ($this->is_dir($path)) {
return $this->rmdir($path);
} else if ($this->is_file($path)) {
return unlink($this->datadir . $path);
return unlink($this->getSourcePath($path));
} else {
return false;
}
@ -200,27 +200,27 @@ if (\OC_Util::runningOnWindows()) {
$this->unlink($path2);
}
return rename($this->datadir . $path1, $this->datadir . $path2);
return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
}
public function copy($path1, $path2) {
if ($this->is_dir($path1)) {
return parent::copy($path1, $path2);
} else {
return copy($this->datadir . $path1, $this->datadir . $path2);
return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
}
}
public function fopen($path, $mode) {
return fopen($this->datadir . $path, $mode);
return fopen($this->getSourcePath($path), $mode);
}
public function hash($type, $path, $raw = false) {
return hash_file($type, $this->datadir . $path, $raw);
return hash_file($type, $this->getSourcePath($path), $raw);
}
public function free_space($path) {
$space = @disk_free_space($this->datadir . $path);
$space = @disk_free_space($this->getSourcePath($path));
if ($space === false || is_null($space)) {
return \OCP\Files\FileInfo::SPACE_UNKNOWN;
}
@ -232,11 +232,11 @@ if (\OC_Util::runningOnWindows()) {
}
public function getLocalFile($path) {
return $this->datadir . $path;
return $this->getSourcePath($path);
}
public function getLocalFolder($path) {
return $this->datadir . $path;
return $this->getSourcePath($path);
}
/**
@ -244,12 +244,16 @@ if (\OC_Util::runningOnWindows()) {
*/
protected function searchInDir($query, $dir = '') {
$files = array();
foreach (scandir($this->datadir . $dir) as $item) {
if ($item == '.' || $item == '..') continue;
$physicalDir = $this->getSourcePath($dir);
foreach (scandir($physicalDir) as $item) {
if ($item == '.' || $item == '..')
continue;
$physicalItem = $physicalDir . '/' . $item;
if (strstr(strtolower($item), strtolower($query)) !== false) {
$files[] = $dir . '/' . $item;
}
if (is_dir($this->datadir . $dir . '/' . $item)) {
if (is_dir($physicalItem)) {
$files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
}
}
@ -271,6 +275,17 @@ if (\OC_Util::runningOnWindows()) {
}
}
/**
* Get the source path (on disk) of a given path
*
* @param string $path
* @return string
*/
protected function getSourcePath($path) {
$fullPath = $this->datadir . $path;
return $fullPath;
}
/**
* {@inheritdoc}
*/

View File

@ -31,7 +31,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function mkdir($path) {
return @mkdir($this->buildPath($path), 0777, true);
return @mkdir($this->getSourcePath($path), 0777, true);
}
public function rmdir($path) {
@ -40,7 +40,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
try {
$it = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->buildPath($path)),
new \RecursiveDirectoryIterator($this->getSourcePath($path)),
\RecursiveIteratorIterator::CHILD_FIRST
);
/**
@ -64,7 +64,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
$it->next();
}
if ($result = @rmdir($this->buildPath($path))) {
if ($result = @rmdir($this->getSourcePath($path))) {
$this->cleanMapper($path);
}
return $result;
@ -75,7 +75,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
public function opendir($path) {
$files = array('.', '..');
$physicalPath = $this->buildPath($path);
$physicalPath = $this->getSourcePath($path);
$logicalPath = $this->mapper->physicalToLogic($physicalPath);
$dh = opendir($physicalPath);
@ -101,15 +101,15 @@ class MappedLocal extends \OC\Files\Storage\Common {
if (substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
return is_dir($this->buildPath($path));
return is_dir($this->getSourcePath($path));
}
public function is_file($path) {
return is_file($this->buildPath($path));
return is_file($this->getSourcePath($path));
}
public function stat($path) {
$fullPath = $this->buildPath($path);
$fullPath = $this->getSourcePath($path);
$statResult = stat($fullPath);
if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
$filesize = $this->filesize($path);
@ -120,9 +120,9 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function filetype($path) {
$filetype = filetype($this->buildPath($path));
$filetype = filetype($this->getSourcePath($path));
if ($filetype == 'link') {
$filetype = filetype(realpath($this->buildPath($path)));
$filetype = filetype(realpath($this->getSourcePath($path)));
}
return $filetype;
}
@ -131,7 +131,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
if ($this->is_dir($path)) {
return 0;
}
$fullPath = $this->buildPath($path);
$fullPath = $this->getSourcePath($path);
if (PHP_INT_SIZE === 4) {
$helper = new \OC\LargeFileHelper;
return $helper->getFilesize($fullPath);
@ -140,19 +140,19 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function isReadable($path) {
return is_readable($this->buildPath($path));
return is_readable($this->getSourcePath($path));
}
public function isUpdatable($path) {
return is_writable($this->buildPath($path));
return is_writable($this->getSourcePath($path));
}
public function file_exists($path) {
return file_exists($this->buildPath($path));
return file_exists($this->getSourcePath($path));
}
public function filemtime($path) {
return filemtime($this->buildPath($path));
return filemtime($this->getSourcePath($path));
}
public function touch($path, $mtime = null) {
@ -160,23 +160,23 @@ class MappedLocal extends \OC\Files\Storage\Common {
// If mtime is nil the current time is set.
// note that the access time of the file always changes to the current time.
if (!is_null($mtime)) {
$result = touch($this->buildPath($path), $mtime);
$result = touch($this->getSourcePath($path), $mtime);
} else {
$result = touch($this->buildPath($path));
$result = touch($this->getSourcePath($path));
}
if ($result) {
clearstatcache(true, $this->buildPath($path));
clearstatcache(true, $this->getSourcePath($path));
}
return $result;
}
public function file_get_contents($path) {
return file_get_contents($this->buildPath($path));
return file_get_contents($this->getSourcePath($path));
}
public function file_put_contents($path, $data) {
return file_put_contents($this->buildPath($path), $data);
return file_put_contents($this->getSourcePath($path), $data);
}
public function unlink($path) {
@ -208,8 +208,8 @@ class MappedLocal extends \OC\Files\Storage\Common {
$this->unlink($path2);
}
$physicPath1 = $this->buildPath($path1);
$physicPath2 = $this->buildPath($path2);
$physicPath1 = $this->getSourcePath($path1);
$physicPath2 = $this->getSourcePath($path2);
if ($return = rename($physicPath1, $physicPath2)) {
// mapper needs to create copies or all children
$this->copyMapping($path1, $path2);
@ -237,7 +237,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
closedir($dir);
return true;
} else {
if ($return = copy($this->buildPath($path1), $this->buildPath($path2))) {
if ($return = copy($this->getSourcePath($path1), $this->getSourcePath($path2))) {
$this->copyMapping($path1, $path2);
}
return $return;
@ -245,7 +245,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function fopen($path, $mode) {
return fopen($this->buildPath($path), $mode);
return fopen($this->getSourcePath($path), $mode);
}
/**
@ -256,7 +256,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
private function delTree($dir, $isLogicPath = true) {
$dirRelative = $dir;
if ($isLogicPath) {
$dir = $this->buildPath($dir);
$dir = $this->getSourcePath($dir);
}
if (!file_exists($dir)) {
return true;
@ -288,11 +288,11 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function hash($type, $path, $raw = false) {
return hash_file($type, $this->buildPath($path), $raw);
return hash_file($type, $this->getSourcePath($path), $raw);
}
public function free_space($path) {
return @disk_free_space($this->buildPath($path));
return @disk_free_space($this->getSourcePath($path));
}
public function search($query) {
@ -300,11 +300,11 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
public function getLocalFile($path) {
return $this->buildPath($path);
return $this->getSourcePath($path);
}
public function getLocalFolder($path) {
return $this->buildPath($path);
return $this->getSourcePath($path);
}
/**
@ -312,7 +312,7 @@ class MappedLocal extends \OC\Files\Storage\Common {
*/
protected function searchInDir($query, $dir = '') {
$files = array();
$physicalDir = $this->buildPath($dir);
$physicalDir = $this->getSourcePath($dir);
foreach (scandir($physicalDir) as $item) {
if ($item == '.' || $item == '..')
continue;
@ -341,14 +341,15 @@ class MappedLocal extends \OC\Files\Storage\Common {
}
/**
* Get the source path (on disk) of a given path
*
* @param string $path
* @param bool $create
* @return string
*/
private function buildPath($path, $create = true) {
protected function getSourcePath($path) {
$path = $this->stripLeading($path);
$fullPath = $this->datadir . $path;
return $this->mapper->logicToPhysical($fullPath, $create);
return $this->mapper->logicToPhysical($fullPath, true);
}
/**