diff options
author | Björn Schießle <bjoern@schiessle.org> | 2012-11-13 19:28:03 +0400 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2012-11-13 19:28:03 +0400 |
commit | 55d8057b7fb5335f6bcb994393e05d91d6d6cc32 (patch) | |
tree | db6686f5eab8b59fdf71a33a67c505d8381911ea | |
parent | 77c07615f81814c1658668edfb94b005de69fa9b (diff) | |
parent | 5e19854d47202073773e2cde3eaabdf66099a85c (diff) |
Merge pull request #335 from owncloud/sharing_folder_sync_stable45
Sharing folder sync stable45
-rw-r--r-- | apps/files_sharing/appinfo/app.php | 1 | ||||
-rw-r--r-- | apps/files_sharing/lib/sharedstorage.php | 36 | ||||
-rw-r--r-- | apps/files_sharing/lib/util.php | 79 | ||||
-rw-r--r-- | lib/connector/sabre/directory.php | 10 | ||||
-rw-r--r-- | lib/connector/sabre/node.php | 47 | ||||
-rw-r--r-- | lib/filecache.php | 12 |
6 files changed, 162 insertions, 23 deletions
diff --git a/apps/files_sharing/appinfo/app.php b/apps/files_sharing/appinfo/app.php index 109f86b2e87..bfed4266ad4 100644 --- a/apps/files_sharing/appinfo/app.php +++ b/apps/files_sharing/appinfo/app.php @@ -3,6 +3,7 @@ OC::$CLASSPATH['OC_Share_Backend_File'] = "apps/files_sharing/lib/share/file.php"; OC::$CLASSPATH['OC_Share_Backend_Folder'] = 'apps/files_sharing/lib/share/folder.php'; OC::$CLASSPATH['OC_Filestorage_Shared'] = "apps/files_sharing/lib/sharedstorage.php"; +OC::$CLASSPATH['OC_Files_Sharing_Util'] = "apps/files_sharing/lib/util.php"; OCP\Util::connectHook('OC_Filesystem', 'setup', 'OC_Filestorage_Shared', 'setup'); OCP\Share::registerBackend('file', 'OC_Share_Backend_File'); OCP\Share::registerBackend('folder', 'OC_Share_Backend_Folder', 'file'); diff --git a/apps/files_sharing/lib/sharedstorage.php b/apps/files_sharing/lib/sharedstorage.php index 6dba76955a0..d2489f9cbad 100644 --- a/apps/files_sharing/lib/sharedstorage.php +++ b/apps/files_sharing/lib/sharedstorage.php @@ -112,8 +112,14 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { if ($path == '' || $path == '/' || !$this->isCreatable(dirname($path))) { return false; } else if ($source = $this->getSourcePath($path)) { + $parts = explode('/', $source, 4); + $user = $parts[1]; + $intPath = '/'.$parts[3]; $storage = OC_Filesystem::getStorage($source); - return $storage->mkdir($this->getInternalPath($source)); + if( ($storage->mkdir($this->getInternalPath($source))) ) { + OC_FileCache::put($intPath ,array('user'=>$user), '/'.$user.'/files'); + return true; + } } return false; } @@ -296,10 +302,15 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { 'target' => $this->sharedFolder.$path, 'source' => $source, ); - OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info); + OCP\Util::emitHook('OC_Filestorage_Shared', 'file_put_contents', $info);
+ $parts = explode('/', $source, 4); + $user = $parts[1]; + $intPath = '/'.$parts[3]; $storage = OC_Filesystem::getStorage($source); - $result = $storage->file_put_contents($this->getInternalPath($source), $data); - return $result; + if( ( $result = $storage->file_put_contents($this->getInternalPath($source), $data) ) ) { + OC_FileCache::put($intPath ,array('user'=>$user), '/'.$user.'/files'); + return $result; + } } return false; } @@ -368,17 +379,18 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { public function fopen($path, $mode) { if ($source = $this->getSourcePath($path)) { + $write = false; switch ($mode) { + case 'w':
+ case 'wb': + case 'w+':
+ case 'wb+': $write = true; case 'r+': case 'rb+': - case 'w+': - case 'wb+': case 'x+': case 'xb+': case 'a+': case 'ab+': - case 'w': - case 'wb': case 'x': case 'xb': case 'a': @@ -394,6 +406,14 @@ class OC_Filestorage_Shared extends OC_Filestorage_Common { ); OCP\Util::emitHook('OC_Filestorage_Shared', 'fopen', $info); $storage = OC_Filesystem::getStorage($source); + + $parts = explode('/', $source, 4); + $user = $parts[1]; + $intPath = '/'.$parts[3];
+
+ if ( $write && $storage->touch($this->getInternalPath($source)) ) {
+ OC_FileCache::put($intPath ,array('user'=>$user), '/'.$user.'/files');
+ } return $storage->fopen($this->getInternalPath($source), $mode); } return false; diff --git a/apps/files_sharing/lib/util.php b/apps/files_sharing/lib/util.php new file mode 100644 index 00000000000..2306408dc0e --- /dev/null +++ b/apps/files_sharing/lib/util.php @@ -0,0 +1,79 @@ +<?php +/**
+ * ownCloud
+ *
+ * @author Bjoern Schiessle
+ * @copyright 2012 Bjoern Schiessle schiessle@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+class OC_Files_Sharing_Util {
+
+ private static $files = array();
+
+ /**
+ * @brief Get the source file path and the permissions granted for a shared file
+ * @param string Shared target file path
+ * @return Returns array with the keys path and permissions or false if not found
+ */
+ private static function getFile($target) {
+ $target = '/'.$target;
+ $target = rtrim($target, '/');
+ if (isset(self::$files[$target])) {
+ return self::$files[$target];
+ } else {
+ $pos = strpos($target, '/', 1);
+ // Get shared folder name
+ if ($pos !== false) {
+ $folder = substr($target, 0, $pos);
+ if (isset(self::$files[$folder])) {
+ $file = self::$files[$folder];
+ } else {
+ $file = OCP\Share::getItemSharedWith('folder', $folder, OC_Share_Backend_File::FORMAT_SHARED_STORAGE);
+ }
+ if ($file) {
+ self::$files[$target]['path'] = $file['path'].substr($target, strlen($folder));
+ self::$files[$target]['permissions'] = $file['permissions'];
+ return self::$files[$target];
+ }
+ } else {
+ $file = OCP\Share::getItemSharedWith('file', $target, OC_Share_Backend_File::FORMAT_SHARED_STORAGE);
+ if ($file) {
+ self::$files[$target] = $file;
+ return self::$files[$target];
+ }
+ }
+ OCP\Util::writeLog('files_sharing', 'File source not found for: '.$target, OCP\Util::ERROR);
+ return false;
+ }
+ }
+
+ /**
+ * @brief Get the source file path for a shared file
+ * @param string Shared target file path
+ * @return Returns source file path or false if not found
+ */
+ public static function getSourcePath($target) {
+ $file = self::getFile($target);
+ if (isset($file['path'])) {
+ $uid = substr($file['path'], 1, strpos($file['path'], '/', 1) - 1);
+ OC_Filesystem::mount('OC_Filestorage_Local', array('datadir' => OC_User::getHome($uid)), $uid);
+ return $file['path'];
+ }
+ return false;
+ }
+}
\ No newline at end of file diff --git a/lib/connector/sabre/directory.php b/lib/connector/sabre/directory.php index 413efef73b7..56c17da1a6e 100644 --- a/lib/connector/sabre/directory.php +++ b/lib/connector/sabre/directory.php @@ -117,16 +117,20 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa */ public function getChildren() { + $source = $this->getFileSource($this->path); + $path = $source['path']; + $user = $source['user']; + $folder_content = OC_Files::getDirectoryContent($this->path); $paths = array(); foreach($folder_content as $info) { - $paths[] = $this->path.'/'.$info['name']; + $paths[] = $path.'/'.$info['name']; } $properties = array_fill_keys($paths, array()); if(count($paths)>0) { $placeholders = join(',', array_fill(0, count($paths), '?')); $query = OC_DB::prepare( 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ?' . ' AND `propertypath` IN ('.$placeholders.')' ); - array_unshift($paths, OC_User::getUser()); // prepend userid + array_unshift($paths, $user); // prepend userid $result = $query->execute( $paths ); while($row = $result->fetchRow()) { $propertypath = $row['propertypath']; @@ -139,7 +143,7 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa $nodes = array(); foreach($folder_content as $info) { $node = $this->getChild($info['name'], $info); - $node->setPropertyCache($properties[$this->path.'/'.$info['name']]); + $node->setPropertyCache($properties[$path.'/'.$info['name']]); $nodes[] = $node; } return $nodes; diff --git a/lib/connector/sabre/node.php b/lib/connector/sabre/node.php index f87e9f9a0b1..6a2ad22fd5d 100644 --- a/lib/connector/sabre/node.php +++ b/lib/connector/sabre/node.php @@ -148,13 +148,16 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr * @return bool|array */ public function updateProperties($properties) { + // get source path of shared files + $source = self::getFileSource($this->path); + $existing = $this->getProperties(array()); foreach($properties as $propertyName => $propertyValue) { // If it was null, we need to delete the property if (is_null($propertyValue)) { if(array_key_exists( $propertyName, $existing )) { $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?' ); - $query->execute( array( OC_User::getUser(), $this->path, $propertyName )); + $query->execute( array( $source['user'], $source['path'], $propertyName )); } } else { @@ -163,10 +166,10 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr } else { if(!array_key_exists( $propertyName, $existing )) { $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*properties` (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)' ); - $query->execute( array( OC_User::getUser(), $this->path, $propertyName,$propertyValue )); + $query->execute( array( $source['user'], $source['path'], $propertyName, $propertyValue )); } else { $query = OC_DB::prepare( 'UPDATE `*PREFIX*properties` SET `propertyvalue` = ? WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` = ?' ); - $query->execute( array( $propertyValue,OC_User::getUser(), $this->path, $propertyName )); + $query->execute( array( $propertyValue, $source['user'], $source['path'], $propertyName )); } } } @@ -188,16 +191,19 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr * @return array */ public function getProperties($properties) { - if (is_null($this->property_cache)) { + + $source = self::getFileSource($this->path); + + if (is_null($this->property_cache) || empty($this->property_cache)) { $query = OC_DB::prepare( 'SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?' ); - $result = $query->execute( array( OC_User::getUser(), $this->path )); + $result = $query->execute( array( $source['user'], $source['path'] )); $this->property_cache = array(); while( $row = $result->fetchRow()) { $this->property_cache[$row['propertyname']] = $row['propertyvalue']; } } - + // if the array was empty, we need to return everything if(count($properties) == 0) { return $this->property_cache; @@ -234,9 +240,12 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr if (empty($tag)) { return null; } + + $source = self::getFileSource($path); + $etag = '"'.$tag.'"'; $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*properties` (`userid`,`propertypath`,`propertyname`,`propertyvalue`) VALUES(?,?,?,?)' ); - $query->execute( array( OC_User::getUser(), $path, self::GETETAG_PROPERTYNAME, $etag )); + $query->execute( array( $source['user'], $source['path'], self::GETETAG_PROPERTYNAME, $etag )); return $etag; } @@ -246,6 +255,9 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr */ static public function removeETagPropertyForPath($path) { // remove tags from this and parent paths + $source = self::getFileSource($path); + $path = $source['path']; + $paths = array(); while ($path != '/' && $path != '.' && $path != '' && $path != '\\') { $paths[] = $path; @@ -261,7 +273,26 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr .' AND `propertyname` = ?' .' AND `propertypath` IN ('.$path_placeholders.')' ); - $vals = array( OC_User::getUser(), self::GETETAG_PROPERTYNAME ); + $vals = array( $source['user'], self::GETETAG_PROPERTYNAME ); $query->execute(array_merge( $vals, $paths )); + + //remove etag for all Shared folders + $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*properties`'
+ .' WHERE `propertypath` = "/Shared"'
+ );
+ $query->execute(array()); + + } + + protected static function getFileSource($path) { + if ( OC_App::isEnabled('files_sharing') && !strncmp($path, '/Shared/', 8)) {
+ $source = OC_Files_Sharing_Util::getSourcePath(str_replace('/Shared/', '', $path));
+ $parts = explode('/', $source, 4);
+ $user = $parts[1];
+ $path = '/'.$parts[3];
+ } else {
+ $user = OC_User::getUser();
+ } + return(array('user' => $user, 'path' => $path)); } } diff --git a/lib/filecache.php b/lib/filecache.php index 9ce94c6f733..40d63021273 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -79,8 +79,8 @@ class OC_FileCache{ // add parent directory to the file cache if it does not exist yet. if ($parent == -1 && $fullpath != $root) { - $parentDir = dirname($path); - self::scanFile($parentDir); + $parentDir = dirname(OC_Filesystem::normalizePath($path)); + self::scanFile($parentDir, $root); $parent = self::getParentId($fullpath); } @@ -94,15 +94,19 @@ class OC_FileCache{ if(!isset($data['versioned'])) { $data['versioned']=false; } + if(!isset($data['user'])) { + $data['user']=OC_User::getUser(); + } + + $mimePart=dirname($data['mimetype']); $data['size']=(int)$data['size']; $data['ctime']=(int)$data['mtime']; $data['writable']=(int)$data['writable']; $data['encrypted']=(int)$data['encrypted']; $data['versioned']=(int)$data['versioned']; - $user=OC_User::getUser(); $query=OC_DB::prepare('INSERT INTO `*PREFIX*fscache`(`parent`, `name`, `path`, `path_hash`, `size`, `mtime`, `ctime`, `mimetype`, `mimepart`,`user`,`writable`,`encrypted`,`versioned`) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)'); - $result=$query->execute(array($parent,basename($fullpath),$fullpath,md5($fullpath),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$user,$data['writable'],$data['encrypted'],$data['versioned'])); + $result=$query->execute(array($parent,basename($fullpath),$fullpath,md5($fullpath),$data['size'],$data['mtime'],$data['ctime'],$data['mimetype'],$mimePart,$data['user'],$data['writable'],$data['encrypted'],$data['versioned'])); if(OC_DB::isError($result)) { OC_Log::write('files','error while writing file('.$fullpath.') to cache',OC_Log::ERROR); } |