From 0e7bf378579e3e6c3bc9edca9afae7c9c5a55d52 Mon Sep 17 00:00:00 2001 From: Tomas Mlcoch Date: Mon, 17 Jun 2013 16:17:41 +0200 Subject: [PATCH] threads: New module with useful set of function for use in GThreadPool. --- src/CMakeLists.txt | 2 + src/createrepo_c.h | 1 + src/error.c | 6 +++ src/error.h | 6 ++- src/threads.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/threads.h | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 src/threads.c create mode 100644 src/threads.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ba825a7..6eaafcb 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,7 @@ SET (createrepo_c_SRCS parsepkg.c repomd.c sqlite.c + threads.c xml_dump.c xml_dump_filelists.c xml_dump_other.c @@ -36,6 +37,7 @@ SET(headers parsepkg.h repomd.h sqlite.h + threads.h version.h xml_dump.h xml_file.h diff --git a/src/createrepo_c.h b/src/createrepo_c.h index 8c3f212..f501a29 100644 --- a/src/createrepo_c.h +++ b/src/createrepo_c.h @@ -51,6 +51,7 @@ extern "C" { #include "parsepkg.h" #include "repomd.h" #include "sqlite.h" +#include "threads.h" #include "version.h" #include "xml_dump.h" #include "xml_file.h" diff --git a/src/error.c b/src/error.c index 645538a..f470157 100644 --- a/src/error.c +++ b/src/error.c @@ -131,6 +131,12 @@ cr_repomd_record_error_quark(void) } GQuark +cr_threads_error_quark(void) +{ + return g_quark_from_static_string("cr_threads_error"); +} + +GQuark cr_xml_dump_filelists_error_quark(void) { return g_quark_from_static_string("cr_xml_dump_filelists_error"); diff --git a/src/error.h b/src/error.h index 2d6eef8..5580568 100644 --- a/src/error.h +++ b/src/error.h @@ -88,10 +88,11 @@ const char *cr_strerror(cr_Error rc); #define CR_DB_ERROR cr_db_error_quark() #define CR_LOAD_METADATA_ERROR cr_load_metadata_error_quark() #define CR_LOCATE_METADATA_ERROR cr_locate_metadata_error_quark() -#define CR_PARSEPKG_ERROR cr_parsepkg_error_quark() #define CR_MISC_ERROR cr_misc_error_quark() +#define CR_PARSEPKG_ERROR cr_parsepkg_error_quark() #define CR_REPOMD_ERROR cr_repomd_error_quark() #define CR_REPOMD_RECORD_ERROR cr_repomd_record_error_quark() +#define CR_THREADS_ERROR cr_threads_error_quark() #define CR_XML_DUMP_FILELISTS_ERROR cr_xml_dump_filelists_error_quark() #define CR_XML_DUMP_OTHER_ERROR cr_xml_dump_other_error_quark() #define CR_XML_DUMP_PRIMARY_ERROR cr_xml_dump_primary_error_quark() @@ -108,10 +109,11 @@ GQuark cr_compression_wrapper_error_quark(void); GQuark cr_db_error_quark(void); GQuark cr_load_metadata_error_quark(void); GQuark cr_locate_metadata_error_quark(void); -GQuark cr_parsepkg_error_quark(void); GQuark cr_misc_error_quark(void); +GQuark cr_parsepkg_error_quark(void); GQuark cr_repomd_error_quark(void); GQuark cr_repomd_record_error_quark(void); +GQuark cr_threads_error_quark(void); GQuark cr_xml_dump_filelists_error_quark(void); GQuark cr_xml_dump_other_error_quark(void); GQuark cr_xml_dump_primary_error_quark(void); diff --git a/src/threads.c b/src/threads.c new file mode 100644 index 0000000..1e24cbc --- /dev/null +++ b/src/threads.c @@ -0,0 +1,108 @@ +/* createrepo_c - Library of routines for manipulation with repodata + * Copyright (C) 2013 Tomas Mlcoch + * + * 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 +#include "threads.h" +#include "error.h" +#include "misc.h" + +/** Compression */ + +cr_CompressionTask * +cr_compressiontask_new(const char *src, + const char *dst, + cr_CompressionType compression_type, + cr_ChecksumType checksum_type, + int delsrc, + GError **err) +{ + cr_ContentStat *stat; + cr_CompressionTask *task; + + assert(src); + assert(compression_type < CR_CW_COMPRESSION_SENTINEL); + assert(checksum_type < CR_CHECKSUM_SENTINEL); + assert(!err || *err == NULL); + + stat = cr_contentstat_new(checksum_type, err); + if (!stat) + return NULL; + + task = g_malloc0(sizeof(cr_CompressionTask)); + if (!task) { + g_set_error(err, CR_THREADS_ERROR, CRE_MEMORY, + "Cannot allocate memory"); + return NULL; + } + + task->src = g_strdup(src); + task->dst = g_strdup(dst); + task->type = compression_type; + task->stat = stat; + task->delsrc = delsrc; + + return task; +} + +void +cr_compressiontask_free(cr_CompressionTask *task, GError **err) +{ + assert(!err || *err == NULL); + + if (!task) + return; + + g_free(task->src); + g_free(task->dst); + cr_contentstat_free(task->stat, err); + if (task->err) + g_error_free(task->err); + g_free(task); +} + +void +cr_compressing_thread(gpointer data, gpointer user_data) +{ + cr_CompressionTask *task = data; + GError *tmp_err = NULL; + + CR_UNUSED(user_data); + + assert(task); + + if (!task->dst) + task->dst = g_strconcat(task->src, + cr_compression_suffix(task->type), + NULL); + + cr_compress_file_with_stat(task->src, + task->dst, + task->type, + task->stat, + &tmp_err); + + if (tmp_err) { + // Error encountered + g_propagate_error(&task->err, tmp_err); + } else { + // Compression was successful + if (task->delsrc) + remove(task->src); + } +} diff --git a/src/threads.h b/src/threads.h new file mode 100644 index 0000000..55ca8bb --- /dev/null +++ b/src/threads.h @@ -0,0 +1,99 @@ +/* createrepo_c - Library of routines for manipulation with repodata + * Copyright (C) 2013 Tomas Mlcoch + * + * 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 __C_CREATEREPOLIB_THREADS_H__ +#define __C_CREATEREPOLIB_THREADS_H__ + +#include +#include "compression_wrapper.h" +#include "checksum.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** \defgroup threads Useful thread function to use in GThreadPool. + * + * \addtogroup threads + * @{ + */ + +/** Compression */ + +typedef struct { + char *src; /*!< + Path to the original file. Must be specified by user. */ + char *dst; /*!< + Path to the destination file. If NULL, src+compression suffix will + be used and this will be filled.*/ + cr_CompressionType type; /*!< + Type of compression to use */ + cr_ContentStat *stat; /*!< + Stats of compressed file or NULL */ + int delsrc; /*!< + Indicate if delete source file after successful compression. */ + GError *err; /*!< + If error was encountered, it will be stored here, if no, then NULL*/ +} cr_CompressionTask; + +/** Function to prepare a new cr_CompressionTask. + * @param src Source filename. + * @param dst Destination filename or NULL (then src+compression + * suffix will be used). + * @param compression_type Type of compression to use. + * @param checksum_type Checksum type for stat calculation. Note: Stat + * is always use. If you don't need a stats use + * CR_CHECKSUM_UNKNOWN, then no checksum calculation + * will be performed, only size would be calculated. + * Don't be afraid, size calculation has almost + * no overhead. + * @param delsrc Delete src after successuful compression. + * 0 = Do not delete, delete otherwise + * @param err GError **. Note: This is a GError for the + * cr_compresiontask_new function. The GError + * that will be at created cr_CompressionTask is + * different. + */ +cr_CompressionTask * +cr_compressiontask_new(const char *src, + const char *dst, + cr_CompressionType compression_type, + cr_ChecksumType checksum_type, + int delsrc, + GError **err); + +/** Frees cr_CompressionTask and all its components. + * @param task cr_CompressionTask task + * @param err GError **. + */ +void +cr_compressiontask_free(cr_CompressionTask *task, GError **err); + +/** Function for GThreadPool. + */ +void +cr_compressing_thread(gpointer data, gpointer user_data); + +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* __C_CREATEREPOLIB_THREADS_H__ */ -- 2.7.4