Skip to content

Commit 661e369

Browse files
author
FolderView Plus Test
committed
Fix concurrent prefs atomic writes
1 parent 4d64d92 commit 661e369

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

  • src/folderview.plus/usr/local/emhttp/plugins/folderview.plus/server

src/folderview.plus/usr/local/emhttp/plugins/folderview.plus/server/lib.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)