createrepo_c library  0.17.5
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 <curl/curl.h>
30 #include "compression_wrapper.h"
31 #include "xml_parser.h"
32 
40 #define CR_STATICSTRLEN(s) (sizeof(s)/sizeof(s[0]))
41 
42 /* Length of static defined array.
43  */
44 #define CR_ARRAYLEN(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
45 
50 const char *cr_flag_to_str(gint64 flags);
51 
54 typedef struct {
55  char *epoch;
56  char *version;
57  char *release;
58 } cr_EVR;
59 
60 typedef struct {
61  char *name;
62  char *epoch;
63  char *version;
64  char *release;
65 } cr_NEVR;
66 
67 typedef struct {
68  char *name;
69  char *epoch;
70  char *version;
71  char *release;
72  char *arch;
73 } cr_NEVRA;
74 
78 struct cr_Version {
79  long major;
80  long minor;
81  long patch;
82  char *suffix;
83 };
84 
85 
94 cr_EVR *cr_str_to_evr(const char *string, GStringChunk *chunk);
95 
102 void
104 
110 static inline int cr_is_primary(const char *filename) {
111  if (!strncmp(filename, "/etc/", 5))
112  return 1;
113  if (!strcmp(filename, "/usr/lib/sendmail"))
114  return 1;
115  if (strstr(filename, "bin/"))
116  return 1;
117  return 0;
118 };
119 
123  unsigned int start;
124  unsigned int end;
125 };
126 
132 struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename,
133  GError **err);
134 
140 char *cr_get_filename(const char *filepath);
141 
147 char *cr_get_cleaned_href(const char *filepath);
148 
157 int cr_download(CURL *handle,
158  const char *url,
159  const char *destination,
160  GError **err);
161 
168 gboolean cr_copy_file(const char *src,
169  const char *dst,
170  GError **err);
171 
183 #define cr_compress_file(SRC, DST, COMTYPE, ZCK_DICT_DIR, ZCK_AUTO_CHUNK, ERR) \
184  cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ZCK_DICT_DIR, \
185  ZCK_AUTO_CHUNK, ERR)
186 
199 int cr_compress_file_with_stat(const char *src,
200  const char *dst,
201  cr_CompressionType comtype,
202  cr_ContentStat *stat,
203  const char *zck_dict_dir,
204  gboolean zck_auto_chunk,
205  GError **err);
206 
217 #define cr_decompress_file(SRC, DST, COMTYPE, ERR) \
218  cr_decompress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
219 
231 int cr_decompress_file_with_stat(const char *src,
232  const char *dst,
233  cr_CompressionType comtype,
234  cr_ContentStat *stat,
235  GError **err);
236 
243 gboolean cr_better_copy_file(const char *src,
244  const char *dst,
245  GError **err);
246 
252 int cr_remove_dir(const char *path, GError **err);
253 
258 char *cr_normalize_dir_path(const char *path);
259 
264 struct cr_Version cr_str_to_version(const char *str);
265 
272 int cr_cmp_version_str(const char* str1, const char *str2);
273 
280 void cr_null_log_fn(const gchar *log_domain,
281  GLogLevelFlags log_level,
282  const gchar *message,
283  gpointer user_data);
284 
291 void cr_log_fn(const gchar *log_domain,
292  GLogLevelFlags log_level,
293  const gchar *message,
294  gpointer user_data);
295 
303 void cr_slist_free_full(GSList *list, GDestroyNotify free_f);
304 
312 void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f);
313 
323 cr_NEVRA *cr_split_rpm_filename(const char *filename);
324 
330 #define cr_cmp_nevra(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\
331  (B)->epoch, (B)->version, (B)->release))
332 
342 int cr_cmp_evr(const char *e1, const char *v1, const char *r1,
343  const char *e2, const char *v2, const char *r2);
344 
345 
351 static inline gchar *
352 cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str)
353 {
354  if (!str) return NULL;
355  return g_string_chunk_insert(chunk, str);
356 }
357 
363 static inline gchar *
364 cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str)
365 {
366  if (!str) return NULL;
367  gchar *copy = g_string_chunk_insert(chunk, str);
368  g_free(str);
369  return copy;
370 }
371 
378 static inline gchar *
379 cr_safe_string_chunk_insert_null(GStringChunk *chunk, const char *str)
380 {
381  if (!str || *str == '\0') return NULL;
382  return g_string_chunk_insert(chunk, str);
383 }
384 
385 
391 static inline gchar *
392 cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str)
393 {
394  if (!str) return NULL;
395  return g_string_chunk_insert_const(chunk, str);
396 }
397 
398 static inline gboolean
399 cr_key_file_get_boolean_default(GKeyFile *key_file,
400  const gchar *group_name,
401  const gchar *key,
402  gboolean default_value,
403  GError **error)
404 {
405  GError *tmp_err = NULL;
406  gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err);
407  if (tmp_err) {
408  g_propagate_error(error, tmp_err);
409  return default_value;
410  }
411  return ret;
412 }
413 
419 int
421  char *msg,
422  void *cbdata,
423  GError **err);
424 
431 gboolean
432 cr_write_to_file(GError **err, gchar *filename, const char *format, ...);
433 
434 typedef enum {
435  CR_CP_DEFAULT = (1<<0),
437  CR_CP_RECURSIVE = (1<<1),
439  CR_CP_PRESERVE_ALL = (1<<2),
441 } cr_CpFlags;
442 
450 gboolean
451 cr_cp(const char *src,
452  const char *dst,
453  cr_CpFlags flags,
454  const char *working_directory,
455  GError **err);
456 
457 typedef enum {
458  CR_RM_DEFAULT = (1<<0),
460  CR_RM_RECURSIVE = (1<<1),
462  CR_RM_FORCE = (1<<2),
464 } cr_RmFlags;
465 
472 gboolean
473 cr_rm(const char *path,
474  cr_RmFlags flags,
475  const char *working_dir,
476  GError **err);
477 
483 gchar *
484 cr_append_pid_and_datetime(const char *str, const char *suffix);
485 
494 gboolean
495 cr_spawn_check_exit_status(gint exit_status, GError **error);
496 
501 cr_NEVR *
502 cr_str_to_nevr(const char *str);
503 
507 void
509 
514 cr_NEVRA *
515 cr_str_to_nevra(const char *str);
516 
520 void
522 
534 gboolean
535 cr_identical_files(const gchar *fn1,
536  const gchar *fn2,
537  gboolean *identical,
538  GError **err);
539 
543 gchar *
544 cr_cut_dirs(gchar *path, gint cut_dirs);
545 
549 const gchar *
551 
565 gchar *
566 cr_get_dict_file(const gchar *dir,
567  const gchar *file);
568 
571 #ifdef __cplusplus
572 }
573 #endif
574 
575 #endif /* __C_CREATEREPOLIB_MISC_H__ */
cr_CompressionType
cr_NEVRA * cr_split_rpm_filename(const char *filename)
int cr_cmp_version_str(const char *str1, const char *str2)
gboolean cr_rm(const char *path, cr_RmFlags flags, const char *working_dir, GError **err)
gboolean cr_write_to_file(GError **err, gchar *filename, const char *format,...)
void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f)
gchar * cr_get_dict_file(const gchar *dir, const gchar *file)
void cr_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
gboolean cr_cp(const char *src, const char *dst, cr_CpFlags flags, const char *working_directory, GError **err)
char * cr_get_filename(const char *filepath)
const gchar * cr_version_string_with_features(void)
void cr_evr_free(cr_EVR *evr)
cr_EVR * cr_str_to_evr(const char *string, GStringChunk *chunk)
gboolean cr_copy_file(const char *src, const char *dst, GError **err)
int cr_download(CURL *handle, const char *url, const char *destination, GError **err)
struct cr_Version cr_str_to_version(const char *str)
void cr_nevr_free(cr_NEVR *nevr)
char * cr_get_cleaned_href(const char *filepath)
void cr_nevra_free(cr_NEVRA *nevra)
cr_NEVR * cr_str_to_nevr(const char *str)
cr_RmFlags
Definition: misc.h:457
int cr_warning_cb(cr_XmlParserWarningType type, char *msg, void *cbdata, GError **err)
gboolean cr_better_copy_file(const char *src, const char *dst, GError **err)
char * cr_normalize_dir_path(const char *path)
gchar * cr_cut_dirs(gchar *path, gint cut_dirs)
void cr_null_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
cr_NEVRA * cr_str_to_nevra(const char *str)
gchar * cr_append_pid_and_datetime(const char *str, const char *suffix)
cr_CpFlags
Definition: misc.h:434
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)
void cr_slist_free_full(GSList *list, GDestroyNotify free_f)
int cr_remove_dir(const char *path, GError **err)
gboolean cr_spawn_check_exit_status(gint exit_status, GError **error)
int cr_decompress_file_with_stat(const char *src, const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, GError **err)
gboolean cr_identical_files(const gchar *fn1, const gchar *fn2, gboolean *identical, GError **err)
struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename, GError **err)
int cr_cmp_evr(const char *e1, const char *v1, const char *r1, const char *e2, const char *v2, const char *r2)
const char * cr_flag_to_str(gint64 flags)
@ CR_RM_FORCE
Definition: misc.h:462
@ CR_RM_DEFAULT
Definition: misc.h:458
@ CR_RM_RECURSIVE
Definition: misc.h:460
@ CR_CP_RECURSIVE
Definition: misc.h:437
@ CR_CP_PRESERVE_ALL
Definition: misc.h:439
@ CR_CP_DEFAULT
Definition: misc.h:435
cr_XmlParserWarningType
Definition: xml_parser.h:42
Definition: misc.h:54
char * version
Definition: misc.h:56
char * epoch
Definition: misc.h:55
char * release
Definition: misc.h:57
unsigned int start
Definition: misc.h:123
unsigned int end
Definition: misc.h:124
Definition: misc.h:67
Definition: misc.h:60
long minor
Definition: misc.h:80
char * suffix
Definition: misc.h:82
long major
Definition: misc.h:79
long patch
Definition: misc.h:81