Blame SOURCES/0166-dd-add-a-function-for-compressing-dumpdirs.patch

562801
From f9ff58e8cde1d3b6f5eb5307e2019d54c5e28487 Mon Sep 17 00:00:00 2001
562801
From: Matej Habrnal <mhabrnal@redhat.com>
562801
Date: Thu, 11 Feb 2016 17:29:50 +0100
562801
Subject: [PATCH] dd: add a function for compressing dumpdirs
562801
562801
Introduce dd_create_archive() function.
562801
562801
I added the argument 'flags' in order to avoid the need to introduce
562801
dd_create_archive_ext() function in the future. I am sure will use this
562801
flag in the future (e.g. Encrypted).
562801
562801
Signed-off-by: Jakub Filak <jfilak@redhat.com>
562801
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
562801
---
562801
 po/POTFILES.in          |   1 +
562801
 src/include/dump_dir.h  |  26 +++++
562801
 src/lib/Makefile.am     |   1 +
562801
 src/lib/dump_dir.c      | 117 ++++++++++++++++++++
562801
 src/plugins/Makefile.am |   2 -
562801
 tests/dump_dir.at       | 275 ++++++++++++++++++++++++++++++++++++++++++++++++
562801
 6 files changed, 420 insertions(+), 2 deletions(-)
562801
562801
diff --git a/po/POTFILES.in b/po/POTFILES.in
562801
index 00046e2..30c9cb5 100644
562801
--- a/po/POTFILES.in
562801
+++ b/po/POTFILES.in
562801
@@ -18,6 +18,7 @@ src/lib/abrt_sock.c
562801
 src/lib/client.c
562801
 src/lib/create_dump_dir.c
562801
 src/lib/curl.c
562801
+src/lib/dump_dir.c
562801
 src/lib/event_config.c
562801
 src/lib/ureport.c
562801
 src/lib/make_descr.c
562801
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
562801
index 07b119a..092ddeb 100644
562801
--- a/src/include/dump_dir.h
562801
+++ b/src/include/dump_dir.h
562801
@@ -21,6 +21,9 @@
562801
 #ifndef LIBREPORT_DUMP_DIR_H_
562801
 #define LIBREPORT_DUMP_DIR_H_
562801
 
562801
+/* For const_string_vector_const_ptr_t */
562801
+#include "libreport_types.h"
562801
+
562801
 /* For DIR */
562801
 #include <sys/types.h>
562801
 #include <dirent.h>
562801
@@ -178,6 +181,29 @@ int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
562801
 */
562801
 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
562801
 
562801
+/* Creates a new archive from the dump directory contents
562801
+ *
562801
+ * The dd argument must be opened for reading.
562801
+ *
562801
+ * The archive_name must not exist. The file will be created with 0600 mode.
562801
+ *
562801
+ * The archive type is deduced from archive_name suffix. The supported archive
562801
+ * suffixes are the following:
562801
+ *   - '.tag.gz' (note: the implementation uses child gzip process)
562801
+ *
562801
+ * The archive will include only the files that are not in the exclude_elements
562801
+ * list. See get_global_always_excluded_elements().
562801
+ *
562801
+ * The argument "flags" is currently unused.
562801
+ *
562801
+ * @return 0 on success; otherwise non-0 value. -ENOSYS if archive type is not
562801
+ * supported. -EEXIST if the archive file already exists. -ECHILD if child
562801
+ * process fails. Other negative values can be converted to errno values by
562801
+ * turning them positive.
562801
+ */
562801
+int dd_create_archive(struct dump_dir *dd, const char *archive_name,
562801
+        const_string_vector_const_ptr_t exclude_elements, int flags);
562801
+
562801
 #ifdef __cplusplus
562801
 }
562801
 #endif
562801
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
562801
index f9ea602..50142f7 100644
562801
--- a/src/lib/Makefile.am
562801
+++ b/src/lib/Makefile.am
562801
@@ -79,6 +79,7 @@ libreport_la_CPPFLAGS = \
562801
     $(AUGEAS_CFLAGS) \
562801
     -D_GNU_SOURCE
562801
 libreport_la_LDFLAGS = \
562801
+    -ltar \
562801
     -version-info 0:1:0
562801
 libreport_la_LIBADD = \
562801
     $(JSON_C_LIBS) \
562801
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
562801
index 9096853..a5cd93e 100644
562801
--- a/src/lib/dump_dir.c
562801
+++ b/src/lib/dump_dir.c
562801
@@ -17,6 +17,7 @@
562801
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
562801
 */
562801
 #include <sys/utsname.h>
562801
+#include <libtar.h>
562801
 #include "internal_libreport.h"
562801
 
562801
 // Locking logic:
562801
@@ -1475,3 +1476,119 @@ int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason)
562801
     dd_save_text(dd, FILENAME_NOT_REPORTABLE, reason);
562801
     return 0;
562801
 }
562801
+
562801
+/* flags - for future needs */
562801
+int dd_create_archive(struct dump_dir *dd, const char *archive_name,
562801
+        const_string_vector_const_ptr_t exclude_elements, int flags)
562801
+{
562801
+    if (suffixcmp(archive_name, ".tar.gz") != 0)
562801
+        return -ENOSYS;
562801
+
562801
+    int result = 0;
562801
+    pid_t child;
562801
+    TAR* tar = NULL;
562801
+    int pipe_from_parent_to_child[2];
562801
+    xpipe(pipe_from_parent_to_child);
562801
+    child = fork();
562801
+    if (child < 0)
562801
+    {
562801
+        result = -errno;
562801
+        /* Don't die, let the caller to execute his clean-up code. */
562801
+        perror_msg("vfork");
562801
+        return result;
562801
+    }
562801
+    if (child == 0)
562801
+    {
562801
+        /* child */
562801
+        close(pipe_from_parent_to_child[1]);
562801
+        xmove_fd(pipe_from_parent_to_child[0], 0);
562801
+
562801
+        int fd = open(archive_name, O_WRONLY | O_CREAT | O_EXCL, 0600);
562801
+        if (fd < 0)
562801
+        {
562801
+            /* This r might interfer with exit status of gzip, but it is
562801
+             * very unlikely (man 1 gzip):
562801
+             *   Exit status is normally 0; if an error occurs, exit status is
562801
+             *   1. If a warning occurs, exit status is 2.
562801
+             */
562801
+            result = errno == EEXIST ? 100 : 3;
562801
+            perror_msg("Can't open '%s'", archive_name);
562801
+            exit(result);
562801
+        }
562801
+
562801
+        xmove_fd(fd, 1);
562801
+        execlp("gzip", "gzip", NULL);
562801
+        perror_msg_and_die("Can't execute '%s'", "gzip");
562801
+    }
562801
+    close(pipe_from_parent_to_child[0]);
562801
+
562801
+    /* If child died (say, in xopen), then parent might get SIGPIPE.
562801
+     * We want to properly unlock dd, therefore we must not die on SIGPIPE:
562801
+     */
562801
+    sighandler_t old_handler = signal(SIGPIPE, SIG_IGN);
562801
+
562801
+    /* Create tar writer object */
562801
+    if (tar_fdopen(&tar, pipe_from_parent_to_child[1], (char *)archive_name,
562801
+                /*fileops:(standard)*/ NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) != 0)
562801
+    {
562801
+        result = -errno;
562801
+        log_warning(_("Failed to open TAR writer"));
562801
+        goto finito;
562801
+    }
562801
+
562801
+    /* Write data to the tarball */
562801
+    dd_init_next_file(dd);
562801
+    char *short_name, *full_name;
562801
+    while (dd_get_next_file(dd, &short_name, &full_name))
562801
+    {
562801
+        if (!(exclude_elements && is_in_string_list(short_name, exclude_elements)))
562801
+        {
562801
+           if (tar_append_file(tar, full_name, short_name))
562801
+               result = -errno;
562801
+        }
562801
+
562801
+        free(short_name);
562801
+        free(full_name);
562801
+
562801
+        if (result != 0)
562801
+            goto finito;
562801
+    }
562801
+
562801
+    /* Close tar writer... */
562801
+    if (tar_append_eof(tar) != 0)
562801
+    {
562801
+        result = -errno;
562801
+        log_warning(_("Failed to finalize TAR archive"));
562801
+        goto finito;
562801
+    }
562801
+
562801
+finito:
562801
+    signal(SIGPIPE, old_handler);
562801
+
562801
+    if (tar != NULL && tar_close(tar) != 0)
562801
+    {
562801
+        result = -errno;
562801
+        log_warning(_("Failed to close TAR writer"));
562801
+    }
562801
+
562801
+    /* ...and check that gzip child finished successfully */
562801
+    int status;
562801
+    safe_waitpid(child, &status, 0);
562801
+    if (status != 0)
562801
+    {
562801
+        result = -ECHILD;
562801
+        if (WIFSIGNALED(status))
562801
+            log_warning(_("gzip killed with signal %d"), WTERMSIG(status));
562801
+        else if (WIFEXITED(status))
562801
+        {
562801
+            if (WEXITSTATUS(status) == 100)
562801
+                result = -EEXIST;
562801
+            else
562801
+                log_warning(_("gzip exited with %d"), WEXITSTATUS(status));
562801
+        }
562801
+        else
562801
+            log_warning(_("gzip process failed"));
562801
+    }
562801
+
562801
+    return result;
562801
+}
562801
diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am
562801
index 7ec08d7..d5d75b6 100644
562801
--- a/src/plugins/Makefile.am
562801
+++ b/src/plugins/Makefile.am
562801
@@ -145,7 +145,6 @@ reporter_rhtsupport_CPPFLAGS = \
562801
     $(LIBREPORT_CFLAGS) \
562801
     $(LIBXML_CFLAGS) \
562801
     -D_GNU_SOURCE
562801
-reporter_rhtsupport_LDFLAGS = -ltar
562801
 reporter_rhtsupport_LDADD = \
562801
     $(GLIB_LIBS) \
562801
     $(LIBXML_LIBS) \
562801
@@ -168,7 +167,6 @@ reporter_upload_CPPFLAGS = \
562801
     $(CURL_CFLAGS) \
562801
     $(LIBREPORT_CFLAGS) \
562801
     -D_GNU_SOURCE
562801
-reporter_upload_LDFLAGS = -ltar
562801
 reporter_upload_LDADD = \
562801
     $(GLIB_LIBS) \
562801
     ../lib/libreport-web.la \
562801
diff --git a/tests/dump_dir.at b/tests/dump_dir.at
562801
index a579243..fb8c7ce 100644
562801
--- a/tests/dump_dir.at
562801
+++ b/tests/dump_dir.at
562801
@@ -47,3 +47,278 @@ int main(void)
562801
     return 0;
562801
 }
562801
 ]])
562801
+
562801
+## ----------------- ##
562801
+## dd_create_archive ##
562801
+## ----------------- ##
562801
+
562801
+AT_TESTFUN([dd_create_archive],
562801
+[[
562801
+#include "internal_libreport.h"
562801
+#include <libtar.h>
562801
+#include <assert.h>
562801
+
562801
+void verify_archive(struct dump_dir *dd, const char *file_name,
562801
+    const_string_vector_const_ptr_t included_files,
562801
+    const_string_vector_const_ptr_t excluded_files)
562801
+{
562801
+    unsigned c = 0;
562801
+    for (const_string_vector_const_ptr_t i = included_files; i && *i; ++i)
562801
+        ++c;
562801
+    int *check_array = xzalloc(c * sizeof(int));
562801
+
562801
+    int pipe_from_parent_to_child[2];
562801
+    xpipe(pipe_from_parent_to_child);
562801
+    pid_t child = fork();
562801
+    if (child < 0)
562801
+        perror_msg_and_die("vfork");
562801
+
562801
+    if (child == 0)
562801
+    {
562801
+        /* child */
562801
+        close(pipe_from_parent_to_child[0]);
562801
+        xmove_fd(xopen(file_name, O_RDONLY), 0);
562801
+        xmove_fd(pipe_from_parent_to_child[1], 1);
562801
+        execlp("gzip", "gzip", "-d", NULL);
562801
+        perror_msg_and_die("Can't execute '%s'", "gzip");
562801
+    }
562801
+    close(pipe_from_parent_to_child[1]);
562801
+
562801
+    /* If child died (say, in xopen), then parent might get SIGPIPE.
562801
+     * We want to properly unlock dd, therefore we must not die on SIGPIPE:
562801
+     */
562801
+    signal(SIGPIPE, SIG_IGN);
562801
+
562801
+    TAR* tar = NULL;
562801
+    /* Create tar writer object */
562801
+    if (tar_fdopen(&tar, pipe_from_parent_to_child[0], (char *)file_name,
562801
+                /*fileops:(standard)*/ NULL, O_RDONLY, 0644, TAR_GNU) != 0)
562801
+    {
562801
+        fprintf(stderr, "Failed to open the pipe to gzip for archive: '%s'\n", file_name);
562801
+        abort();
562801
+    }
562801
+
562801
+    int r = 0;
562801
+    const char *real_file = "/tmp/libreport-attest-extracted";
562801
+    while ((r = th_read(tar)) == 0)
562801
+    {
562801
+        char *path = th_get_pathname(tar);
562801
+
562801
+        if (!TH_ISREG(tar))
562801
+        {
562801
+            fprintf(stderr, "Not regular file: '%s', found in archive: '%s'\n", path, file_name);
562801
+            continue;
562801
+        }
562801
+
562801
+        const_string_vector_const_ptr_t i = included_files;
562801
+        for (c = 0; i && *i; ++i, ++c)
562801
+        {
562801
+            if (strcmp(*i, path) == 0)
562801
+                break;
562801
+        }
562801
+
562801
+        if (i && *i != NULL)
562801
+        {
562801
+            printf("Included file: '%s', found in archive '%s'\n", path, file_name);
562801
+            check_array[c] += 1;
562801
+
562801
+            unlink(real_file);
562801
+            if(tar_extract_regfile(tar, xstrdup(real_file)) != 0)
562801
+            {
562801
+                fprintf(stderr, "TAR failed to extract '%s' to '%s': %s\n", path, real_file, strerror(errno));
562801
+                abort();
562801
+            }
562801
+
562801
+            char *original = dd_load_text(dd, path);
562801
+            assert(original != NULL);
562801
+            assert(original[0] != '\0');
562801
+
562801
+            char *extracted = xmalloc_xopen_read_close("/tmp/libreport-attest-extracted", NULL);
562801
+            assert(extracted != NULL);
562801
+
562801
+            if (strcmp(extracted, original) != 0)
562801
+            {
562801
+                fprintf(stderr, "Invalid file contents: '%s'\nExp: '%s'\nGot: '%s'\n", path, original, extracted);
562801
+                abort();
562801
+            }
562801
+
562801
+            free(original);
562801
+            free(extracted);
562801
+
562801
+            continue;
562801
+        }
562801
+
562801
+        i = excluded_files;
562801
+        for (; i && *i; ++i)
562801
+        {
562801
+            if (strcmp(*i, path) == 0)
562801
+                break;
562801
+        }
562801
+
562801
+        if (i && *i != NULL)
562801
+        {
562801
+            fprintf(stderr, "Excluded file: '%s', found in archive '%s'\n", path, file_name);
562801
+            abort();
562801
+        }
562801
+
562801
+        fprintf(stderr, "Uncategorized file: '%s', found in archive '%s'\n", path, file_name);
562801
+    }
562801
+
562801
+    if (r != 1)
562801
+    {
562801
+        fprintf(stderr, "th_read() failed: %s\n", strerror(errno));
562801
+        abort();
562801
+    }
562801
+
562801
+    tar_close(tar);
562801
+
562801
+    int status;
562801
+    safe_waitpid(child, &status, 0);
562801
+    if (status != 0)
562801
+    {
562801
+        fprintf(stderr, "gzip status code '%d'\n", status);
562801
+        abort();
562801
+    }
562801
+
562801
+    int err = 0;
562801
+    const_string_vector_const_ptr_t i = included_files;
562801
+    for (c = 0; i && *i; ++i, ++c)
562801
+    {
562801
+        switch (check_array[c])
562801
+        {
562801
+            case 0:
562801
+                fprintf(stderr, "Not found included file: '%s', in archive: %s\n", *i, file_name);
562801
+                ++err;
562801
+                break;
562801
+            case 1:
562801
+                fprintf(stdout, "Found included file: '%s', in archive: %s\n", *i, file_name);
562801
+                break;
562801
+            default:
562801
+                fprintf(stderr, "%d occurrences of included file: '%s', in archive: %s\n", check_array[c], *i, file_name);
562801
+                ++err;
562801
+                break;
562801
+        }
562801
+    }
562801
+
562801
+    if (err)
562801
+        abort();
562801
+
562801
+    return;
562801
+}
562801
+
562801
+int main(void)
562801
+{
562801
+    g_verbose = 3;
562801
+
562801
+    char template[] = "/tmp/XXXXXX";
562801
+
562801
+    if (mkdtemp(template) == NULL) {
562801
+        perror("mkdtemp()");
562801
+        return EXIT_FAILURE;
562801
+    }
562801
+
562801
+    printf("Dump dir path: %s\n", template);
562801
+
562801
+    struct dump_dir *dd = dd_create(template, (uid_t)-1, 0640);
562801
+    assert(dd != NULL || !"Cannot create new dump directory");
562801
+
562801
+
562801
+#define COMMON_FILES "time", "last_occurrence", "uid", "kernel", \
562801
+                     "architecture", "hostname", "os_info", "os_release", \
562801
+                     "type", "count", "component", "program_log"
562801
+#define SENSITIVE_FILES "environ", "backtrace", "secret_file", "private_file", \
562801
+                        "useless_file"
562801
+
562801
+    dd_create_basic_files(dd, geteuid(), NULL);
562801
+    dd_save_text(dd, FILENAME_TYPE, "attest");
562801
+    dd_save_text(dd, FILENAME_COUNT, "1");
562801
+    dd_save_text(dd, FILENAME_COMPONENT, "libreport-attest");
562801
+    dd_save_text(dd, "program_log", "Something very important!");
562801
+
562801
+    const gchar *excluded_files[] = {
562801
+        SENSITIVE_FILES,
562801
+        NULL,
562801
+    };
562801
+
562801
+    for (const gchar **iter = excluded_files; *iter; ++iter)
562801
+        dd_save_text(dd, *iter, *iter);
562801
+
562801
+    /* Un-supported archive type */
562801
+    {
562801
+        fprintf(stderr, "TEST-CASE: Un-supported type\n");
562801
+        fprintf(stdout, "TEST-CASE: Un-supported type\n");
562801
+        const int r = dd_create_archive(dd, "/tmp/libreport-attest.omg", NULL, 0);
562801
+        printf("dd_create_archive() == %d\n", r);
562801
+        assert(r == -ENOSYS || !"Not supported");
562801
+    }
562801
+
562801
+    /* File already exists. */
562801
+    dd_close(dd);
562801
+    dd = dd_opendir(template, DD_OPEN_READONLY);
562801
+    {
562801
+        fprintf(stderr, "TEST-CASE: File exists\n");
562801
+        fprintf(stdout, "TEST-CASE: File exists\n");
562801
+        char file_contents[] = "Non emtpy file";
562801
+        const char *file_name = "/tmp/libreport-attest.tar.gz";
562801
+        FILE *test_file = fopen(file_name, "w");
562801
+        assert(test_file != NULL);
562801
+        assert(fprintf(test_file, "%s", file_contents) == strlen(file_contents));
562801
+        fclose(test_file);
562801
+
562801
+        assert(dd_create_archive(dd, file_name, NULL, 0) == -EEXIST || !"Exists");
562801
+
562801
+        char *canary = xmalloc_xopen_read_close(file_name, NULL);
562801
+        assert(canary != NULL);
562801
+        assert(strcmp(canary, file_contents) == 0);
562801
+    }
562801
+
562801
+    dd_close(dd);
562801
+    dd = dd_opendir(template, DD_OPEN_READONLY);
562801
+    /* All elements */
562801
+    {
562801
+        fprintf(stderr, "TEST-CASE: Compress all elements\n");
562801
+        fprintf(stdout, "TEST-CASE: Compress all elements\n");
562801
+
562801
+        const gchar *included_files[] = {
562801
+            COMMON_FILES,
562801
+            SENSITIVE_FILES,
562801
+            NULL,
562801
+        };
562801
+
562801
+        const char *file_name = "/tmp/libreport-attest-all.tar.gz";
562801
+        unlink(file_name);
562801
+        assert(dd_create_archive(dd, file_name, NULL, 0) == 0 || !"All elements");
562801
+
562801
+        verify_archive(dd, file_name, included_files, NULL);
562801
+
562801
+        unlink(file_name);
562801
+    }
562801
+
562801
+    dd_close(dd);
562801
+    dd = dd_opendir(template, DD_OPEN_READONLY);
562801
+    /* Excluded elements */
562801
+    {
562801
+        fprintf(stderr, "TEST-CASE: Exclude elements\n");
562801
+        fprintf(stdout, "TEST-CASE: Exclude elements\n");
562801
+
562801
+        const char *included_files[] = {
562801
+            COMMON_FILES,
562801
+            NULL,
562801
+        };
562801
+
562801
+        const char *file_name = "/tmp/libreport-attest-excluded.tar.gz";
562801
+        unlink(file_name);
562801
+        assert(dd_create_archive(dd, file_name, excluded_files, 0) == 0 || !"Excluded elements");
562801
+
562801
+        verify_archive(dd, file_name, included_files, excluded_files);
562801
+
562801
+        unlink(file_name);
562801
+    }
562801
+
562801
+    dd_close(dd);
562801
+    dd = dd_opendir(template, DD_OPEN_READONLY);
562801
+    assert(dd_delete(dd) == 0);
562801
+
562801
+    return 0;
562801
+}
562801
+]])
562801
-- 
562801
1.8.3.1
562801