Subversion Repositories SvarDOS

Rev

Rev 223 | Rev 225 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 223 Rev 224
1
/*
1
/*
2
 * This file is part of FDNPKG
2
 * This file is part of FDNPKG
3
 * Copyright (C) 2012-2021 Mateusz Viste
3
 * Copyright (C) 2012-2021 Mateusz Viste
4
 * Changes by TK Chia
4
 * Changes by TK Chia
5
 */
5
 */
6
 
6
 
7
#include <ctype.h>     /* toupper() */
7
#include <ctype.h>     /* toupper() */
8
#include <stdio.h>
8
#include <stdio.h>
9
#include <stdlib.h>    /* system() */
9
#include <stdlib.h>    /* system() */
10
#include <string.h>    /* strcpy() */
10
#include <string.h>    /* strcpy() */
11
#include <unistd.h>    /* read() */
11
#include <unistd.h>    /* read() */
12
#include <sys/types.h> /* struct utimbuf */
12
#include <sys/types.h> /* struct utimbuf */
13
 
13
 
14
#include "crc32.h"     /* all CRC32 related stuff */
14
#include "crc32.h"     /* all CRC32 related stuff */
15
#include "fdnpkg.h"    /* PKGINST_NOSOURCE, PKGINST_SKIPLINKS... */
15
#include "fdnpkg.h"    /* PKGINST_NOSOURCE, PKGINST_SKIPLINKS... */
16
#include "helpers.h"   /* slash2backslash(), strtolower() */
16
#include "helpers.h"   /* slash2backslash(), strtolower() */
17
#include "fileexst.h"
17
#include "fileexst.h"
18
#include "kprintf.h"
18
#include "kprintf.h"
19
#include "libunzip.h"  /* zip_listfiles()... */
19
#include "libunzip.h"  /* zip_listfiles()... */
20
#include "showinst.h"  /* pkg_loadflist() */
20
#include "showinst.h"  /* pkg_loadflist() */
21
#include "pkginst.h"   /* include self for control */
21
#include "pkginst.h"   /* include self for control */
22
#include "version.h"
22
#include "version.h"
23
 
23
 
24
 
24
 
25
/* return 1 if fname looks like a link filename, 0 otherwise */
25
/* return 1 if fname looks like a link filename, 0 otherwise */
26
static int islinkfile(char *fname) {
26
static int islinkfile(char *fname) {
27
  char *link1 = "LINKS\\";
27
  char *link1 = "LINKS\\";
28
  char *link2 = "links\\";
28
  char *link2 = "links\\";
29
  int x;
29
  int x;
30
  for (x = 0; ; x++) {
30
  for (x = 0; ; x++) {
31
    if (link1[x] == 0) return(1);
31
    if (link1[x] == 0) return(1);
32
    if ((fname[x] != link1[x]) && (fname[x] != link2[x])) return(0);
32
    if ((fname[x] != link1[x]) && (fname[x] != link2[x])) return(0);
33
  }
33
  }
34
}
34
}
35
 
35
 
36
 
36
 
37
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
37
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
38
 * nonzero otherwise. */
38
 * nonzero otherwise. */
39
static int validfilename(char *fname) {
39
static int validfilename(char *fname) {
40
  int i, i2;
40
  int i, i2;
41
  char *validchars = "!#$%&'()-@^_`{}~";
41
  char *validchars = "!#$%&'()-@^_`{}~";
42
  int elemlen = 0;
42
  int elemlen = 0;
43
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
43
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
44
                         a filename or an extension */
44
                         a filename or an extension */
45
  /* look for invalid chars in the entire string, and check the length of the
45
  /* look for invalid chars in the entire string, and check the length of the
46
   * element at the same time */
46
   * element at the same time */
47
  for (i = 0; fname[i] != 0; i++) {
47
  for (i = 0; fname[i] != 0; i++) {
48
    /* director separators are threated specially */
48
    /* director separators are threated specially */
49
    if (fname[i] == '\\') {
49
    if (fname[i] == '\\') {
50
      elemlen = 0;
50
      elemlen = 0;
51
      elemmaxlen = 8;
51
      elemmaxlen = 8;
52
      continue;
52
      continue;
53
    }
53
    }
54
    /* '.' switches the check into extension mode */
54
    /* '.' switches the check into extension mode */
55
    if (fname[i] == '.') {
55
    if (fname[i] == '.') {
56
      if (elemlen == 0) return(-1); /* a file must have a base name */
56
      if (elemlen == 0) return(-1); /* a file must have a base name */
57
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
57
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
58
      elemlen = 0;
58
      elemlen = 0;
59
      elemmaxlen = 3;
59
      elemmaxlen = 3;
60
      continue;
60
      continue;
61
    }
61
    }
62
    /* check that the element is not too long */
62
    /* check that the element is not too long */
63
    if (++elemlen > elemmaxlen) return(-3);
63
    if (++elemlen > elemmaxlen) return(-3);
64
    /* look for valid characters */
64
    /* look for valid characters */
65
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
65
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
66
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
66
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
67
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
67
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
68
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
68
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
69
    /* look for other valid characters */
69
    /* look for other valid characters */
70
    for (i2 = 0; validchars[i2] != 0; i2++) {
70
    for (i2 = 0; validchars[i2] != 0; i2++) {
71
      if (fname[i] == validchars[i2]) break;
71
      if (fname[i] == validchars[i2]) break;
72
    }
72
    }
73
    if (validchars[i2] != 0) continue;
73
    if (validchars[i2] != 0) continue;
74
    /* if we are here, then the character is invalid */
74
    /* if we are here, then the character is invalid */
75
    return(-4);
75
    return(-4);
76
  }
76
  }
77
  /* all checks passed */
77
  /* all checks passed */
78
  return(0);
78
  return(0);
79
}
79
}
80
 
80
 
81
 
81
 
82
/* processes a link file - that is, reads the target inside, and overwrite
82
/* processes a link file - that is, reads the target inside, and overwrite
83
 * the file with new content */
83
 * the file with new content */
84
static void processlinkfile(char *linkfile, char *dosdir, struct customdirs *dirlist, char *buff) {
84
static void processlinkfile(char *linkfile, char *dosdir, struct customdirs *dirlist, char *buff) {
85
  char origtarget[512];
85
  char origtarget[512];
86
  int x;
86
  int x;
87
  char *shortfile;
87
  char *shortfile;
88
  unsigned char comstub[] = { /* machine code of a COM stub launcher */
88
  unsigned char comstub[] = { /* machine code of a COM stub launcher */
89
    0xBC,0x00,0x00,0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0xBB,0x2A,0x01,0x8C,0x5F,0x04,
89
    0xBC,0x00,0x00,0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0xBB,0x2A,0x01,0x8C,0x5F,0x04,
90
    0x8C,0x5F,0x08,0x8C,0x5F,0x0C,0xB8,0x00,0x4B,0xBA,0x38,0x01,0xCD,0x21,0xB0,0x7F,
90
    0x8C,0x5F,0x08,0x8C,0x5F,0x0C,0xB8,0x00,0x4B,0xBA,0x38,0x01,0xCD,0x21,0xB0,0x7F,
91
    0x72,0x04,0xB4,0x4D,0xCD,0x21,0xB4,0x4C,0xCD,0x21,0x00,0x00,0x80,0x00,0x00,0x00,
91
    0x72,0x04,0xB4,0x4D,0xCD,0x21,0xB4,0x4C,0xCD,0x21,0x00,0x00,0x80,0x00,0x00,0x00,
92
    0x5C,0x00,0x00,0x00,0x6C,0x00,0x00,0x00}; /* read comlink.asm for details */
92
    0x5C,0x00,0x00,0x00,0x6C,0x00,0x00,0x00}; /* read comlink.asm for details */
93
  const unsigned stack_paras = 12;
93
  const unsigned stack_paras = 12;
94
  unsigned alloc_paras, alloc_bytes;
94
  unsigned alloc_paras, alloc_bytes;
95
  FILE *fd;
95
  FILE *fd;
96
  /* open the link file and read the original target */
96
  /* open the link file and read the original target */
97
  fd = fopen(linkfile, "r");
97
  fd = fopen(linkfile, "r");
98
  if (fd == NULL) {
98
  if (fd == NULL) {
99
    kitten_printf(3, 21, "Error: Failed to open link file '%s' for read access.", linkfile);
99
    kitten_printf(3, 21, "Error: Failed to open link file '%s' for read access.", linkfile);
100
    puts("");
100
    puts("");
101
    return;
101
    return;
102
  }
102
  }
103
  x = fread(origtarget, 1, sizeof(origtarget) - 1, fd);
103
  x = fread(origtarget, 1, sizeof(origtarget) - 1, fd);
104
  origtarget[x] = 0;
104
  origtarget[x] = 0;
105
  fclose(fd);
105
  fclose(fd);
106
  /* validate the original target (ltrim to first \r or \n) */
106
  /* validate the original target (ltrim to first \r or \n) */
107
  for (x = 0; origtarget[x] != 0; x++) {
107
  for (x = 0; origtarget[x] != 0; x++) {
108
    if ((origtarget[x] == '\r') || (origtarget[x] == '\n')) {
108
    if ((origtarget[x] == '\r') || (origtarget[x] == '\n')) {
109
      origtarget[x] = 0;
109
      origtarget[x] = 0;
110
      break;
110
      break;
111
    }
111
    }
112
  }
112
  }
113
  /* translate the original target to a local path */
113
  /* translate the original target to a local path */
114
  shortfile = computelocalpath(origtarget, buff, dosdir, dirlist);
114
  shortfile = computelocalpath(origtarget, buff, dosdir, dirlist);
115
  /* compute the amount of memory the stub should leave for itself:
115
  /* compute the amount of memory the stub should leave for itself:
116
	- 16 paragraphs needed for the PSP
116
	- 16 paragraphs needed for the PSP
117
	- sizeof(comstub) bytes needed for code and data
117
	- sizeof(comstub) bytes needed for code and data
118
	- strlen(shortfile) + 1 for the command line
118
	- strlen(shortfile) + 1 for the command line
119
	- stack_paras paragraphs for the stack */
119
	- stack_paras paragraphs for the stack */
120
  alloc_paras = 16 + (sizeof(comstub) + strlen(shortfile) + 16) / 16 + stack_paras;
120
  alloc_paras = 16 + (sizeof(comstub) + strlen(shortfile) + 16) / 16 + stack_paras;
121
  alloc_bytes = 16 * alloc_paras;
121
  alloc_bytes = 16 * alloc_paras;
122
  comstub[1] = alloc_bytes & 0xff;
122
  comstub[1] = alloc_bytes & 0xff;
123
  comstub[2] = alloc_bytes >> 8;
123
  comstub[2] = alloc_bytes >> 8;
124
  comstub[4] = alloc_paras & 0xff;
124
  comstub[4] = alloc_paras & 0xff;
125
  comstub[5] = alloc_paras >> 8;
125
  comstub[5] = alloc_paras >> 8;
126
  /* write new content into the link file */
126
  /* write new content into the link file */
127
  fd = fopen(linkfile, "wb");
127
  fd = fopen(linkfile, "wb");
128
  if (fd == NULL) {
128
  if (fd == NULL) {
129
    kitten_printf(3, 22, "Error: Failed to open link file '%s' for write access.", linkfile);
129
    kitten_printf(3, 22, "Error: Failed to open link file '%s' for write access.", linkfile);
130
    puts("");
130
    puts("");
131
    return;
131
    return;
132
  }
132
  }
133
  fwrite(comstub, 1, sizeof(comstub), fd);
133
  fwrite(comstub, 1, sizeof(comstub), fd);
134
  fprintf(fd, "%s%s%c", buff, shortfile, 0);
134
  fprintf(fd, "%s%s%c", buff, shortfile, 0);
135
  fclose(fd);
135
  fclose(fd);
136
}
136
}
137
 
137
 
138
 
138
 
139
/* returns 0 if pkgname is not installed, non-zero otherwise */
139
/* returns 0 if pkgname is not installed, non-zero otherwise */
140
int is_package_installed(char *pkgname, char *dosdir, char *mapdrv) {
140
int is_package_installed(char *pkgname, char *dosdir, char *mapdrv) {
141
  char fname[512];
141
  char fname[512];
142
  sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
142
  sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
143
  mapdrives(fname, mapdrv);
143
  mapdrives(fname, mapdrv);
144
  if (fileexists(fname) != 0) { /* file exists -> package is installed */
144
  if (fileexists(fname) != 0) { /* file exists -> package is installed */
145
    return(1);
145
    return(1);
146
  } else {
146
  } else {
147
    return(0);
147
    return(0);
148
  }
148
  }
149
}
149
}
150
 
150
 
151
 
151
 
152
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
152
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
153
int validate_package_not_installed(char *pkgname, char *dosdir, char *mapdrv) {
153
int validate_package_not_installed(char *pkgname, char *dosdir, char *mapdrv) {
154
  if (is_package_installed(pkgname, dosdir, mapdrv) != 0) {
154
  if (is_package_installed(pkgname, dosdir, mapdrv) != 0) {
155
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
155
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
156
    puts("");
156
    puts("");
157
    return(-1);
157
    return(-1);
158
  }
158
  }
159
  return(0);
159
  return(0);
160
}
160
}
161
 
161
 
162
 
162
 
163
/* find a filename in a flist linked list, and returns a pointer to it */
163
/* find a filename in a flist linked list, and returns a pointer to it */
164
static struct flist_t *findfileinlist(struct flist_t *flist, char *fname) {
164
static struct flist_t *findfileinlist(struct flist_t *flist, char *fname) {
165
  while (flist != NULL) {
165
  while (flist != NULL) {
166
    if (strcmp(flist->fname, fname) == 0) return(flist);
166
    if (strcmp(flist->fname, fname) == 0) return(flist);
167
    flist = flist->next;
167
    flist = flist->next;
168
  }
168
  }
169
  return(NULL);
169
  return(NULL);
170
}
170
}
171
 
171
 
172
 
172
 
173
/* prepare a package for installation. this is mandatory before actually installing it!
173
/* prepare a package for installation. this is mandatory before actually installing it!
174
 * returns a pointer to the zip file's index on success, NULL on failure. the *zipfile pointer is updated with a file descriptor to the open zip file to install. */
174
 * returns a pointer to the zip file's index on success, NULL on failure. the *zipfile pointer is updated with a file descriptor to the open zip file to install. */
175
struct ziplist *pkginstall_preparepackage(struct pkgdb *pkgdb, char *pkgname, char *tempdir, char *localfile, int flags, char **repolist, FILE **zipfd, char *proxy, int proxyport, char *downloadingstring, char *dosdir, struct customdirs *dirlist, char *buffmem1k, char *mapdrv) {
175
struct ziplist *pkginstall_preparepackage(struct pkgdb *pkgdb, char *pkgname, char *tempdir, char *localfile, int flags, char **repolist, FILE **zipfd, char *proxy, int proxyport, char *downloadingstring, char *dosdir, struct customdirs *dirlist, char *buffmem1k, char *mapdrv) {
176
  char *fname;
176
  char *fname;
177
  char *zipfile;
177
  char *zipfile;
178
  char *appinfofile;
178
  char *appinfofile;
179
  int appinfopresence;
179
  int appinfopresence;
180
  char *shortfile;
180
  char *shortfile;
181
  struct ziplist *ziplinkedlist, *curzipnode, *prevzipnode;
181
  struct ziplist *ziplinkedlist, *curzipnode, *prevzipnode;
182
  struct flist_t *flist;
182
  struct flist_t *flist;
183
 
183
 
184
  fname = buffmem1k;
184
  fname = buffmem1k;
185
  zipfile = buffmem1k + 256;
185
  zipfile = buffmem1k + 256;
186
  appinfofile = buffmem1k + 512;
186
  appinfofile = buffmem1k + 512;
187
 
187
 
188
  strtolower(pkgname); /* convert pkgname to lower case, because the http repo is probably case sensitive */
188
  strtolower(pkgname); /* convert pkgname to lower case, because the http repo is probably case sensitive */
189
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
189
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
190
 
190
 
191
  /* check if not already installed, if already here, print a message "you might want to use update instead"
191
  /* check if not already installed, if already here, print a message "you might want to use update instead"
192
   * of course this must not be done if we are in the process of upgrading said package */
192
   * of course this must not be done if we are in the process of upgrading said package */
193
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir, mapdrv) != 0)) {
193
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir, mapdrv) != 0)) {
194
    return(NULL);
194
    return(NULL);
195
  }
195
  }
196
 
196
 
197
  if (localfile != NULL) {  /* if it's a local file, then we will have to skip all the network stuff */
197
  if (localfile != NULL) {  /* if it's a local file, then we will have to skip all the network stuff */
198
    strcpy(zipfile, localfile);
198
    strcpy(zipfile, localfile);
199
  } else {
199
  } else {
200
    zipfile[0] = 0;
200
    zipfile[0] = 0;
201
  }
201
  }
202
 
202
 
203
#ifndef NOREPOS
-
 
204
  if (zipfile[0] == 0) { /* need to download the package from a repository */
-
 
205
    char *instrepo;
-
 
206
    struct pkgdb *pkgnode, *lastnode;
-
 
207
    struct pkgrepo *pkgrepo;
-
 
208
    int repoid;
-
 
209
    unsigned long zipfilecrc, remotecrc;
-
 
210
    unsigned char *buff;
-
 
211
    int buffreadres;
-
 
212
    char *pkgext; /* zip or zib */
-
 
213
 
-
 
214
    /* look into the db to find the package */
-
 
215
    pkgnode = findpkg(pkgdb, pkgname, &lastnode);
-
 
216
    if (pkgnode == NULL) { /* no such package found in repositories */
-
 
217
      kitten_printf(3, 1, "No package '%s' found in online repositories.", pkgname);
-
 
218
      puts("");
-
 
219
      return(NULL);
-
 
220
    }
-
 
221
 
-
 
222
    /* if found - check the list of repositories */
-
 
223
    if (pkgnode->repolist == NULL) {
-
 
224
      kitten_printf(3, 2, "Package '%s' is not available in repositories.", pkgname);
-
 
225
      puts("");
-
 
226
      return(NULL);
-
 
227
    }
-
 
228
 
-
 
229
    if (pkgnode->repolist->nextrepo != NULL) { /* available from more than 1 repo.. */
-
 
230
      char userchoicestr[8];
-
 
231
      int userchoice, latestver_repoid = 1;
-
 
232
      struct pkgrepo *xrepo, *latestver = pkgnode->repolist;
-
 
233
      /* check out if we are able to find out the newest version */
-
 
234
      repoid = 2; /* setting to 2, because we start iterating at the second repo entry */
-
 
235
      for (xrepo = pkgnode->repolist->nextrepo; xrepo != NULL; xrepo = xrepo->nextrepo) {
-
 
236
        int versionnewerres = isversionnewer(latestver->version, xrepo->version);
-
 
237
        if (versionnewerres > 0) {
-
 
238
          latestver = xrepo;
-
 
239
          latestver_repoid = repoid;
-
 
240
        } else if (versionnewerres < 0) {  /* unable to tell which version is newer */
-
 
241
          latestver_repoid = -1;
-
 
242
          break;
-
 
243
        }
-
 
244
        repoid += 1;
-
 
245
      }
-
 
246
      if (latestver_repoid > 0) {
-
 
247
        repoid = latestver_repoid;
-
 
248
      } else { /* newest version could not be figured out, so let's ask the user to choose */
-
 
249
        puts("");
-
 
250
        kitten_printf(3, 3, "%s is available from several repositories. Choose which one to use:", pkgname);
-
 
251
        puts("");
-
 
252
        repoid = 1;
-
 
253
        for (xrepo = pkgnode->repolist; xrepo != NULL; xrepo = xrepo->nextrepo) {
-
 
254
          printf(" %d) %s %s (%s)\n", repoid, pkgnode->name, xrepo->version, repolist[xrepo->repo]);
-
 
255
          repoid += 1;
-
 
256
        }
-
 
257
        for (;;) {
-
 
258
          kitten_printf(3, 4, "Your choice:");
-
 
259
          printf(" ");
-
 
260
          fgets(userchoicestr, 6, stdin);
-
 
261
          userchoice = atoi(userchoicestr);
-
 
262
          if ((userchoice < 1) || (userchoice >= repoid)) {
-
 
263
            kitten_puts(3, 5, "Invalid choice!");
-
 
264
          } else {
-
 
265
            break;
-
 
266
          }
-
 
267
        }
-
 
268
        repoid = userchoice;
-
 
269
      }
-
 
270
    } else { /* available only from one repository - get it there */
-
 
271
      repoid = 1;
-
 
272
    }
-
 
273
    pkgrepo = pkgnode->repolist;
-
 
274
    for (; repoid > 1; repoid--) pkgrepo = pkgrepo->nextrepo;
-
 
275
    instrepo = repolist[pkgrepo->repo];
-
 
276
 
-
 
277
    /* select the package extension: zip or zib */
-
 
278
    if ((flags & PKGINST_NOSOURCE) && (pkgrepo->crc32zib != 0)) { /* use zib if available and if no sources needed */
-
 
279
      pkgext = "zib"; /* zib is the same thing as zip, but binary-only (no sources) */
-
 
280
      remotecrc = pkgrepo->crc32zib;
-
 
281
    } else { /* otherwise use the full-blown zip package */
-
 
282
      pkgext = "zip";
-
 
283
      remotecrc = pkgrepo->crc32zip;
-
 
284
    }
-
 
285
 
-
 
286
    /* if it's a network repo, download the package from repoid into the temp directory */
-
 
287
    if (detect_localpath(instrepo) == 0) {
-
 
288
      sprintf(fname, "%s%s.%s", instrepo, pkgname, pkgext);
-
 
289
      sprintf(zipfile, "%s\\fdnpkg.tmp", tempdir);
-
 
290
      kitten_printf(3, 6, "Downloading package %s...", fname);
-
 
291
      puts("");
-
 
292
      if (http_get(fname, zipfile, proxy, proxyport, downloadingstring) <= 0) {
-
 
293
        kitten_puts(3, 7, "Error downloading package. Aborted.");
-
 
294
        return(NULL);
-
 
295
      }
-
 
296
    } else { /* else it's an on-disk repo, so we can use the package right from there */
-
 
297
      sprintf(zipfile, "%s%s.%s", instrepo, pkgname, pkgext);
-
 
298
    }
-
 
299
    /* check the CRC of the downloaded file */
-
 
300
    buff = malloc(4096);  /* use a 4K buffer to compute file's CRC */
-
 
301
    if (buff == NULL) {
-
 
302
      kitten_puts(3, 15, "Error: Out of memory while computing the CRC of the package!");
-
 
303
      return(NULL);
-
 
304
    }
-
 
305
    *zipfd = fopen(zipfile, "rb");
-
 
306
    if (*zipfd == NULL) {
-
 
307
      kitten_puts(3, 14, "Error: Failed to open the downloaded package. Installation aborted.");
-
 
308
      free(buff);
-
 
309
      return(NULL);
-
 
310
    }
-
 
311
    zipfilecrc = crc32_init();
-
 
312
    while ((buffreadres = read(fileno(*zipfd), buff, 4096)) > 0) {
-
 
313
      crc32_feed(&zipfilecrc, buff, buffreadres);
-
 
314
    }
-
 
315
    crc32_finish(&zipfilecrc);
-
 
316
    fclose(*zipfd);
-
 
317
    free(buff);
-
 
318
    if (zipfilecrc != remotecrc) {
-
 
319
      kitten_puts(3, 13, "Error: Downloaded package had wrong CRC. Installation aborted.");
-
 
320
      return(NULL);
-
 
321
    }
-
 
322
  } /* if (zipfile[0] == 0) */
-
 
323
#endif
-
 
324
 
-
 
325
  /* Now let's check the content of the zip file */
203
  /* Now let's check the content of the zip file */
326
 
204
 
327
  *zipfd = fopen(zipfile, "rb");
205
  *zipfd = fopen(zipfile, "rb");
328
  if (*zipfd == NULL) {
206
  if (*zipfd == NULL) {
329
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
207
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
330
    return(NULL);
208
    return(NULL);
331
  }
209
  }
332
  ziplinkedlist = zip_listfiles(*zipfd);
210
  ziplinkedlist = zip_listfiles(*zipfd);
333
  if (ziplinkedlist == NULL) {
211
  if (ziplinkedlist == NULL) {
334
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
212
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
335
    fclose(*zipfd);
213
    fclose(*zipfd);
336
    return(NULL);
214
    return(NULL);
337
  }
215
  }
338
  /* if updating, load the list of files belonging to the current package */
216
  /* if updating, load the list of files belonging to the current package */
339
  if ((flags & PKGINST_UPDATE) != 0) {
217
  if ((flags & PKGINST_UPDATE) != 0) {
340
    flist = pkg_loadflist(pkgname, dosdir);
218
    flist = pkg_loadflist(pkgname, dosdir);
341
  } else {
219
  } else {
342
    flist = NULL;
220
    flist = NULL;
343
  }
221
  }
344
  /* Verify that there's no collision with existing local files, look for the appinfo presence, get rid of sources if required, and rename BAT links into COM files */
222
  /* Verify that there's no collision with existing local files, look for the appinfo presence, get rid of sources if required, and rename BAT links into COM files */
345
  appinfopresence = 0;
223
  appinfopresence = 0;
346
  prevzipnode = NULL;
224
  prevzipnode = NULL;
347
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
225
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
348
    /* change all slashes to backslashes, and switch into all-lowercase */
226
    /* change all slashes to backslashes, and switch into all-lowercase */
349
    slash2backslash(curzipnode->filename);
227
    slash2backslash(curzipnode->filename);
350
    strtolower(curzipnode->filename);
228
    strtolower(curzipnode->filename);
351
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
229
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
352
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
230
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
353
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
231
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
354
    }
232
    }
355
    /* if --nosource specified, skip sources */
233
    /* if --nosource specified, skip sources */
356
    if ((flags & PKGINST_NOSOURCE) != 0) {
234
    if ((flags & PKGINST_NOSOURCE) != 0) {
357
      if (fdnpkg_strcasestr(curzipnode->filename, "source\\") == curzipnode->filename) { /* drop this file */
235
      if (fdnpkg_strcasestr(curzipnode->filename, "source\\") == curzipnode->filename) { /* drop this file */
358
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see below */
236
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see below */
359
      }
237
      }
360
    }
238
    }
361
    /* is it a "link file"? */
239
    /* is it a "link file"? */
362
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
240
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
363
      /* skip links, if that's what the user wants */
241
      /* skip links, if that's what the user wants */
364
      if ((flags & PKGINST_SKIPLINKS) != 0) {
242
      if ((flags & PKGINST_SKIPLINKS) != 0) {
365
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
243
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
366
      } else {
244
      } else {
367
        /* if it's a *.BAT link, then rename it to *.COM */
245
        /* if it's a *.BAT link, then rename it to *.COM */
368
        char *ext = getfext(curzipnode->filename);
246
        char *ext = getfext(curzipnode->filename);
369
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
247
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
370
      }
248
      }
371
    }
249
    }
372
 
250
 
373
    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) */
251
    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) */
374
      if (prevzipnode == NULL) {  /* take the item out of the list */
252
      if (prevzipnode == NULL) {  /* take the item out of the list */
375
        ziplinkedlist = curzipnode->nextfile;
253
        ziplinkedlist = curzipnode->nextfile;
376
        free(curzipnode); /* free the item */
254
        free(curzipnode); /* free the item */
377
        curzipnode = ziplinkedlist;
255
        curzipnode = ziplinkedlist;
378
      } else {
256
      } else {
379
        prevzipnode->nextfile = curzipnode->nextfile;
257
        prevzipnode->nextfile = curzipnode->nextfile;
380
        free(curzipnode); /* free the item */
258
        free(curzipnode); /* free the item */
381
        curzipnode = prevzipnode->nextfile;
259
        curzipnode = prevzipnode->nextfile;
382
      }
260
      }
383
      continue; /* go to the next item */
261
      continue; /* go to the next item */
384
    }
262
    }
385
    /* validate that the file has a valid filename (8+3, no shady chars...) */
263
    /* validate that the file has a valid filename (8+3, no shady chars...) */
386
    if (validfilename(curzipnode->filename) != 0) {
264
    if (validfilename(curzipnode->filename) != 0) {
387
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
265
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
388
      printf(" %s\n", curzipnode->filename);
266
      printf(" %s\n", curzipnode->filename);
389
      zip_freelist(&ziplinkedlist);
267
      zip_freelist(&ziplinkedlist);
390
      fclose(*zipfd);
268
      fclose(*zipfd);
391
      return(NULL);
269
      return(NULL);
392
    }
270
    }
393
    /* look out for collisions with already existing files (unless we are
271
    /* look out for collisions with already existing files (unless we are
394
     * updating the package and the local file belongs to it */
272
     * updating the package and the local file belongs to it */
395
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
273
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
396
    mapdrives(fname, mapdrv);
274
    mapdrives(fname, mapdrv);
397
    strcat(fname, shortfile);
275
    strcat(fname, shortfile);
398
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
276
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
399
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
277
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
400
      printf(" %s\n", fname);
278
      printf(" %s\n", fname);
401
      zip_freelist(&ziplinkedlist);
279
      zip_freelist(&ziplinkedlist);
402
      fclose(*zipfd);
280
      fclose(*zipfd);
403
      return(NULL);
281
      return(NULL);
404
    }
282
    }
405
    /* abort if any entry is encrypted */
283
    /* abort if any entry is encrypted */
406
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
284
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
407
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
285
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
408
      puts("");
286
      puts("");
409
      printf(" %s\n", curzipnode->filename);
287
      printf(" %s\n", curzipnode->filename);
410
      zip_freelist(&ziplinkedlist);
288
      zip_freelist(&ziplinkedlist);
411
      fclose(*zipfd);
289
      fclose(*zipfd);
412
      return(NULL);
290
      return(NULL);
413
    }
291
    }
414
    /* abort if any file is compressed with an unsupported method */
292
    /* abort if any file is compressed with an unsupported method */
415
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
293
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
416
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
294
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
417
      puts("");
295
      puts("");
418
      printf(" %s\n", curzipnode->filename);
296
      printf(" %s\n", curzipnode->filename);
419
      zip_freelist(&ziplinkedlist);
297
      zip_freelist(&ziplinkedlist);
420
      fclose(*zipfd);
298
      fclose(*zipfd);
421
      return(NULL);
299
      return(NULL);
422
    }
300
    }
423
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
301
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
424
    prevzipnode = curzipnode;
302
    prevzipnode = curzipnode;
425
    curzipnode = curzipnode->nextfile;
303
    curzipnode = curzipnode->nextfile;
426
  }
304
  }
427
  /* if appinfo file not found, this is not a real FreeDOS package */
305
  /* if appinfo file not found, this is not a real FreeDOS package */
428
  if (appinfopresence != 1) {
306
  if (appinfopresence != 1) {
429
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
307
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
430
    puts("");
308
    puts("");
431
    zip_freelist(&ziplinkedlist);
309
    zip_freelist(&ziplinkedlist);
432
    fclose(*zipfd);
310
    fclose(*zipfd);
433
    return(NULL);
311
    return(NULL);
434
  }
312
  }
435
 
313
 
436
  return(ziplinkedlist);
314
  return(ziplinkedlist);
437
}
315
}
438
 
316
 
439
 
317
 
440
/* install a package that has been prepared already. returns 0 on success,
318
/* install a package that has been prepared already. returns 0 on success,
441
 * or a negative value on error, or a positive value on warning */
319
 * or a negative value on error, or a positive value on warning */
442
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd, char *mapdrv) {
320
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd, char *mapdrv) {
443
  char *buff;
321
  char *buff;
444
  char *fulldestfilename;
322
  char *fulldestfilename;
445
  char packageslst[64];
323
  char packageslst[64];
446
  char *shortfile;
324
  char *shortfile;
447
  long filesextractedsuccess = 0, filesextractedfailure = 0;
325
  long filesextractedsuccess = 0, filesextractedfailure = 0;
448
  struct ziplist *curzipnode;
326
  struct ziplist *curzipnode;
449
  FILE *lstfd;
327
  FILE *lstfd;
450
 
328
 
451
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
329
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
452
 
330
 
453
  buff = malloc(512);
331
  buff = malloc(512);
454
  fulldestfilename = malloc(1024);
332
  fulldestfilename = malloc(1024);
455
  if ((buff == NULL) || (fulldestfilename == NULL)) {
333
  if ((buff == NULL) || (fulldestfilename == NULL)) {
456
    kitten_puts(8, 0, "Out of memory!");
334
    kitten_puts(8, 0, "Out of memory!");
457
    zip_freelist(&ziplinkedlist);
335
    zip_freelist(&ziplinkedlist);
458
    free(buff);
336
    free(buff);
459
    free(fulldestfilename);
337
    free(fulldestfilename);
460
    return(-1);
338
    return(-1);
461
  }
339
  }
462
 
340
 
463
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
341
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
464
  sprintf(buff, "%s\\packages\\", dosdir);
342
  sprintf(buff, "%s\\packages\\", dosdir);
465
  mkpath(buff);
343
  mkpath(buff);
466
 
344
 
467
  /* open the lst file */
345
  /* open the lst file */
468
  sprintf(buff, "%s\\%s", dosdir, packageslst);
346
  sprintf(buff, "%s\\%s", dosdir, packageslst);
469
  mapdrives(buff, mapdrv);
347
  mapdrives(buff, mapdrv);
470
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
348
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
471
  if (lstfd == NULL) {
349
  if (lstfd == NULL) {
472
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
350
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
473
    puts("");
351
    puts("");
474
    zip_freelist(&ziplinkedlist);
352
    zip_freelist(&ziplinkedlist);
475
    free(buff);
353
    free(buff);
476
    free(fulldestfilename);
354
    free(fulldestfilename);
477
    return(-2);
355
    return(-2);
478
  }
356
  }
479
 
357
 
480
  /* write list of files in zip into the lst, and create the directories structure */
358
  /* write list of files in zip into the lst, and create the directories structure */
481
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
359
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
482
    int unzip_result;
360
    int unzip_result;
483
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
361
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
484
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
362
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
485
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
363
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
486
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
364
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
487
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
365
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
488
    /* remap drive letter, if needed (AFTER writing to lstfd) */
366
    /* remap drive letter, if needed (AFTER writing to lstfd) */
489
    mapdrives(buff, mapdrv);
367
    mapdrives(buff, mapdrv);
490
    /* create the path, just in case it doesn't exist yet */
368
    /* create the path, just in case it doesn't exist yet */
491
    mkpath(buff);
369
    mkpath(buff);
492
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
370
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
493
    /* Now unzip the file */
371
    /* Now unzip the file */
494
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
372
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
495
    if (unzip_result != 0) {
373
    if (unzip_result != 0) {
496
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
374
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
497
      printf(" [%d]\n", unzip_result);
375
      printf(" [%d]\n", unzip_result);
498
      filesextractedfailure += 1;
376
      filesextractedfailure += 1;
499
    } else {
377
    } else {
500
      printf(" %s -> %s\n", curzipnode->filename, buff);
378
      printf(" %s -> %s\n", curzipnode->filename, buff);
501
      filesextractedsuccess += 1;
379
      filesextractedsuccess += 1;
502
    }
380
    }
503
    /* if it's a LINK file, recompute a new content */
381
    /* if it's a LINK file, recompute a new content */
504
    if (islinkfile(curzipnode->filename) != 0) {
382
    if (islinkfile(curzipnode->filename) != 0) {
505
      unmapdrives(buff, mapdrv);
383
      unmapdrives(buff, mapdrv);
506
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
384
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
507
    }
385
    }
508
  }
386
  }
509
  fclose(lstfd);
387
  fclose(lstfd);
510
 
388
 
511
  /* free the ziplist and close file descriptor */
389
  /* free the ziplist and close file descriptor */
512
  zip_freelist(&ziplinkedlist);
390
  zip_freelist(&ziplinkedlist);
513
  free(buff);
391
  free(buff);
514
  free(fulldestfilename);
392
  free(fulldestfilename);
515
 
393
 
516
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
394
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
517
  puts("");
395
  puts("");
518
  return(filesextractedfailure);
396
  return(filesextractedfailure);
519
}
397
}
520
 
398