Update apps/files_external/lib/sftp.php
Added a little better exception handling, hopefully making it a little more robust
This commit is contained in:
parent
3375253e1d
commit
00bfcd94ec
1 changed files with 111 additions and 71 deletions
|
@ -52,11 +52,14 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function host_keys_path() {
|
private function host_keys_path() {
|
||||||
$storage_view = \OCP\Files::getStorage('files_external');
|
try {
|
||||||
if ($storage_view) {
|
$storage_view = \OCP\Files::getStorage('files_external');
|
||||||
return \OCP\Config::getSystemValue('datadirectory') .
|
if ($storage_view) {
|
||||||
$storage_view->getAbsolutePath('') .
|
return \OCP\Config::getSystemValue('datadirectory') .
|
||||||
'ssh_host_keys';
|
$storage_view->getAbsolutePath('') .
|
||||||
|
'ssh_host_keys';
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -76,36 +79,47 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
private function read_host_keys() {
|
private function read_host_keys() {
|
||||||
$key_path = $this->host_keys_path();
|
try {
|
||||||
if (file_exists($key_path)) {
|
$key_path = $this->host_keys_path();
|
||||||
$hosts = array();
|
if (file_exists($key_path)) {
|
||||||
$keys = array();
|
$hosts = array();
|
||||||
$lines = file($key_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
$keys = array();
|
||||||
if ($lines) {
|
$lines = file($key_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||||
foreach ($lines as $line) {
|
if ($lines) {
|
||||||
$host_key_arr = explode("::", $line, 2);
|
foreach ($lines as $line) {
|
||||||
if (count($host_key_arr) == 2) {
|
$host_key_arr = explode("::", $line, 2);
|
||||||
$hosts[] = $host_key_arr[0];
|
if (count($host_key_arr) == 2) {
|
||||||
$keys[] = $host_key_arr[1];
|
$hosts[] = $host_key_arr[0];
|
||||||
|
$keys[] = $host_key_arr[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return array_combine($hosts, $keys);
|
||||||
}
|
}
|
||||||
return array_combine($hosts, $keys);
|
|
||||||
}
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
}
|
}
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function mkdir($path) {
|
public function mkdir($path) {
|
||||||
return $this->client->mkdir($this->abs_path($path));
|
try {
|
||||||
|
return $this->client->mkdir($this->abs_path($path));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rmdir($path) {
|
public function rmdir($path) {
|
||||||
return $this->client->delete($this->abs_path($path), true);
|
try {
|
||||||
}
|
return $this->client->delete($this->abs_path($path), true);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function opendir($path) {
|
public function opendir($path) {
|
||||||
$list = $this->client->nlist($this->abs_path($path));
|
|
||||||
try {
|
try {
|
||||||
|
$list = $this->client->nlist($this->abs_path($path));
|
||||||
|
|
||||||
$id = md5('sftp:' . $path);
|
$id = md5('sftp:' . $path);
|
||||||
OC_FakeDirStream::$dirs[$id] = array();
|
OC_FakeDirStream::$dirs[$id] = array();
|
||||||
foreach($list as $file) {
|
foreach($list as $file) {
|
||||||
|
@ -120,9 +134,12 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filetype($path) {
|
public function filetype($path) {
|
||||||
$stat = $this->client->stat($this->abs_path($path));
|
try {
|
||||||
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) return 'file';
|
$stat = $this->client->stat($this->abs_path($path));
|
||||||
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) return 'dir';
|
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) return 'file';
|
||||||
|
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) return 'dir';
|
||||||
|
} catch (Exeption $e) {
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,52 +152,63 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function file_exists($path) {
|
public function file_exists($path) {
|
||||||
return $this->client->stat($this->abs_path($path)) === false ? false : true;
|
try {
|
||||||
|
return $this->client->stat($this->abs_path($path)) === false ? false : true;
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function unlink($path) {
|
public function unlink($path) {
|
||||||
return $this->client->delete($this->abs_path($path), true);
|
try {
|
||||||
|
return $this->client->delete($this->abs_path($path), true);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fopen($path, $mode) {
|
public function fopen($path, $mode) {
|
||||||
$abs_path = $this->abs_path($path);
|
try {
|
||||||
switch($mode) {
|
$abs_path = $this->abs_path($path);
|
||||||
case 'r':
|
switch($mode) {
|
||||||
case 'rb':
|
case 'r':
|
||||||
if ( !$this->file_exists($path)) return false;
|
case 'rb':
|
||||||
if (strrpos($path, '.')!==false) {
|
if ( !$this->file_exists($path)) return false;
|
||||||
$ext=substr($path, strrpos($path, '.'));
|
if (strrpos($path, '.')!==false) {
|
||||||
} else {
|
$ext=substr($path, strrpos($path, '.'));
|
||||||
$ext='';
|
} else {
|
||||||
}
|
$ext='';
|
||||||
$tmp = OC_Helper::tmpFile($ext);
|
}
|
||||||
$this->getFile($abs_path, $tmp);
|
$tmp = OC_Helper::tmpFile($ext);
|
||||||
return fopen($tmp, $mode);
|
$this->getFile($abs_path, $tmp);
|
||||||
|
return fopen($tmp, $mode);
|
||||||
|
|
||||||
case 'w':
|
case 'w':
|
||||||
case 'wb':
|
case 'wb':
|
||||||
case 'a':
|
case 'a':
|
||||||
case 'ab':
|
case 'ab':
|
||||||
case 'r+':
|
case 'r+':
|
||||||
case 'w+':
|
case 'w+':
|
||||||
case 'wb+':
|
case 'wb+':
|
||||||
case 'a+':
|
case 'a+':
|
||||||
case 'x':
|
case 'x':
|
||||||
case 'x+':
|
case 'x+':
|
||||||
case 'c':
|
case 'c':
|
||||||
case 'c+':
|
case 'c+':
|
||||||
if (strrpos($path, '.')!==false) {
|
if (strrpos($path, '.')!==false) {
|
||||||
$ext=substr($path, strrpos($path, '.'));
|
$ext=substr($path, strrpos($path, '.'));
|
||||||
} else {
|
} else {
|
||||||
$ext='';
|
$ext='';
|
||||||
}
|
}
|
||||||
$tmpFile=OC_Helper::tmpFile($ext);
|
$tmpFile=OC_Helper::tmpFile($ext);
|
||||||
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
|
OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this, 'writeBack');
|
||||||
if ($this->file_exists($path)) {
|
if ($this->file_exists($path)) {
|
||||||
$this->getFile($abs_path, $tmpFile);
|
$this->getFile($abs_path, $tmpFile);
|
||||||
}
|
}
|
||||||
self::$tempFiles[$tmpFile]=$abs_path;
|
self::$tempFiles[$tmpFile]=$abs_path;
|
||||||
return fopen('close://'.$tmpFile, $mode);
|
return fopen('close://'.$tmpFile, $mode);
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -198,9 +226,13 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function touch($path, $mtime=null) {
|
public function touch($path, $mtime=null) {
|
||||||
if (!$this->file_exists($path)) {
|
try {
|
||||||
$this->client->put($this->abs_path($path), '');
|
if (!$this->file_exists($path)) {
|
||||||
|
$this->client->put($this->abs_path($path), '');
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFile($path, $target) {
|
public function getFile($path, $target) {
|
||||||
|
@ -212,16 +244,24 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function rename($source, $target) {
|
public function rename($source, $target) {
|
||||||
return $this->client->rename($this->abs_path($source), $this->abs_path($target));
|
try {
|
||||||
}
|
return $this->client->rename($this->abs_path($source), $this->abs_path($target));
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function stat($path) {
|
public function stat($path) {
|
||||||
$stat = $this->client->stat($this->abs_path($path));
|
try {
|
||||||
|
$stat = $this->client->stat($this->abs_path($path));
|
||||||
|
|
||||||
$mtime = $stat ? $stat['mtime'] : -1;
|
$mtime = $stat ? $stat['mtime'] : -1;
|
||||||
$size = $stat ? $stat['size'] : 0;
|
$size = $stat ? $stat['size'] : 0;
|
||||||
|
|
||||||
|
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in a new issue