Repository navigation
Expand file tree
/
Copy pathupdate.c
More file actions
150 lines (120 loc) · 4.19 KB
/
Copy pathupdate.c
File metadata and controls
150 lines (120 loc) · 4.19 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include "upac.h"
static int try_find_repo_pkg(const char *name, char *repo_path, size_t repo_path_size) {
if (access(UPAC_REPO_CACHE_DIR, R_OK) != 0) {
if (upac_update_db() != 0) return -1;
}
DIR *repo_dir = opendir(UPAC_REPO_CACHE_DIR);
if (!repo_dir) return -1;
struct dirent *cat_entry;
int found = 0;
while ((cat_entry = readdir(repo_dir)) != NULL && !found) {
if (cat_entry->d_name[0] == '.') continue;
char cat_path[1024];
snprintf(cat_path, sizeof(cat_path), "%s/%s", UPAC_REPO_CACHE_DIR, cat_entry->d_name);
struct stat st;
if (stat(cat_path, &st) != 0 || !S_ISDIR(st.st_mode)) continue;
char build_file[1024];
snprintf(build_file, sizeof(build_file), "%s/%s/%s.build",
UPAC_REPO_CACHE_DIR, cat_entry->d_name, name);
if (access(build_file, R_OK) == 0) {
snprintf(repo_path, repo_path_size, "%s/%s", cat_entry->d_name, name);
found = 1;
}
}
closedir(repo_dir);
return found ? 0 : -1;
}
static int update_single(const char *name) {
upac_db_init();
InstalledPackage installed;
if (upac_db_find(name, &installed) != 0) {
fprintf(stderr, "[upac] Package '%s' is not installed\n", name);
return -1;
}
char repo_path[512];
if (try_find_repo_pkg(name, repo_path, sizeof(repo_path)) != 0) {
fprintf(stderr, "[upac] Package '%s' not found in repository\n", name);
return -1;
}
Package *pkg = upac_load_repo_package(repo_path);
if (!pkg) return -1;
if (pkg->version[0] == '\0') {
fprintf(stderr, "[upac] Cannot determine repo version for %s\n", name);
upac_free_package(pkg);
return -1;
}
if (strcmp(installed.version, pkg->version) == 0 &&
installed.revision >= pkg->revision) {
printf("[upac] %s-%s-r%d is already up to date\n",
name, installed.version, installed.revision);
upac_free_package(pkg);
return 0;
}
printf("[upac] Updating %s: %s-r%d -> %s-r%d\n",
name, installed.version, installed.revision,
pkg->version, pkg->revision);
int ret = upac_install_package(pkg, installed.install_path[0] ? installed.install_path : "/");
upac_free_package(pkg);
return ret;
}
int upac_update(const char *pkg_name) {
if (pkg_name) {
return update_single(pkg_name);
}
upac_db_init();
InstalledPackage *list;
int count;
if (upac_db_list_all(&list, &count) != 0 || count == 0) {
printf("[upac] No packages installed\n");
return 0;
}
int updated = 0;
int failed = 0;
int up_to_date = 0;
for (int i = 0; i < count; i++) {
char repo_path[512];
if (try_find_repo_pkg(list[i].name, repo_path, sizeof(repo_path)) != 0) {
up_to_date++;
continue;
}
Package *pkg = upac_load_repo_package(repo_path);
if (!pkg) {
failed++;
continue;
}
int need_update = 0;
if (pkg->version[0] != '\0') {
if (compare_versions(pkg->version, list[i].version) > 0) {
need_update = 1;
} else if (compare_versions(pkg->version, list[i].version) == 0 &&
pkg->revision > list[i].revision) {
need_update = 1;
}
}
if (!need_update) {
up_to_date++;
upac_free_package(pkg);
continue;
}
printf("[upac] Updating %s: %s-r%d -> %s-r%d\n",
list[i].name, list[i].version, list[i].revision,
pkg->version, pkg->revision);
if (upac_install_package(pkg, list[i].install_path[0] ? list[i].install_path : "/") == 0) {
updated++;
} else {
fprintf(stderr, "[upac] Failed to update %s\n", list[i].name);
failed++;
}
upac_free_package(pkg);
}
printf("[upac] Update complete: %d updated, %d up to date, %d failed\n",
updated, up_to_date, failed);
return failed > 0 ? -1 : 0;
}