Fix bugs in opendir() and add extra checks to ensure functionality in all cases

This commit is contained in:
Michael Gapczynski 2011-07-16 14:24:59 -04:00
parent 8ad4a44171
commit dbcb35655e
1 changed files with 32 additions and 17 deletions

View File

@ -72,14 +72,16 @@ class OC_FILESTORAGE_SHARED extends OC_FILESTORAGE {
OC_SHARE::unshareFromMySelf($this->datadir.$path); OC_SHARE::unshareFromMySelf($this->datadir.$path);
} }
// TODO Make sure new target is still in the current directory
public function opendir($path) { public function opendir($path) {
if ($path == "" || $path == "/") { if ($path == "" || $path == "/") {
global $FAKEDIRS; global $FAKEDIRS;
$sharedItems = OC_SHARE::getItemsInFolder($this->datadir.$path); $path = $this->datadir.$path;
$sharedItems = OC_SHARE::getItemsInFolder($path);
foreach ($sharedItems as $item) { foreach ($sharedItems as $item) {
// TODO Implement a better fix // If item is in the root of the shared storage provider add it to the fakedirs
$files[] = substr($item['target'], strpos($item['target'], "Share") + 5); if (dirname($item['target'])."/" == $path) {
$files[] = basename($item['target']);
}
} }
$FAKEDIRS['shared'] = $files; $FAKEDIRS['shared'] = $files;
return opendir('fakedir://shared'); return opendir('fakedir://shared');
@ -88,26 +90,39 @@ class OC_FILESTORAGE_SHARED extends OC_FILESTORAGE {
if ($source) { if ($source) {
$storage = OC_FILESYSTEM::getStorage($source); $storage = OC_FILESYSTEM::getStorage($source);
$dh = $storage->opendir($this->getInternalPath($source)); $dh = $storage->opendir($this->getInternalPath($source));
$modifiedItems = OC_SHARE::getItemsInFolder($this->datadir.$path); // Remove any duplicate or trailing '/'
$path = rtrim($this->datadir.$path, "/");
$path = preg_replace('{(/)\1+}', "/", $path);
$modifiedItems = OC_SHARE::getItemsInFolder($path);
if ($modifiedItems && $dh) { if ($modifiedItems && $dh) {
global $FAKEDIRS; global $FAKEDIRS;
$sources = array(); $sources = array();
$targets = array(); $targets = array();
foreach ($modifiedItems as $item) { foreach ($modifiedItems as $item) {
$sources[] = basename($item['source']); // If item is in current directory, add it to the arrays
$targets[] = basename($item['target']); if (dirname($item['target']) == $path) {
} $sources[] = basename($item['source']);
while (($filename = readdir($dh)) !== false) { $targets[] = basename($item['target']);
if ($filename != "." && $filename != "..") {
if (!in_array($filename, $sources)) {
$files[] = $filename;
} else {
$files[] = $targets[array_search($filename, $sources)];
}
} }
} }
$FAKEDIRS['shared'] = $files; // Don't waste time if there aren't any modified items in the current directory
return opendir('fakedir://shared'); if (empty($sources)) {
return $dh;
} else {
while (($filename = readdir($dh)) !== false) {
if ($filename != "." && $filename != "..") {
// If the file isn't in the sources array it isn't modified and can be added as is
if (!in_array($filename, $sources)) {
$files[] = $filename;
// The file has a different name than the source and is added to the fakedirs
} else {
$files[] = $targets[array_search($filename, $sources)];
}
}
}
$FAKEDIRS['shared'] = $files;
return opendir('fakedir://shared');
}
} else { } else {
return $dh; return $dh;
} }