Skip to content
Open
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
25 changes: 22 additions & 3 deletions src/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,35 @@ public function save(string $key, string $variableKey, mixed $data): void

// for performance reasons we don't use SmartFileSystem
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported), null);
$copySuccess = @\copy($tmpPath, $filePath);
@\unlink($tmpPath);

if ($copySuccess) {
/**
* @see https://github.com/rectorphp/rector/issues/9876
*
* Try the atomic write first, as rename() also works on unaffected Windows PHP versions
*/
$renameSuccess = @\rename($tmpPath, $filePath);
if ($renameSuccess) {
return;
}

if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}

/**
* @see https://github.com/rectorphp/rector/issues/8432
* @see https://github.com/php/php-src/issues/7910
*
* Fall back only on affected Windows PHP versions where rename() failed
*/
$copySuccess = @\copy($tmpPath, $filePath);
@\unlink($tmpPath);

if ($copySuccess) {
return;
}

throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}

public function clean(string $key): void
Expand Down
Loading