File tree Expand file tree Collapse file tree
src/folderview.plus/usr/local/emhttp/plugins/folderview.plus/server Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1689,6 +1689,21 @@ function getLastGoodJsonPath(string $path): string {
16891689 return $ path . '.lastgood ' ;
16901690 }
16911691
1692+ function createAtomicWriteTempPath (string $ path ): string {
1693+ $ parent = dirname ($ path );
1694+ $ prefix = basename ($ path ) . '.tmp. ' ;
1695+ $ tempPath = @tempnam ($ parent , $ prefix );
1696+ if (is_string ($ tempPath ) && $ tempPath !== '' ) {
1697+ return $ tempPath ;
1698+ }
1699+
1700+ try {
1701+ return $ path . '.tmp. ' . getmypid () . '. ' . bin2hex (random_bytes (6 ));
1702+ } catch (Throwable $ error ) {
1703+ return $ path . '.tmp. ' . getmypid () . '. ' . uniqid ('' , true );
1704+ }
1705+ }
1706+
16921707 function writeJsonObjectAtomic (string $ path , array $ payload ): void {
16931708 $ parent = dirname ($ path );
16941709 if (!is_dir ($ parent )) {
@@ -1698,7 +1713,7 @@ function writeJsonObjectAtomic(string $path, array $payload): void {
16981713 if (!is_string ($ encoded ) || $ encoded === '' ) {
16991714 throw new RuntimeException ("Failed to encode JSON payload for ' $ path'. " );
17001715 }
1701- $ tmpPath = $ path . ' .tmp ' ;
1716+ $ tmpPath = createAtomicWriteTempPath ( $ path) ;
17021717 if (@file_put_contents ($ tmpPath , $ encoded , LOCK_EX ) === false ) {
17031718 throw new RuntimeException ("Failed to write temp JSON payload for ' $ path'. " );
17041719 }
You can’t perform that action at this time.
0 commit comments