add _alpm_access() wrapper
[pacman-ng.git] / lib / libalpm / util.h
bloba75c5aae7397c209100bb37062f085bdbb5d52a1
1 /*
2 * util.h
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu>
8 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
9 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef _ALPM_UTIL_H
25 #define _ALPM_UTIL_H
27 #include "config.h"
29 #include "alpm_list.h"
30 #include "alpm.h"
31 #include "package.h" /* alpm_pkg_t */
32 #include "handle.h" /* alpm_handle_t */
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdarg.h>
37 #include <stddef.h> /* size_t */
38 #include <time.h>
39 #include <sys/stat.h> /* struct stat */
40 #include <archive.h> /* struct archive */
41 #include <math.h> /* fabs */
42 #include <float.h> /* DBL_EPSILON */
44 #ifdef ENABLE_NLS
45 #include <libintl.h> /* here so it doesn't need to be included elsewhere */
46 /* define _() as shortcut for gettext() */
47 #define _(str) dgettext ("libalpm", str)
48 #else
49 #define _(s) s
50 #endif
52 #define ALLOC_FAIL(s) do { fprintf(stderr, "alloc failure: could not allocate %zd bytes\n", s); } while(0)
54 #define MALLOC(p, s, action) do { p = calloc(1, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
55 #define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0)
56 /* This strdup macro is NULL safe- copying NULL will yield NULL */
57 #define STRDUP(r, s, action) do { if(s != NULL) { r = strdup(s); if(r == NULL) { ALLOC_FAIL(strlen(s)); action; } } else { r = NULL; } } while(0)
58 #define STRNDUP(r, s, l, action) do { if(s != NULL) { r = strndup(s, l); if(r == NULL) { ALLOC_FAIL(strlen(s)); action; } } else { r = NULL; } } while(0)
60 #define FREE(p) do { free(p); p = NULL; } while(0)
62 #define ASSERT(cond, action) do { if(!(cond)) { action; } } while(0)
64 #define RET_ERR_VOID(handle, err) do { \
65 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s : %s\n", err, __func__, alpm_strerror(err)); \
66 (handle)->pm_errno = (err); \
67 return; } while(0)
69 #define RET_ERR(handle, err, ret) do { \
70 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s : %s\n", err, __func__, alpm_strerror(err)); \
71 (handle)->pm_errno = (err); \
72 return (ret); } while(0)
74 #define DOUBLE_EQ(x, y) (fabs((x) - (y)) < DBL_EPSILON)
76 #define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = 0; } while(0)
78 /**
79 * Used as a buffer/state holder for _alpm_archive_fgets().
81 struct archive_read_buffer {
82 char *line;
83 char *line_offset;
84 size_t line_size;
85 size_t max_line_size;
87 char *block;
88 char *block_offset;
89 size_t block_size;
91 int ret;
94 int _alpm_makepath(const char *path);
95 int _alpm_makepath_mode(const char *path, mode_t mode);
96 int _alpm_copyfile(const char *src, const char *dest);
97 char *_alpm_strtrim(char *str);
98 int _alpm_unpack_single(alpm_handle_t *handle, const char *archive,
99 const char *prefix, const char *filename);
100 int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
101 alpm_list_t *list, int breakfirst);
102 int _alpm_rmrf(const char *path);
103 ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count);
104 int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args);
105 int _alpm_run_chroot(alpm_handle_t *handle, const char *path, char *const argv[]);
106 int _alpm_ldconfig(alpm_handle_t *handle);
107 int _alpm_str_cmp(const void *s1, const void *s2);
108 char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename);
109 const char *_alpm_filecache_setup(alpm_handle_t *handle);
110 int _alpm_lstat(const char *path, struct stat *buf);
111 int _alpm_test_md5sum(const char *filepath, const char *md5sum);
112 int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b);
113 int _alpm_splitname(const char *target, char **name, char **version,
114 unsigned long *name_hash);
115 unsigned long _alpm_hash_sdbm(const char *str);
116 long _alpm_parsedate(const char *line);
117 int _alpm_raw_cmp(const char *first, const char *second);
118 int _alpm_raw_ncmp(const char *first, const char *second, size_t max);
119 int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode);
121 #ifndef HAVE_STRSEP
122 char *strsep(char **, const char *);
123 #endif
125 #ifndef HAVE_STRNDUP
126 char *strndup(const char *s, size_t n);
127 #endif
129 /* check exported library symbols with: nm -C -D <lib> */
130 #define SYMEXPORT __attribute__((visibility("default")))
131 #define SYMHIDDEN __attribute__((visibility("internal")))
133 #define UNUSED __attribute__((unused))
135 #endif /* _ALPM_UTIL_H */
137 /* vim: set ts=2 sw=2 noet: */