-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate.c
More file actions
226 lines (189 loc) · 8.34 KB
/
update.c
File metadata and controls
226 lines (189 loc) · 8.34 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
// Include necessary headers
#include "libspm.h"
#include "cutils.h"
//should probably add there to the header when we are done
int update()
{
msg(INFO, "fetching updates");
int new_version_found = 0;
const char *path = getenv("SOVIET_SPM_DIR");
const char *repo_path = getenv("SOVIET_REPOS_DIR");
int num_files;
char **files_array = get_all_files(path, path, &num_files);
if (files_array == NULL) {
msg(WARNING, "no packages installed");
return 0;
}
// Print each file path
for (int i = 0; i < num_files; i++)
{
// This will break if the files are not separated into repos
// But it doesnt cause a crash, just a visual bug
// I think
char* local_repo = strtok(files_array[i], "/");
char* local_package_name = strchr(files_array[i], '\0') + 1;
// Allocate the packages to be compared
struct package* local = calloc(1, sizeof(struct package));
struct package* remote = calloc(1, sizeof(struct package));
char* local_path = calloc(MAX_PATH, sizeof(char));
char* remote_path = calloc(MAX_PATH, sizeof(char));
sprintf(local_path, "%s/%s/%s", path, local_repo, local_package_name);
int num_searched_files;
char **searched_files_array = get_all_files(repo_path, repo_path, &num_searched_files);
if (searched_files_array != NULL)
{
// Print each file path
for (int j = 0; j < num_searched_files; j++)
{
// This will break if the files are not separated into repos
// But it doesnt cause a crash, just a visual bug
// I think
char* remote_repo = strtok(searched_files_array[j], "/");
char* remote_package = strchr(searched_files_array[j], '\0') + 1;
char* remote_package_name = calloc(strlen(remote_package) + 1, sizeof(char));
strcpy(remote_package_name, remote_package);
while(strtok(remote_package_name, "/"))
{
char* tmp = remote_package_name;
remote_package_name = strchr(remote_package_name, '\0') + 1;
if(strcmp(remote_package_name, "") == 0)
{
remote_package_name = tmp;
break;
}
}
//printf("%s, %s \n", remote_package_name, local_package_name);
if (strcmp(remote_repo, local_repo) == 0)
{
if (strcmp(remote_package_name, local_package_name) == 0)
{
// Compare the filename
sprintf(remote_path, "%s/%s/%s", repo_path, remote_repo, remote_package);
open_pkg(local_path, local, "ecmp");
open_pkg(remote_path, remote, "ecmp");
// Compare the versions
if(strcmp(local->version, remote->version) != 0)
{
msg(INFO, "package %s is at version %s, available version is %s", local->name, local->version, remote->version);
new_version_found = 1;
}
free(local);
free(remote);
}
}
// Free each file path string
free(searched_files_array[j]);
}
// Free each file path string
free(files_array[i]);
}
// Free the array of file paths
free(searched_files_array);
}
// Free the array of file paths
free(files_array);
if(new_version_found != 0) {
msg(WARNING, "New version found for one or more packages, use --upgrade to upgrade");
}
else {
msg(WARNING, "all packages are up to date");
}
return 0;
}
int upgrade()
{
msg(INFO, "upgrading");
int new_version_installed = 0;
const char *path = getenv("SOVIET_SPM_DIR");
const char *repo_path = getenv("SOVIET_REPOS_DIR");
int num_files;
char **files_array = get_all_files(path, path, &num_files);
if (files_array != NULL)
{
// Print each file path
for (int i = 0; i < num_files; i++)
{
// This will break if the files are not separated into repos
// But it doesnt cause a crash, just a visual bug
// I think
char* local_repo = strtok(files_array[i], "/");
char* local_package_name = strchr(files_array[i], '\0') + 1;
// Allocate the packages to be compared
struct package* local = calloc(1, sizeof(struct package));
struct package* remote = calloc(1, sizeof(struct package));
char* local_path = calloc(MAX_PATH, sizeof(char));
char* remote_path = calloc(MAX_PATH, sizeof(char));
sprintf(local_path, "%s/%s/%s", path, local_repo, local_package_name);
int num_searched_files;
char **searched_files_array = get_all_files(repo_path, repo_path, &num_searched_files);
if (searched_files_array != NULL)
{
// Print each file path
for (int j = 0; j < num_searched_files; j++)
{
// This will break if the files are not separated into repos
// But it doesnt cause a crash, just a visual bug
// I think
char* remote_repo = strtok(searched_files_array[j], "/");
char* remote_package = strchr(searched_files_array[j], '\0') + 1;
char* remote_package_name = calloc(strlen(remote_package) + 1, sizeof(char));
strcpy(remote_package_name, remote_package);
while(strtok(remote_package_name, "/"))
{
char* tmp = remote_package_name;
remote_package_name = strchr(remote_package_name, '\0') + 1;
if(strcmp(remote_package_name, "") == 0)
{
remote_package_name = tmp;
break;
}
}
//printf("%s, %s \n", remote_package_name, local_package_name);
if (strcmp(remote_repo, local_repo) == 0)
{
if (strcmp(remote_package_name, local_package_name) == 0)
{
// Compare the filename
sprintf(remote_path, "%s/%s/%s", repo_path, remote_repo, remote_package);
open_pkg(local_path, local, "ecmp");
open_pkg(remote_path, remote, "ecmp");
// Compare the versions
if(strcmp(local->version, remote->version) != 0)
{
msg(INFO, "package %s is at version %s, available version is %s", local->name, local->version, remote->version);
msg(INFO, "upgrading %s from %s to %s", local->name, local->version, remote->version);
uninstall(local->name);
f_install_package_source(remote_path, 0, local_repo);
new_version_installed = 1;
}
free(local);
free(remote);
}
}
// Free each file path string
free(searched_files_array[j]);
}
// Free each file path string
free(files_array[i]);
}
// Free the array of file paths
free(searched_files_array);
}
// Free the array of file paths
free(files_array);
}
else
{
// If no files found, print a message
printf("No files found.\n");
}
if(new_version_installed == 0)
{
msg(WARNING, "all packages are up to date");
}
return 0;
}