libalpm
Arch Linux Package Manager Library
util.h
Go to the documentation of this file.
1/*
2 * util.h
3 *
4 * Copyright (c) 2006-2024 Pacman Development Team <pacman-dev@lists.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>
10 *
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.
15 *
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.
20 *
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/>.
23 */
24#ifndef ALPM_UTIL_H
25#define ALPM_UTIL_H
26
27#include "alpm_list.h"
28#include "alpm.h"
29#include "package.h" /* alpm_pkg_t */
30#include "handle.h" /* alpm_handle_t */
31#include "util-common.h"
32
33#include <stdio.h>
34#include <string.h>
35#include <stdarg.h>
36#include <stddef.h> /* size_t */
37#include <sys/types.h>
38#include <math.h> /* fabs */
39#include <float.h> /* DBL_EPSILON */
40#include <fcntl.h> /* open, close */
41
42#include <archive.h> /* struct archive */
43
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) (char *)s
50#endif
51
52void _alpm_alloc_fail(size_t size);
53
54#define MALLOC(p, s, action) do { p = malloc(s); if(p == NULL) { _alpm_alloc_fail(s); action; } } while(0)
55#define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { _alpm_alloc_fail(l * s); action; } } while(0)
56#define REALLOC(p, s, action) do { void* np = realloc(p, s); if(np == NULL) { _alpm_alloc_fail(s); action; } else { p = np; } } while(0)
57/* This strdup macro is NULL safe- copying NULL will yield NULL */
58#define STRDUP(r, s, action) do { if(s != NULL) { r = strdup(s); if(r == NULL) { _alpm_alloc_fail(strlen(s)); action; } } else { r = NULL; } } while(0)
59#define STRNDUP(r, s, l, action) do { if(s != NULL) { r = strndup(s, l); if(r == NULL) { _alpm_alloc_fail(l); action; } } else { r = NULL; } } while(0)
60
61#define FREE(p) do { free(p); p = NULL; } while(0)
62
63#define ASSERT(cond, action) do { if(!(cond)) { action; } } while(0)
64
65#define RET_ERR_VOID(handle, err) do { \
66 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s (%s: %d) : %s\n", err, __func__, __FILE__, __LINE__, alpm_strerror(err)); \
67 (handle)->pm_errno = (err); \
68 return; } while(0)
69
70#define RET_ERR(handle, err, ret) do { \
71 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s (%s: %d) : %s\n", err, __func__, __FILE__, __LINE__, alpm_strerror(err)); \
72 (handle)->pm_errno = (err); \
73 return (ret); } while(0)
74
75#define GOTO_ERR(handle, err, label) do { \
76 _alpm_log(handle, ALPM_LOG_DEBUG, "got error %d at %s (%s: %d) : %s\n", err, __func__, __FILE__, __LINE__, alpm_strerror(err)); \
77 (handle)->pm_errno = (err); \
78 goto label; } while(0)
79
80#define RET_ERR_ASYNC_SAFE(handle, err, ret) do { \
81 (handle)->pm_errno = (err); \
82 return (ret); } while(0)
83
84#define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = ALPM_ERR_OK; } while(0)
85
86/** Standard buffer size used throughout the library. */
87#ifdef BUFSIZ
88#define ALPM_BUFFER_SIZE BUFSIZ
89#else
90#define ALPM_BUFFER_SIZE 8192
91#endif
92
93#ifndef O_BINARY
94#define O_BINARY 0
95#endif
96
97#define OPEN(fd, path, flags) do { fd = open(path, flags | O_BINARY); } while(fd == -1 && errno == EINTR)
98
99/**
100 * Used as a buffer/state holder for _alpm_archive_fgets().
101 */
103 char *line;
105 size_t line_size;
108
109 char *block;
112
113 int ret;
114};
115
116int _alpm_makepath(const char *path);
117int _alpm_makepath_mode(const char *path, mode_t mode);
118int _alpm_copyfile(const char *src, const char *dest);
119char *_alpm_get_fullpath(const char *path, const char *filename, const char *suffix);
120size_t _alpm_strip_newline(char *str, size_t len);
121
122int _alpm_open_archive(alpm_handle_t *handle, const char *path,
123 struct stat *buf, struct archive **archive, alpm_errno_t error);
124int _alpm_unpack_single(alpm_handle_t *handle, const char *archive,
125 const char *prefix, const char *filename);
126int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
127 alpm_list_t *list, int breakfirst);
128
129ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count);
130
131typedef ssize_t (*_alpm_cb_io)(void *buf, ssize_t len, void *ctx);
132
133void _alpm_reset_signals(void);
134int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[],
135 _alpm_cb_io in_cb, void *in_ctx);
136int _alpm_ldconfig(alpm_handle_t *handle);
137int _alpm_str_cmp(const void *s1, const void *s2);
138char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename);
139/* Checks whether a file exists in cache */
140int _alpm_filecache_exists(alpm_handle_t *handle, const char *filename);
141const char *_alpm_filecache_setup(alpm_handle_t *handle);
142char *_alpm_temporary_download_dir_setup(const char *dir, const char *user);
143void _alpm_remove_temporary_download_dir(const char *dir);
144
145/* Unlike many uses of alpm_pkgvalidation_t, _alpm_test_checksum expects
146 * an enum value rather than a bitfield. */
147int _alpm_test_checksum(const char *filepath, const char *expected, alpm_pkgvalidation_t type);
148int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b);
149int _alpm_splitname(const char *target, char **name, char **version,
150 unsigned long *name_hash);
151unsigned long _alpm_hash_sdbm(const char *str);
152off_t _alpm_strtoofft(const char *line);
153alpm_time_t _alpm_parsedate(const char *line);
154int _alpm_raw_cmp(const char *first, const char *second);
155int _alpm_raw_ncmp(const char *first, const char *second, size_t max);
156int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode);
157int _alpm_fnmatch_patterns(alpm_list_t *patterns, const char *string);
158int _alpm_fnmatch(const void *pattern, const void *string);
159void *_alpm_realloc(void **data, size_t *current, const size_t required);
160void *_alpm_greedy_grow(void **data, size_t *current, const size_t required);
161alpm_errno_t _alpm_read_file(const char *filepath, unsigned char **data, size_t *data_len);
162
163#ifndef HAVE_STRSEP
164char *strsep(char **, const char *);
165#endif
166
167/* check exported library symbols with: nm -C -D <lib> */
168#define SYMEXPORT __attribute__((visibility("default")))
169
170#define UNUSED __attribute__((unused))
171
172#endif /* ALPM_UTIL_H */
alpm_errno_t
libalpm's error type
Definition alpm.h:205
A doubly linked list.
Definition alpm_list.h:51
int64_t alpm_time_t
The time type used by libalpm.
Definition alpm.h:126
alpm_pkgvalidation_t
Method used to validate a package.
Definition alpm.h:2349
#define error(...)
size_t block_size
Definition util.h:111
int ret
Definition util.h:113
char * block_offset
Definition util.h:110
size_t line_size
Definition util.h:105
char * line
Definition util.h:103
size_t real_line_size
Definition util.h:107
size_t max_line_size
Definition util.h:106
char * block
Definition util.h:109
char * line_offset
Definition util.h:104
char * strsep(char **, const char *)
Extracts tokens from a string.
Definition util.c:74
Used as a buffer/state holder for _alpm_archive_fgets().
Definition util.h:102