libalpm
Arch Linux Package Manager Library
|
00001 /* 00002 * util.h 00003 * 00004 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org> 00005 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org> 00006 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com> 00007 * Copyright (c) 2005 by Christian Hamar <krics@linuxforum.hu> 00008 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org> 00009 * Copyright (c) 2005, 2006 by Miklos Vajna <vmiklos@frugalware.org> 00010 * 00011 * This program is free software; you can redistribute it and/or modify 00012 * it under the terms of the GNU General Public License as published by 00013 * the Free Software Foundation; either version 2 of the License, or 00014 * (at your option) any later version. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00023 */ 00024 #ifndef _ALPM_UTIL_H 00025 #define _ALPM_UTIL_H 00026 00027 #include "alpm_list.h" 00028 #include "alpm.h" 00029 #include "package.h" /* alpm_pkg_t */ 00030 #include "handle.h" /* alpm_handle_t */ 00031 00032 #include <stdio.h> 00033 #include <string.h> 00034 #include <stdarg.h> 00035 #include <stddef.h> /* size_t */ 00036 #include <sys/types.h> 00037 #include <sys/stat.h> /* struct stat */ 00038 #include <math.h> /* fabs */ 00039 #include <float.h> /* DBL_EPSILON */ 00040 #include <fcntl.h> /* open, close */ 00041 00042 #include <archive.h> /* struct archive */ 00043 00044 #ifdef ENABLE_NLS 00045 #include <libintl.h> /* here so it doesn't need to be included elsewhere */ 00046 /* define _() as shortcut for gettext() */ 00047 #define _(str) dgettext ("libalpm", str) 00048 #else 00049 #define _(s) s 00050 #endif 00051 00052 #define ALLOC_FAIL(s) do { fprintf(stderr, "alloc failure: could not allocate %zd bytes\n", s); } while(0) 00053 00054 #define MALLOC(p, s, action) do { p = malloc(s); if(p == NULL) { ALLOC_FAIL(s); action; } } while(0) 00055 #define CALLOC(p, l, s, action) do { p = calloc(l, s); if(p == NULL) { ALLOC_FAIL(l * s); action; } } while(0) 00056 /* This strdup macro is NULL safe- copying NULL will yield NULL */ 00057 #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) 00058 #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) 00059 00060 #define FREE(p) do { free(p); p = NULL; } while(0) 00061 00062 #define ASSERT(cond, action) do { if(!(cond)) { action; } } while(0) 00063 00064 #define RET_ERR_VOID(handle, err) do { \ 00065 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s : %s\n", err, __func__, alpm_strerror(err)); \ 00066 (handle)->pm_errno = (err); \ 00067 return; } while(0) 00068 00069 #define RET_ERR(handle, err, ret) do { \ 00070 _alpm_log(handle, ALPM_LOG_DEBUG, "returning error %d from %s : %s\n", err, __func__, alpm_strerror(err)); \ 00071 (handle)->pm_errno = (err); \ 00072 return (ret); } while(0) 00073 00074 #define DOUBLE_EQ(x, y) (fabs((x) - (y)) < DBL_EPSILON) 00075 00076 #define CHECK_HANDLE(handle, action) do { if(!(handle)) { action; } (handle)->pm_errno = 0; } while(0) 00077 00078 /** Standard buffer size used throughout the library. */ 00079 #ifdef BUFSIZ 00080 #define ALPM_BUFFER_SIZE BUFSIZ 00081 #else 00082 #define ALPM_BUFFER_SIZE 8192 00083 #endif 00084 00085 #ifndef O_BINARY 00086 #define O_BINARY 0 00087 #endif 00088 00089 #define OPEN(fd, path, flags) do { fd = open(path, flags | O_BINARY); } while(fd == -1 && errno == EINTR) 00090 #define CLOSE(fd) do { int ret; do { ret = close(fd); } while(ret == -1 && errno == EINTR); } while(0) 00091 00092 /** 00093 * Used as a buffer/state holder for _alpm_archive_fgets(). 00094 */ 00095 struct archive_read_buffer { 00096 char *line; 00097 char *line_offset; 00098 size_t line_size; 00099 size_t max_line_size; 00100 00101 char *block; 00102 char *block_offset; 00103 size_t block_size; 00104 00105 int ret; 00106 }; 00107 00108 enum _alpm_csum { 00109 ALPM_CSUM_MD5, 00110 ALPM_CSUM_SHA256, 00111 }; 00112 00113 int _alpm_makepath(const char *path); 00114 int _alpm_makepath_mode(const char *path, mode_t mode); 00115 int _alpm_copyfile(const char *src, const char *dest); 00116 size_t _alpm_strip_newline(char *str); 00117 00118 int _alpm_open_archive(alpm_handle_t *handle, const char *path, 00119 struct stat *buf, struct archive **archive, alpm_errno_t error); 00120 int _alpm_unpack_single(alpm_handle_t *handle, const char *archive, 00121 const char *prefix, const char *filename); 00122 int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix, 00123 alpm_list_t *list, int breakfirst); 00124 int _alpm_rmrf(const char *path); 00125 ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count); 00126 int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args); 00127 int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]); 00128 int _alpm_ldconfig(alpm_handle_t *handle); 00129 int _alpm_str_cmp(const void *s1, const void *s2); 00130 char *_alpm_filecache_find(alpm_handle_t *handle, const char *filename); 00131 const char *_alpm_filecache_setup(alpm_handle_t *handle); 00132 int _alpm_lstat(const char *path, struct stat *buf); 00133 int _alpm_test_checksum(const char *filepath, const char *expected, enum _alpm_csum type); 00134 int _alpm_archive_fgets(struct archive *a, struct archive_read_buffer *b); 00135 int _alpm_splitname(const char *target, char **name, char **version, 00136 unsigned long *name_hash); 00137 unsigned long _alpm_hash_sdbm(const char *str); 00138 off_t _alpm_strtoofft(const char *line); 00139 alpm_time_t _alpm_parsedate(const char *line); 00140 int _alpm_raw_cmp(const char *first, const char *second); 00141 int _alpm_raw_ncmp(const char *first, const char *second, size_t max); 00142 int _alpm_access(alpm_handle_t *handle, const char *dir, const char *file, int amode); 00143 int _alpm_fnmatch(const void *pattern, const void *string); 00144 00145 #ifndef HAVE_STRSEP 00146 char *strsep(char **, const char *); 00147 #endif 00148 00149 #ifndef HAVE_STRNDUP 00150 char *strndup(const char *s, size_t n); 00151 #endif 00152 00153 /* check exported library symbols with: nm -C -D <lib> */ 00154 #define SYMEXPORT __attribute__((visibility("default"))) 00155 #define SYMHIDDEN __attribute__((visibility("internal"))) 00156 00157 #define UNUSED __attribute__((unused)) 00158 00159 #endif /* _ALPM_UTIL_H */ 00160 00161 /* vim: set ts=2 sw=2 noet: */