219 |
mateuszvis |
1 |
/*
|
230 |
mateuszvis |
2 |
* This file is part of pkginst (SvarDOS)
|
219 |
mateuszvis |
3 |
* Copyright (C) 2012-2021 Mateusz Viste
|
|
|
4 |
* Changes by TK Chia
|
|
|
5 |
*/
|
|
|
6 |
|
|
|
7 |
#include <ctype.h> /* toupper() */
|
|
|
8 |
#include <stdio.h>
|
|
|
9 |
#include <stdlib.h> /* system() */
|
|
|
10 |
#include <string.h> /* strcpy() */
|
|
|
11 |
#include <unistd.h> /* read() */
|
|
|
12 |
#include <sys/types.h> /* struct utimbuf */
|
|
|
13 |
|
|
|
14 |
#include "crc32.h" /* all CRC32 related stuff */
|
|
|
15 |
#include "helpers.h" /* slash2backslash(), strtolower() */
|
|
|
16 |
#include "fileexst.h"
|
|
|
17 |
#include "kprintf.h"
|
|
|
18 |
#include "libunzip.h" /* zip_listfiles()... */
|
|
|
19 |
#include "showinst.h" /* pkg_loadflist() */
|
|
|
20 |
#include "pkginst.h" /* include self for control */
|
|
|
21 |
#include "version.h"
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
|
|
|
25 |
* nonzero otherwise. */
|
236 |
mateuszvis |
26 |
static int validfilename(const char *fname) {
|
219 |
mateuszvis |
27 |
int i, i2;
|
|
|
28 |
char *validchars = "!#$%&'()-@^_`{}~";
|
|
|
29 |
int elemlen = 0;
|
|
|
30 |
int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
|
|
|
31 |
a filename or an extension */
|
|
|
32 |
/* look for invalid chars in the entire string, and check the length of the
|
|
|
33 |
* element at the same time */
|
|
|
34 |
for (i = 0; fname[i] != 0; i++) {
|
|
|
35 |
/* director separators are threated specially */
|
|
|
36 |
if (fname[i] == '\\') {
|
|
|
37 |
elemlen = 0;
|
|
|
38 |
elemmaxlen = 8;
|
|
|
39 |
continue;
|
|
|
40 |
}
|
|
|
41 |
/* '.' switches the check into extension mode */
|
|
|
42 |
if (fname[i] == '.') {
|
|
|
43 |
if (elemlen == 0) return(-1); /* a file must have a base name */
|
|
|
44 |
if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
|
|
|
45 |
elemlen = 0;
|
|
|
46 |
elemmaxlen = 3;
|
|
|
47 |
continue;
|
|
|
48 |
}
|
|
|
49 |
/* check that the element is not too long */
|
|
|
50 |
if (++elemlen > elemmaxlen) return(-3);
|
|
|
51 |
/* look for valid characters */
|
|
|
52 |
if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
|
|
|
53 |
if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
|
|
|
54 |
if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
|
|
|
55 |
if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
|
|
|
56 |
/* look for other valid characters */
|
|
|
57 |
for (i2 = 0; validchars[i2] != 0; i2++) {
|
|
|
58 |
if (fname[i] == validchars[i2]) break;
|
|
|
59 |
}
|
|
|
60 |
if (validchars[i2] != 0) continue;
|
|
|
61 |
/* if we are here, then the character is invalid */
|
|
|
62 |
return(-4);
|
|
|
63 |
}
|
|
|
64 |
/* all checks passed */
|
|
|
65 |
return(0);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
|
|
|
69 |
/* returns 0 if pkgname is not installed, non-zero otherwise */
|
230 |
mateuszvis |
70 |
int is_package_installed(const char *pkgname, const char *dosdir) {
|
219 |
mateuszvis |
71 |
char fname[512];
|
|
|
72 |
sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
|
230 |
mateuszvis |
73 |
return(fileexists(fname)); /* file exists -> package is installed */
|
219 |
mateuszvis |
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
|
230 |
mateuszvis |
78 |
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
|
225 |
mateuszvis |
79 |
if (is_package_installed(pkgname, dosdir) != 0) {
|
219 |
mateuszvis |
80 |
kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
|
|
|
81 |
puts("");
|
|
|
82 |
return(-1);
|
|
|
83 |
}
|
|
|
84 |
return(0);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
/* find a filename in a flist linked list, and returns a pointer to it */
|
236 |
mateuszvis |
89 |
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
|
219 |
mateuszvis |
90 |
while (flist != NULL) {
|
260 |
mateuszvis |
91 |
if (strcasecmp(flist->fname, fname) == 0) return(flist);
|
219 |
mateuszvis |
92 |
flist = flist->next;
|
|
|
93 |
}
|
|
|
94 |
return(NULL);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
/* prepare a package for installation. this is mandatory before actually installing it!
|
230 |
mateuszvis |
99 |
* returns a pointer to the zip file's index on success, NULL on failure. the **zipfd pointer is updated with a file descriptor to the open zip file to install. */
|
236 |
mateuszvis |
100 |
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
|
|
|
101 |
char fname[256];
|
|
|
102 |
char appinfofile[256];
|
219 |
mateuszvis |
103 |
int appinfopresence;
|
|
|
104 |
char *shortfile;
|
230 |
mateuszvis |
105 |
struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
|
|
|
106 |
struct flist_t *flist = NULL;
|
219 |
mateuszvis |
107 |
|
|
|
108 |
sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
|
|
|
109 |
|
|
|
110 |
/* check if not already installed, if already here, print a message "you might want to use update instead"
|
|
|
111 |
* of course this must not be done if we are in the process of upgrading said package */
|
225 |
mateuszvis |
112 |
if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
|
219 |
mateuszvis |
113 |
return(NULL);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/* Now let's check the content of the zip file */
|
|
|
117 |
|
|
|
118 |
*zipfd = fopen(zipfile, "rb");
|
|
|
119 |
if (*zipfd == NULL) {
|
|
|
120 |
kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
|
230 |
mateuszvis |
121 |
goto RAII;
|
219 |
mateuszvis |
122 |
}
|
|
|
123 |
ziplinkedlist = zip_listfiles(*zipfd);
|
|
|
124 |
if (ziplinkedlist == NULL) {
|
|
|
125 |
kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
|
230 |
mateuszvis |
126 |
goto RAII;
|
219 |
mateuszvis |
127 |
}
|
|
|
128 |
/* if updating, load the list of files belonging to the current package */
|
|
|
129 |
if ((flags & PKGINST_UPDATE) != 0) {
|
|
|
130 |
flist = pkg_loadflist(pkgname, dosdir);
|
|
|
131 |
}
|
260 |
mateuszvis |
132 |
/* Verify that there's no collision with existing local files, look for the appinfo presence */
|
219 |
mateuszvis |
133 |
appinfopresence = 0;
|
|
|
134 |
prevzipnode = NULL;
|
|
|
135 |
for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
|
|
|
136 |
/* change all slashes to backslashes, and switch into all-lowercase */
|
|
|
137 |
slash2backslash(curzipnode->filename);
|
|
|
138 |
strtolower(curzipnode->filename);
|
|
|
139 |
/* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
|
|
|
140 |
if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
|
|
|
141 |
curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
|
|
|
142 |
}
|
260 |
mateuszvis |
143 |
/* is it a "link file"? skip it - link files are no longer supported */
|
219 |
mateuszvis |
144 |
if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
|
260 |
mateuszvis |
145 |
curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
|
219 |
mateuszvis |
146 |
}
|
|
|
147 |
|
|
|
148 |
if (curzipnode->filename[0] == 0) { /* ignore empty filenames (maybe it was empty originally, or has been emptied because it's a dropped source or link) */
|
|
|
149 |
if (prevzipnode == NULL) { /* take the item out of the list */
|
|
|
150 |
ziplinkedlist = curzipnode->nextfile;
|
|
|
151 |
free(curzipnode); /* free the item */
|
|
|
152 |
curzipnode = ziplinkedlist;
|
|
|
153 |
} else {
|
|
|
154 |
prevzipnode->nextfile = curzipnode->nextfile;
|
|
|
155 |
free(curzipnode); /* free the item */
|
|
|
156 |
curzipnode = prevzipnode->nextfile;
|
|
|
157 |
}
|
|
|
158 |
continue; /* go to the next item */
|
|
|
159 |
}
|
|
|
160 |
/* validate that the file has a valid filename (8+3, no shady chars...) */
|
|
|
161 |
if (validfilename(curzipnode->filename) != 0) {
|
|
|
162 |
kitten_puts(3, 23, "Error: Package contains an invalid filename:");
|
|
|
163 |
printf(" %s\n", curzipnode->filename);
|
230 |
mateuszvis |
164 |
goto RAII_ERR;
|
219 |
mateuszvis |
165 |
}
|
|
|
166 |
/* look out for collisions with already existing files (unless we are
|
|
|
167 |
* updating the package and the local file belongs to it */
|
|
|
168 |
shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
|
|
|
169 |
strcat(fname, shortfile);
|
|
|
170 |
if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
|
|
|
171 |
kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
|
|
|
172 |
printf(" %s\n", fname);
|
230 |
mateuszvis |
173 |
goto RAII_ERR;
|
219 |
mateuszvis |
174 |
}
|
|
|
175 |
/* abort if any entry is encrypted */
|
|
|
176 |
if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
|
|
|
177 |
kitten_printf(3, 20, "Error: Package contains an encrypted file:");
|
|
|
178 |
puts("");
|
|
|
179 |
printf(" %s\n", curzipnode->filename);
|
230 |
mateuszvis |
180 |
goto RAII_ERR;
|
219 |
mateuszvis |
181 |
}
|
|
|
182 |
/* abort if any file is compressed with an unsupported method */
|
223 |
mateuszvis |
183 |
if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
|
219 |
mateuszvis |
184 |
kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
|
|
|
185 |
puts("");
|
|
|
186 |
printf(" %s\n", curzipnode->filename);
|
230 |
mateuszvis |
187 |
goto RAII_ERR;
|
219 |
mateuszvis |
188 |
}
|
|
|
189 |
if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
|
|
|
190 |
prevzipnode = curzipnode;
|
|
|
191 |
curzipnode = curzipnode->nextfile;
|
|
|
192 |
}
|
|
|
193 |
/* if appinfo file not found, this is not a real FreeDOS package */
|
|
|
194 |
if (appinfopresence != 1) {
|
|
|
195 |
kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
|
|
|
196 |
puts("");
|
230 |
mateuszvis |
197 |
goto RAII_ERR;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
goto RAII;
|
|
|
201 |
|
|
|
202 |
RAII_ERR:
|
|
|
203 |
zip_freelist(&ziplinkedlist);
|
|
|
204 |
ziplinkedlist = NULL;
|
|
|
205 |
if ((zipfd != NULL) && (*zipfd != NULL)) {
|
219 |
mateuszvis |
206 |
fclose(*zipfd);
|
230 |
mateuszvis |
207 |
*zipfd = NULL;
|
219 |
mateuszvis |
208 |
}
|
|
|
209 |
|
230 |
mateuszvis |
210 |
RAII:
|
|
|
211 |
pkg_freeflist(flist);
|
219 |
mateuszvis |
212 |
return(ziplinkedlist);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
/* install a package that has been prepared already. returns 0 on success,
|
|
|
217 |
* or a negative value on error, or a positive value on warning */
|
236 |
mateuszvis |
218 |
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
|
219 |
mateuszvis |
219 |
char *buff;
|
|
|
220 |
char *fulldestfilename;
|
|
|
221 |
char packageslst[64];
|
|
|
222 |
char *shortfile;
|
|
|
223 |
long filesextractedsuccess = 0, filesextractedfailure = 0;
|
|
|
224 |
struct ziplist *curzipnode;
|
|
|
225 |
FILE *lstfd;
|
|
|
226 |
|
|
|
227 |
sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
|
|
|
228 |
|
|
|
229 |
buff = malloc(512);
|
|
|
230 |
fulldestfilename = malloc(1024);
|
|
|
231 |
if ((buff == NULL) || (fulldestfilename == NULL)) {
|
|
|
232 |
kitten_puts(8, 0, "Out of memory!");
|
|
|
233 |
zip_freelist(&ziplinkedlist);
|
|
|
234 |
free(buff);
|
|
|
235 |
free(fulldestfilename);
|
|
|
236 |
return(-1);
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
|
|
|
240 |
sprintf(buff, "%s\\packages\\", dosdir);
|
|
|
241 |
mkpath(buff);
|
|
|
242 |
|
|
|
243 |
/* open the lst file */
|
|
|
244 |
sprintf(buff, "%s\\%s", dosdir, packageslst);
|
|
|
245 |
lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
|
|
|
246 |
if (lstfd == NULL) {
|
|
|
247 |
kitten_printf(3, 10, "Error: Could not create %s!", buff);
|
|
|
248 |
puts("");
|
|
|
249 |
zip_freelist(&ziplinkedlist);
|
|
|
250 |
free(buff);
|
|
|
251 |
free(fulldestfilename);
|
|
|
252 |
return(-2);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/* write list of files in zip into the lst, and create the directories structure */
|
|
|
256 |
for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
|
|
|
257 |
int unzip_result;
|
|
|
258 |
if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
|
|
|
259 |
if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
|
|
|
260 |
shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
|
|
|
261 |
/* log the filename to packages\pkg.lst (with original, unmapped drive) */
|
|
|
262 |
fprintf(lstfd, "%s%s\r\n", buff, shortfile);
|
|
|
263 |
/* create the path, just in case it doesn't exist yet */
|
|
|
264 |
mkpath(buff);
|
|
|
265 |
sprintf(fulldestfilename, "%s%s", buff, shortfile);
|
|
|
266 |
/* Now unzip the file */
|
|
|
267 |
unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
|
|
|
268 |
if (unzip_result != 0) {
|
|
|
269 |
kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
|
|
|
270 |
printf(" [%d]\n", unzip_result);
|
|
|
271 |
filesextractedfailure += 1;
|
|
|
272 |
} else {
|
|
|
273 |
printf(" %s -> %s\n", curzipnode->filename, buff);
|
|
|
274 |
filesextractedsuccess += 1;
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
fclose(lstfd);
|
|
|
278 |
|
|
|
279 |
/* free the ziplist and close file descriptor */
|
|
|
280 |
zip_freelist(&ziplinkedlist);
|
|
|
281 |
free(buff);
|
|
|
282 |
free(fulldestfilename);
|
|
|
283 |
|
|
|
284 |
kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
|
|
|
285 |
puts("");
|
|
|
286 |
return(filesextractedfailure);
|
|
|
287 |
}
|