fix namespaces in filesystem and filesystemview

This commit is contained in:
Robin Appelman 2012-10-10 12:25:46 +02:00
parent 9df60d27bd
commit 07c5384189
3 changed files with 225 additions and 194 deletions

View File

@ -714,16 +714,16 @@ class OC_App{
/** /**
* @param string $appid * @param string $appid
* @return OC_FilesystemView * @return \OC\Files\View
*/ */
public static function getStorage($appid) { public static function getStorage($appid) {
if(OC_App::isEnabled($appid)) {//sanity check if(OC_App::isEnabled($appid)) {//sanity check
if(OC_User::isLoggedIn()) { if(OC_User::isLoggedIn()) {
$view = new OC_FilesystemView('/'.OC_User::getUser()); $view = new \OC\Files\View('/'.OC_User::getUser());
if(!$view->file_exists($appid)) { if(!$view->file_exists($appid)) {
$view->mkdir($appid); $view->mkdir($appid);
} }
return new OC_FilesystemView('/'.OC_User::getUser().'/'.$appid); return new \OC\Files\View('/'.OC_User::getUser().'/'.$appid);
}else{ }else{
OC_Log::write('core', 'Can\'t get app storage, app, user not logged in', OC_Log::ERROR); OC_Log::write('core', 'Can\'t get app storage, app, user not logged in', OC_Log::ERROR);
return false; return false;

View File

@ -15,16 +15,16 @@
* read(path) * read(path)
* write(path, &run) * write(path, &run)
* post_write(path) * post_write(path)
* create(path, &run) (when a file is created, both create and write will be emited in that order) * create(path, &run) (when a file is created, both create and write will be emitted in that order)
* post_create(path) * post_create(path)
* delete(path, &run) * delete(path, &run)
* post_delete(path) * post_delete(path)
* rename(oldpath,newpath, &run) * rename(oldpath,newpath, &run)
* post_rename(oldpath,newpath) * post_rename(oldpath,newpath)
* copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emited in that order) * copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emitted in that order)
* post_rename(oldpath,newpath) * post_rename(oldpath,newpath)
* *
* the &run parameter can be set to false to prevent the operation from occuring * the &run parameter can be set to false to prevent the operation from occurring
*/ */
namespace OC\Files; namespace OC\Files;
@ -34,7 +34,7 @@ class Filesystem {
static private $mounts = array(); static private $mounts = array();
public static $loaded = false; public static $loaded = false;
/** /**
* @var \OC\Files\Storage\Storage $defaultInstance * @var \OC\Files\View $defaultInstance
*/ */
static private $defaultInstance; static private $defaultInstance;
@ -46,80 +46,80 @@ class Filesystem {
const CLASSNAME = 'OC_Filesystem'; const CLASSNAME = 'OC_Filesystem';
/** /**
* signalname emited before file renaming * signalname emitted before file renaming
* *
* @param oldpath * @param string $oldpath
* @param newpath * @param string $newpath
*/ */
const signal_rename = 'rename'; const signal_rename = 'rename';
/** /**
* signal emited after file renaming * signal emitted after file renaming
* *
* @param oldpath * @param string $oldpath
* @param newpath * @param string $newpath
*/ */
const signal_post_rename = 'post_rename'; const signal_post_rename = 'post_rename';
/** /**
* signal emited before file/dir creation * signal emitted before file/dir creation
* *
* @param path * @param string $path
* @param run changing this flag to false in hook handler will cancel event * @param bool $run changing this flag to false in hook handler will cancel event
*/ */
const signal_create = 'create'; const signal_create = 'create';
/** /**
* signal emited after file/dir creation * signal emitted after file/dir creation
* *
* @param path * @param string $path
* @param run changing this flag to false in hook handler will cancel event * @param bool $run changing this flag to false in hook handler will cancel event
*/ */
const signal_post_create = 'post_create'; const signal_post_create = 'post_create';
/** /**
* signal emits before file/dir copy * signal emits before file/dir copy
* *
* @param oldpath * @param string $oldpath
* @param newpath * @param string $newpath
* @param run changing this flag to false in hook handler will cancel event * @param bool $run changing this flag to false in hook handler will cancel event
*/ */
const signal_copy = 'copy'; const signal_copy = 'copy';
/** /**
* signal emits after file/dir copy * signal emits after file/dir copy
* *
* @param oldpath * @param string $oldpath
* @param newpath * @param string $newpath
*/ */
const signal_post_copy = 'post_copy'; const signal_post_copy = 'post_copy';
/** /**
* signal emits before file/dir save * signal emits before file/dir save
* *
* @param path * @param string $path
* @param run changing this flag to false in hook handler will cancel event * @param bool $run changing this flag to false in hook handler will cancel event
*/ */
const signal_write = 'write'; const signal_write = 'write';
/** /**
* signal emits after file/dir save * signal emits after file/dir save
* *
* @param path * @param string $path
*/ */
const signal_post_write = 'post_write'; const signal_post_write = 'post_write';
/** /**
* signal emits when reading file/dir * signal emits when reading file/dir
* *
* @param path * @param string $path
*/ */
const signal_read = 'read'; const signal_read = 'read';
/** /**
* signal emits when removing file/dir * signal emits when removing file/dir
* *
* @param path * @param string $path
*/ */
const signal_delete = 'delete'; const signal_delete = 'delete';
@ -139,11 +139,11 @@ class Filesystem {
* get the mountpoint of the storage object for a path * get the mountpoint of the storage object for a path
( note: because a storage is not always mounted inside the fakeroot, the returned mountpoint is relative to the absolute root of the filesystem and doesn't take the chroot into account ( note: because a storage is not always mounted inside the fakeroot, the returned mountpoint is relative to the absolute root of the filesystem and doesn't take the chroot into account
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
static public function getMountPoint($path) { static public function getMountPoint($path) {
OC_Hook::emit(self::CLASSNAME, 'get_mountpoint', array('path' => $path)); \OC_Hook::emit(self::CLASSNAME, 'get_mountpoint', array('path' => $path));
if (!$path) { if (!$path) {
$path = '/'; $path = '/';
} }
@ -152,7 +152,7 @@ class Filesystem {
} }
$path = str_replace('//', '/', $path); $path = str_replace('//', '/', $path);
$foundMountPoint = ''; $foundMountPoint = '';
$mountPoints = array_keys(OC_Filesystem::$mounts); $mountPoints = array_keys(self::$mounts);
foreach ($mountPoints as $mountpoint) { foreach ($mountPoints as $mountpoint) {
if ($mountpoint == $path) { if ($mountpoint == $path) {
return $mountpoint; return $mountpoint;
@ -167,7 +167,7 @@ class Filesystem {
/** /**
* get the part of the path relative to the mountpoint of the storage it's stored in * get the part of the path relative to the mountpoint of the storage it's stored in
* *
* @param string path * @param string $path
* @return bool * @return bool
*/ */
static public function getInternalPath($path) { static public function getInternalPath($path) {
@ -179,17 +179,19 @@ class Filesystem {
/** /**
* get the storage object for a path * get the storage object for a path
* *
* @param string path * @param string $path
* @return \OC\Files\Storage\Storage * @return \OC\Files\Storage\Storage
*/ */
static public function getStorage($path) { static public function getStorage($path) {
$mountpoint = self::getMountPoint($path); $mountpoint = self::getMountPoint($path);
if ($mountpoint) { if ($mountpoint) {
if (!isset(OC_Filesystem::$storages[$mountpoint])) { if (!isset(self::$storages[$mountpoint])) {
$mount = OC_Filesystem::$mounts[$mountpoint]; $mount = self::$mounts[$mountpoint];
OC_Filesystem::$storages[$mountpoint] = OC_Filesystem::createStorage($mount['class'], $mount['arguments']); self::$storages[$mountpoint] = self::createStorage($mount['class'], $mount['arguments']);
} }
return OC_Filesystem::$storages[$mountpoint]; return self::$storages[$mountpoint];
}else{
return null;
} }
} }
@ -202,13 +204,15 @@ class Filesystem {
static public function resolvePath($path) { static public function resolvePath($path) {
$mountpoint = self::getMountPoint($path); $mountpoint = self::getMountPoint($path);
if ($mountpoint) { if ($mountpoint) {
if (!isset(OC_Filesystem::$storages[$mountpoint])) { if (!isset(self::$storages[$mountpoint])) {
$mount = OC_Filesystem::$mounts[$mountpoint]; $mount = self::$mounts[$mountpoint];
OC_Filesystem::$storages[$mountpoint] = OC_Filesystem::createStorage($mount['class'], $mount['arguments']); self::$storages[$mountpoint] = self::createStorage($mount['class'], $mount['arguments']);
} }
$storage = OC_Filesystem::$storages[$mountpoint]; $storage = self::$storages[$mountpoint];
$internalPath = substr($path, strlen($mountpoint)); $internalPath = substr($path, strlen($mountpoint));
return array($storage, $internalPath); return array($storage, $internalPath);
}else{
return array(null, null);
} }
} }
@ -216,11 +220,11 @@ class Filesystem {
if (self::$defaultInstance) { if (self::$defaultInstance) {
return false; return false;
} }
self::$defaultInstance = new OC_FilesystemView($root); self::$defaultInstance = new View($root);
//load custom mount config //load custom mount config
if (is_file(OC::$SERVERROOT . '/config/mount.php')) { if (is_file(\OC::$SERVERROOT . '/config/mount.php')) {
$mountConfig = include(OC::$SERVERROOT . '/config/mount.php'); $mountConfig = include 'config/mount.php';
if (isset($mountConfig['global'])) { if (isset($mountConfig['global'])) {
foreach ($mountConfig['global'] as $mountPoint => $options) { foreach ($mountConfig['global'] as $mountPoint => $options) {
self::mount($options['class'], $options['options'], $mountPoint); self::mount($options['class'], $options['options'], $mountPoint);
@ -229,7 +233,7 @@ class Filesystem {
if (isset($mountConfig['group'])) { if (isset($mountConfig['group'])) {
foreach ($mountConfig['group'] as $group => $mounts) { foreach ($mountConfig['group'] as $group => $mounts) {
if (OC_Group::inGroup(OC_User::getUser(), $group)) { if (\OC_Group::inGroup(\OC_User::getUser(), $group)) {
foreach ($mounts as $mountPoint => $options) { foreach ($mounts as $mountPoint => $options) {
$mountPoint = self::setUserVars($mountPoint); $mountPoint = self::setUserVars($mountPoint);
foreach ($options as &$option) { foreach ($options as &$option) {
@ -243,7 +247,7 @@ class Filesystem {
if (isset($mountConfig['user'])) { if (isset($mountConfig['user'])) {
foreach ($mountConfig['user'] as $user => $mounts) { foreach ($mountConfig['user'] as $user => $mounts) {
if ($user === 'all' or strtolower($user) === strtolower(OC_User::getUser())) { if ($user === 'all' or strtolower($user) === strtolower(\OC_User::getUser())) {
foreach ($mounts as $mountPoint => $options) { foreach ($mounts as $mountPoint => $options) {
$mountPoint = self::setUserVars($mountPoint); $mountPoint = self::setUserVars($mountPoint);
foreach ($options as &$option) { foreach ($options as &$option) {
@ -257,22 +261,24 @@ class Filesystem {
} }
self::$loaded = true; self::$loaded = true;
return true;
} }
/** /**
* fill in the correct values for $user, and $password placeholders * fill in the correct values for $user, and $password placeholders
* *
* @param string intput * @param string $input
* @return string * @return string
*/ */
private static function setUserVars($input) { private static function setUserVars($input) {
return str_replace('$user', OC_User::getUser(), $input); return str_replace('$user', \OC_User::getUser(), $input);
} }
/** /**
* get the default filesystem view * get the default filesystem view
* *
* @return OC_FilesystemView * @return View
*/ */
static public function getView() { static public function getView() {
return self::$defaultInstance; return self::$defaultInstance;
@ -288,20 +294,20 @@ class Filesystem {
/** /**
* create a new storage of a specific type * create a new storage of a specific type
* *
* @param string type * @param string $type
* @param array arguments * @param array $arguments
* @return \OC\Files\Storage\Storage * @return \OC\Files\Storage\Storage
*/ */
static private function createStorage($class, $arguments) { static private function createStorage($class, $arguments) {
if (class_exists($class)) { if (class_exists($class)) {
try { try {
return new $class($arguments); return new $class($arguments);
} catch (Exception $exception) { } catch (\Exception $exception) {
OC_Log::write('core', $exception->getMessage(), OC_Log::ERROR); \OC_Log::write('core', $exception->getMessage(), \OC_Log::ERROR);
return false; return false;
} }
} else { } else {
OC_Log::write('core', 'storage backend ' . $class . ' not found', OC_Log::ERROR); \OC_Log::write('core', 'storage backend ' . $class . ' not found', \OC_Log::ERROR);
return false; return false;
} }
} }
@ -309,7 +315,7 @@ class Filesystem {
/** /**
* change the root to a fake root * change the root to a fake root
* *
* @param string fakeRoot * @param string $fakeRoot
* @return bool * @return bool
*/ */
static public function chroot($fakeRoot) { static public function chroot($fakeRoot) {
@ -337,8 +343,9 @@ class Filesystem {
/** /**
* mount an \OC\Files\Storage\Storage in our virtual filesystem * mount an \OC\Files\Storage\Storage in our virtual filesystem
* *
* @param \OC\Files\Storage\Storage storage * @param \OC\Files\Storage\Storage $storage
* @param string mountpoint * @param array $arguments
* @param string $mountpoint
*/ */
static public function mount($class, $arguments, $mountpoint) { static public function mount($class, $arguments, $mountpoint) {
if ($mountpoint[0] != '/') { if ($mountpoint[0] != '/') {
@ -354,7 +361,7 @@ class Filesystem {
* return the path to a local version of the file * return the path to a local version of the file
* we need this because we can't know if a file is stored local or not from outside the filestorage and for some purposes a local file is needed * we need this because we can't know if a file is stored local or not from outside the filestorage and for some purposes a local file is needed
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
static public function getLocalFile($path) { static public function getLocalFile($path) {
@ -362,7 +369,7 @@ class Filesystem {
} }
/** /**
* @param string path * @param string $path
* @return string * @return string
*/ */
static public function getLocalFolder($path) { static public function getLocalFolder($path) {
@ -372,11 +379,11 @@ class Filesystem {
/** /**
* return path to file which reflects one visible in browser * return path to file which reflects one visible in browser
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
static public function getLocalPath($path) { static public function getLocalPath($path) {
$datadir = OC_User::getHome(OC_User::getUser()) . '/files'; $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files';
$newpath = $path; $newpath = $path;
if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { if (strncmp($newpath, $datadir, strlen($datadir)) == 0) {
$newpath = substr($path, strlen($datadir)); $newpath = substr($path, strlen($datadir));
@ -387,7 +394,7 @@ class Filesystem {
/** /**
* check if the requested path is valid * check if the requested path is valid
* *
* @param string path * @param string $path
* @return bool * @return bool
*/ */
static public function isValidPath($path) { static public function isValidPath($path) {
@ -468,7 +475,7 @@ class Filesystem {
* @deprecated Replaced by isReadable() as part of CRUDS * @deprecated Replaced by isReadable() as part of CRUDS
*/ */
static public function is_readable($path) { static public function is_readable($path) {
return self::$defaultInstance->is_readable($path); return self::$defaultInstance->isReadable($path);
} }
/** /**
@ -559,7 +566,7 @@ class Filesystem {
} }
static public function search($query) { static public function search($query) {
return OC_FileCache::search($query); return Cache\Cache::search($query);
} }
/** /**
@ -580,19 +587,19 @@ class Filesystem {
} }
if ($root) { // reduce path to the required part of it (no 'username/files') if ($root) { // reduce path to the required part of it (no 'username/files')
$fakeRootView = new OC_FilesystemView($root); $fakeRootView = new View($root);
$count = 1; $count = 1;
$path = str_replace(OC_App::getStorage("files")->getAbsolutePath($path), "", $fakeRootView->getAbsolutePath($path), $count); $path = str_replace(\OC_App::getStorage("files")->getAbsolutePath($path), "", $fakeRootView->getAbsolutePath($path), $count);
} }
$path = self::normalizePath($path); $path = self::normalizePath($path);
OC_Connector_Sabre_Node::removeETagPropertyForPath($path); \OC_Connector_Sabre_Node::removeETagPropertyForPath($path);
} }
/** /**
* normalize a path * normalize a path
* *
* @param string path * @param string $path
* @param bool $stripTrailingSlash * @param bool $stripTrailingSlash
* @return string * @return string
*/ */
@ -606,7 +613,7 @@ class Filesystem {
if ($path[0] !== '/') { if ($path[0] !== '/') {
$path = '/' . $path; $path = '/' . $path;
} }
//remove trainling slash //remove trailing slash
if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') { if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
$path = substr($path, 0, -1); $path = substr($path, 0, -1);
} }
@ -616,14 +623,14 @@ class Filesystem {
} }
//normalize unicode if possible //normalize unicode if possible
if (class_exists('Normalizer')) { if (class_exists('Normalizer')) {
$path = Normalizer::normalize($path); $path = \Normalizer::normalize($path);
} }
return $path; return $path;
} }
} }
OC_Hook::connect('OC_Filesystem', 'post_write', 'OC_Filesystem', 'removeETagHook'); \OC_Hook::connect('OC_Filesystem', 'post_write', 'OC_Filesystem', 'removeETagHook');
OC_Hook::connect('OC_Filesystem', 'post_delete', 'OC_Filesystem', 'removeETagHook'); \OC_Hook::connect('OC_Filesystem', 'post_delete', 'OC_Filesystem', 'removeETagHook');
OC_Hook::connect('OC_Filesystem', 'post_rename', 'OC_Filesystem', 'removeETagHook'); \OC_Hook::connect('OC_Filesystem', 'post_rename', 'OC_Filesystem', 'removeETagHook');
OC_Util::setupFS(); \OC_Util::setupFS();

View File

@ -45,9 +45,9 @@ class View {
} }
/** /**
* change the root to a fake toor * change the root to a fake root
* *
* @param string fakeRoot * @param string $fakeRoot
* @return bool * @return bool
*/ */
public function chroot($fakeRoot) { public function chroot($fakeRoot) {
@ -71,12 +71,12 @@ class View {
/** /**
* get the part of the path relative to the mountpoint of the storage it's stored in * get the part of the path relative to the mountpoint of the storage it's stored in
* *
* @param string path * @param string $path
* @return bool * @return bool
*/ */
public function getInternalPath($path) { public function getInternalPath($path) {
if (!isset($this->internal_path_cache[$path])) { if (!isset($this->internal_path_cache[$path])) {
$this->internal_path_cache[$path] = OC_Filesystem::getInternalPath($this->getAbsolutePath($path)); $this->internal_path_cache[$path] = Filesystem::getInternalPath($this->getAbsolutePath($path));
} }
return $this->internal_path_cache[$path]; return $this->internal_path_cache[$path];
} }
@ -84,7 +84,7 @@ class View {
/** /**
* get path relative to the root of the view * get path relative to the root of the view
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
public function getRelativePath($path) { public function getRelativePath($path) {
@ -106,12 +106,12 @@ class View {
/** /**
* get the storage object for a path * get the storage object for a path
* *
* @param string path * @param string $path
* @return \OC\Files\Storage\Storage * @return \OC\Files\Storage\Storage
*/ */
public function getStorage($path) { public function getStorage($path) {
if (!isset($this->storage_cache[$path])) { if (!isset($this->storage_cache[$path])) {
$this->storage_cache[$path] = OC_Filesystem::getStorage($this->getAbsolutePath($path)); $this->storage_cache[$path] = Filesystem::getStorage($this->getAbsolutePath($path));
} }
return $this->storage_cache[$path]; return $this->storage_cache[$path];
} }
@ -120,35 +120,39 @@ class View {
* get the mountpoint of the storage object for a path * get the mountpoint of the storage object for a path
( note: because a storage is not always mounted inside the fakeroot, the returned mountpoint is relative to the absolute root of the filesystem and doesn't take the chroot into account ( note: because a storage is not always mounted inside the fakeroot, the returned mountpoint is relative to the absolute root of the filesystem and doesn't take the chroot into account
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
public function getMountPoint($path) { public function getMountPoint($path) {
return OC_Filesystem::getMountPoint($this->getAbsolutePath($path)); return Filesystem::getMountPoint($this->getAbsolutePath($path));
} }
/** /**
* return the path to a local version of the file * return the path to a local version of the file
* we need this because we can't know if a file is stored local or not from outside the filestorage and for some purposes a local file is needed * we need this because we can't know if a file is stored local or not from outside the filestorage and for some purposes a local file is needed
* *
* @param string path * @param string $path
* @return string * @return string
*/ */
public function getLocalFile($path) { public function getLocalFile($path) {
$parent = substr($path, 0, strrpos($path, '/')); $parent = substr($path, 0, strrpos($path, '/'));
if (OC_Filesystem::isValidPath($parent) and $storage = $this->getStorage($path)) { if (Filesystem::isValidPath($parent) and $storage = $this->getStorage($path)) {
return $storage->getLocalFile($this->getInternalPath($path)); return $storage->getLocalFile($this->getInternalPath($path));
} else {
return null;
} }
} }
/** /**
* @param string path * @param string $path
* @return string * @return string
*/ */
public function getLocalFolder($path) { public function getLocalFolder($path) {
$parent = substr($path, 0, strrpos($path, '/')); $parent = substr($path, 0, strrpos($path, '/'));
if (OC_Filesystem::isValidPath($parent) and $storage = $this->getStorage($path)) { if (Filesystem::isValidPath($parent) and $storage = $this->getStorage($path)) {
return $storage->getLocalFolder($this->getInternalPath($path)); return $storage->getLocalFolder($this->getInternalPath($path));
} else {
return null;
} }
} }
@ -274,28 +278,28 @@ class View {
public function file_put_contents($path, $data) { public function file_put_contents($path, $data) {
if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
$absolutePath = OC_Filesystem::normalizePath($this->getAbsolutePath($path)); $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) && OC_Filesystem::isValidPath($path)) { if (\OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data) && Filesystem::isValidPath($path)) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
$exists = $this->file_exists($path); $exists = $this->file_exists($path);
$run = true; $run = true;
if ($this->fakeRoot == OC_Filesystem::getRoot()) { if ($this->fakeRoot == Filesystem::getRoot()) {
if (!$exists) { if (!$exists) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_create, Filesystem::signal_create,
array( array(
OC_Filesystem::signal_param_path => $path, Filesystem::signal_param_path => $path,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} }
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_write, Filesystem::signal_write,
array( array(
OC_Filesystem::signal_param_path => $path, Filesystem::signal_param_path => $path,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} }
@ -304,28 +308,30 @@ class View {
} }
$target = $this->fopen($path, 'w'); $target = $this->fopen($path, 'w');
if ($target) { if ($target) {
$count = OC_Helper::streamCopy($data, $target); $count = \OC_Helper::streamCopy($data, $target);
fclose($target); fclose($target);
fclose($data); fclose($data);
if ($this->fakeRoot == OC_Filesystem::getRoot()) { if ($this->fakeRoot == Filesystem::getRoot()) {
if (!$exists) { if (!$exists) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_create, Filesystem::signal_post_create,
array(OC_Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $path)
); );
} }
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_write, Filesystem::signal_post_write,
array(OC_Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $path)
); );
} }
OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count); \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
return $count > 0; return $count > 0;
} else { } else {
return false; return false;
} }
} else {
return false;
} }
} else { } else {
return $this->basicOperation('file_put_contents', $path, array('create', 'write'), $data); return $this->basicOperation('file_put_contents', $path, array('create', 'write'), $data);
@ -343,9 +349,9 @@ class View {
public function rename($path1, $path2) { public function rename($path1, $path2) {
$postFix1 = (substr($path1, -1, 1) === '/') ? '/' : ''; $postFix1 = (substr($path1, -1, 1) === '/') ? '/' : '';
$postFix2 = (substr($path2, -1, 1) === '/') ? '/' : ''; $postFix2 = (substr($path2, -1, 1) === '/') ? '/' : '';
$absolutePath1 = OC_Filesystem::normalizePath($this->getAbsolutePath($path1)); $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
$absolutePath2 = OC_Filesystem::normalizePath($this->getAbsolutePath($path2)); $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
if (OC_FileProxy::runPreProxies('rename', $absolutePath1, $absolutePath2) and OC_Filesystem::isValidPath($path2)) { if (\OC_FileProxy::runPreProxies('rename', $absolutePath1, $absolutePath2) and Filesystem::isValidPath($path2)) {
$path1 = $this->getRelativePath($absolutePath1); $path1 = $this->getRelativePath($absolutePath1);
$path2 = $this->getRelativePath($absolutePath2); $path2 = $this->getRelativePath($absolutePath2);
@ -353,13 +359,13 @@ class View {
return false; return false;
} }
$run = true; $run = true;
if ($this->fakeRoot == OC_Filesystem::getRoot()) { if ($this->fakeRoot == Filesystem::getRoot()) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, OC_Filesystem::signal_rename, Filesystem::CLASSNAME, Filesystem::signal_rename,
array( array(
OC_Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $path1,
OC_Filesystem::signal_param_newpath => $path2, Filesystem::signal_param_newpath => $path2,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} }
@ -369,36 +375,42 @@ class View {
if ($mp1 == $mp2) { if ($mp1 == $mp2) {
if ($storage = $this->getStorage($path1)) { if ($storage = $this->getStorage($path1)) {
$result = $storage->rename($this->getInternalPath($path1 . $postFix1), $this->getInternalPath($path2 . $postFix2)); $result = $storage->rename($this->getInternalPath($path1 . $postFix1), $this->getInternalPath($path2 . $postFix2));
} else {
$result = false;
} }
} else { } else {
$source = $this->fopen($path1 . $postFix1, 'r'); $source = $this->fopen($path1 . $postFix1, 'r');
$target = $this->fopen($path2 . $postFix2, 'w'); $target = $this->fopen($path2 . $postFix2, 'w');
$count = OC_Helper::streamCopy($source, $target); $count = \OC_Helper::streamCopy($source, $target);
$storage1 = $this->getStorage($path1); $storage1 = $this->getStorage($path1);
$storage1->unlink($this->getInternalPath($path1 . $postFix1)); $storage1->unlink($this->getInternalPath($path1 . $postFix1));
$result = $count > 0; $result = $count > 0;
} }
if ($this->fakeRoot == OC_Filesystem::getRoot()) { if ($this->fakeRoot == Filesystem::getRoot()) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_rename, Filesystem::signal_post_rename,
array( array(
OC_Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $path1,
OC_Filesystem::signal_param_newpath => $path2 Filesystem::signal_param_newpath => $path2
) )
); );
} }
return $result; return $result;
} else {
return false;
} }
} else {
return false;
} }
} }
public function copy($path1, $path2) { public function copy($path1, $path2) {
$postFix1 = (substr($path1, -1, 1) === '/') ? '/' : ''; $postFix1 = (substr($path1, -1, 1) === '/') ? '/' : '';
$postFix2 = (substr($path2, -1, 1) === '/') ? '/' : ''; $postFix2 = (substr($path2, -1, 1) === '/') ? '/' : '';
$absolutePath1 = OC_Filesystem::normalizePath($this->getAbsolutePath($path1)); $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
$absolutePath2 = OC_Filesystem::normalizePath($this->getAbsolutePath($path2)); $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
if (OC_FileProxy::runPreProxies('copy', $absolutePath1, $absolutePath2) and OC_Filesystem::isValidPath($path2)) { if (\OC_FileProxy::runPreProxies('copy', $absolutePath1, $absolutePath2) and Filesystem::isValidPath($path2)) {
$path1 = $this->getRelativePath($absolutePath1); $path1 = $this->getRelativePath($absolutePath1);
$path2 = $this->getRelativePath($absolutePath2); $path2 = $this->getRelativePath($absolutePath2);
@ -406,34 +418,34 @@ class View {
return false; return false;
} }
$run = true; $run = true;
if ($this->fakeRoot == OC_Filesystem::getRoot()) { $exists = $this->file_exists($path2);
OC_Hook::emit( if ($this->fakeRoot == Filesystem::getRoot()) {
OC_Filesystem::CLASSNAME, \OC_Hook::emit(
OC_Filesystem::signal_copy, Filesystem::CLASSNAME,
Filesystem::signal_copy,
array( array(
OC_Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $path1,
OC_Filesystem::signal_param_newpath => $path2, Filesystem::signal_param_newpath => $path2,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
$exists = $this->file_exists($path2);
if ($run and !$exists) { if ($run and !$exists) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_create, Filesystem::signal_create,
array( array(
OC_Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $path2,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} }
if ($run) { if ($run) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_write, Filesystem::signal_write,
array( array(
OC_Filesystem::signal_param_path => $path2, Filesystem::signal_param_path => $path2,
OC_Filesystem::signal_param_run => &$run Filesystem::signal_param_run => &$run
) )
); );
} }
@ -444,39 +456,45 @@ class View {
if ($mp1 == $mp2) { if ($mp1 == $mp2) {
if ($storage = $this->getStorage($path1 . $postFix1)) { if ($storage = $this->getStorage($path1 . $postFix1)) {
$result = $storage->copy($this->getInternalPath($path1 . $postFix1), $this->getInternalPath($path2 . $postFix2)); $result = $storage->copy($this->getInternalPath($path1 . $postFix1), $this->getInternalPath($path2 . $postFix2));
} else {
$result = false;
} }
} else { } else {
$source = $this->fopen($path1 . $postFix1, 'r'); $source = $this->fopen($path1 . $postFix1, 'r');
$target = $this->fopen($path2 . $postFix2, 'w'); $target = $this->fopen($path2 . $postFix2, 'w');
$result = OC_Helper::streamCopy($source, $target); $result = \OC_Helper::streamCopy($source, $target);
} }
if ($this->fakeRoot == OC_Filesystem::getRoot()) { if ($this->fakeRoot == Filesystem::getRoot()) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_copy, Filesystem::signal_post_copy,
array( array(
OC_Filesystem::signal_param_oldpath => $path1, Filesystem::signal_param_oldpath => $path1,
OC_Filesystem::signal_param_newpath => $path2 Filesystem::signal_param_newpath => $path2
) )
); );
if (!$exists) { if (!$exists) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_create, Filesystem::signal_post_create,
array(OC_Filesystem::signal_param_path => $path2) array(Filesystem::signal_param_path => $path2)
); );
} }
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_post_write, Filesystem::signal_post_write,
array(OC_Filesystem::signal_param_path => $path2) array(Filesystem::signal_param_path => $path2)
); );
} else { // no real copy, file comes from somewhere else, e.g. version rollback -> just update the file cache and the webdav properties without all the other post_write actions } else { // no real copy, file comes from somewhere else, e.g. version rollback -> just update the file cache and the webdav properties without all the other post_write actions
OC_FileCache_Update::update($path2, $this->fakeRoot); // OC_FileCache_Update::update($path2, $this->fakeRoot);
OC_Filesystem::removeETagHook(array("path" => $path2), $this->fakeRoot); Filesystem::removeETagHook(array("path" => $path2), $this->fakeRoot);
} }
return $result; return $result;
} else {
return false;
} }
} else {
return false;
} }
} }
@ -507,14 +525,14 @@ class View {
$hooks[] = 'write'; $hooks[] = 'write';
break; break;
default: default:
OC_Log::write('core', 'invalid mode (' . $mode . ') for ' . $path, OC_Log::ERROR); \OC_Log::write('core', 'invalid mode (' . $mode . ') for ' . $path, \OC_Log::ERROR);
} }
return $this->basicOperation('fopen', $path, $hooks, $mode); return $this->basicOperation('fopen', $path, $hooks, $mode);
} }
public function toTmpFile($path) { public function toTmpFile($path) {
if (OC_Filesystem::isValidPath($path)) { if (Filesystem::isValidPath($path)) {
$source = $this->fopen($path, 'r'); $source = $this->fopen($path, 'r');
if ($source) { if ($source) {
$extension = ''; $extension = '';
@ -522,15 +540,19 @@ class View {
if ($extOffset !== false) { if ($extOffset !== false) {
$extension = substr($path, strrpos($path, '.')); $extension = substr($path, strrpos($path, '.'));
} }
$tmpFile = OC_Helper::tmpFile($extension); $tmpFile = \OC_Helper::tmpFile($extension);
file_put_contents($tmpFile, $source); file_put_contents($tmpFile, $source);
return $tmpFile; return $tmpFile;
} else {
return false;
} }
} else {
return false;
} }
} }
public function fromTmpFile($tmpFile, $path) { public function fromTmpFile($tmpFile, $path) {
if (OC_Filesystem::isValidPath($path)) { if (Filesystem::isValidPath($path)) {
if (!$tmpFile) { if (!$tmpFile) {
debug_print_backtrace(); debug_print_backtrace();
} }
@ -540,6 +562,7 @@ class View {
unlink($tmpFile); unlink($tmpFile);
return true; return true;
} else { } else {
return false;
} }
} else { } else {
return false; return false;
@ -552,22 +575,22 @@ class View {
public function hash($type, $path, $raw = false) { public function hash($type, $path, $raw = false) {
$postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
$absolutePath = OC_Filesystem::normalizePath($this->getAbsolutePath($path)); $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (OC_FileProxy::runPreProxies('hash', $absolutePath) && OC_Filesystem::isValidPath($path)) { if (\OC_FileProxy::runPreProxies('hash', $absolutePath) && Filesystem::isValidPath($path)) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
if ($path == null) { if ($path == null) {
return false; return false;
} }
if (OC_Filesystem::$loaded && $this->fakeRoot == OC_Filesystem::getRoot()) { if (Filesystem::$loaded && $this->fakeRoot == Filesystem::getRoot()) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
OC_Filesystem::signal_read, Filesystem::signal_read,
array(OC_Filesystem::signal_param_path => $path) array(Filesystem::signal_param_path => $path)
); );
} }
if ($storage = $this->getStorage($path . $postFix)) { if ($storage = $this->getStorage($path . $postFix)) {
$result = $storage->hash($type, $this->getInternalPath($path . $postFix), $raw); $result = $storage->hash($type, $this->getInternalPath($path . $postFix), $raw);
$result = OC_FileProxy::runPostProxies('hash', $absolutePath, $result); $result = \OC_FileProxy::runPostProxies('hash', $absolutePath, $result);
return $result; return $result;
} }
} }
@ -581,9 +604,9 @@ class View {
/** /**
* @brief abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage * @brief abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
* @param string $operation * @param string $operation
* @param string #path * @param string $path
* @param array (optional) hooks * @param array $hooks (optional)
* @param mixed (optional) $extraParam * @param mixed $extraParam (optional)
* @return mixed * @return mixed
* *
* This method takes requests for basic filesystem functions (e.g. reading & writing * This method takes requests for basic filesystem functions (e.g. reading & writing
@ -592,8 +615,8 @@ class View {
*/ */
private function basicOperation($operation, $path, $hooks = array(), $extraParam = null) { private function basicOperation($operation, $path, $hooks = array(), $extraParam = null) {
$postFix = (substr($path, -1, 1) === '/') ? '/' : ''; $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
$absolutePath = OC_Filesystem::normalizePath($this->getAbsolutePath($path)); $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
if (OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam) and OC_Filesystem::isValidPath($path)) { if (\OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam) and Filesystem::isValidPath($path)) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
if ($path == null) { if ($path == null) {
return false; return false;
@ -606,8 +629,8 @@ class View {
} else { } else {
$result = $storage->$operation($internalPath); $result = $storage->$operation($internalPath);
} }
$result = OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result); $result = \OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result);
if (OC_Filesystem::$loaded and $this->fakeRoot == OC_Filesystem::getRoot()) { if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot()) {
if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
$this->runHooks($hooks, $path, true); $this->runHooks($hooks, $path, true);
} }
@ -621,23 +644,23 @@ class View {
private function runHooks($hooks, $path, $post = false) { private function runHooks($hooks, $path, $post = false) {
$prefix = ($post) ? 'post_' : ''; $prefix = ($post) ? 'post_' : '';
$run = true; $run = true;
if (OC_Filesystem::$loaded and $this->fakeRoot == OC_Filesystem::getRoot()) { if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot()) {
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
if ($hook != 'read') { if ($hook != 'read') {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
$prefix . $hook, $prefix . $hook,
array( array(
OC_Filesystem::signal_param_run => &$run, Filesystem::signal_param_run => &$run,
OC_Filesystem::signal_param_path => $path Filesystem::signal_param_path => $path
) )
); );
} elseif (!$post) { } elseif (!$post) {
OC_Hook::emit( \OC_Hook::emit(
OC_Filesystem::CLASSNAME, Filesystem::CLASSNAME,
$prefix . $hook, $prefix . $hook,
array( array(
OC_Filesystem::signal_param_path => $path Filesystem::signal_param_path => $path
) )
); );
} }
@ -649,6 +672,7 @@ class View {
/** /**
* check if a file or folder has been updated since $time * check if a file or folder has been updated since $time
* *
* @param string $path
* @param int $time * @param int $time
* @return bool * @return bool
*/ */