|
|
4b6aa8 |
From 45b75331a246a4ee48698ad1df552f38c99de3c3 Mon Sep 17 00:00:00 2001
|
|
|
4b6aa8 |
From: Ernestas Kulik <ekulik@redhat.com>
|
|
|
4b6aa8 |
Date: Tue, 11 Jun 2019 17:11:01 +0200
|
|
|
4b6aa8 |
Subject: [PATCH] lib: copy_file_recursive: Use GLib abstractions
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
The current recursive copy implementation is rather cumbersome to read
|
|
|
4b6aa8 |
and causes Coverity to complain when building RHEL packages. Using
|
|
|
4b6aa8 |
GLib/GIO should improve readability and eliminate warnings while
|
|
|
4b6aa8 |
retaining compatibility.
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
Signed-off-by: Ernestas Kulik <ekulik@redhat.com>
|
|
|
4b6aa8 |
---
|
|
|
4b6aa8 |
configure.ac | 6 +-
|
|
|
4b6aa8 |
src/lib/Makefile.am | 2 +
|
|
|
4b6aa8 |
src/lib/copy_file_recursive.c | 217 ++++++++++++++--------------------
|
|
|
4b6aa8 |
3 files changed, 98 insertions(+), 127 deletions(-)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
diff --git a/configure.ac b/configure.ac
|
|
|
4b6aa8 |
index a7f67c9..6bcd230 100644
|
|
|
4b6aa8 |
--- a/configure.ac
|
|
|
4b6aa8 |
+++ b/configure.ac
|
|
|
4b6aa8 |
@@ -158,7 +158,10 @@ PYTHON_LIBS=`python-config --libs 2> /dev/null`
|
|
|
4b6aa8 |
AC_SUBST(PYTHON_CFLAGS)
|
|
|
4b6aa8 |
AC_SUBST(PYTHON_LIBS)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
-PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.21])
|
|
|
4b6aa8 |
+m4_define([glib_version], [2.21])
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+PKG_CHECK_MODULES([GLIB], [glib-2.0 >= glib_version])
|
|
|
4b6aa8 |
+PKG_CHECK_MODULES([GIO], [gio-2.0 >= glib_version])
|
|
|
4b6aa8 |
PKG_CHECK_MODULES([GOBJECT], [gobject-2.0])
|
|
|
4b6aa8 |
PKG_CHECK_MODULES([DBUS], [dbus-1])
|
|
|
4b6aa8 |
PKG_CHECK_MODULES([LIBXML], [libxml-2.0])
|
|
|
4b6aa8 |
@@ -188,7 +191,6 @@ LIBREPORT_PARSE_WITH([gtk]))
|
|
|
4b6aa8 |
if test -z "$NO_GTK"; then
|
|
|
4b6aa8 |
AM_CONDITIONAL(BUILD_GTK, true)
|
|
|
4b6aa8 |
PKG_CHECK_MODULES([GTK], [gtk+-3.0])
|
|
|
4b6aa8 |
-PKG_CHECK_MODULES([GIO], [gio-2.0])
|
|
|
4b6aa8 |
else
|
|
|
4b6aa8 |
AM_CONDITIONAL(BUILD_GTK, false)
|
|
|
4b6aa8 |
fi dnl end NO_GTK
|
|
|
4b6aa8 |
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am
|
|
|
4b6aa8 |
index c11a42d..5a44257 100644
|
|
|
4b6aa8 |
--- a/src/lib/Makefile.am
|
|
|
4b6aa8 |
+++ b/src/lib/Makefile.am
|
|
|
4b6aa8 |
@@ -76,6 +76,7 @@ libreport_la_CPPFLAGS = \
|
|
|
4b6aa8 |
-DDUMP_DIR_OWNED_BY_USER=$(DUMP_DIR_OWNED_BY_USER) \
|
|
|
4b6aa8 |
-DLARGE_DATA_TMP_DIR=\"$(LARGE_DATA_TMP_DIR)\" \
|
|
|
4b6aa8 |
$(JSON_C_CFLAGS) \
|
|
|
4b6aa8 |
+ $(GIO_CFLAGS) \
|
|
|
4b6aa8 |
$(GLIB_CFLAGS) \
|
|
|
4b6aa8 |
$(GOBJECT_CFLAGS) \
|
|
|
4b6aa8 |
$(AUGEAS_CFLAGS) \
|
|
|
4b6aa8 |
@@ -86,6 +87,7 @@ libreport_la_LDFLAGS = \
|
|
|
4b6aa8 |
-version-info 0:1:0
|
|
|
4b6aa8 |
libreport_la_LIBADD = \
|
|
|
4b6aa8 |
$(JSON_C_LIBS) \
|
|
|
4b6aa8 |
+ $(GIO_LIBS) \
|
|
|
4b6aa8 |
$(GLIB_LIBS) \
|
|
|
4b6aa8 |
$(JOURNAL_LIBS) \
|
|
|
4b6aa8 |
$(GOBJECT_LIBS) \
|
|
|
4b6aa8 |
diff --git a/src/lib/copy_file_recursive.c b/src/lib/copy_file_recursive.c
|
|
|
4b6aa8 |
index 6bad978..daee675 100644
|
|
|
4b6aa8 |
--- a/src/lib/copy_file_recursive.c
|
|
|
4b6aa8 |
+++ b/src/lib/copy_file_recursive.c
|
|
|
4b6aa8 |
@@ -19,131 +19,98 @@
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
#include "internal_libreport.h"
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
+#include <gio/gio.h>
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+static int report_copy_gfile_recursive(GFile *source, GFile *destination)
|
|
|
4b6aa8 |
+{
|
|
|
4b6aa8 |
+ const char *blacklist[] =
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ ".libreport",
|
|
|
4b6aa8 |
+ ".lock",
|
|
|
4b6aa8 |
+ };
|
|
|
4b6aa8 |
+ g_autofree char *name = NULL;
|
|
|
4b6aa8 |
+ g_autoptr(GError) error = NULL;
|
|
|
4b6aa8 |
+ bool file_copied;
|
|
|
4b6aa8 |
+ bool recurse;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ name = g_file_get_basename(source);
|
|
|
4b6aa8 |
+ for (size_t i = 0; i < G_N_ELEMENTS(blacklist); i++)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ if (g_strcmp0(name, blacklist[i]) == 0)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ log_debug("Skipping ā%sā", name);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return 0;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+ file_copied = g_file_copy(source, destination,
|
|
|
4b6aa8 |
+ (G_FILE_COPY_OVERWRITE |
|
|
|
4b6aa8 |
+ G_FILE_COPY_NOFOLLOW_SYMLINKS |
|
|
|
4b6aa8 |
+ G_FILE_COPY_ALL_METADATA),
|
|
|
4b6aa8 |
+ NULL, NULL, NULL, &error);
|
|
|
4b6aa8 |
+ recurse = !file_copied && g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE);
|
|
|
4b6aa8 |
+ if (recurse)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ g_autoptr(GFileEnumerator) enumerator = NULL;
|
|
|
4b6aa8 |
+ GFileInfo *child_info;
|
|
|
4b6aa8 |
+ GFile *child;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ g_clear_error(&error);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ enumerator = g_file_enumerate_children(source,
|
|
|
4b6aa8 |
+ G_FILE_ATTRIBUTE_STANDARD_NAME,
|
|
|
4b6aa8 |
+ G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
|
|
|
4b6aa8 |
+ NULL, &error);
|
|
|
4b6aa8 |
+ if (NULL != error)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ log_error("Error occurred while enumerating files: %s", error->message);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return -1;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ while (g_file_enumerator_iterate(enumerator, &child_info, &child, NULL, &error))
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ const char *child_name;
|
|
|
4b6aa8 |
+ g_autoptr(GFile) child_destination = NULL;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ if (NULL == child)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ break;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ child_name = g_file_info_get_name(child_info);
|
|
|
4b6aa8 |
+ child_destination = g_file_get_child(destination, child_name);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ report_copy_gfile_recursive(child, child_destination);
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ if (NULL != error)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ log_error("Error occurred while iterating files: %s", error->message);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return -1;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+ else if (NULL != error)
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
+ log_error("Error occurred while copying file: %s", error->message);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return -1;
|
|
|
4b6aa8 |
+ }
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return 0;
|
|
|
4b6aa8 |
+}
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
int copy_file_recursive(const char *source, const char *dest)
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
- /* This is a recursive function, try to minimize stack usage */
|
|
|
4b6aa8 |
- /* NB: each struct stat is ~100 bytes */
|
|
|
4b6aa8 |
- struct stat source_stat;
|
|
|
4b6aa8 |
- struct stat dest_stat;
|
|
|
4b6aa8 |
- int retval = 0;
|
|
|
4b6aa8 |
- int dest_exists = 0;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (strcmp(source, ".lock") == 0)
|
|
|
4b6aa8 |
- goto skip;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (stat(source, &source_stat) < 0) {
|
|
|
4b6aa8 |
- perror_msg("Can't stat '%s'", source);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (lstat(dest, &dest_stat) < 0) {
|
|
|
4b6aa8 |
- if (errno != ENOENT) {
|
|
|
4b6aa8 |
- perror_msg("Can't stat '%s'", dest);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- } else {
|
|
|
4b6aa8 |
- if (source_stat.st_dev == dest_stat.st_dev
|
|
|
4b6aa8 |
- && source_stat.st_ino == dest_stat.st_ino
|
|
|
4b6aa8 |
- ) {
|
|
|
4b6aa8 |
- error_msg("'%s' and '%s' are the same file", source, dest);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- dest_exists = 1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (S_ISDIR(source_stat.st_mode)) {
|
|
|
4b6aa8 |
- DIR *dp;
|
|
|
4b6aa8 |
- struct dirent *d;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (dest_exists) {
|
|
|
4b6aa8 |
- if (!S_ISDIR(dest_stat.st_mode)) {
|
|
|
4b6aa8 |
- error_msg("Target '%s' is not a directory", dest);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- /* race here: user can substitute a symlink between
|
|
|
4b6aa8 |
- * this check and actual creation of files inside dest */
|
|
|
4b6aa8 |
- } else {
|
|
|
4b6aa8 |
- /* Create DEST */
|
|
|
4b6aa8 |
- mode_t mode = source_stat.st_mode;
|
|
|
4b6aa8 |
- /* Allow owner to access new dir (at least for now) */
|
|
|
4b6aa8 |
- mode |= S_IRWXU;
|
|
|
4b6aa8 |
- if (mkdir(dest, mode) < 0) {
|
|
|
4b6aa8 |
- perror_msg("Can't create directory '%s'", dest);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- /* Recursively copy files in SOURCE */
|
|
|
4b6aa8 |
- dp = opendir(source);
|
|
|
4b6aa8 |
- if (dp == NULL) {
|
|
|
4b6aa8 |
- retval = -1;
|
|
|
4b6aa8 |
- goto ret;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- while (retval == 0 && (d = readdir(dp)) != NULL) {
|
|
|
4b6aa8 |
- char *new_source, *new_dest;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (dot_or_dotdot(d->d_name))
|
|
|
4b6aa8 |
- continue;
|
|
|
4b6aa8 |
- new_source = concat_path_file(source, d->d_name);
|
|
|
4b6aa8 |
- new_dest = concat_path_file(dest, d->d_name);
|
|
|
4b6aa8 |
- if (copy_file_recursive(new_source, new_dest) < 0)
|
|
|
4b6aa8 |
- retval = -1;
|
|
|
4b6aa8 |
- free(new_source);
|
|
|
4b6aa8 |
- free(new_dest);
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- closedir(dp);
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- goto ret;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (S_ISREG(source_stat.st_mode)) {
|
|
|
4b6aa8 |
- int src_fd;
|
|
|
4b6aa8 |
- int dst_fd;
|
|
|
4b6aa8 |
- mode_t new_mode;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- src_fd = open(source, O_RDONLY);
|
|
|
4b6aa8 |
- if (src_fd < 0) {
|
|
|
4b6aa8 |
- perror_msg("Can't open '%s'", source);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- /* Do not try to open with weird mode fields */
|
|
|
4b6aa8 |
- new_mode = source_stat.st_mode;
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- // security problem versus (sym)link attacks
|
|
|
4b6aa8 |
- // dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
|
|
|
4b6aa8 |
- /* safe way: */
|
|
|
4b6aa8 |
- dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
|
|
|
4b6aa8 |
- if (dst_fd < 0) {
|
|
|
4b6aa8 |
- close(src_fd);
|
|
|
4b6aa8 |
- return -1;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- if (copyfd_eof(src_fd, dst_fd, COPYFD_SPARSE) == -1)
|
|
|
4b6aa8 |
- retval = -1;
|
|
|
4b6aa8 |
- close(src_fd);
|
|
|
4b6aa8 |
- /* Careful: do check that buffered writes succeeded... */
|
|
|
4b6aa8 |
- if (close(dst_fd) < 0) {
|
|
|
4b6aa8 |
- perror_msg("Error writing to '%s'", dest);
|
|
|
4b6aa8 |
- retval = -1;
|
|
|
4b6aa8 |
- } else {
|
|
|
4b6aa8 |
- /* (Try to) copy atime and mtime */
|
|
|
4b6aa8 |
- struct timeval atime_mtime[2];
|
|
|
4b6aa8 |
- atime_mtime[0].tv_sec = source_stat.st_atime;
|
|
|
4b6aa8 |
- // note: if "st_atim.tv_nsec" doesn't compile, try "st_atimensec":
|
|
|
4b6aa8 |
- atime_mtime[0].tv_usec = source_stat.st_atim.tv_nsec / 1000;
|
|
|
4b6aa8 |
- atime_mtime[1].tv_sec = source_stat.st_mtime;
|
|
|
4b6aa8 |
- atime_mtime[1].tv_usec = source_stat.st_mtim.tv_nsec / 1000;
|
|
|
4b6aa8 |
- // note: can use utimensat when it is more widely supported:
|
|
|
4b6aa8 |
- utimes(dest, atime_mtime);
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
- goto ret;
|
|
|
4b6aa8 |
- }
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- /* Neither dir not regular file: skip */
|
|
|
4b6aa8 |
-
|
|
|
4b6aa8 |
- skip:
|
|
|
4b6aa8 |
- log("Skipping '%s'", source);
|
|
|
4b6aa8 |
- ret:
|
|
|
4b6aa8 |
- return retval;
|
|
|
4b6aa8 |
+ g_autoptr(GFile) source_file = NULL;
|
|
|
4b6aa8 |
+ g_autoptr(GFile) destination_file = NULL;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ g_return_val_if_fail(NULL != source, -1);
|
|
|
4b6aa8 |
+ g_return_val_if_fail(NULL != dest, -1);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ source_file = g_file_new_for_path(source);
|
|
|
4b6aa8 |
+ destination_file = g_file_new_for_path(dest);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
+ return report_copy_gfile_recursive(source_file, destination_file);
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
--
|
|
|
4b6aa8 |
2.21.0
|
|
|
4b6aa8 |
|