Skip to content

Commit 0ec097a

Browse files
author
Julien Jacottet
committed
replace PharData with tar command to prevent memory exhaustion
1 parent 69d116e commit 0ec097a

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/Automate/Archiver.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
use Symfony\Component\Filesystem\Filesystem;
1515
use Symfony\Component\Filesystem\Path;
16-
use Symfony\Component\Finder\Finder;
16+
use Symfony\Component\Process\Exception\ProcessFailedException;
17+
use Symfony\Component\Process\Process;
1718

1819
use function Symfony\Component\String\u;
1920

@@ -31,29 +32,34 @@ public function __construct()
3132
/**
3233
* @param string[] $exclude
3334
*/
34-
public function archive(string $path, array $exclude = []): \PharData
35+
public function archive(string $path, array $exclude = []): string
3536
{
3637
$this->clear($path);
3738

38-
$archive = new \PharData($this->getArchiveFileName($path, false));
39+
$archiveFile = $this->getArchiveFileName($path);
3940
$cwd = getcwd();
4041

41-
if (is_dir($path)) {
42-
$finder = new Finder();
43-
$finder->files()
44-
->ignoreDotFiles(false)
45-
->in($path)
46-
->notPath($exclude);
42+
// Build tar command
43+
$command = ['tar', '-czf', $archiveFile];
4744

48-
$archive->buildFromIterator($finder->getIterator(), $cwd);
49-
} else {
50-
$archive->addFile($path, Path::makeRelative($path, $cwd));
45+
// Add exclusions
46+
foreach ($exclude as $pattern) {
47+
$command[] = '--exclude='.$pattern;
5148
}
5249

53-
/** @var \PharData $compressed */
54-
$compressed = $archive->compress(\Phar::GZ);
50+
// Add the path to archive (relative to cwd)
51+
$relativePath = Path::makeRelative($path, $cwd);
52+
$command[] = $relativePath;
5553

56-
return $compressed;
54+
// Execute tar command
55+
$process = new Process($command, $cwd);
56+
$process->run();
57+
58+
if (!$process->isSuccessful()) {
59+
throw new ProcessFailedException($process);
60+
}
61+
62+
return $archiveFile;
5763
}
5864

5965
public function getArchiveFileName(string $path, bool $compressed = true): string

src/Automate/Workflow/Context.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ public function upload(string $path, ?array $exclude, ?array $serversList = null
134134
}
135135

136136
$this->logger->info(' Upload preparation ...');
137-
$archive = $this->archiver->archive($path, $exclude);
137+
$archiveFile = $this->archiver->archive($path, $exclude);
138138
$archiveFileName = $this->archiver->getArchiveFileName($path);
139139

140140
$this->logger->info(' Send data ...');
141-
$this->exec(static function (Session $session) use ($archive, $archiveFileName): void {
141+
$this->exec(static function (Session $session) use ($archiveFile, $archiveFileName): void {
142142
$targetPath = Path::join($session->getReleasePath(), $archiveFileName);
143-
$session->upload($archive->getPath(), $targetPath);
143+
$session->upload($archiveFile, $targetPath);
144144
}, $serversList);
145145

146146
$this->logger->info(' Untar data...');

tests/Automate/ArchiverTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ protected function tearDown(): void
3131

3232
public function testArchive(): void
3333
{
34-
$phar = $this->archiver->archive(self::PATH);
34+
$archiveFile = $this->archiver->archive(self::PATH);
3535

36+
// Verify the archive file was created
37+
$this->assertFileExists($archiveFile);
38+
39+
// Open and verify archive contents
40+
$phar = new \PharData($archiveFile);
3641
$this->assertTrue($phar->offsetExists('tests/fixtures/folder/a.txt'));
3742
$this->assertTrue($phar->offsetExists('tests/fixtures/folder/sub/b.txt'));
3843
$this->assertTrue($phar->offsetExists('tests/fixtures/folder/.sub/c.txt'));

0 commit comments

Comments
 (0)