-
-
Notifications
You must be signed in to change notification settings - Fork 487
fix: client updater #1632
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
base: main
Are you sure you want to change the base?
fix: client updater #1632
Changes from 1 commit
47b759a
07b9f44
63390e7
e17abfb
dd08712
4a35f31
799a015
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,16 +23,16 @@ function sendError($error) { | |
| } | ||
|
|
||
| $data = json_decode(file_get_contents("php://input")); | ||
| //if(!$data) { | ||
| // sendError("Invalid input data"); | ||
| //} | ||
| if (!$data || !is_object($data)) { | ||
| sendError("Invalid input data"); | ||
| } | ||
|
|
||
| $version = $data->version ?: 0; // APP_VERSION from init.lua | ||
| $build = $data->build ?: ""; // 2.4, 2.4.1, 2.5, etc | ||
| $os = $data->os ?: "unknown"; // android, windows, mac, linux, unknown | ||
| $platform = $data->platform ?: ""; // WIN32-WGL, X11-GLX, ANDROID-EGL, etc | ||
| $args = $data->args; // custom args when calling Updater.check() | ||
| $binary = $binaries[$platform] ?: ""; | ||
| $version = $data->version ?? 0; // APP_VERSION from init.lua | ||
| $build = $data->build ?? ""; // 2.4, 2.4.1, 2.5, etc | ||
| $os = $data->os ?? "unknown"; // android, windows, mac, linux, unknown | ||
| $platform = $data->platform ?? ""; // WIN32-WGL, X11-GLX, ANDROID-EGL, etc | ||
| $args = $data->args ?? []; // custom args when calling Updater.check() | ||
| $binary = $binaries[$platform] ?? ""; | ||
|
|
||
| $cache = null; | ||
| $cache_file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $checksum_file; | ||
|
|
@@ -41,39 +41,68 @@ function sendError($error) { | |
| } | ||
| if(!$cache) { // update cache | ||
| $dir = realpath($files_dir); | ||
| $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); | ||
| $cache = array(); | ||
| foreach ($rii as $file) { | ||
| if (!$dir || !is_dir($dir)) { | ||
| sendError("Server configuration error: files directory not found"); | ||
| } | ||
|
|
||
| // File locking to prevent race conditions | ||
| $lock_file = $cache_file . ".lock"; | ||
| $lock = fopen($lock_file, "w"); | ||
| if (!flock($lock, LOCK_EX | LOCK_NB)) { | ||
| // Another process is updating cache, wait and use existing cache | ||
| fclose($lock); | ||
| usleep(100000); // 100ms | ||
| if (file_exists($cache_file)) { | ||
| $cache = json_decode(file_get_contents($cache_file), true); | ||
| } | ||
| } | ||
|
|
||
| if (!$cache) { | ||
| $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); | ||
| $cache = array(); | ||
| foreach ($rii as $file) { | ||
| if (!$file->isFile()) | ||
| continue; | ||
| $path = str_replace($dir, '', $file->getPathname()); | ||
| $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); | ||
| $checksum = hash_file("crc32b", $file->getPathname()); | ||
|
|
||
| if ($checksum === true || $checksum != "") { | ||
| $parsed_checksum = ltrim($checksum, '0'); | ||
| if ($parsed_checksum === '') { | ||
| $parsed_checksum = '0'; | ||
| if ($checksum !== false && $checksum !== "") { | ||
| $parsed_checksum = ltrim($checksum, '0'); | ||
| if ($parsed_checksum === '') { | ||
| $parsed_checksum = '0'; | ||
| } | ||
| $cache[$path] = $parsed_checksum; | ||
| } | ||
| $cache[$path] = $parsed_checksum; | ||
| } | ||
| file_put_contents($cache_file . ".tmp", json_encode($cache)); | ||
| rename($cache_file . ".tmp", $cache_file); | ||
|
|
||
| // Release lock | ||
| flock($lock, LOCK_UN); | ||
| } | ||
| if (isset($lock) && is_resource($lock)) { | ||
| fclose($lock); | ||
| } | ||
|
Comment on lines
+49
to
98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Race condition: cache can be built without lock protection. When Additionally, if this path is taken and cache is built, line 82 calls 🐛 Proposed fix to maintain lock throughout cache building // File locking to prevent race conditions
$lock_file = $cache_file . ".lock";
$lock = fopen($lock_file, "w");
if (!flock($lock, LOCK_EX | LOCK_NB)) {
// Another process is updating cache, wait and use existing cache
- fclose($lock);
usleep(100000); // 100ms
if (file_exists($cache_file)) {
$cache = json_decode(file_get_contents($cache_file), true);
}
+ if (!$cache) {
+ // Wait for the lock if cache still not available
+ flock($lock, LOCK_EX);
+ // Re-check cache after acquiring lock
+ if (file_exists($cache_file)) {
+ $cache = json_decode(file_get_contents($cache_file), true);
+ }
+ }
}
if (!$cache) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
$cache = array();
foreach ($rii as $file) {
// ... rest of cache building ...
}
file_put_contents($cache_file . ".tmp", json_encode($cache));
rename($cache_file . ".tmp", $cache_file);
-
- // Release lock
- flock($lock, LOCK_UN);
}
- if (isset($lock) && is_resource($lock)) {
- fclose($lock);
- }
+ // Release lock and close handle
+ flock($lock, LOCK_UN);
+ fclose($lock);
}🤖 Prompt for AI Agents |
||
| file_put_contents($cache_file . ".tmp", json_encode($cache)); | ||
| rename($cache_file . ".tmp", $cache_file); | ||
| } | ||
| $ret = array("url" => $files_url, "files" => array(), "keepFiles" => false); | ||
| foreach($cache as $file => $checksum) { | ||
| $base = trim(explode("/", ltrim($file, "/"))[0]); | ||
| if(in_array($base, $files_and_dirs)) { | ||
| $ret["files"][$file] = $checksum; | ||
| } | ||
| if($base == $binary && !empty($binary)) { | ||
| // Use basename to correctly match binary filename regardless of path | ||
| $filename = basename($file); | ||
| if($filename == $binary && !empty($binary)) { | ||
| $ret["binary"] = array("file" => $file, "checksum" => $checksum); | ||
| } | ||
| } | ||
|
|
||
| $body = json_encode($ret, JSON_PRETTY_PRINT); | ||
| header("Content-length: " . strlen($body)); | ||
| header("Content-Type: application/json"); | ||
| header("Content-Length: " . strlen($body)); | ||
| header("X-Content-Type-Options: nosniff"); | ||
| header("Cache-Control: no-store"); | ||
| echo($body); | ||
|
|
||
| ?> | ||
Uh oh!
There was an error while loading. Please reload this page.