-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacback.c
91 lines (83 loc) · 2.18 KB
/
pacback.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* pacback.c
*
* Print a list of the pacman backup files, along with the corresponding
* package name and md5sum.
*
* Author: Alastair Hughes
* Contact: <hobbitalastair at yandex dot com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alpm.h>
#include <alpm_list.h>
/* Generate the name of the given package file.
* This needs to be freed afterwards!
*/
char *get_pkg_name(alpm_pkg_t * pkg)
{
const char *pkgname = alpm_pkg_get_name(pkg);
const char *version = alpm_pkg_get_version(pkg);
const char *arch = alpm_pkg_get_arch(pkg);
const char *ext = PKGEXT;
/* Allocate enough memory for the name and delimiters */
char *name = malloc(strlen(pkgname) + 1 + strlen(version) + 1 +
strlen(arch) + strlen(ext) + 1);
size_t offset = 0;
while (*pkgname) {
name[offset] = *pkgname;
offset++;
pkgname++;
}
name[offset] = '-';
offset++;
while (*version) {
name[offset] = *version;
offset++;
version++;
}
name[offset] = '-';
offset++;
while (*arch) {
name[offset] = *arch;
offset++;
arch++;
}
while (*ext) {
name[offset] = *ext;
offset++;
ext++;
}
name[offset] = '\0';
return name;
}
int main()
{
alpm_errno_t err = 0;
alpm_handle_t *alpm_handle = alpm_initialize(ROOTDIR, DBPATH, &err);
if (err != 0) {
fprintf(stderr, "Failed to initialise alpm: %s\n",
alpm_strerror(err));
return EXIT_FAILURE;
}
alpm_db_t *db = alpm_get_localdb(alpm_handle);
alpm_list_t *pkgs;
alpm_list_t *backups;
alpm_pkg_t *pkg;
alpm_backup_t *backup;
char *pkgname;
for (pkgs = alpm_db_get_pkgcache(db); pkgs;
pkgs = alpm_list_next(pkgs)) {
pkg = pkgs->data;
backups = alpm_pkg_get_backup(pkg);
if (backups) {
pkgname = get_pkg_name(pkg);
for (; backups; backups = alpm_list_next(backups)) {
backup = backups->data;
printf("%s %s %s\n", backup->name, backup->hash, pkgname);
}
free(pkgname);
}
}
return EXIT_SUCCESS;
}