Subversion Repositories SvarDOS

Rev

Rev 1980 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of pkg (SvarDOS)
1678 mateusz.vi 3
 * Copyright (C) 2012-2024 Mateusz Viste
219 mateuszvis 4
 */
5
 
6
#include <ctype.h>     /* toupper() */
7
#include <stdio.h>
8
#include <stdlib.h>    /* system() */
9
#include <string.h>    /* strcpy() */
2235 bernd.boec 10
#include <strings.h>
219 mateuszvis 11
#include <unistd.h>    /* read() */
12
#include <sys/types.h> /* struct utimbuf */
13
 
995 mateusz.vi 14
#include "helpers.h"   /* slash2backslash() */
219 mateuszvis 15
#include "fileexst.h"
16
#include "libunzip.h"  /* zip_listfiles()... */
17
#include "showinst.h"  /* pkg_loadflist() */
2235 bernd.boec 18
#include "svarlang.lib/svarlang.h"
268 mateuszvis 19
 
219 mateuszvis 20
#include "pkginst.h"   /* include self for control */
21
 
22
 
23
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
24
 * nonzero otherwise. */
236 mateuszvis 25
static int validfilename(const char *fname) {
219 mateuszvis 26
  int i, i2;
1964 mateusz.vi 27
  const char *validchars = "!#$%&'()-@^_`{}~";
219 mateuszvis 28
  int elemlen = 0;
29
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
30
                         a filename or an extension */
31
  /* look for invalid chars in the entire string, and check the length of the
32
   * element at the same time */
33
  for (i = 0; fname[i] != 0; i++) {
34
    /* director separators are threated specially */
35
    if (fname[i] == '\\') {
36
      elemlen = 0;
37
      elemmaxlen = 8;
38
      continue;
39
    }
40
    /* '.' switches the check into extension mode */
41
    if (fname[i] == '.') {
42
      if (elemlen == 0) return(-1); /* a file must have a base name */
43
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
44
      elemlen = 0;
45
      elemmaxlen = 3;
46
      continue;
47
    }
48
    /* check that the element is not too long */
49
    if (++elemlen > elemmaxlen) return(-3);
50
    /* look for valid characters */
51
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
52
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
53
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
54
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
55
    /* look for other valid characters */
56
    for (i2 = 0; validchars[i2] != 0; i2++) {
57
      if (fname[i] == validchars[i2]) break;
58
    }
59
    if (validchars[i2] != 0) continue;
60
    /* if we are here, then the character is invalid */
61
    return(-4);
62
  }
63
  /* all checks passed */
64
  return(0);
65
}
66
 
67
 
68
/* returns 0 if pkgname is not installed, non-zero otherwise */
230 mateuszvis 69
int is_package_installed(const char *pkgname, const char *dosdir) {
1678 mateusz.vi 70
  char fname[256];
71
  sprintf(fname, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
230 mateuszvis 72
  return(fileexists(fname)); /* file exists -> package is installed */
219 mateuszvis 73
}
74
 
75
 
76
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
230 mateuszvis 77
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
225 mateuszvis 78
  if (is_package_installed(pkgname, dosdir) != 0) {
1965 mateusz.vi 79
    char msg[256];
80
    /* "Package %s is already installed! You might want to use the 'update' action." */
81
    sprintf(msg, svarlang_str(3,18), pkgname);
82
    outputnl(msg);
219 mateuszvis 83
    return(-1);
84
  }
85
  return(0);
86
}
87
 
88
 
89
/* find a filename in a flist linked list, and returns a pointer to it */
236 mateuszvis 90
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
219 mateuszvis 91
  while (flist != NULL) {
260 mateuszvis 92
    if (strcasecmp(flist->fname, fname) == 0) return(flist);
219 mateuszvis 93
    flist = flist->next;
94
  }
95
  return(NULL);
96
}
97
 
98
 
1678 mateusz.vi 99
/* prepare a package for installation. this is mandatory before installing it!
100
 * returns a pointer to the zip file's index on success, NULL on failure.
1880 mateusz.vi 101
 * pkgname must be at least 9 bytes long and is filled with the package name.
1678 mateusz.vi 102
 * the **zipfd pointer is updated with file descriptor of the open (to be
103
 * installed) zip file.
104
 * the returned ziplist is guaranteed to have the APPINFO file as first node
105
 * the ziplist is also guaranteed not to contain any directory entries */
1980 mateusz.vi 106
struct ziplist *pkginstall_preparepackage(char *pkgname, const char *zipfile, unsigned char flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist, char bootdrive) {
236 mateuszvis 107
  char fname[256];
1880 mateusz.vi 108
  struct ziplist *appinfoptr = NULL;
219 mateuszvis 109
  char *shortfile;
230 mateuszvis 110
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
111
  struct flist_t *flist = NULL;
219 mateuszvis 112
 
1600 mateusz.vi 113
  /* copy zip filename into fname */
114
  strcpy(fname, zipfile);
115
 
116
  /* append an .SVP extension if not present */
117
  {
118
    int slen = strlen(fname);
1601 mateusz.vi 119
    if ((slen < 4) || (fname[slen - 4] != '.')) strcat(fname, ".SVP");
1600 mateusz.vi 120
  }
121
 
1800 mateusz.vi 122
  /* does the file exist? */
123
  if (!fileexists(fname)) {
1963 mateusz.vi 124
    outputnl(svarlang_str(10, 1)); /* ERROR: File not found */
1800 mateusz.vi 125
    goto RAII;
126
  }
127
 
1880 mateusz.vi 128
  /* open the SVP archive and get the list of files */
1600 mateusz.vi 129
  *zipfd = fopen(fname, "rb");
219 mateuszvis 130
  if (*zipfd == NULL) {
1963 mateusz.vi 131
    outputnl(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
1880 mateusz.vi 132
    goto RAII_ERR;
219 mateuszvis 133
  }
134
  ziplinkedlist = zip_listfiles(*zipfd);
135
  if (ziplinkedlist == NULL) {
1963 mateusz.vi 136
    outputnl(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
1880 mateusz.vi 137
    goto RAII_ERR;
219 mateuszvis 138
  }
1678 mateusz.vi 139
 
1880 mateusz.vi 140
  /* process the entire ziplist and sanitize it + locate the appinfo file so
141
   * I know the package name */
219 mateuszvis 142
  prevzipnode = NULL;
1880 mateusz.vi 143
  curzipnode = ziplinkedlist;
144
  while (curzipnode != NULL) {
1678 mateusz.vi 145
 
219 mateuszvis 146
    /* change all slashes to backslashes, and switch into all-lowercase */
147
    slash2backslash(curzipnode->filename);
995 mateusz.vi 148
    strlwr(curzipnode->filename);
219 mateuszvis 149
 
150
    /* validate that the file has a valid filename (8+3, no shady chars...) */
151
    if (validfilename(curzipnode->filename) != 0) {
1963 mateusz.vi 152
      outputnl(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
1966 mateusz.vi 153
      outputnl(curzipnode->filename);
230 mateuszvis 154
      goto RAII_ERR;
219 mateuszvis 155
    }
1678 mateusz.vi 156
 
1880 mateusz.vi 157
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
158
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) goto DELETE_ZIP_NODE;
1678 mateusz.vi 159
 
1880 mateusz.vi 160
    /* abort if entry is encrypted */
219 mateuszvis 161
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
1963 mateusz.vi 162
      outputnl(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
1964 mateusz.vi 163
      outputnl(curzipnode->filename);
230 mateuszvis 164
      goto RAII_ERR;
219 mateuszvis 165
    }
1678 mateusz.vi 166
 
1880 mateusz.vi 167
    /* abort if file is compressed with an unsupported method */
295 mateuszvis 168
    if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
1965 mateusz.vi 169
      /* "ERROR: Package contains a file compressed with an unsupported method:" */
170
      outputnl(svarlang_str(8,2));
1964 mateusz.vi 171
      outputnl(curzipnode->filename);
230 mateuszvis 172
      goto RAII_ERR;
219 mateuszvis 173
    }
1678 mateusz.vi 174
 
1880 mateusz.vi 175
    /* is it the appinfo file? detach it from the list for now */
176
    if (strstr(curzipnode->filename, "appinfo\\") == curzipnode->filename) {
177
      if (appinfoptr != NULL) {
1963 mateusz.vi 178
        outputnl(svarlang_str(3, 12)); /* "ERROR: This is not a valid SvarDOS package" */
1880 mateusz.vi 179
        goto RAII_ERR;
180
      }
181
      appinfoptr = curzipnode;
182
      curzipnode = curzipnode->nextfile;
183
      if (prevzipnode == NULL) {
184
        ziplinkedlist = curzipnode;
185
      } else {
186
        prevzipnode->nextfile = curzipnode;
187
      }
188
      continue;
189
    }
190
 
191
    /* all good, move to the next item in the list */
219 mateuszvis 192
    prevzipnode = curzipnode;
193
    curzipnode = curzipnode->nextfile;
1880 mateusz.vi 194
    continue;
195
 
196
    DELETE_ZIP_NODE:
197
    if (prevzipnode == NULL) {  /* take the item out of the list */
198
      ziplinkedlist = curzipnode->nextfile;
199
      free(curzipnode); /* free the item */
200
      curzipnode = ziplinkedlist;
201
    } else {
202
      prevzipnode->nextfile = curzipnode->nextfile;
203
      free(curzipnode); /* free the item */
204
      curzipnode = prevzipnode->nextfile;
205
    }
206
    /* go to the next item */
219 mateuszvis 207
  }
1678 mateusz.vi 208
 
1880 mateusz.vi 209
  /* if appinfo file not found, this is not a SvarDOS package */
1678 mateusz.vi 210
  if (appinfoptr == NULL) {
1963 mateusz.vi 211
    outputnl(svarlang_str(3, 12)); /* "ERROR: This is not a valid SvarDOS package." */
230 mateuszvis 212
    goto RAII_ERR;
213
  }
214
 
1678 mateusz.vi 215
  /* attach the appinfo node to the top of the list (installation second stage
216
   * relies on this) */
217
  appinfoptr->nextfile = ziplinkedlist;
218
  ziplinkedlist = appinfoptr;
219
 
1880 mateusz.vi 220
  /* fill in pkgname based on what was found in APPINFO */
221
  {
222
    unsigned short i;
223
    /* copy and stop at the nearest dot */
224
    for (i = 0; i < 8; i++) {
225
      if (appinfoptr->filename[8 + i] == '.') break;
226
      pkgname[i] = appinfoptr->filename[8 + i];
227
    }
228
    pkgname[i] = 0;
229
    if ((i == 0) || (strcmp(appinfoptr->filename + 8 + i, ".lsm") != 0)) {
1963 mateusz.vi 230
      outputnl(svarlang_str(3, 12)); /* "ERROR: This is not a valid SvarDOS package." */
1880 mateusz.vi 231
      goto RAII_ERR;
232
    }
233
  }
234
 
235
  /* if updating, load the list of files belonging to the current package */
236
  if ((flags & PKGINST_UPDATE) != 0) {
237
    flist = pkg_loadflist(pkgname, dosdir);
238
  } else {
239
    /* if NOT updating, check that package is not installed already */
240
    if (validate_package_not_installed(pkgname, dosdir) != 0) goto RAII_ERR;
241
  }
242
 
243
  /* Verify that there's no collision with existing local files, but skip the
244
   * first entry as it is the appinfo (LSM) file that is handled specially */
245
 
246
  for (curzipnode = ziplinkedlist->nextfile; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
247
 
248
    /* look out for collisions with already existing files (unless we are
249
     * updating the package and the local file belongs to it */
1892 mateusz.vi 250
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist, bootdrive);
1880 mateusz.vi 251
    strcat(fname, shortfile);
252
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
1963 mateusz.vi 253
      outputnl(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
1966 mateusz.vi 254
      outputnl(fname);
1880 mateusz.vi 255
      goto RAII_ERR;
256
    }
257
  }
258
 
230 mateuszvis 259
  goto RAII;
260
 
261
  RAII_ERR:
262
  zip_freelist(&ziplinkedlist);
263
  ziplinkedlist = NULL;
264
  if ((zipfd != NULL) && (*zipfd != NULL)) {
219 mateuszvis 265
    fclose(*zipfd);
230 mateuszvis 266
    *zipfd = NULL;
219 mateuszvis 267
  }
268
 
230 mateuszvis 269
  RAII:
270
  pkg_freeflist(flist);
219 mateuszvis 271
  return(ziplinkedlist);
272
}
273
 
274
 
1599 mateusz.vi 275
/* look for a "warn" field in the package's LSM file and display it if found */
276
static void display_warn_if_exists(const char *pkgname, const char *dosdir, char *buff, size_t buffsz) {
277
  FILE *fd;
278
  char *msgptr;
279
  int warncount = 0, i;
280
 
281
  sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
282
  fd = fopen(buff, "rb");
283
  if (fd == NULL) return;
284
 
285
  while (freadtokval(fd, buff, buffsz, &msgptr, ':') == 0) {
286
    if (msgptr != NULL) {
287
      if (strcasecmp(buff, "warn") == 0) {
288
        /* print a visual delimiter */
289
        if (warncount == 0) {
1963 mateusz.vi 290
          outputnl("");
1964 mateusz.vi 291
          for (i = 0; i < 79; i++) output("*");
1963 mateusz.vi 292
          outputnl("");
1599 mateusz.vi 293
        }
294
        /* there may be more than one "warn" line */
1963 mateusz.vi 295
        outputnl(msgptr);
1599 mateusz.vi 296
        warncount++;
297
      }
298
    }
299
  }
300
 
301
  fclose(fd);
302
 
303
  /* if one or more warn lines have been displayed then close with a delimiter again */
304
  if (warncount > 0) {
1971 mateusz.vi 305
    for (i = 0; i < 79; i++) output("*");
1963 mateusz.vi 306
    outputnl("");
1599 mateusz.vi 307
  }
308
 
309
}
310
 
311
 
219 mateuszvis 312
/* install a package that has been prepared already. returns 0 on success,
313
 * or a negative value on error, or a positive value on warning */
1980 mateusz.vi 314
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd, char bootdrive, unsigned char *buff15k, unsigned char flags) {
293 mateuszvis 315
  char buff[256];
316
  char fulldestfilename[256];
219 mateuszvis 317
  char *shortfile;
318
  long filesextractedsuccess = 0, filesextractedfailure = 0;
319
  struct ziplist *curzipnode;
1678 mateusz.vi 320
  FILE *lsmfd;
321
  int unzip_result;
219 mateuszvis 322
 
1678 mateusz.vi 323
  /* create the %DOSDIR%/APPINFO directory, just in case it doesn't exist yet */
1957 mateusz.vi 324
  sprintf(buff, "%s\\appinfo\\", dosdir);
219 mateuszvis 325
  mkpath(buff);
326
 
1678 mateusz.vi 327
  /* start by extracting the APPINFO (LSM) file - I need it so I can append the
328
   * list of files belonging to the packages later */
1894 mateusz.vi 329
  output(ziplinkedlist->filename);
330
  output(" -> ");
331
  output(buff);
1957 mateusz.vi 332
  strcat(buff, pkgname);
333
  strcat(buff, ".lsm");
1964 mateusz.vi 334
  unzip_result = zip_unzip(zipfd, ziplinkedlist, buff, buff15k);
1963 mateusz.vi 335
  outputnl("");
1678 mateusz.vi 336
  if (unzip_result != 0) {
1965 mateusz.vi 337
    sprintf(buff, svarlang_str(10,4), unzip_result); /* "ERROR: unzip failure (%d)" */
1976 mateusz.vi 338
    output(buff);
339
    if (unzip_result == -4) {
340
      output(" [");
341
      output(svarlang_str(2,14)); /* "Out of memory" */
342
      output("]");
343
    }
1678 mateusz.vi 344
    return(-1);
345
  }
346
  filesextractedsuccess++;
347
 
348
  /* open the (freshly created) LSM file */
349
  lsmfd = fopen(buff, "ab"); /* opening in APPEND mode so I do not loose the LSM content */
350
  if (lsmfd == NULL) {
1965 mateusz.vi 351
    /* "ERROR: Could not create %s!" */
352
    sprintf(buff15k, svarlang_str(3,10), buff);
353
    outputnl(buff15k);
219 mateuszvis 354
    return(-2);
355
  }
1678 mateusz.vi 356
  fprintf(lsmfd, "\r\n"); /* in case the LSM does not end with a clear line already */
219 mateuszvis 357
 
358
  /* write list of files in zip into the lst, and create the directories structure */
1678 mateusz.vi 359
  for (curzipnode = ziplinkedlist->nextfile; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
360
 
361
    /* substitute paths to custom dirs */
1892 mateusz.vi 362
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist, bootdrive);
1678 mateusz.vi 363
 
364
    /* log the filename to LSM metadata file + its CRC */
365
    fprintf(lsmfd, "%s%s?%08lX\r\n", buff, shortfile, curzipnode->crc32);
366
 
219 mateuszvis 367
    /* create the path, just in case it doesn't exist yet */
368
    mkpath(buff);
369
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
1678 mateusz.vi 370
 
219 mateuszvis 371
    /* Now unzip the file */
1893 mateusz.vi 372
    output(curzipnode->filename);
373
    output(" -> ");
374
    output(buff);
1964 mateusz.vi 375
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename, buff15k);
1963 mateusz.vi 376
    outputnl("");
1974 mateusz.vi 377
    if (unzip_result == 0) {
378
      filesextractedsuccess += 1;
379
    } else {
380
      /* "ERROR: unzip failure (%d)" */
381
      sprintf(buff, svarlang_str(10,4), unzip_result);
382
      output(buff);
383
      if (unzip_result == -4) {
384
        output(" [");
385
        output(svarlang_str(2,14)); /* "Out of memory" */
386
        output("]");
387
      }
388
      outputnl("");
219 mateuszvis 389
      filesextractedfailure += 1;
390
    }
391
  }
1678 mateusz.vi 392
  fclose(lsmfd);
219 mateuszvis 393
 
1965 mateusz.vi 394
  /* "Package %s installed: %ld errors, %ld files extracted." */
395
  sprintf(buff15k, svarlang_str(3, 19), pkgname, filesextractedfailure, filesextractedsuccess);
396
  outputnl(buff15k);
1599 mateusz.vi 397
 
398
  /* scan the LSM file for a "warn" message to display */
1980 mateusz.vi 399
  if ((flags & PKGINST_HIDEWARN) == 0) {
400
    display_warn_if_exists(pkgname, dosdir, buff, sizeof(buff));
401
  }
1599 mateusz.vi 402
 
219 mateuszvis 403
  return(filesextractedfailure);
404
}