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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#define _POSIX_C_SOURCE 200809L
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <xbps.h>
#include <stdlib.h>
#include <limits.h>
/* random macro to log function names because why not */
#define LOGFV(s) printf("[verbose] calling %s\n", s);
void usage(void) {
printf("Usage: mbpk [OPTIONS]\n\n");
printf("TODO: Add OPTIONS list here");
}
int main(int argc, char **argv) {
/* parse arguments */
int verbose = 0;
int update_repos = 0;
int install_pkg = 0;
int remove_pkg = 0;
const char *pkgname = NULL;
if (argc < 2) {
usage();
return 1;
}
int opt;
while ((opt = getopt(argc, argv, "Si:r:vh")) != -1) {
switch (opt) {
case 'S': update_repos = 1; break;
case 'i': pkgname = optarg; install_pkg = 1; break;
case 'r': pkgname = optarg; remove_pkg = 1; break;
case 'v': verbose = 1; break;
case 'h': usage(); return 0;
default: usage(); return 1;
}
}
/* xbps init stuff */
struct xbps_handle x;
memset(&x, 0, sizeof(x));
const char *home = getenv("HOME");
if (home == NULL) {
struct passwd *pw = getpwuid(getuid());
if (pw != NULL) home = pw->pw_dir;
}
if (home == NULL) {
fprintf(stderr, "cant find home directory\n");
return 1;
}
if (verbose) printf("[verbose] setting x.rootdir to $home/.local/mbpk/prefix\n");
snprintf(x.rootdir, sizeof(x.rootdir), "%s/.local/mbpk/prefix", home);
if (verbose) printf("[verbose] setting x.cachedir to $home/.local/mbpk/cache\n");
snprintf(x.cachedir, sizeof(x.cachedir), "%s/.local/mbpk/cache", home);
if (verbose) printf("[verbose] setting x.confdir to $home/.local/mbpk/conf.d\n");
snprintf(x.confdir, sizeof(x.confdir), "%s/.local/mbpk/conf.d", home);
if (verbose) printf("[verbose] setting x.sysconfdir to $home/.local/mbpk/sysconf.d\n");
snprintf(x.sysconfdir, sizeof(x.sysconfdir), "%s/.local/mbpk/sysconf.d", home);
if (verbose) {
x.flags |= XBPS_FLAG_VERBOSE;
x.flags |= XBPS_FLAG_DEBUG;
}
if (verbose) LOGFV("xbps_init()");
int r = xbps_init(&x);
if (r != 0) {
fprintf(stderr, "xbps_init failed: %s\n", strerror(r));
return r;
}
/* actually do stuff */
if (update_repos) {
printf("[*] syncing repositories...\n");
if (verbose) LOGFV("xbps_rpool_sync()");
r = xbps_rpool_sync(&x, NULL);
if (r != 0) {
fprintf(stderr, "sync failed: %s\n", strerror(r));
return r;
}
printf("[*] done!\n");
}
if (pkgname) {
if (install_pkg) {
if (verbose) LOGFV("xbps_rpool_get_pkg()");
xbps_dictionary_t pkgd = xbps_rpool_get_pkg(&x, pkgname);
if (pkgd == NULL) {
fprintf(stderr, "ERROR: package %s not found in repository pool :c\n", pkgname);
return 1;
}
if (verbose) LOGFV("xbps_object_release()");
xbps_object_release(pkgd);
printf("[*] adding package %s to transaction...\n", pkgname);
if (verbose) LOGFV("xbps_transaction_install_pkg()");
r = xbps_transaction_install_pkg(&x, pkgname, false);
if (r != 0) {
fprintf(stderr, "failed to add %s to transaction: %s\n", pkgname, strerror(r));
return r;
}
printf("[*] preparing transaction...\n");
if (verbose) LOGFV("xbps_transaction_prepare()");
r = xbps_transaction_prepare(&x);
if (r != 0) {
fprintf(stderr, "failed to prepare transaction: %s", strerror(r));
return r;
}
printf("[*] committing...\n");
if (verbose) LOGFV("xbps_transaction_commit()");
r = xbps_transaction_commit(&x);
if (r != 0) {
fprintf(stderr, "failed to commit transaction: %s", strerror(r));
return r;
}
printf("[*] done!");
}
if (remove_pkg) {
}
}
return 0;
}
|