createrepo_c library 1.2.1 C library for metadata manipulation

misc.h
1 /* createrepo_c - Library of routines for manipulation with repodata
2 * Copyright (C) 2012 Tomas Mlcoch
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 * USA.
18 */
19
20 #ifndef __C_CREATEREPOLIB_MISC_H__
21 #define __C_CREATEREPOLIB_MISC_H__
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 #include <glib.h>
28 #include <string.h>
29 #include <gio/gio.h>
30 #include <curl/curl.h>
31 #include "compression_wrapper.h"
32 #include "xml_parser.h"
33
41 #define CR_STATICSTRLEN(s) (sizeof(s)/sizeof(s[0]))
42
43 /* Length of static defined array.
44 */
45 #define CR_ARRAYLEN(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
46
51 const char *cr_flag_to_str(gint64 flags);
52
55 typedef struct {
56 char *epoch;
57 char *version;
58 char *release;
59 } cr_EVR;
60
61 typedef struct {
62 char *name;
63 char *epoch;
64 char *version;
65 char *release;
66 } cr_NEVR;
67
68 typedef struct {
69 char *name;
70 char *epoch;
71 char *version;
72 char *release;
73 char *arch;
74 } cr_NEVRA;
75
79 struct cr_Version {
80 long major;
81 long minor;
82 long patch;
83 char *suffix;
84 };
85
86
95 cr_EVR *cr_str_to_evr(const char *string, GStringChunk *chunk);
96
103 void
104 cr_evr_free(cr_EVR *evr);
105
111 static inline int cr_is_primary(const char *filename) {
112 if (!strncmp(filename, "/etc/", 5))
113 return 1;
114 if (!strcmp(filename, "/usr/lib/sendmail"))
115 return 1;
116 if (strstr(filename, "bin/"))
117 return 1;
118 return 0;
119 }
120
123 struct cr_HeaderRangeStruct {
124 unsigned int start;
125 unsigned int end;
126 };
127
133 struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename,
134 GError **err);
135
141 char *cr_get_filename(const char *filepath);
142
148 char *cr_get_cleaned_href(const char *filepath);
149
158 int cr_download(CURL *handle,
159 const char *url,
160 const char *destination,
161 GError **err);
162
169 gboolean cr_copy_file(const char *src,
170 const char *dst,
171 GError **err);
172
184 #define cr_compress_file(SRC, DST, COMTYPE, ZCK_DICT_DIR, ZCK_AUTO_CHUNK, ERR) \
185 cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ZCK_DICT_DIR, \
186 ZCK_AUTO_CHUNK, ERR)
187
200 int cr_compress_file_with_stat(const char *src,
201 const char *dst,
202 cr_CompressionType comtype,
203 cr_ContentStat *stat,
204 const char *zck_dict_dir,
205 gboolean zck_auto_chunk,
206 GError **err);
207
218 #define cr_decompress_file(SRC, DST, COMTYPE, ERR) \
219 cr_decompress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
220
232 int cr_decompress_file_with_stat(const char *src,
233 const char *dst,
234 cr_CompressionType comtype,
235 cr_ContentStat *stat,
236 GError **err);
237
244 gboolean cr_better_copy_file(const char *src,
245 const char *dst,
246 GError **err);
247
253 int cr_remove_dir(const char *path, GError **err);
254
262 gboolean cr_move_recursive(const char *srcDir, const char *dstDir, GError **err);
263
268 char *cr_normalize_dir_path(const char *path);
269
274 struct cr_Version cr_str_to_version(const char *str);
275
282 int cr_cmp_version_str(const char* str1, const char *str2);
283
290 void cr_null_log_fn(const gchar *log_domain,
291 GLogLevelFlags log_level,
292 const gchar *message,
293 gpointer user_data);
294
301 void cr_log_fn(const gchar *log_domain,
302 GLogLevelFlags log_level,
303 const gchar *message,
304 gpointer user_data);
305
313 void cr_slist_free_full(GSList *list, GDestroyNotify free_f);
314
322 void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f);
323
333 cr_NEVRA *cr_split_rpm_filename(const char *filename);
334
340 #define cr_cmp_nevra(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\
341 (B)->epoch, (B)->version, (B)->release))
342
352 int cr_cmp_evr(const char *e1, const char *v1, const char *r1,
353 const char *e2, const char *v2, const char *r2);
354
355
361 static inline gchar *
362 cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str)
363 {
364 if (!str) return NULL;
365 return g_string_chunk_insert(chunk, str);
366 }
367
373 static inline gchar *
374 cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str)
375 {
376 if (!str) return NULL;
377 gchar *copy = g_string_chunk_insert(chunk, str);
378 g_free(str);
379 return copy;
380 }
381
388 static inline gchar *
389 cr_safe_string_chunk_insert_null(GStringChunk *chunk, const char *str)
390 {
391 if (!str || *str == '\0') return NULL;
392 return g_string_chunk_insert(chunk, str);
393 }
394
395
401 static inline gchar *
402 cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str)
403 {
404 if (!str) return NULL;
405 return g_string_chunk_insert_const(chunk, str);
406 }
407
408 static inline gboolean
409 cr_key_file_get_boolean_default(GKeyFile *key_file,
410 const gchar *group_name,
411 const gchar *key,
412 gboolean default_value,
413 GError **error)
414 {
415 GError *tmp_err = NULL;
416 gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err);
417 if (tmp_err) {
418 g_propagate_error(error, tmp_err);
419 return default_value;
420 }
421 return ret;
422 }
423
429 int
430 cr_warning_cb(cr_XmlParserWarningType type,
431 char *msg,
432 void *cbdata,
433 GError **err);
434
441 gboolean
442 cr_write_to_file(GError **err, gchar *filename, const char *format, ...);
443
444 typedef enum {
445 CR_CP_DEFAULT = (1<<0),
447 CR_CP_RECURSIVE = (1<<1),
449 CR_CP_PRESERVE_ALL = (1<<2),
451 } cr_CpFlags;
452
460 gboolean
461 cr_cp(const char *src,
462 const char *dst,
463 cr_CpFlags flags,
464 const char *working_directory,
465 GError **err) __attribute__ ((deprecated ("please use `cr_gio_cp` instead")));
466
474 gboolean
475 cr_gio_cp(GFile *src,
476 GFile *dst,
477 GFileCopyFlags flags,
478 GCancellable *cancellable,
479 GError **err);
480
481 typedef enum {
482 CR_RM_DEFAULT = (1<<0),
484 CR_RM_RECURSIVE = (1<<1),
486 CR_RM_FORCE = (1<<2),
488 } cr_RmFlags;
489
496 gboolean
497 cr_rm(const char *path,
498 cr_RmFlags flags,
499 const char *working_dir,
500 GError **err);
501
507 gchar *
508 cr_append_pid_and_datetime(const char *str, const char *suffix);
509
518 gboolean
519 cr_spawn_check_exit_status(gint exit_status, GError **error);
520
525 cr_NEVR *
526 cr_str_to_nevr(const char *str);
527
531 void
532 cr_nevr_free(cr_NEVR *nevr);
533
538 cr_NEVRA *
539 cr_str_to_nevra(const char *str);
540
544 void
545 cr_nevra_free(cr_NEVRA *nevra);
546
558 gboolean
559 cr_identical_files(const gchar *fn1,
560 const gchar *fn2,
561 gboolean *identical,
562 GError **err);
563
567 gchar *
568 cr_cut_dirs(gchar *path, gint cut_dirs);
569
573 const gchar *
574 cr_version_string_with_features(void);
575
589 gchar *
590 cr_get_dict_file(const gchar *dir,
591 const gchar *file);
592
595 #ifdef __cplusplus
596 }
597 #endif
598
599 #endif /* __C_CREATEREPOLIB_MISC_H__ */
cr_cut_dirs
gchar * cr_cut_dirs(gchar *path, gint cut_dirs)
cr_download
int cr_download(CURL *handle, const char *url, const char *destination, GError **err)
cr_copy_file
gboolean cr_copy_file(const char *src, const char *dst, GError **err)
cr_flag_to_str
const char * cr_flag_to_str(gint64 flags)
cr_queue_free_full
void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f)
cr_EVR::epoch
char * epoch
Definition: misc.h:56
cr_compress_file_with_stat
int cr_compress_file_with_stat(const char *src, const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, const char *zck_dict_dir, gboolean zck_auto_chunk, GError **err)
cr_HeaderRangeStruct
Definition: misc.h:123
cr_cmp_version_str
int cr_cmp_version_str(const char *str1, const char *str2)
cr_nevr_free
void cr_nevr_free(cr_NEVR *nevr)
CR_RM_RECURSIVE
@ CR_RM_RECURSIVE
Definition: misc.h:484
cr_Version::minor
long minor
Definition: misc.h:81
cr_identical_files
gboolean cr_identical_files(const gchar *fn1, const gchar *fn2, gboolean *identical, GError **err)
cr_cp
gboolean 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")))
cr_log_fn
void cr_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
cr_move_recursive
gboolean cr_move_recursive(const char *srcDir, const char *dstDir, GError **err)
cr_NEVR
Definition: misc.h:61
cr_get_cleaned_href
char * cr_get_cleaned_href(const char *filepath)
cr_get_header_byte_range
struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename, GError **err)
cr_gio_cp
gboolean cr_gio_cp(GFile *src, GFile *dst, GFileCopyFlags flags, GCancellable *cancellable, GError **err)
cr_evr_free
void cr_evr_free(cr_EVR *evr)
cr_decompress_file_with_stat
int cr_decompress_file_with_stat(const char *src, const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, GError **err)
cr_str_to_version
struct cr_Version cr_str_to_version(const char *str)
cr_warning_cb
int cr_warning_cb(cr_XmlParserWarningType type, char *msg, void *cbdata, GError **err)
cr_RmFlags
cr_RmFlags
Definition: misc.h:481
cr_HeaderRangeStruct::end
unsigned int end
Definition: misc.h:125
cr_null_log_fn
void cr_null_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
cr_EVR::release
char * release
Definition: misc.h:58
cr_Version::suffix
char * suffix
Definition: misc.h:83
cr_EVR
Definition: misc.h:55
CR_CP_RECURSIVE
@ CR_CP_RECURSIVE
Definition: misc.h:447
cr_CpFlags
cr_CpFlags
Definition: misc.h:444
cr_HeaderRangeStruct::start
unsigned int start
Definition: misc.h:124
cr_XmlParserWarningType
cr_XmlParserWarningType
Definition: xml_parser.h:42
cr_cmp_evr
int cr_cmp_evr(const char *e1, const char *v1, const char *r1, const char *e2, const char *v2, const char *r2)
cr_split_rpm_filename
cr_NEVRA * cr_split_rpm_filename(const char *filename)
CR_RM_DEFAULT
@ CR_RM_DEFAULT
Definition: misc.h:482
CR_CP_DEFAULT
@ CR_CP_DEFAULT
Definition: misc.h:445
cr_spawn_check_exit_status
gboolean cr_spawn_check_exit_status(gint exit_status, GError **error)
CR_CP_PRESERVE_ALL
@ CR_CP_PRESERVE_ALL
Definition: misc.h:449
cr_append_pid_and_datetime
gchar * cr_append_pid_and_datetime(const char *str, const char *suffix)
cr_Version::major
long major
Definition: misc.h:80
cr_normalize_dir_path
char * cr_normalize_dir_path(const char *path)
cr_EVR::version
char * version
Definition: misc.h:57
cr_NEVRA
Definition: misc.h:68
cr_better_copy_file
gboolean cr_better_copy_file(const char *src, const char *dst, GError **err)
cr_str_to_evr
cr_EVR * cr_str_to_evr(const char *string, GStringChunk *chunk)
cr_Version
Definition: misc.h:79
cr_remove_dir
int cr_remove_dir(const char *path, GError **err)
cr_CompressionType
cr_CompressionType
Definition: compression_wrapper.h:37
cr_version_string_with_features
const gchar * cr_version_string_with_features(void)
cr_rm
gboolean cr_rm(const char *path, cr_RmFlags flags, const char *working_dir, GError **err)
cr_write_to_file
gboolean cr_write_to_file(GError **err, gchar *filename, const char *format,...)
cr_slist_free_full
void cr_slist_free_full(GSList *list, GDestroyNotify free_f)
cr_get_filename
char * cr_get_filename(const char *filepath)
cr_str_to_nevra
cr_NEVRA * cr_str_to_nevra(const char *str)
CR_RM_FORCE
@ CR_RM_FORCE
Definition: misc.h:486
cr_str_to_nevr
cr_NEVR * cr_str_to_nevr(const char *str)
cr_get_dict_file
gchar * cr_get_dict_file(const gchar *dir, const gchar *file)
cr_ContentStat
Definition: compression_wrapper.h:59
cr_Version::patch
long patch
Definition: misc.h:82
cr_nevra_free
void cr_nevra_free(cr_NEVRA *nevra)