Blame SOURCES/0167-uploader-use-shared-dd_create_archive-function.patch

28bab8
From 072e3f9847e3c5bf42175aa0f837e1a6445cdbc8 Mon Sep 17 00:00:00 2001
28bab8
From: Matej Habrnal <mhabrnal@redhat.com>
28bab8
Date: Fri, 12 Feb 2016 13:58:09 +0100
28bab8
Subject: [PATCH] uploader: use shared dd_create_archive function
28bab8
28bab8
Use the test function instead of own untested one.
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
28bab8
28bab8
Conflicts:
28bab8
	src/plugins/reporter-upload.c
28bab8
---
28bab8
 src/plugins/reporter-upload.c | 184 ++++++++++++++----------------------------
28bab8
 1 file changed, 60 insertions(+), 124 deletions(-)
28bab8
28bab8
diff --git a/src/plugins/reporter-upload.c b/src/plugins/reporter-upload.c
28bab8
index 6d83d2f..b148d95 100644
28bab8
--- a/src/plugins/reporter-upload.c
28bab8
+++ b/src/plugins/reporter-upload.c
28bab8
@@ -16,7 +16,6 @@
28bab8
     with this program; if not, write to the Free Software Foundation, Inc.,
28bab8
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28bab8
 */
28bab8
-#include <libtar.h>
28bab8
 #include "libreport_curl.h"
28bab8
 #include "internal_libreport.h"
28bab8
 #include "client.h"
28bab8
@@ -33,33 +32,45 @@ static char *ask_url(const char *message)
28bab8
     return url;
28bab8
 }
28bab8
 
28bab8
+static int interactive_upload_file(const char *url, const char *file_name)
28bab8
+{
28bab8
+    post_state_t *state = new_post_state(POST_WANT_ERROR_MSG);
28bab8
+    state->username = getenv("Upload_Username");
28bab8
+    char *password_inp = NULL;
28bab8
+    if (state->username != NULL && state->username[0] != '\0')
28bab8
+    {
28bab8
+        /* Load Password only if Username is configured, it doesn't make */
28bab8
+        /* much sense to load Password without Username. */
28bab8
+        state->password = getenv("Upload_Password");
28bab8
+        if (state->password == NULL)
28bab8
+        {
28bab8
+            /* Be permissive and nice, ask only once and don't check */
28bab8
+            /* the result. User can dismiss this prompt but the upload */
28bab8
+            /* may work somehow??? */
28bab8
+            char *msg = xasprintf(_("Please enter password for uploading:"), state->username);
28bab8
+            state->password = password_inp = ask_password(msg);
28bab8
+            free(msg);
28bab8
+        }
28bab8
+    }
28bab8
+
28bab8
+    char *remote_name = upload_file_ext(state, url, file_name, UPLOAD_FILE_HANDLE_ACCESS_DENIALS);
28bab8
+    int result = (remote_name == NULL); /* error if NULL */
28bab8
+
28bab8
+    free(remote_name);
28bab8
+    free(password_inp);
28bab8
+    free_post_state(state);
28bab8
+
28bab8
+    return result;
28bab8
+}
28bab8
+
28bab8
 static int create_and_upload_archive(
28bab8
                 const char *dump_dir_name,
28bab8
                 map_string_t *settings)
28bab8
 {
28bab8
     int result = 1; /* error */
28bab8
 
28bab8
-    pid_t child;
28bab8
-    TAR* tar = NULL;
28bab8
-    const char* errmsg = NULL;
28bab8
     char* tempfile = NULL;
28bab8
 
28bab8
-    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
28bab8
-    if (!dd)
28bab8
-        xfunc_die(); /* error msg is already logged by dd_opendir */
28bab8
-
28bab8
-    /* Gzipping e.g. 0.5gig coredump takes a while. Let client know what we are doing */
28bab8
-    log(_("Compressing data"));
28bab8
-
28bab8
-//TODO:
28bab8
-//Encrypt = yes
28bab8
-//ArchiveType = .tar.bz2
28bab8
-//ExcludeFiles = foo,bar*,b*z
28bab8
-    const char* opt = getenv("Upload_URL");
28bab8
-    if (!opt)
28bab8
-        opt = get_map_string_item_or_empty(settings, "URL");
28bab8
-    char *url = opt[0] != '\0' ? xstrdup(opt) : ask_url(_("Upload URL is not provided by configuration. Please enter upload URL:"));
28bab8
-
28bab8
     /* Create a child gzip which will compress the data */
28bab8
     /* SELinux guys are not happy with /tmp, using /var/run/abrt */
28bab8
     /* Reverted back to /tmp for ABRT2 */
28bab8
@@ -67,114 +78,31 @@ static int create_and_upload_archive(
28bab8
     tempfile = concat_path_basename(LARGE_DATA_TMP_DIR, dump_dir_name);
28bab8
     tempfile = append_to_malloced_string(tempfile, ".tar.gz");
28bab8
 
28bab8
-    int pipe_from_parent_to_child[2];
28bab8
-    xpipe(pipe_from_parent_to_child);
28bab8
-    child = vfork();
28bab8
-    if (child == 0)
28bab8
-    {
28bab8
-        /* child */
28bab8
-        close(pipe_from_parent_to_child[1]);
28bab8
-        xmove_fd(pipe_from_parent_to_child[0], 0);
28bab8
-        xmove_fd(xopen3(tempfile, O_WRONLY | O_CREAT | O_EXCL, 0600), 1);
28bab8
-        execlp("gzip", "gzip", NULL);
28bab8
-        perror_msg_and_die("Can't execute '%s'", "gzip");
28bab8
-    }
28bab8
-    close(pipe_from_parent_to_child[0]);
28bab8
+    const char* opt = getenv("Upload_URL");
28bab8
+    if (!opt)
28bab8
+        opt = get_map_string_item_or_empty(settings, "URL");
28bab8
+    char *url = opt[0] != '\0' ? xstrdup(opt) : ask_url(_("Please enter a URL (scp, ftp, etc.) where the problem data is to be exported:"));
28bab8
 
28bab8
-    /* If child died (say, in xopen), then parent might get SIGPIPE.
28bab8
-     * We want to properly unlock dd, therefore we must not die on SIGPIPE:
28bab8
-     */
28bab8
-    signal(SIGPIPE, SIG_IGN);
28bab8
+    string_vector_ptr_t exclude_from_report = get_global_always_excluded_elements();
28bab8
+    struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0);
28bab8
+    if (!dd)
28bab8
+        xfunc_die(); /* error msg is already logged by dd_opendir */
28bab8
 
28bab8
-    /* Create tar writer object */
28bab8
-    if (tar_fdopen(&tar, pipe_from_parent_to_child[1], tempfile,
28bab8
-                /*fileops:(standard)*/ NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) != 0)
28bab8
+    /* Compressing e.g. 0.5gig coredump takes a while. Let client know what we are doing */
28bab8
+    log(_("Compressing data"));
28bab8
+    if (dd_create_archive(dd, tempfile, (const char * const*)exclude_from_report, 0) != 0)
28bab8
     {
28bab8
-        errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR;
28bab8
+        log_error("Can't create temporary file in %s", LARGE_DATA_TMP_DIR);
28bab8
         goto ret;
28bab8
     }
28bab8
 
28bab8
-    /* Write data to the tarball */
28bab8
-    {
28bab8
-        string_vector_ptr_t exclude_from_report = get_global_always_excluded_elements();
28bab8
-        dd_init_next_file(dd);
28bab8
-        char *short_name, *full_name;
28bab8
-        while (dd_get_next_file(dd, &short_name, &full_name))
28bab8
-        {
28bab8
-            if (exclude_from_report && is_in_string_list(short_name, (const_string_vector_const_ptr_t)exclude_from_report))
28bab8
-                goto next;
28bab8
-
28bab8
-            // dd_get_next_file guarantees that it's a REG:
28bab8
-            //struct stat stbuf;
28bab8
-            //if (stat(full_name, &stbuf) != 0)
28bab8
-            // || !S_ISREG(stbuf.st_mode)
28bab8
-            //) {
28bab8
-            //     goto next;
28bab8
-            //}
28bab8
-            if (tar_append_file(tar, full_name, short_name) != 0)
28bab8
-            {
28bab8
-                errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR;
28bab8
-                free(short_name);
28bab8
-                free(full_name);
28bab8
-                goto ret;
28bab8
-            }
28bab8
- next:
28bab8
-            free(short_name);
28bab8
-            free(full_name);
28bab8
-        }
28bab8
-    }
28bab8
     dd_close(dd);
28bab8
     dd = NULL;
28bab8
 
28bab8
-    /* Close tar writer... */
28bab8
-    if (tar_append_eof(tar) != 0 || tar_close(tar) != 0)
28bab8
-    {
28bab8
-        errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR;
28bab8
-        goto ret;
28bab8
-    }
28bab8
-    tar = NULL;
28bab8
-    /* ...and check that gzip child finished successfully */
28bab8
-    int status;
28bab8
-    safe_waitpid(child, &status, 0);
28bab8
-    child = -1;
28bab8
-    if (status != 0)
28bab8
-    {
28bab8
-        /* We assume the error was out-of-disk-space or out-of-quota */
28bab8
-        errmsg = "Can't create temporary file in "LARGE_DATA_TMP_DIR;
28bab8
-        goto ret;
28bab8
-    }
28bab8
-
28bab8
-    /* Upload the tarball */
28bab8
+    /* Upload the archive */
28bab8
     /* Upload from /tmp to /tmp + deletion -> BAD, exclude this possibility */
28bab8
     if (url && url[0] && strcmp(url, "file://"LARGE_DATA_TMP_DIR"/") != 0)
28bab8
-    {
28bab8
-        post_state_t *state = new_post_state(POST_WANT_ERROR_MSG);
28bab8
-        state->username = getenv("Upload_Username");
28bab8
-        char *password_inp = NULL;
28bab8
-        if (state->username != NULL && state->username[0] != '\0')
28bab8
-        {
28bab8
-            /* Load Password only if Username is configured, it doesn't make */
28bab8
-            /* much sense to load Password without Username. */
28bab8
-            state->password = getenv("Upload_Password");
28bab8
-            if (state->password == NULL)
28bab8
-            {
28bab8
-                /* Be permissive and nice, ask only once and don't check */
28bab8
-                /* the result. User can dismiss this prompt but the upload */
28bab8
-                /* may work somehow??? */
28bab8
-                char *msg = xasprintf(_("Please enter password for uploading:"), state->username);
28bab8
-                state->password = password_inp = ask_password(msg);
28bab8
-                free(msg);
28bab8
-            }
28bab8
-        }
28bab8
-
28bab8
-        char *remote_name = upload_file_ext(state, url, tempfile, UPLOAD_FILE_HANDLE_ACCESS_DENIALS);
28bab8
-
28bab8
-        result = (remote_name == NULL); /* error if NULL */
28bab8
-        free(remote_name);
28bab8
-        free(password_inp);
28bab8
-        free_post_state(state);
28bab8
-        /* cleanup code will delete tempfile */
28bab8
-    }
28bab8
+        result = interactive_upload_file(url, tempfile);
28bab8
     else
28bab8
     {
28bab8
         result = 0; /* success */
28bab8
@@ -186,18 +114,12 @@ static int create_and_upload_archive(
28bab8
  ret:
28bab8
     free(url);
28bab8
     dd_close(dd);
28bab8
-    if (tar)
28bab8
-        tar_close(tar);
28bab8
-    /* close(pipe_from_parent_to_child[1]); - tar_close() does it itself */
28bab8
-    if (child > 0)
28bab8
-        safe_waitpid(child, NULL, 0);
28bab8
+
28bab8
     if (tempfile)
28bab8
     {
28bab8
         unlink(tempfile);
28bab8
         free(tempfile);
28bab8
     }
28bab8
-    if (errmsg)
28bab8
-        error_msg_and_die("%s", errmsg);
28bab8
 
28bab8
     return result;
28bab8
 }
28bab8
@@ -206,6 +128,9 @@ int main(int argc, char **argv)
28bab8
 {
28bab8
     abrt_init(argv);
28bab8
 
28bab8
+    if (!load_global_configuration())
28bab8
+        error_msg_and_die("Cannot continue without libreport global configuration.");
28bab8
+
28bab8
     /* I18n */
28bab8
     setlocale(LC_ALL, "");
28bab8
 #if ENABLE_NLS
28bab8
@@ -255,6 +180,17 @@ int main(int argc, char **argv)
28bab8
 
28bab8
     export_abrt_envvars(0);
28bab8
 
28bab8
+    // 2015-10-16 (jfilak):
28bab8
+    //   It looks like there is no demand for encryption and other archive
28bab8
+    //   types. Configurable ExcludeFiles sounds reasonable to me, I am
28bab8
+    //   not sure about globbing though.
28bab8
+    //
28bab8
+    //Encrypt = yes
28bab8
+    //ArchiveType = .tar.bz2
28bab8
+    //
28bab8
+    //TODO:
28bab8
+    //ExcludeFiles = foo,bar*,b*z
28bab8
+
28bab8
     map_string_t *settings = new_map_string();
28bab8
     if (url)
28bab8
         replace_map_string_item(settings, xstrdup("URL"), xstrdup(url));
28bab8
-- 
28bab8
1.8.3.1
28bab8