Skip to content

Commit

Permalink
Support for .shar files
Browse files Browse the repository at this point in the history
  • Loading branch information
raulfraile committed Dec 14, 2014
1 parent 514b837 commit 0fd6a5d
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/ContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(
Method\Command\GnuGzip::getClass(),
Method\Command\GnuTar::getClass(),
Method\Command\Unrar::getClass(),
Method\Command\Unshar::getClass(),
Method\Command\Unzip::getClass(),
Method\Command\x7zip::getClass(),
Method\Command\Xz::getClass(),
Expand Down
2 changes: 1 addition & 1 deletion src/Format/Simple/Cpio.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Cpio extends AbstractFormat
*/
public function getCompressionRatioLevel()
{
return FormatInterface::RATIO_LEVEL_MIDDLE;
return FormatInterface::RATIO_LEVEL_LOWEST;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Format/Simple/Shar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Shar extends AbstractFormat
*/
public function getCompressionRatioLevel()
{
return FormatInterface::RATIO_LEVEL_MIDDLE;
return FormatInterface::RATIO_LEVEL_LOWEST;
}

/**
Expand Down
68 changes: 68 additions & 0 deletions src/Method/Command/Unshar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Distill package.
*
* (c) Raul Fraile <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Distill\Method\Command;

use Distill\Exception;
use Distill\Format;

/**
* Extracts files from shar archives.
*
* @author Raul Fraile <[email protected]>
*/
class Unshar extends AbstractCommandMethod
{

/**
* {@inheritdoc}
*/
public function extract($file, $target, Format\FormatInterface $format)
{
$this->checkSupport($format);

$this->getFilesystem()->mkdir($target);

$command = sprintf('cd %s && sh %s', escapeshellarg($target), escapeshellarg($file));

$exitCode = $this->executeCommand($command);

return $this->isExitCodeSuccessful($exitCode);
}

/**
* {@inheritdoc}
*/
public function isSupported()
{
if (null === $this->supported) {
$this->supported = $this->existsCommand('sh');
}

return $this->supported;
}

/**
* {@inheritdoc}
*/
public static function getClass()
{
return get_class();
}

/**
* {@inheritdoc}
*/
public function isFormatSupported(Format\FormatInterface $format = null)
{
return $format instanceof Format\Simple\Shar;
}
}
19 changes: 19 additions & 0 deletions tests/Format/Simple/SharTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Distill\Tests\Format\Simple;

use Distill\Format\Simple\Shar;
use Distill\Tests\Format\AbstractFormatTest;

class SharTest extends AbstractFormatTest
{
public function setUp()
{
$this->format = new Shar();
}

public function testCompressionRatioLevelIsValid()
{
$this->assertLevelValid($this->format->getCompressionRatioLevel());
}
}
34 changes: 34 additions & 0 deletions tests/Method/Command/UnsharTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Distill\Tests\Method\Command;

use Distill\Method;
use Distill\Format;
use Distill\Tests\Method\AbstractMethodTest;

class UnsharTest extends AbstractMethodTest
{
public function setUp()
{
$this->method = new Method\Command\Unshar();

if (false === $this->method->isSupported()) {
$this->markTestSkipped('The unshar command is not installed');
}

parent::setUp();
}

public function testExtractCorrectSharFile()
{
$target = $this->getTemporaryPath();
$this->clearTemporaryPath();

$response = $this->extract('file_ok.shar', $target, new Format\Simple\Shar());

$this->assertTrue($response);
$this->assertUncompressed($target, 'file_ok.shar');
$this->clearTemporaryPath();
}

}
Binary file added tests/Resources/files/file_fake.shar
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/Resources/files/file_ok.shar
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This is a shell archive. Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file". Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
# 1.txt
# 2.txt
# 3.txt
#
echo x - 1.txt
sed 's/^X//' >1.txt << 'END-of-1.txt'
X1.txt file
END-of-1.txt
echo x - 2.txt
sed 's/^X//' >2.txt << 'END-of-2.txt'
X2.txt file
END-of-2.txt
echo x - 3.txt
sed 's/^X//' >3.txt << 'END-of-3.txt'
X3.txt file
END-of-3.txt
exit

3 changes: 3 additions & 0 deletions tests/Resources/files/file_ok.shar.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/1.txt|1.txt file
/2.txt|2.txt file
/3.txt|3.txt file
4 changes: 3 additions & 1 deletion tests/Resources/scripts/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ $DIR/epub.sh
$DIR/jar.sh
$DIR/dmg.sh
$DIR/iso.sh
$DIR/ar+deb.sh
$DIR/ar+deb.sh
$DIR/cpio.sh
$DIR/shar.sh
24 changes: 24 additions & 0 deletions tests/Resources/scripts/shar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

################################################################################
# Initial configuration
################################################################################
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
FILES_DIR="$DIR/../files"

################################################################################
# Clean files
################################################################################
rm -f $FILES_DIR/file_ok.shar $FILES_DIR/file_fake.shar

################################################################################
# Generate files
################################################################################

# shar: fake file
dd if=/dev/urandom of=$FILES_DIR/file_fake.shar bs=1 count=1240

# shar: regular file
cd $FILES_DIR/uncompressed
shar 1.txt 2.txt 3.txt > ../file_ok.shar
printf "\1.txt|1.txt file\n\2.txt|2.txt file\n\3.txt|3.txt file" > ../file_ok.shar.key

0 comments on commit 0fd6a5d

Please sign in to comment.