Subversion Repositories SvarDOS

Rev

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