Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 72544b0b authored by Daniel Jacob Chittoor's avatar Daniel Jacob Chittoor
Browse files

Implement retry logic for failed checksum scenario

Introduce a retry mechanism (up to 3 attempts) to re-download the file
if the integrity check fails, improving the robustness of the installer
against transient network issues.
parent e2ef30a3
Loading
Loading
Loading
Loading
+31 −15
Original line number Diff line number Diff line
@@ -39,24 +39,40 @@ export class Downloader {
        const file = folder[i];
        current_file = file.path;
        if (filesRequired.includes(file.name) || file.unzip) {
          const blob = await this.download(file.path, (value, total) => {
          const MAX_RETRIES = 3;
          let attempt = 0;
          let blob;
          let checksumSuccess = false;

          while (attempt < MAX_RETRIES && !checksumSuccess) {
            attempt++;
            try {
              blob = await this.download(file.path, (value, total) => {
                onDownloadProgress(value, total, file.name);
              });

              // Simple zip integrity check: fetch "<path>.sha256sum" and compare.
          try {
            const expected = await this.fetchChecksum(`${file.path}.sha256sum`);
              const expected = await this.fetchChecksum(
                `${file.path}.sha256sum`,
              );
              const actual = await this.computeSha256(blob);
              if (expected && actual !== expected) {
                throw new Error(
                  `Checksum mismatch for ${file.name}: expected ${expected} got ${actual}`,
                );
              }
          } catch (checksumError) {
              checksumSuccess = true;
            } catch (err) {
              console.warn(
                `Attempt ${attempt}/${MAX_RETRIES} failed for ${file.name}: ${err.message}`,
              );
              if (attempt >= MAX_RETRIES) {
                throw new Error(
              `Checksum verification failed for ${file.name}: ${checksumError.message || checksumError}`,
                  `Failed to download and verify ${file.name} after ${MAX_RETRIES} attempts: ${err.message}`,
                );
              }
            }
          }

          if (file.unzip) {
            const zipReader = new ZipReader(new BlobReader(blob));