1 /*
2 * backup.c
3 *
4 * Copyright (c) 2006-2018 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2005 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 Miklos Vajna <vmiklos@frugalware.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* libalpm */
28 #include "backup.h"
29 #include "alpm_list.h"
30 #include "log.h"
31 #include "util.h"
32
33 /* split a backup string "file\thash" into the relevant components */
_alpm_split_backup(const char * string,alpm_backup_t ** backup)34 int _alpm_split_backup(const char *string, alpm_backup_t **backup)
35 {
36 char *str, *ptr;
37
38 STRDUP(str, string, return -1);
39
40 /* tab delimiter */
41 ptr = str ? strchr(str, '\t') : NULL;
42 if(ptr == NULL) {
43 (*backup)->name = str;
44 (*backup)->hash = NULL;
45 return 0;
46 }
47 *ptr = '\0';
48 ptr++;
49 /* now str points to the filename and ptr points to the hash */
50 STRDUP((*backup)->name, str, FREE(str); return -1);
51 STRDUP((*backup)->hash, ptr, FREE((*backup)->name); FREE(str); return -1);
52 FREE(str);
53 return 0;
54 }
55
56 /* Look for a filename in a alpm_pkg_t.backup list. If we find it,
57 * then we return the full backup entry.
58 */
_alpm_needbackup(const char * file,alpm_pkg_t * pkg)59 alpm_backup_t *_alpm_needbackup(const char *file, alpm_pkg_t *pkg)
60 {
61 const alpm_list_t *lp;
62
63 if(file == NULL || pkg == NULL) {
64 return NULL;
65 }
66
67 for(lp = alpm_pkg_get_backup(pkg); lp; lp = lp->next) {
68 alpm_backup_t *backup = lp->data;
69
70 if(strcmp(file, backup->name) == 0) {
71 return backup;
72 }
73 }
74
75 return NULL;
76 }
77
_alpm_backup_free(alpm_backup_t * backup)78 void _alpm_backup_free(alpm_backup_t *backup)
79 {
80 ASSERT(backup != NULL, return);
81 FREE(backup->name);
82 FREE(backup->hash);
83 FREE(backup);
84 }
85
_alpm_backup_dup(const alpm_backup_t * backup)86 alpm_backup_t *_alpm_backup_dup(const alpm_backup_t *backup)
87 {
88 alpm_backup_t *newbackup;
89 CALLOC(newbackup, 1, sizeof(alpm_backup_t), return NULL);
90
91 STRDUP(newbackup->name, backup->name, goto error);
92 STRDUP(newbackup->hash, backup->hash, goto error);
93
94 return newbackup;
95
96 error:
97 free(newbackup->name);
98 free(newbackup);
99 return NULL;
100 }
101