createrepo_c library  0.7.5
C library for metadata manipulation
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
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_UNUSED(x) (void)(x)
41 
44 #define CR_STATICSTRLEN(s) (sizeof(s)/sizeof(s[0]))
45 
46 /* Length of static defined array.
47  */
48 #define CR_ARRAYLEN(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
49 
54 const char *cr_flag_to_str(gint64 flags);
55 
58 typedef struct {
59  char *epoch;
60  char *version;
61  char *release;
62 } cr_EVR;
63 
64 typedef struct {
65  char *name;
66  char *epoch;
67  char *version;
68  char *release;
69 } cr_NEVR;
70 
71 typedef struct {
72  char *name;
73  char *epoch;
74  char *version;
75  char *release;
76  char *arch;
77 } cr_NEVRA;
78 
82 struct cr_Version {
83  long version;
84  long release;
85  long patch;
86  char *suffix;
87 };
88 
89 
98 cr_EVR *cr_str_to_evr(const char *string, GStringChunk *chunk);
99 
106 void
107 cr_evr_free(cr_EVR *evr);
108 
114 static inline int cr_is_primary(const char *filename) {
115  if (!strncmp(filename, "/etc/", 5))
116  return 1;
117  if (!strcmp(filename, "/usr/lib/sendmail"))
118  return 1;
119  if (strstr(filename, "bin/"))
120  return 1;
121  return 0;
122 };
123 
127  unsigned int start;
128  unsigned int end;
129 };
130 
136 struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename,
137  GError **err);
138 
144 char *cr_get_filename(const char *filepath);
145 
154 int cr_download(CURL *handle,
155  const char *url,
156  const char *destination,
157  GError **err);
158 
165 int cr_copy_file(const char *src,
166  const char *dst,
167  GError **err);
168 
178 #define cr_compress_file(SRC, DST, COMTYPE, ERR) \
179  cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
180 
191 int cr_compress_file_with_stat(const char *src,
192  const char *dst,
193  cr_CompressionType comtype,
194  cr_ContentStat *stat,
195  GError **err);
196 
207 #define cr_decompress_file(SRC, DST, COMTYPE, ERR) \
208  cr_decompress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
209 
221 int cr_decompress_file_with_stat(const char *src,
222  const char *dst,
223  cr_CompressionType comtype,
224  cr_ContentStat *stat,
225  GError **err);
226 
233 int cr_better_copy_file(const char *src,
234  const char *dst,
235  GError **err);
236 
242 int cr_remove_dir(const char *path, GError **err);
243 
248 char *cr_normalize_dir_path(const char *path);
249 
254 struct cr_Version cr_str_to_version(const char *str);
255 
262 int cr_cmp_version_str(const char* str1, const char *str2);
263 
270 void cr_null_log_fn(const gchar *log_domain,
271  GLogLevelFlags log_level,
272  const gchar *message,
273  gpointer user_data);
274 
281 void cr_log_fn(const gchar *log_domain,
282  GLogLevelFlags log_level,
283  const gchar *message,
284  gpointer user_data);
285 
293 void cr_slist_free_full(GSList *list, GDestroyNotify free_f);
294 
302 void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f);
303 
313 cr_NEVRA *cr_split_rpm_filename(const char *filename);
314 
320 #define cr_cmp_nevra(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\
321  (B)->epoch, (B)->version, (B)->release))
322 
332 int cr_cmp_evr(const char *e1, const char *v1, const char *r1,
333  const char *e2, const char *v2, const char *r2);
334 
335 
341 static inline gchar *
342 cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str)
343 {
344  if (!str) return NULL;
345  return g_string_chunk_insert(chunk, str);
346 }
347 
353 static inline gchar *
354 cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str)
355 {
356  if (!str) return NULL;
357  gchar *copy = g_string_chunk_insert(chunk, str);
358  g_free(str);
359  return copy;
360 }
361 
368 static inline gchar *
369 cr_safe_string_chunk_insert_null(GStringChunk *chunk, const char *str)
370 {
371  if (!str || *str == '\0') return NULL;
372  return g_string_chunk_insert(chunk, str);
373 }
374 
375 
381 static inline gchar *
382 cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str)
383 {
384  if (!str) return NULL;
385  return g_string_chunk_insert_const(chunk, str);
386 }
387 
388 static inline gboolean
389 cr_key_file_get_boolean_default(GKeyFile *key_file,
390  const gchar *group_name,
391  const gchar *key,
392  gboolean default_value,
393  GError **error)
394 {
395  GError *tmp_err = NULL;
396  gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err);
397  if (tmp_err) {
398  g_propagate_error(error, tmp_err);
399  return default_value;
400  }
401  return ret;
402 }
403 
409 int
411  char *msg,
412  void *cbdata,
413  GError **err);
414 
421 gboolean
422 cr_write_to_file(GError **err, gchar *filename, const char *format, ...);
423 
424 typedef enum {
425  CR_CP_DEFAULT = (1<<0),
427  CR_CP_RECURSIVE = (1<<1),
431 } cr_CpFlags;
432 
440 gboolean
441 cr_cp(const char *src,
442  const char *dst,
443  cr_CpFlags flags,
444  const char *working_directory,
445  GError **err);
446 
447 typedef enum {
448  CR_RM_DEFAULT = (1<<0),
450  CR_RM_RECURSIVE = (1<<1),
452  CR_RM_FORCE = (1<<2),
454 } cr_RmFlags;
455 
462 gboolean
463 cr_rm(const char *path,
464  cr_RmFlags flags,
465  const char *working_dir,
466  GError **err);
467 
473 gchar *
474 cr_append_pid_and_datetime(const char *str, const char *suffix);
475 
484 gboolean
485 cr_spawn_check_exit_status(gint exit_status, GError **error);
486 
491 cr_NEVR *
492 cr_str_to_nevr(const char *str);
493 
497 void
498 cr_nevr_free(cr_NEVR *nevr);
499 
504 cr_NEVRA *
505 cr_str_to_nevra(const char *str);
506 
510 void
511 cr_nevra_free(cr_NEVRA *nevra);
512 
515 #ifdef __cplusplus
516 }
517 #endif
518 
519 #endif /* __C_CREATEREPOLIB_MISC_H__ */
gchar * cr_append_pid_and_datetime(const char *str, const char *suffix)
cr_NEVRA * cr_str_to_nevra(const char *str)
unsigned int end
Definition: misc.h:128
char * cr_normalize_dir_path(const char *path)
long version
Definition: misc.h:83
cr_CompressionType
void cr_nevra_free(cr_NEVRA *nevra)
int cr_cmp_evr(const char *e1, const char *v1, const char *r1, const char *e2, const char *v2, const char *r2)
int cr_warning_cb(cr_XmlParserWarningType type, char *msg, void *cbdata, GError **err)
struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename, GError **err)
long release
Definition: misc.h:84
cr_RmFlags
Definition: misc.h:447
unsigned int start
Definition: misc.h:127
cr_EVR * cr_str_to_evr(const char *string, GStringChunk *chunk)
cr_XmlParserWarningType
Definition: xml_parser.h:42
struct cr_Version cr_str_to_version(const char *str)
Definition: misc.h:64
Definition: misc.h:71
void cr_null_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
void cr_queue_free_full(GQueue *queue, GDestroyNotify free_f)
long patch
Definition: misc.h:85
int cr_cmp_version_str(const char *str1, const char *str2)
char * suffix
Definition: misc.h:86
void cr_evr_free(cr_EVR *evr)
gboolean cr_rm(const char *path, cr_RmFlags flags, const char *working_dir, GError **err)
void cr_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
cr_NEVRA * cr_split_rpm_filename(const char *filename)
int cr_copy_file(const char *src, const char *dst, GError **err)
gboolean cr_cp(const char *src, const char *dst, cr_CpFlags flags, const char *working_directory, GError **err)
gboolean cr_spawn_check_exit_status(gint exit_status, GError **error)
char * release
Definition: misc.h:61
int cr_better_copy_file(const char *src, const char *dst, GError **err)
int cr_compress_file_with_stat(const char *src, const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, GError **err)
cr_CpFlags
Definition: misc.h:424
char * version
Definition: misc.h:60
char * cr_get_filename(const char *filepath)
gboolean cr_write_to_file(GError **err, gchar *filename, const char *format,...)
int cr_remove_dir(const char *path, GError **err)
const char * cr_flag_to_str(gint64 flags)
cr_NEVR * cr_str_to_nevr(const char *str)
char * epoch
Definition: misc.h:59
Definition: misc.h:58
int cr_decompress_file_with_stat(const char *src, const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, GError **err)
int cr_download(CURL *handle, const char *url, const char *destination, GError **err)
void cr_nevr_free(cr_NEVR *nevr)
void cr_slist_free_full(GSList *list, GDestroyNotify free_f)