Skip to content

Commit dca8dae

Browse files
author
Julien Jacottet
committed
create tar archive in temp directory to prevent "file changed" error
1 parent 0ec097a commit dca8dae

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Automate/Archiver.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ public function archive(string $path, array $exclude = []): string
3939
$archiveFile = $this->getArchiveFileName($path);
4040
$cwd = getcwd();
4141

42+
// Create archive in temp directory to avoid "file changed as we read it" error
43+
$tempArchive = sys_get_temp_dir().'/'.basename($archiveFile);
44+
4245
// Build tar command
43-
$command = ['tar', '-czf', $archiveFile];
46+
$command = ['tar', '-czf', $tempArchive];
4447

4548
// Add exclusions
4649
foreach ($exclude as $pattern) {
4750
$command[] = '--exclude='.$pattern;
4851
}
4952

50-
// Add the path to archive (relative to cwd)
53+
// Add the path to archive (use relative path to avoid absolute path warnings)
5154
$relativePath = Path::makeRelative($path, $cwd);
52-
$command[] = $relativePath;
55+
$command[] = $relativePath ?: '.';
5356

5457
// Execute tar command
5558
$process = new Process($command, $cwd);
@@ -59,19 +62,18 @@ public function archive(string $path, array $exclude = []): string
5962
throw new ProcessFailedException($process);
6063
}
6164

65+
$this->filesystem->rename($tempArchive, $archiveFile, true);
66+
6267
return $archiveFile;
6368
}
6469

65-
public function getArchiveFileName(string $path, bool $compressed = true): string
70+
public function getArchiveFileName(string $path): string
6671
{
67-
$name = u(self::BASE_ARCHIVE_NAME.$path)->snake();
68-
69-
return $compressed ? $name.'.tar.gz' : $name.'.tar';
72+
return u(self::BASE_ARCHIVE_NAME.$path)->snake().'.tar.gz';
7073
}
7174

7275
public function clear(string $path): void
7376
{
74-
$this->filesystem->remove($this->getArchiveFileName($path, false));
7577
$this->filesystem->remove($this->getArchiveFileName($path));
7678
}
7779
}

0 commit comments

Comments
 (0)