Use AtomicWriter to write the updated license file
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Sep 2023 09:21:03 +0000 (11:21 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 29 Sep 2023 09:21:03 +0000 (11:21 +0200)
The license file is rewritten automatically when refreshing the package list.
Depending on the timing it might only be partially written in case of
concurrent requests.

wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php

index 5823bd620ce7a9cee20f075c8b804bcd8a29da08..e0151f559f9f992776e36ba80a74e04a52ab59da 100644 (file)
@@ -8,6 +8,7 @@ use CuyZ\Valinor\Mapper\Source\Source;
 use CuyZ\Valinor\MapperBuilder;
 use GuzzleHttp\Psr7\Request;
 use wcf\data\package\update\server\PackageUpdateServer;
+use wcf\system\io\AtomicWriter;
 use wcf\system\io\HttpFactory;
 use wcf\system\package\license\exception\MissingCredentials;
 use wcf\system\package\license\exception\ParsingFailed;
@@ -27,18 +28,17 @@ final class LicenseApi
 
     public function updateLicenseFile(?LicenseData $data): void
     {
-        @\file_put_contents(
-            self::LICENSE_FILE,
-            \sprintf(
-                <<<'EOT'
+        $writer = new AtomicWriter(self::LICENSE_FILE);
+        $writer->write(\sprintf(
+            <<<'EOT'
                 <?php
                 /* GENERATED AT %s -- DO NOT EDIT */
                 return unserialize(%s);
                 EOT,
-                \gmdate('r', \TIME_NOW),
-                \var_export(\serialize($data), true),
-            )
-        );
+            \gmdate('r', \TIME_NOW),
+            \var_export(\serialize($data), true),
+        ));
+        $writer->flush();
 
         WCF::resetZendOpcache(self::LICENSE_FILE);
     }