Skip to content

Commit

Permalink
Add cr_gio_cp and deprecate cr_cp
Browse files Browse the repository at this point in the history
It's preferable to use glib gio to copy files rather than
rely on the behaviour of the system cp binary.
  • Loading branch information
Kangie authored and kontura committed Feb 22, 2023
1 parent eaa65f0 commit 358ed69
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
SET(G_LOG_DOMAIN "C_CREATEREPOLIB")

# Find necessary libraries

find_package(PkgConfig)
find_package(BZip2 REQUIRED)
find_package(CURL REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)

pkg_check_modules(GLIB2 REQUIRED glib-2.0)
pkg_check_modules(GIO REQUIRED gio-2.0)
pkg_check_modules(GTHREAD2 REQUIRED gthread-2.0)
pkg_check_modules(LZMA REQUIRED liblzma)
pkg_check_modules(SQLITE3 REQUIRED sqlite3)
Expand All @@ -57,6 +58,7 @@ ENDIF()
include_directories(${BZIP2_INCLUDE_DIRS})
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${GLIB2_INCLUDE_DIRS})
include_directories(${GIO_INCLUDE_DIRS})
include_directories(${LIBXML2_INCLUDE_DIR})
include_directories(${OPENSSL_INCLUDE_DIR})
include_directories(${ZLIB_INCLUDE_DIR})
Expand Down Expand Up @@ -154,4 +156,3 @@ ADD_SUBDIRECTORY (src)
ADD_SUBDIRECTORY (doc)
ENABLE_TESTING()
ADD_SUBDIRECTORY (tests EXCLUDE_FROM_ALL)

1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ADD_LIBRARY(libcreaterepo_c ${createrepo_c_library_type} ${createrepo_c_SRCS})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${BZIP2_LIBRARIES})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${CURL_LIBRARY})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${GLIB2_LIBRARIES})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${GIO_LIBRARIES})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${LIBMAGIC_LIBRARIES})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${LIBMODULEMD_LIBRARIES})
TARGET_LINK_LIBRARIES(libcreaterepo_c ${LIBXML2_LIBRARIES})
Expand Down
10 changes: 4 additions & 6 deletions src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <errno.h>
#include <string.h>
#include <time.h>
Expand Down Expand Up @@ -448,10 +449,9 @@ cr_old_metadata_retention(const char *old_repo,
continue;
}

// COPY!
cr_cp(full_path,
new_full_path,
CR_CP_RECURSIVE|CR_CP_PRESERVE_ALL,
cr_gio_cp(g_file_new_for_path(full_path),
g_file_new_for_path(new_full_path),
G_FILE_COPY_ALL_METADATA,
NULL,
&tmp_err);

Expand All @@ -476,5 +476,3 @@ cr_old_metadata_retention(const char *old_repo,

return ret;
}


44 changes: 40 additions & 4 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <glib/gstdio.h>
#include <glib.h>
#include <gio/gio.h>
#include <arpa/inet.h>
#include <assert.h>
#include <curl/curl.h>
Expand Down Expand Up @@ -794,8 +795,6 @@ cr_download(CURL *in_handle,
return CRE_OK;
}



gboolean
cr_better_copy_file(const char *src, const char *in_dst, GError **err)
{
Expand All @@ -820,7 +819,6 @@ cr_better_copy_file(const char *src, const char *in_dst, GError **err)
return TRUE;
}


int
cr_remove_dir_cb(const char *fpath,
G_GNUC_UNUSED const struct stat *sb,
Expand Down Expand Up @@ -856,7 +854,7 @@ gboolean
cr_move_recursive(const char *srcDir, const char *dstDir, GError **err)
{
if (rename(srcDir, dstDir) == -1) {
if (!cr_cp(srcDir, dstDir, CR_CP_RECURSIVE, NULL, err))
if (!cr_gio_cp(g_file_new_for_path(srcDir), g_file_new_for_path(dstDir), G_FILE_COPY_ALL_METADATA, NULL, err))
return FALSE;
return (cr_remove_dir(srcDir, err) == CRE_OK);
}
Expand Down Expand Up @@ -1425,6 +1423,44 @@ cr_cp(const char *src,
return ret;
}

gboolean
cr_gio_cp(GFile *src,
GFile *dst,
GFileCopyFlags flags,
GCancellable *cancellable,
GError **err)
{
assert(src);
assert(dst);
assert(!err || *err == NULL);

GFileType type = g_file_query_file_type(src, G_FILE_QUERY_INFO_NONE, NULL);

if (type == G_FILE_TYPE_DIRECTORY) {
g_file_make_directory(dst, cancellable, err);
g_file_copy_attributes(src, dst, flags, cancellable, err);

GFileEnumerator *enumerator = g_file_enumerate_children(src, G_FILE_ATTRIBUTE_STANDARD_NAME, G_FILE_QUERY_INFO_NONE, cancellable, err);
for (GFileInfo *info = g_file_enumerator_next_file(enumerator, cancellable, err); info != NULL; info = g_file_enumerator_next_file(enumerator, cancellable, err)) {
const char *relative_path = g_file_info_get_name(info);
cr_gio_cp(
g_file_resolve_relative_path(src, relative_path),
g_file_resolve_relative_path(dst, relative_path),
flags, cancellable, err);
}
} else if (type == G_FILE_TYPE_REGULAR) {
g_file_copy(src, dst, flags, cancellable, NULL, NULL, err);
}

if (err != NULL) {
return TRUE;
}
else {
return FALSE;
}

}

gboolean
cr_rm(const char *path,
cr_RmFlags flags,
Expand Down
17 changes: 16 additions & 1 deletion src/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {

#include <glib.h>
#include <string.h>
#include <gio/gio.h>
#include <curl/curl.h>
#include "compression_wrapper.h"
#include "xml_parser.h"
Expand Down Expand Up @@ -449,7 +450,7 @@ typedef enum {
preserve the all attributes (if possible) */
} cr_CpFlags;

/** Recursive copy of directory (works on files as well)
/** Wrapper for cp
* @param src Source (supports wildcards)
* @param dst Destination (supports wildcards)
* @param flags Flags
Expand All @@ -461,6 +462,20 @@ cr_cp(const char *src,
const char *dst,
cr_CpFlags flags,
const char *working_directory,
GError **err) __attribute__ ((deprecated ("please use `cr_gio_cp` instead")));

/** Recursive copy of directory (works on files as well)
* @param src Source (supports wildcards)
* @param dst Destination (supports wildcards)
* @param flags Flags
* @param cancellable Can this be cancelled by another thread?
* @param err GError **
*/
gboolean
cr_gio_cp(GFile *src,
GFile *dst,
GFileCopyFlags flags,
GCancellable *cancellable,
GError **err);

typedef enum {
Expand Down

0 comments on commit 358ed69

Please sign in to comment.