Skip to content

Commit

Permalink
Finish restore from TAR.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed May 17, 2017
1 parent b129274 commit 8169dfb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 30 deletions.
67 changes: 63 additions & 4 deletions framework/Backup/lib/Horde/Backup/Reader.php
Expand Up @@ -13,10 +13,12 @@

namespace Horde\Backup;

use Horde_Compress_Tar as Tar;
use Horde_Compress_Zip as Zip;
use Horde_Pack_Driver_Json as Json;
use Horde\Backup\Exception;
use Horde\Backup\Reader\ZipIterator;
use Horde\Backup\Reader\CompressIterator\Tar as TarIterator;
use Horde\Backup\Reader\CompressIterator\Zip as ZipIterator;
use Horde\Backup\Translation;

/**
Expand Down Expand Up @@ -131,12 +133,70 @@ protected function _restoreFromZip($file, $applications)
{
$user = basename($file, '.zip');
$contents = file_get_contents($file);
$packer = new Json();
$compress = new Zip();
$files = $compress->decompress(
$contents, array('action' => Zip::ZIP_LIST)
);

return $this->_buildCollections(
$files,
$applications,
$contents,
$user,
function ($application, $resource, $files, $contents)
{
return new ZipIterator(
$application, $resource, $files, $contents
);
}
);
}

/**
* Restores user data from a TAR file.
*
* @param string $file Pathname to a TAR backup file.
* @param array $applications A list of applications to restore. Defaults
* to all backups.
*
* @return \Horde\Backup\Collection[] All restored object collections.
*/
protected function _restoreFromTar($file, $applications)
{
$user = basename($file, '.tar');
$contents = file_get_contents($file);
$compress = new Tar();
$files = $compress->decompress($contents);

return $this->_buildCollections(
$files,
$applications,
$contents,
$user,
function ($application, $resource, $files, $contents)
{
return new TarIterator($application, $resource, $files);
}
);
}

/**
* Builds a list of object collections from any Horde_Compress backend.
*
* @param array $files Archive info from Horde_Compress.
* @param array $applications A list of applications to restore. Defaults
* to all backups.
* @param string $contents The archive file contents.
* @param string $user A user name.
* @param callable $factory A factory for iterators that are passed to
* \Horde\Backup\Collection.
*
* @return \Horde\Backup\Collection[] All restored object collections.
*/
protected function _buildCollections(
$files, $applications, $contents, $user, $factory
)
{
$data = array();
foreach ($files as $key => $info) {
$path = explode('/', $info['name']);
Expand All @@ -150,7 +210,7 @@ protected function _restoreFromZip($file, $applications)
$collections[$application] = array();
foreach (array_keys($resources) as $resource) {
$collections[$application][] = new Collection(
new ZipIterator($contents, $application, $resource, $files),
$factory($application, $resource, $files, $contents),
$user,
$resource
);
Expand All @@ -160,7 +220,6 @@ protected function _restoreFromZip($file, $applications)
return $collections;
}


/**
* Builds a backup iterator for individual users.
*
Expand Down
Expand Up @@ -20,37 +20,23 @@
use Horde_Pack_Driver_Json as Json;

/**
* Iterates over certain files in a ZIP archive.
* Iterates over certain files from a Horde_Compress archive.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Backup
*/
class ZipIterator extends IteratorIterator
abstract class CompressIterator extends IteratorIterator
{
/**
* The ZIP file contents.
*
* @var string
*/
protected $_contents;

/**
* Archive info from Horde_Compress_Zip.
*
* @var array
*/
protected $_info;

/**
* The ZIP uncompressor.
*
* @var Horde_Compress_Zip
*/
protected $_compress;

/**
* The JSON unpacker.
*
Expand All @@ -61,18 +47,15 @@ class ZipIterator extends IteratorIterator
/**
* Constructor.
*
* @param string $contents The ZIP file contents.
* @param string $application An application name.
* @param string $type A collection type like "calendar" or
* "contact".
* @param array $info ZIP archive info from Horde_Compress_Zip.
*/
public function __construct($contents, $application, $type, $info)
public function __construct($application, $type, $info)
{
$this->_contents = $contents;
$this->_info = $info;
$this->_packer = new Json();
$this->_compress = new Zip();

$iterator = new CallbackFilterIterator(
new ArrayIterator($info),
Expand All @@ -88,7 +71,7 @@ function ($current, $key, $iterator) use ($application, $type)
/**
* Returns the unpacked backup data.
*
* @return \Horde\Backup\User
* @return mixed Backup data.
*/
public function current()
{
Expand Down
14 changes: 10 additions & 4 deletions framework/Backup/package.xml
Expand Up @@ -10,7 +10,7 @@
<email>jan@horde.org</email>
<active>yes</active>
</lead>
<date>2017-05-16</date>
<date>2017-05-17</date>
<version>
<release>1.0.0alpha1</release>
<api>1.0.0alpha1</api>
Expand All @@ -36,7 +36,11 @@
<dir name="Horde">
<dir name="Backup">
<dir name="Reader">
<file name="ZipIterator.php" role="php" />
<dir name="CompressIterator">
<file name="Tar.php" role="php" />
<file name="Zip.php" role="php" />
</dir> <!-- /lib/Horde/Backup/Reader/CompressIterator -->
<file name="CompressIterator.php" role="php" />
</dir> <!-- /lib/Horde/Backup/Reader -->
<file name="Collection.php" role="php" />
<file name="Exception.php" role="php" />
Expand Down Expand Up @@ -92,7 +96,9 @@
<install as="Horde/Backup/User.php" name="lib/Horde/Backup/User.php" />
<install as="Horde/Backup/Users.php" name="lib/Horde/Backup/Users.php" />
<install as="Horde/Backup/Writer.php" name="lib/Horde/Backup/Writer.php" />
<install as="Horde/Backup/Reader/ZipIterator.php" name="lib/Horde/Backup/Reader/ZipIterator.php" />
<install as="Horde/Backup/Reader/CompressIterator.php" name="lib/Horde/Backup/Reader/CompressIterator.php" />
<install as="Horde/Backup/Reader/CompressIterator/Tar.php" name="lib/Horde/Backup/Reader/CompressIterator/Tar.php" />
<install as="Horde/Backup/Reader/CompressIterator/Zip.php" name="lib/Horde/Backup/Reader/CompressIterator/Zip.php" />
<install as="Horde/Backup/AllTests.php" name="test/Horde/Backup/AllTests.php" />
<install as="Horde/Backup/ApplicationTest.php" name="test/Horde/Backup/ApplicationTest.php" />
<install as="Horde/Backup/BackupRestoreTest.php" name="test/Horde/Backup/BackupRestoreTest.php" />
Expand All @@ -113,7 +119,7 @@
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2017-05-16</date>
<date>2017-05-17</date>
<license uri="http://www.horde.org/licenses/bsd">BSD</license>
<notes>
* Initial release.
Expand Down
1 change: 0 additions & 1 deletion framework/Backup/test/Horde/Backup/BackupRestoreTest.php
Expand Up @@ -148,7 +148,6 @@ public function testBackupToTar()
*/
public function testRestoreFromTar($temp)
{
$this->markTestIncomplete();
$this->_restoreTest(
$temp,
array(
Expand Down

0 comments on commit 8169dfb

Please sign in to comment.