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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#include <fcntl.h>
#include "anna.h"
int get_lowmem_level (void) {
int l;
l=open(LOWMEM_STATUS_FILE, O_RDONLY);
if (l != -1) {
char buf[2];
read(l, buf, 1);
close(l);
return atoi(buf);
}
return 0;
}
int is_queued(di_package *package) {
FILE *fp;
char buf[1024];
if ((fp = fopen("/var/lib/anna-install/queue", "r")) != NULL) {
while (fgets(buf, sizeof(buf), fp)) {
buf[strlen(buf) - 1] = '\0';
if (strcmp(buf, package->package) == 0) {
fclose(fp);
return 1;
}
}
fclose(fp);
}
return 0;
}
/* This function checks if p is in the given list of installed packages
* and compares versions. */
bool is_installed(di_package *p, di_packages *status) {
di_package *q;
di_package_version *pv, *qv;
bool ret;
/* If we don't understand the version number, we play safe
* and assume we should install it */
if (p->version == NULL || !(pv = di_package_version_parse(p)))
return false;
q = di_packages_get_package(status, p->package, 0);
if (q == NULL || q->version == NULL || !(qv = di_package_version_parse(q)))
return false;
ret = (di_package_version_compare(pv, qv) <= 0);
di_package_version_free(pv);
di_package_version_free(qv);
return ret;
}
static size_t choice_strcpy(char *dest, char *src, size_t size) {
size_t n=0;
while (*src && (n < size-2)) {
if (*src == ',')
dest[n++] = '\\';
dest[n++] = *src++;
}
dest[n] = '\0';
return n;
}
size_t package_to_choice(di_package *package, char *buf, size_t size) {
int n;
n = choice_strcpy(buf, package->package, size);
n += choice_strcpy(buf+n, ": ", size-n);
n += choice_strcpy(buf+n, package->short_description, size-n);
return n;
}
char *list_to_choices(di_package **packages) {
char buf[200], *ret;
int count = 0;
size_t ret_size = 1024, ret_used = 1, size;
di_package *p;
ret = di_malloc(1024);
ret[0] = '\0';
while ((p = packages[count])) {
size = package_to_choice(p, buf, 200);
if (ret_used + size + 2 > ret_size) {
ret_size += 1024;
ret = di_realloc(ret, ret_size);
}
strcat(ret, buf);
ret_used += size + 2;
count++;
if (packages[count])
strcat(ret, ", ");
}
return ret;
}
int unpack_package (const char *pkgfile) {
char *command;
int ret;
if (asprintf(&command, "%s %s", DPKG_UNPACK_COMMAND, pkgfile) == -1)
return 0;
ret = !di_exec_shell_log(command);
free(command);
return ret;
}
int configure_package (const char *package) {
char *command;
int ret;
if (asprintf(&command, "%s %s", DPKG_CONFIGURE_COMMAND, package) == -1)
return 0;
ret = !di_exec_shell_log(command);
free(command);
return ret;
}
/* Check whether the md5sum of file matches sum. If not, return 0. */
int md5sum(const char *sum, const char *file) {
FILE *fp;
char line[1024];
/* Trivially true if the Packages file doesn't have md5sum lines */
if (sum == NULL)
return 1;
snprintf(line, sizeof(line), "/usr/bin/md5sum %s", file);
fp = popen(line, "r");
if (fp == NULL)
return 0;
if (fgets(line, sizeof(line), fp) != NULL) {
fclose(fp);
if (strlen(line) < 32)
return 0;
line[32] = '\0';
return !strcmp(line, sum);
}
fclose(fp);
return 0;
}
/* Used to qsort a package array by name. */
int package_name_compare(const void *v1, const void *v2) {
di_package *p1, *p2;
p1 = *(di_package **)v1;
p2 = *(di_package **)v2;
return strcmp(p1->package, p2->package);
}
/* The INCLUDE_FILE lists packages that should be installed by default. */
void take_includes(di_packages *packages) {
di_package *p;
FILE *fp;
char buf[1024], *ptr;
di_slist_node *node;
if ((fp = fopen(INCLUDE_FILE, "r")) == NULL)
return;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '#')
continue;
if ((ptr = strchr(buf, '\n')) != NULL)
*ptr = '\0';
for (node = packages->list.head; node; node = node->next) {
p = node->data;
if (strcmp(p->package, buf) == 0)
p->status_want = di_package_status_want_install;
}
}
fclose(fp);
}
/* While the EXCLUDE file lists packages that should not be installed by
* default. */
void drop_excludes(di_packages *packages) {
di_package *p;
FILE *fp;
char buf[1024], *ptr;
di_slist_node *node;
if ((fp = fopen(EXCLUDE_FILE, "r")) == NULL)
return;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if (buf[0] == '#')
continue;
if ((ptr = strchr(buf, '\n')) != NULL)
*ptr = '\0';
for (node = packages->list.head; node; node = node->next) {
p = node->data;
if (strcmp(p->package, buf) == 0)
p->status_want = di_package_status_want_deinstall;
}
}
fclose(fp);
}
|