[evince] comics: Add test program for archive handling code



commit 5e92c9acb3717e1b9b6e053973671036d42c436b
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Jul 18 03:49:25 2017 +0200

    comics: Add test program for archive handling code
    
    To make it easier to find bugs related to archive handling specifically.
    This test program just lists the files in the archive provided, along
    with their sizes and whether they are password protected.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=784963

 backend/comics/Makefile.am       |   11 ++++
 backend/comics/test-ev-archive.c |  116 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+), 0 deletions(-)
---
diff --git a/backend/comics/Makefile.am b/backend/comics/Makefile.am
index 1e87d04..f2fd773 100644
--- a/backend/comics/Makefile.am
+++ b/backend/comics/Makefile.am
@@ -37,6 +37,17 @@ appstream_in_files = evince-comicsdocument.metainfo.xml.in.in
 appstream_DATA = $(appstream_in_files:.xml.in.in=.xml)
 @INTLTOOL_XML_RULE@
 
+noinst_PROGRAMS = test-ev-archive
+
+test_ev_archive_SOURCES = ev-archive.c ev-archive.h test-ev-archive.c
+test_ev_archive_CPPFLAGS = $(libcomicsdocument_la_CPPFLAGS)
+test_ev_archive_CFLAGS = $(libcomicsdocument_la_CFLAGS)
+test_ev_archive_LDADD =                                        \
+       $(top_builddir)/cut-n-paste/unarr/libunarr.la   \
+       $(LIBARCHIVE_LIBS)                              \
+       $(BACKEND_LIBS)                                 \
+       $(LIB_LIBS)
+
 EXTRA_DIST = $(backend_in_files) $(appstream_in_files)
 
 CLEANFILES = $(backend_DATA) $(appstream_DATA)
diff --git a/backend/comics/test-ev-archive.c b/backend/comics/test-ev-archive.c
new file mode 100644
index 0000000..772495a
--- /dev/null
+++ b/backend/comics/test-ev-archive.c
@@ -0,0 +1,116 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
+/*
+ * Copyright (C) 2017, Bastien Nocera <hadess hadess net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "config.h"
+
+#include "ev-archive.h"
+
+static void
+usage (const char *prog)
+{
+       g_print ("- Lists file in a supported archive format\n");
+       g_print ("Usage: %s archive-type filename\n", prog);
+       g_print ("Where archive-type is one of rar, zip, 7z or tar\n");
+}
+
+static EvArchiveType
+str_to_archive_type (const char *str)
+{
+       g_return_val_if_fail (str != NULL, EV_ARCHIVE_TYPE_NONE);
+
+       if (g_strcmp0 (str, "rar") == 0)
+               return EV_ARCHIVE_TYPE_RAR;
+       if (g_strcmp0 (str, "zip") == 0)
+               return EV_ARCHIVE_TYPE_ZIP;
+       if (g_strcmp0 (str, "7z") == 0)
+               return EV_ARCHIVE_TYPE_7Z;
+       if (g_strcmp0 (str, "tar") == 0)
+               return EV_ARCHIVE_TYPE_TAR;
+
+       g_warning ("Archive type '%s' not supported", str);
+       return EV_ARCHIVE_TYPE_NONE;
+}
+
+int
+main (int argc, char **argv)
+{
+       EvArchive *ar;
+       EvArchiveType ar_type;
+       GError *error = NULL;
+       gboolean printed_header = FALSE;
+
+       if (argc != 3) {
+               usage (argv[0]);
+               return 1;
+       }
+
+       ar_type = str_to_archive_type (argv[1]);
+       if (ar_type == EV_ARCHIVE_TYPE_NONE)
+               return 1;
+
+       ar = ev_archive_new ();
+       if (!ev_archive_set_archive_type (ar, ar_type)) {
+               g_warning ("Failed to set archive type");
+               goto out;
+       }
+
+       if (!ev_archive_open_filename (ar, argv[2], &error)) {
+               g_warning ("Failed to open '%s': %s",
+                          argv[2], error->message);
+               g_error_free (error);
+               goto out;
+       }
+
+       while (1) {
+               const char *name;
+               gboolean is_encrypted;
+               gint64 size;
+
+               if (!ev_archive_read_next_header (ar, &error)) {
+                       if (error != NULL) {
+                               g_warning ("Fatal error handling archive: %s", error->message);
+                               g_clear_error (&error);
+                               goto out;
+                       }
+                       break;
+               }
+
+               name = ev_archive_get_entry_pathname (ar);
+               is_encrypted = ev_archive_get_entry_is_encrypted (ar);
+               size = ev_archive_get_entry_size (ar);
+
+               if (!printed_header) {
+                       g_print ("P\tSIZE\tNAME\n");
+                       printed_header = TRUE;
+               }
+
+               g_print ("%c\t%"G_GINT64_FORMAT"\t%s\n",
+                        is_encrypted ? 'P' : ' ',
+                        size, name);
+       }
+
+       ev_archive_reset (ar);
+       g_clear_object (&ar);
+
+       return 0;
+
+out:
+       g_clear_object (&ar);
+       return 1;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]