createrepo_c library  0.4.0
C library for metadata manipulation
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups 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 struct cr_EVR {
59  char *epoch;
60  char *version;
61  char *release;
62 };
63 
66 struct cr_NVREA {
67  char *name;
68  char *version;
69  char *release;
70  char *epoch;
71  char *arch;
72 };
73 
77 struct cr_Version {
78  long version;
79  long release;
80  long patch;
81  char *suffix;
82 };
83 
84 
93 struct cr_EVR cr_str_to_evr(const char *string, GStringChunk *chunk);
94 
100 static inline int cr_is_primary(const char *filename) {
101  if (!strncmp(filename, "/etc/", 5))
102  return 1;
103  if (!strcmp(filename, "/usr/lib/sendmail"))
104  return 1;
105  if (strstr(filename, "bin/"))
106  return 1;
107  return 0;
108 };
109 
113  unsigned int start;
114  unsigned int end;
115 };
116 
122 struct cr_HeaderRangeStruct cr_get_header_byte_range(const char *filename,
123  GError **err);
124 
130 char *cr_get_filename(const char *filepath);
131 
140 int cr_download(CURL *handle,
141  const char *url,
142  const char *destination,
143  GError **err);
144 
151 int cr_copy_file(const char *src,
152  const char *dst,
153  GError **err);
154 
164 #define cr_compress_file(SRC, DST, COMTYPE, ERR) \
165  cr_compress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
166 
177 int cr_compress_file_with_stat(const char *src,
178  const char *dst,
179  cr_CompressionType comtype,
180  cr_ContentStat *stat,
181  GError **err);
182 
193 #define cr_decompress_file(SRC, DST, COMTYPE, ERR) \
194  cr_decompress_file_with_stat(SRC, DST, COMTYPE, NULL, ERR)
195 
207 int cr_decompress_file_with_stat(const char *src,
208  const char *dst,
209  cr_CompressionType comtype,
210  cr_ContentStat *stat,
211  GError **err);
212 
219 int cr_better_copy_file(const char *src,
220  const char *dst,
221  GError **err);
222 
228 int cr_remove_dir(const char *path, GError **err);
229 
234 char *cr_normalize_dir_path(const char *path);
235 
240 struct cr_Version cr_str_to_version(const char *str);
241 
248 int cr_cmp_version_str(const char* str1, const char *str2);
249 
256 void cr_null_log_fn(const gchar *log_domain,
257  GLogLevelFlags log_level,
258  const gchar *message,
259  gpointer user_data);
260 
267 void cr_log_fn(const gchar *log_domain,
268  GLogLevelFlags log_level,
269  const gchar *message,
270  gpointer user_data);
271 
279 void cr_slist_free_full(GSList *list, GDestroyNotify free_f);
280 
285 struct cr_NVREA *cr_split_rpm_filename(const char *filename);
286 
290 void cr_nvrea_free(struct cr_NVREA *nvrea);
291 
297 #define cr_cmp_nvrea(A, B) (cr_cmp_evr((A)->epoch, (A)->version, (A)->release,\
298  (B)->epoch, (B)->version, (B)->release))
299 
309 int cr_cmp_evr(const char *e1, const char *v1, const char *r1,
310  const char *e2, const char *v2, const char *r2);
311 
312 
318 static inline gchar *
319 cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str)
320 {
321  if (!str) return NULL;
322  return g_string_chunk_insert(chunk, str);
323 }
324 
330 static inline gchar *
331 cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str)
332 {
333  if (!str) return NULL;
334  gchar *copy = g_string_chunk_insert(chunk, str);
335  g_free(str);
336  return copy;
337 }
338 
345 static inline gchar *
346 cr_safe_string_chunk_insert_null(GStringChunk *chunk, const char *str)
347 {
348  if (!str || *str == '\0') return NULL;
349  return g_string_chunk_insert(chunk, str);
350 }
351 
352 
358 static inline gchar *
359 cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str)
360 {
361  if (!str) return NULL;
362  return g_string_chunk_insert_const(chunk, str);
363 }
364 
365 static inline gboolean
366 cr_key_file_get_boolean_default(GKeyFile *key_file,
367  const gchar *group_name,
368  const gchar *key,
369  gboolean default_value,
370  GError **error)
371 {
372  GError *tmp_err = NULL;
373  gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err);
374  if (tmp_err) {
375  g_propagate_error(error, tmp_err);
376  return default_value;
377  }
378  return ret;
379 }
380 
386 int
388  char *msg,
389  void *cbdata,
390  GError **err);
391 
398 gboolean
399 cr_write_to_file(GError **err, gchar *filename, const char *format, ...);
400 
401 typedef enum {
402  CR_CP_DEFAULT = (1<<0),
404  CR_CP_RECURSIVE = (1<<1),
408 } cr_CpFlags;
409 
417 gboolean
418 cr_cp(const char *src,
419  const char *dst,
420  cr_CpFlags flags,
421  const char *working_directory,
422  GError **err);
423 
424 typedef enum {
425  CR_RM_DEFAULT = (1<<0),
427  CR_RM_RECURSIVE = (1<<1),
429  CR_RM_FORCE = (1<<2),
431 } cr_RmFlags;
432 
439 gboolean
440 cr_rm(const char *path,
441  cr_RmFlags flags,
442  const char *working_dir,
443  GError **err);
444 
450 gchar *
451 cr_append_pid_and_datetime(const char *str, const char *suffix);
452 
461 gboolean
462 cr_spawn_check_exit_status(gint exit_status, GError **error);
463 
466 #ifdef __cplusplus
467 }
468 #endif
469 
470 #endif /* __C_CREATEREPOLIB_MISC_H__ */
gchar * cr_append_pid_and_datetime(const char *str, const char *suffix)
char * epoch
Definition: misc.h:70
unsigned int end
Definition: misc.h:114
char * cr_normalize_dir_path(const char *path)
long version
Definition: misc.h:78
cr_CompressionType
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:79
cr_RmFlags
Definition: misc.h:424
struct cr_NVREA * cr_split_rpm_filename(const char *filename)
unsigned int start
Definition: misc.h:113
cr_XmlParserWarningType
Definition: xml_parser.h:40
struct cr_Version cr_str_to_version(const char *str)
void cr_null_log_fn(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
char * name
Definition: misc.h:67
long patch
Definition: misc.h:80
int cr_cmp_version_str(const char *str1, const char *str2)
struct cr_EVR cr_str_to_evr(const char *string, GStringChunk *chunk)
char * suffix
Definition: misc.h:81
char * arch
Definition: misc.h:71
Definition: misc.h:66
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)
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:401
char * version
Definition: misc.h:60
char * cr_get_filename(const char *filepath)
char * version
Definition: misc.h:68
gboolean cr_write_to_file(GError **err, gchar *filename, const char *format,...)
int cr_remove_dir(const char *path, GError **err)
char * release
Definition: misc.h:69
const char * cr_flag_to_str(gint64 flags)
char * epoch
Definition: misc.h:59
Definition: misc.h:58
void cr_nvrea_free(struct cr_NVREA *nvrea)
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_slist_free_full(GSList *list, GDestroyNotify free_f)