Merge pull request #15496 from owncloud/enc-check-if-key-exists-before-deleting

Check if the key exists, before trying to delete it
This commit is contained in:
Thomas Müller 2015-04-09 14:45:40 +02:00
commit 1d9bd3d31e
3 changed files with 29 additions and 12 deletions

View File

@ -140,11 +140,11 @@ class Storage implements \OCP\Encryption\Keys\IStorage {
* @param string $uid ID if the user for whom we want to delete the key * @param string $uid ID if the user for whom we want to delete the key
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteUserKey($uid, $keyId) { public function deleteUserKey($uid, $keyId) {
$path = $this->constructUserKeyPath($keyId, $uid); $path = $this->constructUserKeyPath($keyId, $uid);
return $this->view->unlink($path); return !$this->view->file_exists($path) || $this->view->unlink($path);
} }
/** /**
@ -153,22 +153,23 @@ class Storage implements \OCP\Encryption\Keys\IStorage {
* @param string $path path to file * @param string $path path to file
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteFileKey($path, $keyId) { public function deleteFileKey($path, $keyId) {
$keyDir = $this->getFileKeyDir($path); $keyDir = $this->getFileKeyDir($path);
return $this->view->unlink($keyDir . $keyId); return !$this->view->file_exists($keyDir . $keyId) || $this->view->unlink($keyDir . $keyId);
} }
/** /**
* delete all file keys for a given file * delete all file keys for a given file
* *
* @param string $path to the file * @param string $path to the file
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteAllFileKeys($path) { public function deleteAllFileKeys($path) {
$keyDir = $this->getFileKeyDir($path); $keyDir = $this->getFileKeyDir($path);
return $this->view->deleteAll(dirname($keyDir)); $path = dirname($keyDir);
return !$this->view->file_exists($path) || $this->view->deleteAll($path);
} }
/** /**
@ -177,11 +178,11 @@ class Storage implements \OCP\Encryption\Keys\IStorage {
* *
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteSystemUserKey($keyId) { public function deleteSystemUserKey($keyId) {
$path = $this->constructUserKeyPath($keyId); $path = $this->constructUserKeyPath($keyId);
return $this->view->unlink($path); return !$this->view->file_exists($path) || $this->view->unlink($path);
} }

View File

@ -89,7 +89,7 @@ interface IStorage {
* @param string $uid ID if the user for whom we want to delete the key * @param string $uid ID if the user for whom we want to delete the key
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteUserKey($uid, $keyId); public function deleteUserKey($uid, $keyId);
@ -99,7 +99,7 @@ interface IStorage {
* @param string $path path to file * @param string $path path to file
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteFileKey($path, $keyId); public function deleteFileKey($path, $keyId);
@ -107,7 +107,7 @@ interface IStorage {
* delete all file keys for a given file * delete all file keys for a given file
* *
* @param string $path to the file * @param string $path to the file
* @return boolean * @return boolean False when the keys could not be deleted
*/ */
public function deleteAllFileKeys($path); public function deleteAllFileKeys($path);
@ -117,7 +117,7 @@ interface IStorage {
* *
* @param string $keyId id of the key * @param string $keyId id of the key
* *
* @return boolean * @return boolean False when the key could not be deleted
*/ */
public function deleteSystemUserKey($keyId); public function deleteSystemUserKey($keyId);

View File

@ -197,6 +197,10 @@ class StorageTest extends TestCase {
} }
public function testDeleteUserKey() { public function testDeleteUserKey() {
$this->view->expects($this->once())
->method('file_exists')
->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
->willReturn(true);
$this->view->expects($this->once()) $this->view->expects($this->once())
->method('unlink') ->method('unlink')
->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey')) ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
@ -208,6 +212,10 @@ class StorageTest extends TestCase {
} }
public function testDeleteSystemUserKey() { public function testDeleteSystemUserKey() {
$this->view->expects($this->once())
->method('file_exists')
->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
->willReturn(true);
$this->view->expects($this->once()) $this->view->expects($this->once())
->method('unlink') ->method('unlink')
->with($this->equalTo('/files_encryption/encModule/shareKey_56884')) ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
@ -228,6 +236,10 @@ class StorageTest extends TestCase {
$this->util->expects($this->any()) $this->util->expects($this->any())
->method('isSystemWideMountPoint') ->method('isSystemWideMountPoint')
->willReturn(true); ->willReturn(true);
$this->view->expects($this->once())
->method('file_exists')
->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
->willReturn(true);
$this->view->expects($this->once()) $this->view->expects($this->once())
->method('unlink') ->method('unlink')
->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey')) ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
@ -248,6 +260,10 @@ class StorageTest extends TestCase {
$this->util->expects($this->any()) $this->util->expects($this->any())
->method('isSystemWideMountPoint') ->method('isSystemWideMountPoint')
->willReturn(false); ->willReturn(false);
$this->view->expects($this->once())
->method('file_exists')
->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
->willReturn(true);
$this->view->expects($this->once()) $this->view->expects($this->once())
->method('unlink') ->method('unlink')
->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey')) ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))