Skip to content

Commit

Permalink
added retry to file downloads
Browse files Browse the repository at this point in the history
since I kinda erased tht's implementation while working on the new way to download files
  • Loading branch information
AleksArt000 committed Jul 24, 2024
1 parent 55658fa commit 79eabea
Showing 1 changed file with 59 additions and 27 deletions.
86 changes: 59 additions & 27 deletions src/make.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ int make(char* package_dir, struct package* pkg) {
// Parse the files
for (int i = 0; i < pkg->filesCount; i++)
{
int download_attempts = 3;
int download_success = 0;

struct stat st_source = {0};
struct stat st_source_loc = {0};

Expand Down Expand Up @@ -81,44 +84,73 @@ int make(char* package_dir, struct package* pkg) {
// Check if the checksum shall be bypassed
if (!INSECURE)
{
// Check the hash, abort if mismatch
unsigned char hash[SHA256_DIGEST_LENGTH];
char* hash_str = calloc(SHA256_DIGEST_LENGTH, 8);
do
{
// Check the hash, abort if mismatch
unsigned char hash[SHA256_DIGEST_LENGTH];
char* hash_str = calloc(SHA256_DIGEST_LENGTH, 8);

struct stat st;
stat(location, &st);
int size = st.st_size;
struct stat st;
stat(location, &st);
int size = st.st_size;

char* buffer = malloc(size);
FILE *ptr;
ptr = fopen(location,"r");
fread(buffer, sizeof(char), size, ptr);
char* buffer = malloc(size);
FILE *ptr;

Check warning on line 98 in src/make.c

View workflow job for this annotation

GitHub Actions / c-linter

src/make.c:98:27 [cppcoreguidelines-init-variables]

variable 'ptr' is not initialized
ptr = fopen(location,"r");
fread(buffer, sizeof(char), size, ptr);
fclose(ptr);

if (!(buffer == NULL))
{
SHA256(buffer, size, hash);
if (!(buffer == NULL))
{
SHA256(buffer, size, hash);

Check warning on line 105 in src/make.c

View workflow job for this annotation

GitHub Actions / c-linter

src/make.c:105:32 [clang-diagnostic-pointer-sign]

passing 'char *' to parameter of type 'const unsigned char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not
free(buffer);

if (!((hash == NULL) || (hash[0] == '\0')))
if (!((hash == NULL) || (hash[0] == '\0')))
{
dbg(1, "Hash is %s", file_sha256);
for(int k = 0; k < SHA256_DIGEST_LENGTH; k++)
{
char* temp = calloc(8, 1);
sprintf(temp, "%02x", hash[k]);
strcat(hash_str, temp);
free(temp);
}

dbg(1, "Got %s", hash_str);
if(strcmp(hash_str, file_sha256) != 0)
{
free(hash_str);

if(download_attempts == 0)
{
msg(FATAL, "Hash mismatch, aborting");
}
else
{
msg(WARNING, "Hash mismatch, retrying...");
--download_attempts;
}
}
else
{
download_success = 1;
}
}
}
else
{
dbg(1, "Hash is %s", file_sha256);
for(int k = 0; k < SHA256_DIGEST_LENGTH; k++)
if(download_attempts == 0)
{
char* temp = calloc(8, 1);
sprintf(temp, "%02x", hash[k]);
strcat(hash_str, temp);
msg(FATAL, "Could not download file, aborting");
}

dbg(1, "Got %s", hash_str);
if(strcmp(hash_str, file_sha256) != 0)
else
{
msg(FATAL, "Hash mismatch, aborting");
msg(WARNING, "Could not download file, retrying...");
--download_attempts;
}
}
}
else
{
msg(FATAL, "Could not verify the file's hash");
}
while (download_success != 1);
}
else
{
Expand Down

0 comments on commit 79eabea

Please sign in to comment.