Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
* This file is part of pkg (SvarDOS)
|
2 |
* This file is part of pkg (SvarDOS)
|
3 |
* Copyright (C) 2012-2023 Mateusz Viste
|
3 |
* Copyright (C) 2012-2024 Mateusz Viste
|
4 |
*/
|
4 |
*/
|
5 |
|
5 |
|
6 |
#include <ctype.h> /* toupper() */
|
6 |
#include <ctype.h> /* toupper() */
|
7 |
#include <stdio.h>
|
7 |
#include <stdio.h>
|
8 |
#include <stdlib.h> /* system() */
|
8 |
#include <stdlib.h> /* system() */
|
Line 65... |
Line 65... |
65 |
}
|
65 |
}
|
66 |
|
66 |
|
67 |
|
67 |
|
68 |
/* returns 0 if pkgname is not installed, non-zero otherwise */
|
68 |
/* returns 0 if pkgname is not installed, non-zero otherwise */
|
69 |
int is_package_installed(const char *pkgname, const char *dosdir) {
|
69 |
int is_package_installed(const char *pkgname, const char *dosdir) {
|
70 |
char fname[512];
|
70 |
char fname[256];
|
71 |
sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
|
71 |
sprintf(fname, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
|
72 |
return(fileexists(fname)); /* file exists -> package is installed */
|
72 |
return(fileexists(fname)); /* file exists -> package is installed */
|
73 |
}
|
73 |
}
|
74 |
|
74 |
|
75 |
|
75 |
|
76 |
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
|
76 |
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
|
Line 92... |
Line 92... |
92 |
}
|
92 |
}
|
93 |
return(NULL);
|
93 |
return(NULL);
|
94 |
}
|
94 |
}
|
95 |
|
95 |
|
96 |
|
96 |
|
97 |
/* prepare a package for installation. this is mandatory before actually installing it!
|
97 |
/* prepare a package for installation. this is mandatory before installing it!
|
98 |
* 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. */
|
98 |
* returns a pointer to the zip file's index on success, NULL on failure.
|
- |
|
99 |
* the **zipfd pointer is updated with file descriptor of the open (to be
|
- |
|
100 |
* installed) zip file.
|
- |
|
101 |
* the returned ziplist is guaranteed to have the APPINFO file as first node
|
- |
|
102 |
* the ziplist is also guaranteed not to contain any directory entries */
|
99 |
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
|
103 |
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
|
100 |
char fname[256];
|
104 |
char fname[256];
|
101 |
char appinfofile[256];
|
105 |
char appinfofile[32];
|
102 |
int appinfopresence;
|
106 |
struct ziplist *appinfoptr;
|
103 |
char *shortfile;
|
107 |
char *shortfile;
|
104 |
struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
|
108 |
struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
|
105 |
struct flist_t *flist = NULL;
|
109 |
struct flist_t *flist = NULL;
|
106 |
|
110 |
|
107 |
sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
|
111 |
sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
|
Line 119... |
Line 123... |
119 |
{
|
123 |
{
|
120 |
int slen = strlen(fname);
|
124 |
int slen = strlen(fname);
|
121 |
if ((slen < 4) || (fname[slen - 4] != '.')) strcat(fname, ".SVP");
|
125 |
if ((slen < 4) || (fname[slen - 4] != '.')) strcat(fname, ".SVP");
|
122 |
}
|
126 |
}
|
123 |
|
127 |
|
124 |
/* Now let's check the content of the zip file */
|
128 |
/* now let's check the content of the zip file */
|
125 |
|
129 |
|
126 |
*zipfd = fopen(fname, "rb");
|
130 |
*zipfd = fopen(fname, "rb");
|
127 |
if (*zipfd == NULL) {
|
131 |
if (*zipfd == NULL) {
|
128 |
puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
|
132 |
puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
|
129 |
goto RAII;
|
133 |
goto RAII;
|
Line 135... |
Line 139... |
135 |
}
|
139 |
}
|
136 |
/* if updating, load the list of files belonging to the current package */
|
140 |
/* if updating, load the list of files belonging to the current package */
|
137 |
if ((flags & PKGINST_UPDATE) != 0) {
|
141 |
if ((flags & PKGINST_UPDATE) != 0) {
|
138 |
flist = pkg_loadflist(pkgname, dosdir);
|
142 |
flist = pkg_loadflist(pkgname, dosdir);
|
139 |
}
|
143 |
}
|
- |
|
144 |
|
140 |
/* Verify that there's no collision with existing local files, look for the appinfo presence */
|
145 |
/* Verify that there's no collision with existing local files, look for the appinfo presence */
|
141 |
appinfopresence = 0;
|
146 |
appinfoptr = NULL;
|
142 |
prevzipnode = NULL;
|
147 |
prevzipnode = NULL;
|
- |
|
148 |
|
143 |
for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
|
149 |
for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
|
144 |
/* change all slashes to backslashes, and switch into all-lowercase */
|
150 |
/* change all slashes to backslashes, and switch into all-lowercase */
|
145 |
slash2backslash(curzipnode->filename);
|
151 |
slash2backslash(curzipnode->filename);
|
146 |
strlwr(curzipnode->filename);
|
152 |
strlwr(curzipnode->filename);
|
147 |
/* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
|
153 |
/* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
|
148 |
if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
|
154 |
if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
|
149 |
curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
|
155 |
curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
|
150 |
}
|
156 |
}
|
151 |
/* is it a "link file"? skip it - link files are no longer supported */
|
157 |
/* is it a "link file"? skip it - link files are no longer supported */
|
152 |
if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
|
158 |
if (strstr(curzipnode->filename, "links\\") == curzipnode->filename) {
|
153 |
curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
|
159 |
curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
|
154 |
}
|
160 |
}
|
155 |
|
161 |
|
- |
|
162 |
/* is it the appinfo file? remember it and keep it separate from the list for now */
|
- |
|
163 |
if (strcmp(curzipnode->filename, appinfofile) == 0) {
|
- |
|
164 |
appinfoptr = curzipnode;
|
- |
|
165 |
curzipnode = curzipnode->nextfile;
|
- |
|
166 |
if (prevzipnode != NULL) prevzipnode->nextfile = curzipnode;
|
- |
|
167 |
continue;
|
- |
|
168 |
}
|
- |
|
169 |
|
156 |
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) */
|
170 |
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) */
|
157 |
if (prevzipnode == NULL) { /* take the item out of the list */
|
171 |
if (prevzipnode == NULL) { /* take the item out of the list */
|
158 |
ziplinkedlist = curzipnode->nextfile;
|
172 |
ziplinkedlist = curzipnode->nextfile;
|
159 |
free(curzipnode); /* free the item */
|
173 |
free(curzipnode); /* free the item */
|
160 |
curzipnode = ziplinkedlist;
|
174 |
curzipnode = ziplinkedlist;
|
Line 163... |
Line 177... |
163 |
free(curzipnode); /* free the item */
|
177 |
free(curzipnode); /* free the item */
|
164 |
curzipnode = prevzipnode->nextfile;
|
178 |
curzipnode = prevzipnode->nextfile;
|
165 |
}
|
179 |
}
|
166 |
continue; /* go to the next item */
|
180 |
continue; /* go to the next item */
|
167 |
}
|
181 |
}
|
- |
|
182 |
|
168 |
/* validate that the file has a valid filename (8+3, no shady chars...) */
|
183 |
/* validate that the file has a valid filename (8+3, no shady chars...) */
|
169 |
if (validfilename(curzipnode->filename) != 0) {
|
184 |
if (validfilename(curzipnode->filename) != 0) {
|
170 |
puts(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
|
185 |
puts(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
|
171 |
printf(" %s\n", curzipnode->filename);
|
186 |
printf(" %s\n", curzipnode->filename);
|
172 |
goto RAII_ERR;
|
187 |
goto RAII_ERR;
|
173 |
}
|
188 |
}
|
- |
|
189 |
|
174 |
/* look out for collisions with already existing files (unless we are
|
190 |
/* look out for collisions with already existing files (unless we are
|
175 |
* updating the package and the local file belongs to it */
|
191 |
* updating the package and the local file belongs to it */
|
176 |
shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
|
192 |
shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
|
177 |
strcat(fname, shortfile);
|
193 |
strcat(fname, shortfile);
|
178 |
if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
|
194 |
if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
|
179 |
puts(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
|
195 |
puts(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
|
180 |
printf(" %s\n", fname);
|
196 |
printf(" %s\n", fname);
|
181 |
goto RAII_ERR;
|
197 |
goto RAII_ERR;
|
182 |
}
|
198 |
}
|
- |
|
199 |
|
183 |
/* abort if any entry is encrypted */
|
200 |
/* abort if any entry is encrypted */
|
184 |
if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
|
201 |
if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
|
185 |
puts(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
|
202 |
puts(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
|
186 |
printf(" %s\n", curzipnode->filename);
|
203 |
printf(" %s\n", curzipnode->filename);
|
187 |
goto RAII_ERR;
|
204 |
goto RAII_ERR;
|
188 |
}
|
205 |
}
|
- |
|
206 |
|
189 |
/* abort if any file is compressed with an unsupported method */
|
207 |
/* abort if any file is compressed with an unsupported method */
|
190 |
if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
|
208 |
if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
|
191 |
kitten_printf(8, 2, curzipnode->compmethod); /* "ERROR: Package contains a file compressed with an unsupported method (%d):" */
|
209 |
kitten_printf(8, 2, curzipnode->compmethod); /* "ERROR: Package contains a file compressed with an unsupported method (%d):" */
|
192 |
puts("");
|
210 |
puts("");
|
193 |
printf(" %s\n", curzipnode->filename);
|
211 |
printf(" %s\n", curzipnode->filename);
|
194 |
goto RAII_ERR;
|
212 |
goto RAII_ERR;
|
195 |
}
|
213 |
}
|
- |
|
214 |
|
196 |
if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
|
215 |
/* add node to list */
|
197 |
prevzipnode = curzipnode;
|
216 |
prevzipnode = curzipnode;
|
198 |
curzipnode = curzipnode->nextfile;
|
217 |
curzipnode = curzipnode->nextfile;
|
199 |
}
|
218 |
}
|
- |
|
219 |
|
200 |
/* if appinfo file not found, this is not a real SvarDOS package */
|
220 |
/* if appinfo file not found, this is not a real SvarDOS package */
|
201 |
if (appinfopresence != 1) {
|
221 |
if (appinfoptr == NULL) {
|
202 |
kitten_printf(3, 12, appinfofile); /* "ERROR: Package do not contain the %s file! Not a valid SvarDOS package." */
|
222 |
kitten_printf(3, 12, appinfofile); /* "ERROR: Package do not contain the %s file! Not a valid SvarDOS package." */
|
203 |
puts("");
|
223 |
puts("");
|
204 |
goto RAII_ERR;
|
224 |
goto RAII_ERR;
|
205 |
}
|
225 |
}
|
206 |
|
226 |
|
- |
|
227 |
/* attach the appinfo node to the top of the list (installation second stage
|
- |
|
228 |
* relies on this) */
|
- |
|
229 |
appinfoptr->nextfile = ziplinkedlist;
|
- |
|
230 |
ziplinkedlist = appinfoptr;
|
- |
|
231 |
|
207 |
goto RAII;
|
232 |
goto RAII;
|
208 |
|
233 |
|
209 |
RAII_ERR:
|
234 |
RAII_ERR:
|
210 |
zip_freelist(&ziplinkedlist);
|
235 |
zip_freelist(&ziplinkedlist);
|
211 |
ziplinkedlist = NULL;
|
236 |
ziplinkedlist = NULL;
|
Line 260... |
Line 285... |
260 |
/* install a package that has been prepared already. returns 0 on success,
|
285 |
/* install a package that has been prepared already. returns 0 on success,
|
261 |
* or a negative value on error, or a positive value on warning */
|
286 |
* or a negative value on error, or a positive value on warning */
|
262 |
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
|
287 |
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
|
263 |
char buff[256];
|
288 |
char buff[256];
|
264 |
char fulldestfilename[256];
|
289 |
char fulldestfilename[256];
|
265 |
char packageslst[32];
|
- |
|
266 |
char *shortfile;
|
290 |
char *shortfile;
|
267 |
long filesextractedsuccess = 0, filesextractedfailure = 0;
|
291 |
long filesextractedsuccess = 0, filesextractedfailure = 0;
|
268 |
struct ziplist *curzipnode;
|
292 |
struct ziplist *curzipnode;
|
269 |
FILE *lstfd;
|
293 |
FILE *lsmfd;
|
270 |
|
- |
|
271 |
sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
|
294 |
int unzip_result;
|
272 |
|
295 |
|
273 |
/* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
|
296 |
/* create the %DOSDIR%/APPINFO directory, just in case it doesn't exist yet */
|
274 |
sprintf(buff, "%s\\packages\\", dosdir);
|
297 |
sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
|
275 |
mkpath(buff);
|
298 |
mkpath(buff);
|
276 |
|
299 |
|
- |
|
300 |
/* start by extracting the APPINFO (LSM) file - I need it so I can append the
|
- |
|
301 |
* list of files belonging to the packages later */
|
- |
|
302 |
unzip_result = zip_unzip(zipfd, ziplinkedlist, buff);
|
277 |
/* open the lst file */
|
303 |
if (unzip_result != 0) {
|
- |
|
304 |
kitten_printf(8, 3, ziplinkedlist, buff); /* "ERROR: failed extracting '%s' to '%s'!" */
|
278 |
sprintf(buff, "%s\\%s", dosdir, packageslst);
|
305 |
printf(" [%d]\n", unzip_result);
|
- |
|
306 |
return(-1);
|
- |
|
307 |
}
|
- |
|
308 |
printf(" %s -> %s\n", ziplinkedlist->filename, buff);
|
- |
|
309 |
filesextractedsuccess++;
|
- |
|
310 |
|
- |
|
311 |
/* open the (freshly created) LSM file */
|
279 |
lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
|
312 |
lsmfd = fopen(buff, "ab"); /* opening in APPEND mode so I do not loose the LSM content */
|
280 |
if (lstfd == NULL) {
|
313 |
if (lsmfd == NULL) {
|
281 |
kitten_printf(3, 10, buff); /* "ERROR: Could not create %s!" */
|
314 |
kitten_printf(3, 10, buff); /* "ERROR: Could not create %s!" */
|
282 |
puts("");
|
315 |
puts("");
|
283 |
return(-2);
|
316 |
return(-2);
|
284 |
}
|
317 |
}
|
- |
|
318 |
fprintf(lsmfd, "\r\n"); /* in case the LSM does not end with a clear line already */
|
285 |
|
319 |
|
286 |
/* write list of files in zip into the lst, and create the directories structure */
|
320 |
/* write list of files in zip into the lst, and create the directories structure */
|
287 |
for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
|
321 |
for (curzipnode = ziplinkedlist->nextfile; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
|
- |
|
322 |
|
288 |
int unzip_result;
|
323 |
/* substitute paths to custom dirs */
|
289 |
if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
|
- |
|
290 |
shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
|
324 |
shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist);
|
- |
|
325 |
|
291 |
/* log the filename to packages\pkg.lst */
|
326 |
/* log the filename to LSM metadata file + its CRC */
|
292 |
fprintf(lstfd, "%s%s\r\n", buff, shortfile);
|
327 |
fprintf(lsmfd, "%s%s?%08lX\r\n", buff, shortfile, curzipnode->crc32);
|
- |
|
328 |
|
293 |
/* create the path, just in case it doesn't exist yet */
|
329 |
/* create the path, just in case it doesn't exist yet */
|
294 |
mkpath(buff);
|
330 |
mkpath(buff);
|
295 |
sprintf(fulldestfilename, "%s%s", buff, shortfile);
|
331 |
sprintf(fulldestfilename, "%s%s", buff, shortfile);
|
- |
|
332 |
|
296 |
/* Now unzip the file */
|
333 |
/* Now unzip the file */
|
297 |
unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
|
334 |
unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
|
298 |
if (unzip_result != 0) {
|
335 |
if (unzip_result != 0) {
|
299 |
kitten_printf(8, 3, curzipnode->filename, fulldestfilename); /* "ERROR: failed extracting '%s' to '%s'!" */
|
336 |
kitten_printf(8, 3, curzipnode->filename, fulldestfilename); /* "ERROR: failed extracting '%s' to '%s'!" */
|
300 |
printf(" [%d]\n", unzip_result);
|
337 |
printf(" [%d]\n", unzip_result);
|
Line 302... |
Line 339... |
302 |
} else {
|
339 |
} else {
|
303 |
printf(" %s -> %s\n", curzipnode->filename, buff);
|
340 |
printf(" %s -> %s\n", curzipnode->filename, buff);
|
304 |
filesextractedsuccess += 1;
|
341 |
filesextractedsuccess += 1;
|
305 |
}
|
342 |
}
|
306 |
}
|
343 |
}
|
307 |
fclose(lstfd);
|
344 |
fclose(lsmfd);
|
308 |
|
345 |
|
309 |
kitten_printf(3, 19, pkgname, filesextractedsuccess, filesextractedfailure); /* "Package %s installed: %ld files extracted, %ld errors." */
|
346 |
kitten_printf(3, 19, pkgname, filesextractedsuccess, filesextractedfailure); /* "Package %s installed: %ld files extracted, %ld errors." */
|
310 |
puts("");
|
347 |
puts("");
|
311 |
|
348 |
|
312 |
/* scan the LSM file for a "warn" message to display */
|
349 |
/* scan the LSM file for a "warn" message to display */
|