219 |
mateuszvis |
1 |
/*
|
|
|
2 |
* This file is part of FDNPKG
|
|
|
3 |
* Copyright (C) 2013-2017 Mateusz Viste. All rights reserved.
|
|
|
4 |
*/
|
|
|
5 |
|
|
|
6 |
#include <stdio.h>
|
|
|
7 |
#include <ctype.h> /* tolower() */
|
|
|
8 |
#include <stdlib.h> /* atoi(), qsort() - not using it after all, redefining it manually later */
|
|
|
9 |
#include <string.h> /* strlen() */
|
|
|
10 |
#include <sys/types.h>
|
|
|
11 |
|
|
|
12 |
/* opendir() and friends */
|
|
|
13 |
#ifdef __WATCOMC__
|
|
|
14 |
#include <direct.h>
|
|
|
15 |
#else
|
|
|
16 |
#include <dirent.h>
|
|
|
17 |
#endif
|
|
|
18 |
|
|
|
19 |
#include "fdnpkg.h" /* PKGINST_UPDATE */
|
|
|
20 |
#include "fileexst.h"
|
|
|
21 |
#include "getdelim.h"
|
|
|
22 |
#include "helpers.h" /* fdnpkg_strcasestr(), slash2backslash()... */
|
|
|
23 |
#include "kprintf.h"
|
|
|
24 |
#include "libunzip.h" /* zip_freelist()... */
|
|
|
25 |
#include "lsm.h"
|
|
|
26 |
#include "pkgdb.h"
|
|
|
27 |
#include "pkginst.h"
|
|
|
28 |
#include "pkgrem.h"
|
|
|
29 |
#include "rtrim.h"
|
|
|
30 |
#include "showinst.h" /* include self for control */
|
|
|
31 |
#include "version.h"
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
/* this is a wrapper around strcasecmp(), to be used by qsort() */
|
|
|
35 |
static int strcompare(const void *str1, const void *str2) {
|
|
|
36 |
char **s1 = (char **)str1;
|
|
|
37 |
char **s2 = (char **)str2;
|
|
|
38 |
return(strcasecmp(*s1, *s2));
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
/* clears current line */
|
|
|
43 |
static void clrline(void) {
|
|
|
44 |
printf("\r \r");
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, char *filterstr, char *dosdir) {
|
|
|
49 |
DIR *dp;
|
|
|
50 |
int packagelist_len = 0, x;
|
|
|
51 |
struct dirent *ep;
|
|
|
52 |
#define verstr_maxlen 64
|
|
|
53 |
char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
|
|
|
54 |
sprintf(pkgdir, "%s\\packages", dosdir);
|
|
|
55 |
dp = opendir(pkgdir);
|
|
|
56 |
if (dp != NULL) {
|
|
|
57 |
while ((ep = readdir(dp)) != NULL) {
|
|
|
58 |
if (ep->d_name[0] != '.') { /* ignore '.', '..', and hidden directories */
|
|
|
59 |
if (strlen(ep->d_name) > 4) {
|
|
|
60 |
int tlen = strlen(ep->d_name);
|
|
|
61 |
if ((ep->d_name[tlen - 4] != '.') || (tolower(ep->d_name[tlen - 3]) != 'l') || (tolower(ep->d_name[tlen - 2]) != 's') || (tolower(ep->d_name[tlen - 1]) != 't')) continue; /* if it's not an .lst file, skip it silently */
|
|
|
62 |
|
|
|
63 |
if (filterstr != NULL) {
|
|
|
64 |
if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
|
|
|
65 |
}
|
|
|
66 |
if (packagelist_len >= packagelist_maxlen) {
|
|
|
67 |
closedir(dp);
|
|
|
68 |
return(-1); /* if not enough place in the list - return an error */
|
|
|
69 |
}
|
|
|
70 |
packagelist[packagelist_len] = strdup(ep->d_name);
|
|
|
71 |
packagelist[packagelist_len][strlen(packagelist[packagelist_len]) - 4] = 0; /* cut out the .lst extension */
|
|
|
72 |
packagelist_len += 1;
|
|
|
73 |
}
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
closedir(dp);
|
|
|
77 |
qsort(packagelist, packagelist_len, sizeof (char **), strcompare); /* sort the package list */
|
|
|
78 |
for (x = 0; x < packagelist_len; x++) {
|
|
|
79 |
/* for each package, load the metadata from %DOSDIR\APPINFO\*.lsm */
|
|
|
80 |
sprintf(lsmfilename, "%s\\appinfo\\%s.lsm", dosdir, packagelist[x]);
|
|
|
81 |
if (readlsm(lsmfilename, verstr, verstr_maxlen) != 0) sprintf(verstr, "(unknown version)");
|
|
|
82 |
packagelist_ver[x] = strdup(verstr);
|
|
|
83 |
}
|
|
|
84 |
return(packagelist_len);
|
|
|
85 |
} else {
|
|
|
86 |
kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
|
|
|
87 |
puts("");
|
|
|
88 |
return(-1);
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
#define packagelist_maxlen 1024
|
|
|
93 |
|
|
|
94 |
void showinstalledpkgs(char *filterstr, char *dosdir) {
|
|
|
95 |
char *packagelist[packagelist_maxlen];
|
|
|
96 |
char *packagelist_ver[packagelist_maxlen];
|
|
|
97 |
int packagelist_len, x;
|
|
|
98 |
|
|
|
99 |
/* load the list of packages */
|
|
|
100 |
packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir); /* Populate the packages list */
|
|
|
101 |
if (packagelist_len < 0) return;
|
|
|
102 |
if (packagelist_len == 0) {
|
|
|
103 |
kitten_puts(5, 0, "No package matched the search.");
|
|
|
104 |
return;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/* iterate through all packages */
|
|
|
108 |
for (x = 0; x < packagelist_len; x++) {
|
|
|
109 |
/* print the package/version couple on screen */
|
|
|
110 |
printf("%s %s\n", packagelist[x], packagelist_ver[x]);
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
/* displays the list of available updates for local packages (or a single package if pkg is not NULL). Returns 0 if some updates are found, non zero otherwise. */
|
|
|
116 |
int checkupdates(char *dosdir, struct pkgdb *pkgdb, char **repolist, char *pkg, char *tempdir, int flags, struct customdirs *dirlist, char *proxy, int proxyport, char *downloadingstring, char *mapdrv) {
|
|
|
117 |
struct pkgdb *curpkg;
|
|
|
118 |
struct pkgrepo *currepo;
|
|
|
119 |
char *packagelist[packagelist_maxlen];
|
|
|
120 |
char *packagelist_ver[packagelist_maxlen];
|
|
|
121 |
int packagelist_len, x, foundupdate = 0, totalupdatesfound = 0;
|
|
|
122 |
int packages_updated = 0, packages_updatefailed = 0;
|
|
|
123 |
packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, NULL, dosdir);
|
|
|
124 |
for (x = 0; x < packagelist_len; x++) {
|
|
|
125 |
if (pkg != NULL) {
|
|
|
126 |
if (strcasecmp(pkg, packagelist[x]) != 0) continue; /* if we got asked for a specific package, skip all other packages. */
|
|
|
127 |
} else { /* else display a progress so slower PCs don't think we are freezed */
|
|
|
128 |
long percprogress = x; /* long, just in case we have more than 3'200 packages installed... */
|
|
|
129 |
percprogress *= 100;
|
|
|
130 |
percprogress /= packagelist_len;
|
|
|
131 |
kitten_printf(10, 7, "Looking for updates...");
|
|
|
132 |
printf(" %ld%% (%s) \r", percprogress, packagelist[x]); /* empty spaces trailing for flushing the previous content on the line */
|
|
|
133 |
}
|
|
|
134 |
for (curpkg = pkgdb; curpkg != NULL; curpkg = curpkg->nextpkg) { /* iterate through packages */
|
|
|
135 |
if (strcasecmp(curpkg->name, packagelist[x]) == 0) {
|
|
|
136 |
foundupdate = 0;
|
|
|
137 |
for (currepo = curpkg->repolist; currepo != NULL; currepo = currepo->nextrepo) { /* check for possible newer version in every repo */
|
|
|
138 |
if (isversionnewer(packagelist_ver[x], currepo->version) > 0) { /* isversionnewer() returns 1 if version is newer */
|
|
|
139 |
if (foundupdate == 0) {
|
|
|
140 |
foundupdate = 1;
|
|
|
141 |
totalupdatesfound += 1;
|
|
|
142 |
if (pkg == NULL) {
|
|
|
143 |
clrline();
|
|
|
144 |
kitten_printf(10, 0, "%s (local version: %s)", packagelist[x], packagelist_ver[x]);
|
|
|
145 |
puts("");
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
if (pkg == NULL) {
|
|
|
149 |
clrline();
|
|
|
150 |
printf(" ");
|
|
|
151 |
kitten_printf(10, 1, "version %s at %s", currepo->version, repolist[currepo->repo]);
|
|
|
152 |
puts("");
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
/* actually upgrade the package, if requested so */
|
|
|
157 |
if (foundupdate != 0) {
|
|
|
158 |
if (flags & PKGINST_UPDATE) {
|
|
|
159 |
FILE *zipfilefd;
|
|
|
160 |
struct ziplist *zipfileidx;
|
|
|
161 |
char buffmem1k[1024];
|
|
|
162 |
clrline();
|
|
|
163 |
kitten_printf(10, 3, "An update of '%s' has been found. Update in progress...", packagelist[x]);
|
|
|
164 |
puts("");
|
|
|
165 |
packages_updatefailed += 1; /* increment the updatefailed counter - later we will decrement it if we're okay */
|
|
|
166 |
zipfileidx = pkginstall_preparepackage(pkgdb, packagelist[x], tempdir, NULL, flags, repolist, &zipfilefd, proxy, proxyport, downloadingstring, dosdir, dirlist, buffmem1k, mapdrv);
|
|
|
167 |
if (zipfileidx != NULL) {
|
|
|
168 |
if (pkgrem(packagelist[x], dosdir, mapdrv) != 0) {
|
|
|
169 |
/* ooops, package removal failed... */
|
|
|
170 |
zip_freelist(&zipfileidx);
|
|
|
171 |
} else { /* pkgrem == 0 */
|
|
|
172 |
pkginstall_installpackage(packagelist[x], dosdir, dirlist, zipfileidx, zipfilefd, mapdrv);
|
|
|
173 |
packages_updated += 1;
|
|
|
174 |
packages_updatefailed -= 1; /* decrement the updatefailed counter to leverage the fact we incremented it without reason earlier */
|
|
|
175 |
}
|
|
|
176 |
fclose(zipfilefd);
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
puts(""); /* add a line feed to visually separate packages */
|
|
|
180 |
}
|
|
|
181 |
break; /* get out of the loop to get over the next local package */
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
if (pkg == NULL) {
|
|
|
186 |
clrline();
|
|
|
187 |
kitten_printf(10, 5, "%d package update(s) found.", totalupdatesfound);
|
|
|
188 |
puts("");
|
|
|
189 |
if (flags & PKGINST_UPDATE) {
|
|
|
190 |
kitten_printf(10, 4, "%d package(s) updated, %d package(s) failed.", packages_updated, packages_updatefailed);
|
|
|
191 |
puts("");
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
if (foundupdate != 0) {
|
|
|
195 |
return(0);
|
|
|
196 |
} else {
|
|
|
197 |
return(-1);
|
|
|
198 |
}
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
|
|
|
202 |
/* frees a linked list of filenames */
|
|
|
203 |
void pkg_freeflist(struct flist_t *flist) {
|
|
|
204 |
while (flist != NULL) {
|
|
|
205 |
struct flist_t *victim = flist;
|
|
|
206 |
flist = flist->next;
|
|
|
207 |
free(victim);
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
|
|
|
212 |
/* returns a linked list of the files that belong to package pkgname */
|
|
|
213 |
struct flist_t *pkg_loadflist(char *pkgname, char *dosdir) {
|
|
|
214 |
struct flist_t *res = NULL, *newnode;
|
|
|
215 |
FILE *fd;
|
|
|
216 |
char *lineptr;
|
|
|
217 |
char buff[512];
|
|
|
218 |
int getdelimlen, fnamelen;
|
|
|
219 |
size_t getdelimcount = 0;
|
|
|
220 |
sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
|
|
|
221 |
if (fileexists(buff) == 0) { /* file does not exist */
|
|
|
222 |
kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
|
|
|
223 |
puts("");
|
|
|
224 |
return(NULL);
|
|
|
225 |
}
|
|
|
226 |
fd = fopen(buff, "rb");
|
|
|
227 |
if (fd == NULL) {
|
|
|
228 |
kitten_puts(4, 1, "Error opening lst file!");
|
|
|
229 |
return(NULL);
|
|
|
230 |
}
|
|
|
231 |
/* iterate through all lines of the file */
|
|
|
232 |
for (;;) {
|
|
|
233 |
lineptr = NULL;
|
|
|
234 |
getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
|
|
|
235 |
if (getdelimlen < 0) { /* EOF */
|
|
|
236 |
free(lineptr);
|
|
|
237 |
break;
|
|
|
238 |
}
|
|
|
239 |
rtrim(lineptr); /* right-trim the filename */
|
|
|
240 |
slash2backslash(lineptr); /* change all / to \ */
|
|
|
241 |
if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
|
|
|
242 |
if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
|
|
|
243 |
if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
|
|
|
244 |
fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
|
|
|
245 |
} else { /* else it's a relative path starting at %dosdir% */
|
|
|
246 |
fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
|
|
|
247 |
}
|
|
|
248 |
free(lineptr); /* free the memory occupied by the line */
|
|
|
249 |
/* add the new node to the result */
|
|
|
250 |
newnode = malloc(sizeof(struct flist_t) + fnamelen);
|
|
|
251 |
if (newnode == NULL) {
|
|
|
252 |
kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
|
|
|
253 |
continue;
|
|
|
254 |
}
|
|
|
255 |
newnode->next = res;
|
|
|
256 |
strcpy(newnode->fname, buff);
|
|
|
257 |
res = newnode;
|
|
|
258 |
}
|
|
|
259 |
fclose(fd);
|
|
|
260 |
return(res);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
/* Prints files owned by a package */
|
|
|
265 |
void listfilesofpkg(char *pkgname, char *dosdir) {
|
|
|
266 |
struct flist_t *flist, *flist_ptr;
|
|
|
267 |
/* load the list of files belonging to pkgname */
|
|
|
268 |
flist = pkg_loadflist(pkgname, dosdir);
|
|
|
269 |
/* display each filename on screen */
|
|
|
270 |
for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
|
|
|
271 |
printf("%s\n", flist_ptr->fname);
|
|
|
272 |
}
|
|
|
273 |
/* free the list of files */
|
|
|
274 |
pkg_freeflist(flist);
|
|
|
275 |
}
|