00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __C_CREATEREPOLIB_MISC_H__
00021 #define __C_CREATEREPOLIB_MISC_H__
00022
00023 #ifdef __cplusplus
00024 extern "C" {
00025 #endif
00026
00027 #include <glib.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030 #include "compression_wrapper.h"
00031 #include "xml_parser.h"
00032
00040 #define CR_UNUSED(x) (void)(x)
00041
00044 #define CR_STATICSTRLEN(s) (sizeof(s)/sizeof(s[0]))
00045
00046
00047
00048 #define CR_ARRAYLEN(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
00049
00054 const char *cr_flag_to_str(gint64 flags);
00055
00058 typedef struct {
00059 char *epoch;
00060 char *version;
00061 char *release;
00062 } cr_EVR;
00063
00064 typedef struct {
00065 char *name;
00066 char *epoch;
00067 char *version;
00068 char *release;
00069 } cr_NEVR;
00070
00071 typedef struct {
00072 char *name;
00073 char *epoch;
00074 char *version;
00075 char *release;
00076 char *arch;
00077 } cr_NEVRA;
00078
00082 struct cr_Version {
00083 long version;
00084 long release;
00085 long patch;
00086 char *suffix;
00087 };
00088
00089
00098 cr_EVR *cr_str_to_evr(const char *string, GStringChunk *chunk);
00099
00106 void
00107 cr_evr_free(cr_EVR *evr);
00108
00114 static inline int cr_is_primary(const char *filename) {
00115 if (!strncmp(filename, "/etc/", 5))
00116 return 1;
00117 if (!strcmp(filename, "/usr/lib/sendmail"))
00118 return 1;
00119 if (strstr(filename, "bin/"))
00120 return 1;
00121 return 0;
00122 };
00123
00126 struct cr_HeaderRangeStruct {
00127 unsigned int start;
00128 unsigned int end;
00129 };
00130
00136 struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename,
00137 GError **err);
00138
00144 char *cr_get_filename(const char *filepath);
00145
00154 int cr_download(CURL *handle,
00155 const char *url,
00156 const char *destination,
00157 GError **err);
00158
00165 int cr_copy_file(const char *src,
00166 const char *dst,
00167 GError **err);
00168
00178 #define cr_compress_file(SRC, DST, COMTYPE, ERR) \
00179 cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
00180
00191 int cr_compress_file_with_stat(const char *src,
00192 const char *dst,
00193 cr_CompressionType comtype,
00194 cr_ContentStat *stat,
00195 GError **err);
00196
00207 #define cr_decompress_file(SRC, DST, COMTYPE, ERR) \
00208 cr_decompress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
00209
00221 int cr_decompress_file_with_stat(const char *src,
00222 const char *dst,
00223 cr_CompressionType comtype,
00224 cr_ContentStat *stat,
00225 GError **err);
00226
00233 int cr_better_copy_file(const char *src,
00234 const char *dst,
00235 GError **err);
00236
00242 int cr_remove_dir(const char *path, GError **err);
00243
00248 char *cr_normalize_dir_path(const char *path);
00249
00254 struct cr_Version cr_str_to_version(const char *str);
00255
00262 int cr_cmp_version_str(const char* str1, const char *str2);
00263
00270 void cr_null_log_fn(const gchar *log_domain,
00271 GLogLevelFlags log_level,
00272 const gchar *message,
00273 gpointer user_data);
00274
00281 void cr_log_fn(const gchar *log_domain,
00282 GLogLevelFlags log_level,
00283 const gchar *message,
00284 gpointer user_data);
00285
00293 void cr_slist_free_full(GSList *list, GDestroyNotify free_f);
00294
00302 void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f);
00303
00313 cr_NEVRA *cr_split_rpm_filename(const char *filename);
00314
00320 #define cr_cmp_nevra(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\
00321 (B)->epoch, (B)->version, (B)->release))
00322
00332 int cr_cmp_evr(const char *e1, const char *v1, const char *r1,
00333 const char *e2, const char *v2, const char *r2);
00334
00335
00341 static inline gchar *
00342 cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str)
00343 {
00344 if (!str) return NULL;
00345 return g_string_chunk_insert(chunk, str);
00346 }
00347
00353 static inline gchar *
00354 cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str)
00355 {
00356 if (!str) return NULL;
00357 gchar *copy = g_string_chunk_insert(chunk, str);
00358 g_free(str);
00359 return copy;
00360 }
00361
00368 static inline gchar *
00369 cr_safe_string_chunk_insert_null(GStringChunk *chunk, const char *str)
00370 {
00371 if (!str || *str == '\0') return NULL;
00372 return g_string_chunk_insert(chunk, str);
00373 }
00374
00375
00381 static inline gchar *
00382 cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str)
00383 {
00384 if (!str) return NULL;
00385 return g_string_chunk_insert_const(chunk, str);
00386 }
00387
00388 static inline gboolean
00389 cr_key_file_get_boolean_default(GKeyFile *key_file,
00390 const gchar *group_name,
00391 const gchar *key,
00392 gboolean default_value,
00393 GError **error)
00394 {
00395 GError *tmp_err = NULL;
00396 gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err);
00397 if (tmp_err) {
00398 g_propagate_error(error, tmp_err);
00399 return default_value;
00400 }
00401 return ret;
00402 }
00403
00409 int
00410 cr_warning_cb(cr_XmlParserWarningType type,
00411 char *msg,
00412 void *cbdata,
00413 GError **err);
00414
00421 gboolean
00422 cr_write_to_file(GError **err, gchar *filename, const char *format, ...);
00423
00424 typedef enum {
00425 CR_CP_DEFAULT = (1<<0),
00427 CR_CP_RECURSIVE = (1<<1),
00429 CR_CP_PRESERVE_ALL = (1<<2),
00431 } cr_CpFlags;
00432
00440 gboolean
00441 cr_cp(const char *src,
00442 const char *dst,
00443 cr_CpFlags flags,
00444 const char *working_directory,
00445 GError **err);
00446
00447 typedef enum {
00448 CR_RM_DEFAULT = (1<<0),
00450 CR_RM_RECURSIVE = (1<<1),
00452 CR_RM_FORCE = (1<<2),
00454 } cr_RmFlags;
00455
00462 gboolean
00463 cr_rm(const char *path,
00464 cr_RmFlags flags,
00465 const char *working_dir,
00466 GError **err);
00467
00473 gchar *
00474 cr_append_pid_and_datetime(const char *str, const char *suffix);
00475
00484 gboolean
00485 cr_spawn_check_exit_status(gint exit_status, GError **error);
00486
00491 cr_NEVR *
00492 cr_str_to_nevr(const char *str);
00493
00497 void
00498 cr_nevr_free(cr_NEVR *nevr);
00499
00504 cr_NEVRA *
00505 cr_str_to_nevra(const char *str);
00506
00510 void
00511 cr_nevra_free(cr_NEVRA *nevra);
00512
00515 #ifdef __cplusplus
00516 }
00517 #endif
00518
00519 #endif