Subversion Repositories SvarDOS

Rev

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