From 9c45dcd0ff02f6aa07927a40a13d050e64cca4ed Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 3 Feb 2021 00:29:50 +0100 Subject: clean up logic some more --- src/dxvk.c | 5 +++-- src/net.c | 24 +++++++++++++++++++++--- src/net.h | 1 + src/tar.c | 4 ++-- src/wine.c | 7 +++++-- 5 files changed, 32 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/dxvk.c b/src/dxvk.c index 9b0a921..6b05ebb 100644 --- a/src/dxvk.c +++ b/src/dxvk.c @@ -70,14 +70,15 @@ COMMAND(dxvk, download) fprintf(stderr, "Extracting %s\n", name); extract(archive, dxvkdir); fprintf(stderr, "Done\n"); + + free(archive->memory); + free(archive); } else { fprintf(stderr, "Something went wrong. The archive went missing\n"); } - free(archive->memory); - free(archive); } else { diff --git a/src/net.c b/src/net.c index b2851db..96e09ca 100644 --- a/src/net.c +++ b/src/net.c @@ -10,7 +10,7 @@ #include "net.h" #include "common.h" -static size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp) +static inline size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp) { size_t realsize = size * nmemb; struct MemoryStruct* mem = (struct MemoryStruct*)userp; @@ -30,7 +30,7 @@ static size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* us return realsize; } -static int xferinfo(UNUSED void *p, curl_off_t dltotal, curl_off_t dlnow, UNUSED curl_off_t ultotal, UNUSED curl_off_t ulnow) +static inline int xferinfo(UNUSED void *p, curl_off_t dltotal, curl_off_t dlnow, UNUSED curl_off_t ultotal, UNUSED curl_off_t ulnow) { curl_off_t progress = 0; if (dltotal != 0) @@ -99,6 +99,25 @@ struct MemoryStruct* downloadToRam(const char* URL, long noprogress) return chunk; } +void downloadToFile(const char* URL, const char* path) +{ + struct MemoryStruct* chunk = downloadToRam(URL, 1L); + + if (chunk) + { + FILE* fp = fopen(path, "wb"); + + if (fp) + { + fwrite(chunk->memory, sizeof(uint8_t), chunk->size, fp); + fclose(fp); + } + + free(chunk->memory); + free(chunk); + } +} + struct json_object* fetchJSON(const char* URL) { struct MemoryStruct* chunk = downloadToRam(URL, 1L); @@ -111,7 +130,6 @@ struct json_object* fetchJSON(const char* URL) free(chunk->memory); free(chunk); - } return json; diff --git a/src/net.h b/src/net.h index 722c6bb..8282706 100644 --- a/src/net.h +++ b/src/net.h @@ -8,6 +8,7 @@ size_t WriteMemoryCallback(void*, size_t, size_t, void*); struct MemoryStruct* downloadToRam(const char* URL, long); +void downloadToFile(const char*, const char*); struct json_object* fetchJSON(const char*); #endif diff --git a/src/tar.c b/src/tar.c index cbaf875..0daf657 100644 --- a/src/tar.c +++ b/src/tar.c @@ -9,7 +9,7 @@ #include "common.h" #include "tar.h" -static int copy_data(struct archive* ar, struct archive* aw) +static inline int copy_data(struct archive* ar, struct archive* aw) { int r; const void *buff; @@ -63,7 +63,7 @@ void extract(const struct MemoryStruct* tar, const char* outputdir) archive_write_disk_set_options(ext, flags); archive_write_disk_set_standard_lookup(ext); - if ((r = archive_read_open_memory(a, tar->memory, tar->size))) return; + if (archive_read_open_memory(a, tar->memory, tar->size)) return; for (;;) { diff --git a/src/wine.c b/src/wine.c index 1a21eb8..80defa1 100644 --- a/src/wine.c +++ b/src/wine.c @@ -76,14 +76,15 @@ COMMAND(wine, download) fprintf(stderr, "Extracting %s\n", name); extract(archive, winedir); fprintf(stderr, "Done\n"); + + free(archive->memory); + free(archive); } else { fprintf(stderr, "Something went wrong. The archive went missing\n"); } - free(archive->memory); - free(archive); } else { @@ -397,6 +398,8 @@ enum wine_type_t check_wine_ver(char* winepath, size_t size) free(winepathcopy); return WINE_PROTON; } + + free(winepathcopy); } else { -- cgit v1.2.3