From d27792e1ee93f2f92747f45f007d668f66487045 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Sat, 2 Mar 2024 14:52:30 +0300 Subject: [PATCH] Move open_archive() to separate module Also make parameter const and move static functions to anonymous namespace. --- src/archives.cc | 226 +++++++++++++++++++++++++++++++++++++ src/archives.h | 31 +++++ src/img-view.cc | 1 + src/layout-image.cc | 1 + src/layout-util.cc | 1 + src/meson.build | 4 +- src/misc.cc | 193 ------------------------------- src/misc.h | 5 +- src/pan-view/pan-view.cc | 1 - src/preferences.cc | 1 + src/view-file/view-file.cc | 1 + 11 files changed, 266 insertions(+), 199 deletions(-) create mode 100644 src/archives.cc create mode 100644 src/archives.h diff --git a/src/archives.cc b/src/archives.cc new file mode 100644 index 00000000..7a6a7794 --- /dev/null +++ b/src/archives.cc @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2008 - 2016 The Geeqie Team + * + * Authors: Vladimir Nadvornik, Laurent Monin + * + * 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 of the License, 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 "archives.h" + +#include + +#include "debug.h" +#include "intl.h" + +#if HAVE_ARCHIVE +#include + +#include +#include +#include + +#include +#include + +#include "filedata.h" +#include "main-defines.h" +#include "main.h" +#include "ui-fileops.h" + +/* Copied from the libarchive .repo. examples */ + +namespace +{ + +int verbose = 0; + +void msg(const char *m) +{ + log_printf("Open Archive - libarchive error: %s \n", m); +} + +void errmsg(const char *m) +{ + if (m == nullptr) + { + m = "Error: No error description provided.\n"; + } + msg(m); +} + +int copy_data(struct archive *ar, struct archive *aw) +{ + int r; + const void *buff; + size_t size; + int64_t offset; + + for (;;) + { + r = archive_read_data_block(ar, &buff, &size, &offset); + if (r == ARCHIVE_EOF) + return (ARCHIVE_OK); + if (r != ARCHIVE_OK) + { + errmsg(archive_error_string(ar)); + return (r); + } + r = archive_write_data_block(aw, buff, size, offset); + if (r != ARCHIVE_OK) + { + errmsg(archive_error_string(ar)); + return (r); + } + } +} + +gboolean extract(const char *filename, bool do_extract, int flags) +{ + struct archive *a; + struct archive *ext; + struct archive_entry *entry; + int r; + + a = archive_read_new(); + ext = archive_write_disk_new(); + archive_write_disk_set_options(ext, flags); + archive_write_disk_set_standard_lookup(ext); + archive_read_support_filter_all(a); + archive_read_support_format_all(a); + + if (filename != nullptr && strcmp(filename, "-") == 0) + { + filename = nullptr; + } + if ((r = archive_read_open_filename(a, filename, 10240))) + { + errmsg(archive_error_string(a)); + errmsg("\n"); + return(FALSE); + } + for (;;) + { + int needcr = 0; + + r = archive_read_next_header(a, &entry); + if (r == ARCHIVE_EOF) + { + break; + } + if (r != ARCHIVE_OK) + { + errmsg(archive_error_string(a)); + errmsg("\n"); + return(FALSE); + } + if (verbose && do_extract) + { + msg("x "); + } + if (verbose || !do_extract) + { + msg(archive_entry_pathname(entry)); + msg(" "); + needcr = 1; + } + if (do_extract) + { + r = archive_write_header(ext, entry); + if (r != ARCHIVE_OK) + { + errmsg(archive_error_string(a)); + needcr = 1; + } + else + { + r = copy_data(a, ext); + if (r != ARCHIVE_OK) + { + needcr = 1; + } + } + } + if (needcr) + { + msg("\n"); + } + } + archive_read_close(a); + archive_read_free(a); + + archive_write_close(ext); + archive_write_free(ext); + return(TRUE); +} + +} // namespace + +gchar *open_archive(const FileData *fd) +{ + int flags; + gchar *current_dir; + gchar *destination_dir; + gboolean success; + gint error; + + destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL); + + if (!recursive_mkdir_if_not_exists(destination_dir, 0755)) + { + log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n"); + g_free(destination_dir); + return nullptr; + } + + current_dir = g_get_current_dir(); + error = chdir(destination_dir); + if (error) + { + log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n"); + g_free(destination_dir); + g_free(current_dir); + return nullptr; + } + + flags = ARCHIVE_EXTRACT_TIME; + success = extract(fd->path, true, flags); + + error = chdir(current_dir); + if (error) + { + log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n"); + g_free(destination_dir); + g_free(current_dir); + return nullptr; + } + g_free(current_dir); + + if (!success) + { + g_free(destination_dir); + destination_dir = nullptr; + } + + return destination_dir; +} +#else +gchar *open_archive(const FileData *) +{ + log_printf("%s", _("Warning: libarchive not installed")); + return nullptr; +} +#endif /* HAVE_ARCHIVE */ +/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/archives.h b/src/archives.h new file mode 100644 index 00000000..fc801c9f --- /dev/null +++ b/src/archives.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2008 - 2016 The Geeqie Team + * + * Authors: Vladimir Nadvornik, Laurent Monin + * + * 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 of the License, 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. + */ + +#ifndef _ARCHIVES_H +#define _ARCHIVES_H + +#include + +struct FileData; + +gchar *open_archive(const FileData *fd); + +#endif /* _ARCHIVES_H */ +/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/img-view.cc b/src/img-view.cc index dc3fb9bb..677fd403 100644 --- a/src/img-view.cc +++ b/src/img-view.cc @@ -25,6 +25,7 @@ #include #include +#include "archives.h" #include "collect-io.h" #include "collect.h" #include "compat.h" diff --git a/src/layout-image.cc b/src/layout-image.cc index 0d4042b2..9699e029 100644 --- a/src/layout-image.cc +++ b/src/layout-image.cc @@ -31,6 +31,7 @@ #include +#include "archives.h" #include "collect.h" #include "debug.h" #include "dnd.h" diff --git a/src/layout-util.cc b/src/layout-util.cc index c8844a07..754ae203 100644 --- a/src/layout-util.cc +++ b/src/layout-util.cc @@ -34,6 +34,7 @@ #include #include "advanced-exif.h" +#include "archives.h" #include "bar-keywords.h" #include "bar-sort.h" #include "bar.h" diff --git a/src/meson.build b/src/meson.build index 288c301d..799213bf 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,14 +13,16 @@ main_sources = files('advanced-exif.cc', 'advanced-exif.h', +'archives.cc', +'archives.h', 'bar.cc', +'bar.h', 'bar-comment.cc', 'bar-comment.h', 'bar-exif.cc', 'bar-exif.h', 'bar-gps.cc', 'bar-gps.h', -'bar.h', 'bar-histogram.cc', 'bar-histogram.h', 'bar-keywords.cc', diff --git a/src/misc.cc b/src/misc.cc index 9767e016..15439d02 100644 --- a/src/misc.cc +++ b/src/misc.cc @@ -23,8 +23,6 @@ #include #include -#include -#include #include #include #include @@ -36,9 +34,6 @@ #include #include "debug.h" -#include "filedata.h" -#include "intl.h" -#include "main-defines.h" #include "main.h" #include "options.h" #include "ui-fileops.h" @@ -439,192 +434,4 @@ void gq_gtk_grid_attach_default(GtkGrid *grid, GtkWidget *child, guint left_atta gtk_grid_attach(grid, child, left_attach, top_attach, right_attach - left_attach, bottom_attach - top_attach); } -/* Copied from the libarchive .repo. examples */ - -#if !HAVE_ARCHIVE -gchar *open_archive(FileData *) -{ - log_printf("%s", _("Warning: libarchive not installed")); - return NULL; -} - -#else - -#include -#include - -static void errmsg(const char *); -static gboolean extract(const char *filename, int do_extract, int flags); -static int copy_data(struct archive *, struct archive *); -static void msg(const char *); -static int verbose = 0; - -gchar *open_archive(FileData *fd) -{ - int flags; - gchar *current_dir; - gchar *destination_dir; - gboolean success; - gint error; - - destination_dir = g_build_filename(g_get_tmp_dir(), GQ_ARCHIVE_DIR, instance_identifier, fd->path, NULL); - - if (!recursive_mkdir_if_not_exists(destination_dir, 0755)) - { - log_printf("%s%s%s", _("Open Archive - Cannot create directory: "), destination_dir, "\n"); - g_free(destination_dir); - return nullptr; - } - - current_dir = g_get_current_dir(); - error = chdir(destination_dir); - if (error) - { - log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), destination_dir, _("\n Error code: "), strerror(errno), "\n"); - g_free(destination_dir); - g_free(current_dir); - return nullptr; - } - - flags = ARCHIVE_EXTRACT_TIME; - success = extract(fd->path, 1, flags); - - error = chdir(current_dir); - if (error) - { - log_printf("%s%s%s%s%s", _("Open Archive - Cannot change directory to: "), current_dir, _("\n Error code: "), strerror(errno), "\n"); - g_free(destination_dir); - g_free(current_dir); - return nullptr; - } - g_free(current_dir); - - if (!success) - { - g_free(destination_dir); - destination_dir = nullptr; - } - - return destination_dir; -} - -static gboolean extract(const char *filename, int do_extract, int flags) -{ - struct archive *a; - struct archive *ext; - struct archive_entry *entry; - int r; - - a = archive_read_new(); - ext = archive_write_disk_new(); - archive_write_disk_set_options(ext, flags); - archive_write_disk_set_standard_lookup(ext); - archive_read_support_filter_all(a); - archive_read_support_format_all(a); - - if (filename != nullptr && strcmp(filename, "-") == 0) - { - filename = nullptr; - } - if ((r = archive_read_open_filename(a, filename, 10240))) - { - errmsg(archive_error_string(a)); - errmsg("\n"); - return(FALSE); - } - for (;;) - { - int needcr = 0; - - r = archive_read_next_header(a, &entry); - if (r == ARCHIVE_EOF) - { - break; - } - if (r != ARCHIVE_OK) - { - errmsg(archive_error_string(a)); - errmsg("\n"); - return(FALSE); - } - if (verbose && do_extract) - { - msg("x "); - } - if (verbose || !do_extract) - { - msg(archive_entry_pathname(entry)); - msg(" "); - needcr = 1; - } - if (do_extract) - { - r = archive_write_header(ext, entry); - if (r != ARCHIVE_OK) - { - errmsg(archive_error_string(a)); - needcr = 1; - } - else - { - r = copy_data(a, ext); - if (r != ARCHIVE_OK) - { - needcr = 1; - } - } - } - if (needcr) - { - msg("\n"); - } - } - archive_read_close(a); - archive_read_free(a); - - archive_write_close(ext); - archive_write_free(ext); - return(TRUE); -} - -static int copy_data(struct archive *ar, struct archive *aw) -{ - int r; - const void *buff; - size_t size; - int64_t offset; - - for (;;) - { - r = archive_read_data_block(ar, &buff, &size, &offset); - if (r == ARCHIVE_EOF) - return (ARCHIVE_OK); - if (r != ARCHIVE_OK) - { - errmsg(archive_error_string(ar)); - return (r); - } - r = archive_write_data_block(aw, buff, size, offset); - if (r != ARCHIVE_OK) - { - errmsg(archive_error_string(ar)); - return (r); - } - } -} - -static void msg(const char *m) -{ - log_printf("Open Archive - libarchive error: %s \n", m); -} - -static void errmsg(const char *m) -{ - if (m == nullptr) - { - m = "Error: No error description provided.\n"; - } - log_printf("Open Archive - libarchive error: %s \n", m); -} -#endif /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/misc.h b/src/misc.h index 4223bb13..a9e841d1 100644 --- a/src/misc.h +++ b/src/misc.h @@ -27,8 +27,6 @@ #include -struct FileData; - const gchar *gq_gtk_entry_get_text(GtkEntry *entry); gchar *convert_rating_to_stars(gint rating); gchar *date_get_abbreviated_day_name(gint day); @@ -49,8 +47,7 @@ void gq_gtk_grid_attach(GtkGrid *grid, GtkWidget *child, guint left_attach, guin void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba); #else void convert_gdkcolor_to_gdkrgba(gpointer data, GdkRGBA *gdk_rgba); -#endif +#endif /* HAVE_GTK4 */ -gchar *open_archive(FileData *fd); #endif /* MISC_H */ /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ diff --git a/src/pan-view/pan-view.cc b/src/pan-view/pan-view.cc index 8c8052e3..13a0839f 100644 --- a/src/pan-view/pan-view.cc +++ b/src/pan-view/pan-view.cc @@ -49,7 +49,6 @@ #include "main.h" #include "menu.h" #include "metadata.h" -#include "misc.h" #include "options.h" #include "pan-calendar.h" #include "pan-folder.h" diff --git a/src/preferences.cc b/src/preferences.cc index 869322a0..ab391c8d 100644 --- a/src/preferences.cc +++ b/src/preferences.cc @@ -45,6 +45,7 @@ #include +#include "archives.h" #include "bar-keywords.h" #include "cache.h" #include "color-man.h" diff --git a/src/view-file/view-file.cc b/src/view-file/view-file.cc index a2ae72ba..fa842c57 100644 --- a/src/view-file/view-file.cc +++ b/src/view-file/view-file.cc @@ -23,6 +23,7 @@ #include #include +#include "archives.h" #include "compat.h" #include "debug.h" #include "dupe.h" -- 2.20.1