From e3c3f3b90a43186afebe7443aee17d119d6b62e6 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 17 Nov 2023 09:15:55 +0100 Subject: [PATCH] fix(landerctl): don't fail when libmagic fails --- landerctl/config.mk | 2 +- landerctl/src/post.c | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/landerctl/config.mk b/landerctl/config.mk index a9ec814..e870a57 100644 --- a/landerctl/config.mk +++ b/landerctl/config.mk @@ -7,7 +7,7 @@ SRC_DIR = src TEST_DIR = test INC_DIRS = include -LIBS = magic curl +LIBS ?= magic curl LIB_DIRS = # -MMD: generate a .d file for every source file. This file can be imported by diff --git a/landerctl/src/post.c b/landerctl/src/post.c index ecdbdc1..d25627c 100644 --- a/landerctl/src/post.c +++ b/landerctl/src/post.c @@ -102,11 +102,21 @@ landerctl_err landerctl_post_file(landerctl_ctx *ctx) { curl_easy_setopt(ctx->curl, CURLOPT_NOPROGRESS, 0L); magic_t cookie = magic_open(MAGIC_MIME_TYPE); - magic_load(cookie, NULL); - const char *mime_type = magic_file(cookie, ctx->arg); - char content_type_header[strlen(mime_type) + 24]; - sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); + if (magic_load(cookie, NULL) == 0) { + const char *mime_type = magic_file(cookie, ctx->arg); + + if (mime_type != NULL) { + char content_type_header[strlen(mime_type) + 24]; + sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); + + ctx->headers = curl_slist_append(ctx->headers, content_type_header); + } else { + printf("Couldn't determine mime type; skipping Content-Type header"); + } + } else { + printf("Couldn't load magic file; skipping Content-Type header"); + } char s[strlen(ctx->arg) + 1]; strcpy(s, ctx->arg); @@ -115,7 +125,6 @@ landerctl_err landerctl_post_file(landerctl_ctx *ctx) { char filename_header[strlen(base_name) + 20]; sprintf(filename_header, "X-Lander-Filename: %s", base_name); - ctx->headers = curl_slist_append(ctx->headers, content_type_header); ctx->headers = curl_slist_append(ctx->headers, filename_header); return landerctl_err_ok;