From 9deab8302f4c41daed3db6f3b470480b2710c502 Mon Sep 17 00:00:00 2001
From: Michael Gapczynski <GapczynskiM@gmail.com>
Date: Tue, 5 Jul 2011 11:56:02 -0400
Subject: [PATCH] Fix bugs in getSource() and implement new target path
 standard

---
 apps/files_sharing/lib_share.php     |  4 +++-
 apps/files_sharing/sharedstorage.php | 19 +++++--------------
 lib/filesystem.php                   | 10 ++++++++++
 3 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/apps/files_sharing/lib_share.php b/apps/files_sharing/lib_share.php
index b03720b753..77b0bc4a70 100644
--- a/apps/files_sharing/lib_share.php
+++ b/apps/files_sharing/lib_share.php
@@ -82,6 +82,8 @@ class OC_SHARE {
 	 * @return source path
 	 */
 	public static function getSource($target) {
+		// Remove any trailing '/'
+		$target = rtrim($target, "/");
 		$query = OC_DB::prepare("SELECT source FROM *PREFIX*sharing WHERE target = ? AND uid_shared_with = ? LIMIT 1");
 		$result = $query->execute(array($target, $_SESSION['user_id']))->fetchAll();
 		if (count($result) > 0) {
@@ -89,7 +91,7 @@ class OC_SHARE {
 		} else {
 			// Check if the parent directory of this target is shared
 			$parentDir = dirname($target);
-			if ($parentDir != ".") {
+			if ($parentDir != "" && $parentDir != "/" && $parentDir != ".") {
 				$result = OC_SHARE::getSource($parentDir);
 				if ($result) {
 					return $result."/".basename($target);
diff --git a/apps/files_sharing/sharedstorage.php b/apps/files_sharing/sharedstorage.php
index e17685a09e..0303cce79f 100644
--- a/apps/files_sharing/sharedstorage.php
+++ b/apps/files_sharing/sharedstorage.php
@@ -43,22 +43,13 @@ class OC_FILESTORAGE_SHARED {
 	}
 	
 	public function getSource($target) {
-		if ($target == "") {
-			return false;
-		} elseif (array_key_exists($target, $this->sourcePaths)) {
+		$target = OC_FILESYSTEM::getStorageMountPoint($this).$target;
+		if (array_key_exists($target, $this->sourcePaths)) {
 			return $this->sourcePaths[$target];
 		} else {
-			$parentDir = dirname($target);
-			if ($parentDir != ".") {
-				$source = $this->getSource($parentDir);
-				return $source."/".basename($target);
-			} else {
-				$source = OC_SHARE::getSource($target);
-				if ($source) {
-					$this->sourcePaths[$target] = $source;
-				}
-				return $source;
-			}
+			$source = OC_SHARE::getSource($target);
+			$this->sourcePaths[$target] = $source;
+			return $source;
 		}
 	}
 	
diff --git a/lib/filesystem.php b/lib/filesystem.php
index 4116cb9316..c7e2070fa0 100644
--- a/lib/filesystem.php
+++ b/lib/filesystem.php
@@ -214,6 +214,16 @@ class OC_FILESYSTEM{
 		}
 		return $foundMountPoint;
 	}
+	
+	/**
+	* get the mountpoint of the storage object
+	* @param OC_FILESTORAGE storage
+	* @return string
+	*/
+	static public function getStorageMountPoint($storage){
+		return array_search($storage, self::$storages);
+	}
+	
 	/**
 	* 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
-- 
GitLab