Subversion Repositories SvarDOS

Rev

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