From 2d4a2fc03859065291fcb05d8df29ba8f50ab6e3 Mon Sep 17 00:00:00 2001 From: Tomas Bzatek Date: Sun, 27 Mar 2016 18:32:33 +0200 Subject: gallery-utils: Copy extra files recursively This brings support for copying whole directories when specified in album extra files or template supplemental files. --- src/gallery-utils.c | 35 +++++++++++++++++++++++++++++++++++ src/gallery-utils.h | 8 ++++++++ src/job-manager.c | 3 +-- 3 files changed, 44 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/gallery-utils.c b/src/gallery-utils.c index 3caff02..d97cef3 100644 --- a/src/gallery-utils.c +++ b/src/gallery-utils.c @@ -154,6 +154,41 @@ copy_file (const gchar *src, const gchar *dst) return TRUE; } +/* + * copy_file_dir: recursively copy file or directory from src to dst + */ +gboolean +copy_file_dir (const gchar *src, const gchar *dst) +{ + int n; + struct dirent **namelist; + char *new_src, *new_dst; + + if (g_file_test (src, G_FILE_TEST_IS_DIR)) { + if (g_mkdir_with_parents (dst, DEFAULT_DATA_DIR_MODE)) { + log_error ("error making target directory '%s': %s\n", dst, strerror (errno)); + return FALSE; + } + n = scandir (src, &namelist, NULL, NULL); + if (n < 0) { + log_error ("error listing source directory '%s': %s\n", src, strerror (errno)); + return FALSE; + } + while (n--) { + if (g_strcmp0 (".", namelist[n]->d_name) != 0 && g_strcmp0 ("..", namelist[n]->d_name) != 0) { + new_src = g_build_filename (src, namelist[n]->d_name, NULL); + new_dst = g_build_filename (dst, namelist[n]->d_name, NULL); + copy_file_dir (new_src, new_dst); + g_free (new_src); + g_free (new_dst); + } + free (namelist[n]); + } + free (namelist); + return TRUE; + } else return copy_file (src, dst); +} + /* * make_string: make string of 'substr' substrings diff --git a/src/gallery-utils.h b/src/gallery-utils.h index 4559fa4..1af8b84 100644 --- a/src/gallery-utils.h +++ b/src/gallery-utils.h @@ -27,6 +27,9 @@ G_BEGIN_DECLS #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '~') #define IS_EQUAL_SIGN(ch) ((ch) == '=') +#define DEFAULT_DATA_DIR_MODE S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH + + /* * str_replace: replace substring 'search' with a 'replace' string * - multiple occurences of the string are replaced @@ -39,6 +42,11 @@ void str_replace (gchar **dst, const gchar *search, const gchar *replace); */ gboolean copy_file (const gchar *src, const gchar *dst); +/* + * copy_file_dir: recursively copy file or directory from src to dst + */ +gboolean copy_file_dir (const gchar *src, const gchar *dst); + /* * make_string: make string of 'substr' substrings * - returns newly allocated string diff --git a/src/job-manager.c b/src/job-manager.c index 28e81ff..9dcb3cd 100644 --- a/src/job-manager.c +++ b/src/job-manager.c @@ -34,7 +34,6 @@ -#define DEFAULT_DATA_DIR_MODE S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH #define AUTH_PASSWD_FILE ".htpasswd" @@ -87,7 +86,7 @@ mirror_files (TGallerySetup *setup, gchar **files, const gchar *src_tree, const printf ("%s", label); printf (" %s", s1); } - copy_file (s2, s3); + copy_file_dir (s2, s3); processed++; } g_free (s2); -- cgit v1.2.3