From f02134d6b3a93dce6859bf04e0834b0286ecc40b Mon Sep 17 00:00:00 2001 From: darylc Date: Sun, 8 Jan 2023 07:32:49 +0000 Subject: [PATCH] Check size of fppos file before upgrading and remove partial downloads #1400 --- SD/upgradeOS-part1.sh | 19 +++++++++++++++++ www/api/controllers/git.php | 41 ++++++++++++++++++++++++++++++++++++- www/api/index.php | 1 + www/upgradeOS.php | 11 +++++++--- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/SD/upgradeOS-part1.sh b/SD/upgradeOS-part1.sh index f48ee59a1..44b85858d 100755 --- a/SD/upgradeOS-part1.sh +++ b/SD/upgradeOS-part1.sh @@ -1,5 +1,24 @@ #!/bin/bash +FPPOS=`/usr/bin/basename $1` +GITHUBSIZE=`curl -fsSL http://127.0.0.1/api/git/releases/sizes | grep ${FPPOS} | awk -F, '{print $2}'` +OURSIZE=`/usr/bin/stat -c %s $1` + +if ! [[ $GITHUBSIZE =~ ^-?[0-9]+$ ]]; +then + echo "Couldn't get fppos size from Github, attempting upgrade anyway" +else + if [ "$OURSIZE" -lt "$GITHUBSIZE" ]; + then + echo "Download size seems too small. Our size: $OURSIZE, Github size: $GITHUBSIZE deleting $1" + echo "Please try to download the fppos again" + rm $1 + exit 1; + else + echo "fppos size matches Github, continuing" + fi +fi + mount $1 /mnt ORIGTYPE=$( \ No newline at end of file +// GET http://fpp/api/git/releases/sizes +// Returns fppos file sizes +// Should re-write this to call GetOSReleases() and parse instead of hitting github directly. +function GitOSReleaseSizes() +{ + global $settings; + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, "https://api.github.com/repos/FalconChristmas/fpp/releases?per_page=100"); + curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0"); + curl_setopt($curl, CURLOPT_FAILONERROR, true); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 4000); + $request_content = curl_exec($curl); + curl_close($curl); + + $rc = ""; + + if ($request_content != false) { + $data = json_decode($request_content, true); + foreach ($data as $r) { + if (isset($r["assets"]) && $settings['OSImagePrefix'] != "") { + foreach ($r["assets"] as $file) { + $name = $file["name"]; + if (endsWith($name, ".fppos") && startsWith($name, $settings['OSImagePrefix'])) { + $rc = $rc . $name . "," . $file["size"] . "\n"; + } + } + } + } + + } else { + $rc = "Error: " . curl_error($curl) . "\n"; + } + + return $rc; +} + +?> diff --git a/www/api/index.php b/www/api/index.php index 0505284c3..9f2540241 100644 --- a/www/api/index.php +++ b/www/api/index.php @@ -51,6 +51,7 @@ dispatch_get('/git/originLog', 'GetGitOriginLog'); dispatch_get('/git/releases/os', 'GitOSReleases'); +dispatch_get('/git/releases/sizes', 'GitOSReleaseSizes'); dispatch_get('/git/reset', 'GitReset'); dispatch_get('/git/status', 'GitStatus'); diff --git a/www/upgradeOS.php b/www/upgradeOS.php index 0eb39f65e..30463a8d2 100755 --- a/www/upgradeOS.php +++ b/www/upgradeOS.php @@ -83,7 +83,8 @@ copy("$fppDir/SD/upgradeOS-part1.sh", $TMP_FILE); chmod($TMP_FILE, 0775); #system($SUDO . " stdbuf --output=L --error=L $TMP_FILE /home/fpp/media/upload/$baseFile"); - system($SUDO . " $TMP_FILE /home/fpp/media/upload/$baseFile"); + $return_code = 0; + system($SUDO . " $TMP_FILE /home/fpp/media/upload/$baseFile",$return_code); } else { echo ("Skipping update\n"); } @@ -98,15 +99,19 @@