From e70c64b74a0bd3f2dc9445a214888e5166a9c51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Wennerstr=C3=B6m?= Date: Thu, 10 Dec 2020 19:04:32 +0100 Subject: [PATCH 1/2] Create download directory under XDG data path * If the downloads directory does not exist, create it. * Change some cons_show to cons_show_error (because they log errors). --- src/command/cmd_funcs.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 2397ccfc..74052c9e 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9118,7 +9118,7 @@ gboolean cmd_url_open(ProfWin* window, const char* const command, gchar** args) { if (window->type != WIN_CHAT && window->type != WIN_MUC && window->type != WIN_PRIVATE) { - cons_show("url open not supported in this window"); + cons_show_error("url open not supported in this window"); return TRUE; } @@ -9134,20 +9134,37 @@ cmd_url_open(ProfWin* window, const char* const command, gchar** args) scheme = g_uri_parse_scheme(url); if (scheme == NULL) { - cons_show("URL '%s' is not valid.", args[1]); + cons_show_error("URL '%s' is not valid.", args[1]); goto out; } cmd_template = prefs_get_string(PREF_URL_OPEN_CMD); if (cmd_template == NULL) { - cons_show("No default `url open` command found in executables preferences."); + cons_show_error("No default `url open` command found in executables preferences."); goto out; } #ifdef HAVE_OMEMO // OMEMO URLs (aesgcm://) must be saved and decrypted before being opened. - if (0 == g_strcmp0(scheme, "aesgcm")) { - filename = unique_filename_from_url(url, files_get_data_path(DIR_DOWNLOADS)); + if (g_strcmp0(scheme, "aesgcm") == 0) { + + // Ensure that the downloads directory exists for saving cleartexts. + gchar* downloads_dir = files_get_data_path(DIR_DOWNLOADS); + if (g_mkdir_with_parents(downloads_dir, S_IRWXU) != 0) { + cons_show_error("Failed to create download directory " + "at '%s' with error '%s'", + downloads_dir, strerror(errno)); + g_free(downloads_dir); + goto out; + } + + // Generate an unique filename from the URL that should be stored in the + // downloads directory. + filename = unique_filename_from_url(url, downloads_dir); + g_free(downloads_dir); + + // Download, decrypt and open the cleartext version of the AESGCM + // encrypted file. _url_aesgcm_method(window, cmd_template, url, filename); goto out; } @@ -9186,15 +9203,15 @@ cmd_url_save(ProfWin* window, const char* const command, gchar** args) scheme = g_uri_parse_scheme(url); if (scheme == NULL) { - cons_show("URL '%s' is not valid.", args[1]); + cons_show_error("URL '%s' is not valid.", args[1]); goto out; } filename = unique_filename_from_url(url, path); if (filename == NULL) { - cons_show("Failed to generate unique filename" - "from URL '%s' for path '%s'", - url, path); + cons_show_error("Failed to generate unique filename" + "from URL '%s' for path '%s'", + url, path); goto out; } From 42a0518ff4e3dae48ba988c00299d92e1e2903c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Wennerstr=C3=B6m?= Date: Thu, 10 Dec 2020 19:26:06 +0100 Subject: [PATCH 2/2] Fix deadlock on error before HTTP download has begun --- src/tools/http_download.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/tools/http_download.c b/src/tools/http_download.c index f97fd704..2ff49b61 100644 --- a/src/tools/http_download.c +++ b/src/tools/http_download.c @@ -123,7 +123,7 @@ http_file_get(void* userdata) "output file at '%s' for writing (%s).", download->url, download->filename, g_strerror(errno)); - return NULL; + goto out; } char* cert_path = prefs_get_string(PREF_TLS_CERTPATH); @@ -185,9 +185,6 @@ http_file_get(void* userdata) } } - download_processes = g_slist_remove(download_processes, download); - pthread_mutex_unlock(&lock); - if (download->cmd_template != NULL) { gchar** argv = format_call_external_argv(download->cmd_template, download->url, @@ -208,6 +205,11 @@ http_file_get(void* userdata) free(download->cmd_template); } +out: + + download_processes = g_slist_remove(download_processes, download); + pthread_mutex_unlock(&lock); + free(download->url); free(download->filename); free(download);