Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle directory symlinks properly. #75

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Model/ZipEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ public function getUnixMode()
return $mode;
}

return $this->isDirectory ? 040755 : 0100644;
return $this->isDirectory && !$this->isUnixSymlink() ? 040755 : 0100644;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Util/FilesUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function removeDir($dir)

/** @var \SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
$function = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$function = ($fileInfo->isDir() && !$fileInfo->isLink() ? 'rmdir' : 'unlink');
$function($fileInfo->getPathname());
}
@rmdir($dir);
Expand Down
3 changes: 2 additions & 1 deletion src/ZipFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public function addSplFile(\SplFileInfo $file, $entryName = null, array $options
}

$entryName = $this->normalizeEntryName($entryName);
$entryName = $file->isDir() ? rtrim($entryName, '/\\') . '/' : $entryName;
$entryName = $file->isDir() && !$file->isLink() ? rtrim($entryName, '/\\') . '/' : $entryName;

$zipEntry = new ZipEntry($entryName);
$zipEntry->setCreatedOS(ZipPlatform::OS_UNIX);
Expand All @@ -706,6 +706,7 @@ public function addSplFile(\SplFileInfo $file, $entryName = null, array $options
$zipEntry->setCompressedSize($lengthLinkTarget);
$zipEntry->setCrc(crc32($linkTarget));
$filePerms |= UnixStat::UNX_IFLNK;
$filePerms &= ~UnixStat::UNX_IFDIR;

$zipData = new ZipNewData($zipEntry, $linkTarget);
} elseif ($file->isFile()) {
Expand Down
62 changes: 62 additions & 0 deletions tests/SymlinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
final class SymlinkTest extends ZipTestCase
{


/**
* @dataProvider provideAllowSymlink
*
Expand Down Expand Up @@ -67,6 +69,66 @@ public function testSymlink($allowSymlink)
}
}

/**
* @dataProvider provideAllowSymlink
*
* @param bool $allowSymlink
*
* @throws \Exception
*/
public function testSymlinkedDirectory($allowSymlink)
{
if (self::skipTestForWindows()) {
return;
}

if (!is_dir($this->outputDirname)) {
self::assertTrue(mkdir($this->outputDirname, 0755, true));
}

$dirToBeLinked = $this->outputDirname . '/dir-to-be-linked';
self::assertTrue(mkdir($dirToBeLinked, 0755, true));

$contentsFile = random_bytes(100);
$filePath = $dirToBeLinked . '/file.bin';
self::assertNotFalse(file_put_contents($filePath, $contentsFile));
$symlinkPath = $this->outputDirname . '/symlink.dir';
$symlinkTarget = basename($dirToBeLinked);
self::assertTrue(symlink($symlinkTarget, $symlinkPath));

$finder = (new Finder())->in($this->outputDirname);
$zipFile = new ZipFile();
$zipFile->addFromFinder($finder);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();

self::assertCorrectZipArchive($this->outputFilename);

FilesUtil::removeDir($this->outputDirname);
self::assertFalse(is_dir($this->outputDirname));
self::assertTrue(mkdir($this->outputDirname, 0755, true));

$zipFile->openFile($this->outputFilename);
$zipFile->extractTo($this->outputDirname, null, [
ZipOptions::EXTRACT_SYMLINKS => $allowSymlink,
]);
$zipFile->close();

$splFileInfo = new \SplFileInfo($symlinkPath);

if ($allowSymlink) {
self::assertTrue($splFileInfo->isLink());
self::assertSame($splFileInfo->getLinkTarget(), $symlinkTarget);
$linkedFilename = $symlinkPath."/".basename($filePath);
self::assertFileExists($linkedFilename);
$linkedFileContents = file_get_contents($linkedFilename);
self::assertEquals($contentsFile, $linkedFileContents);
} else {
self::assertFalse($splFileInfo->isLink());
self::assertStringEqualsFile($symlinkPath, $symlinkTarget);
}
}

/**
* @return \Generator
*/
Expand Down