Subversion Repositories SvarDOS

Rev

Rev 259 | Rev 263 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 259 Rev 260
1
/*
1
/*
2
 * This file is part of pkginst (SvarDOS)
2
 * This file is part of pkginst (SvarDOS)
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 "helpers.h"   /* slash2backslash(), strtolower() */
15
#include "helpers.h"   /* slash2backslash(), strtolower() */
16
#include "fileexst.h"
16
#include "fileexst.h"
17
#include "kprintf.h"
17
#include "kprintf.h"
18
#include "libunzip.h"  /* zip_listfiles()... */
18
#include "libunzip.h"  /* zip_listfiles()... */
19
#include "showinst.h"  /* pkg_loadflist() */
19
#include "showinst.h"  /* pkg_loadflist() */
20
#include "pkginst.h"   /* include self for control */
20
#include "pkginst.h"   /* include self for control */
21
#include "version.h"
21
#include "version.h"
22
 
22
 
23
 
23
 
24
/* return 1 if fname looks like a link filename, 0 otherwise */
-
 
25
static int islinkfile(const char *fname) {
-
 
26
  char *link1 = "LINKS\\";
-
 
27
  char *link2 = "links\\";
-
 
28
  int x;
-
 
29
  for (x = 0; ; x++) {
-
 
30
    if (link1[x] == 0) return(1);
-
 
31
    if ((fname[x] != link1[x]) && (fname[x] != link2[x])) return(0);
-
 
32
  }
-
 
33
}
-
 
34
 
-
 
35
 
-
 
36
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
24
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
37
 * nonzero otherwise. */
25
 * nonzero otherwise. */
38
static int validfilename(const char *fname) {
26
static int validfilename(const char *fname) {
39
  int i, i2;
27
  int i, i2;
40
  char *validchars = "!#$%&'()-@^_`{}~";
28
  char *validchars = "!#$%&'()-@^_`{}~";
41
  int elemlen = 0;
29
  int elemlen = 0;
42
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
30
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
43
                         a filename or an extension */
31
                         a filename or an extension */
44
  /* look for invalid chars in the entire string, and check the length of the
32
  /* look for invalid chars in the entire string, and check the length of the
45
   * element at the same time */
33
   * element at the same time */
46
  for (i = 0; fname[i] != 0; i++) {
34
  for (i = 0; fname[i] != 0; i++) {
47
    /* director separators are threated specially */
35
    /* director separators are threated specially */
48
    if (fname[i] == '\\') {
36
    if (fname[i] == '\\') {
49
      elemlen = 0;
37
      elemlen = 0;
50
      elemmaxlen = 8;
38
      elemmaxlen = 8;
51
      continue;
39
      continue;
52
    }
40
    }
53
    /* '.' switches the check into extension mode */
41
    /* '.' switches the check into extension mode */
54
    if (fname[i] == '.') {
42
    if (fname[i] == '.') {
55
      if (elemlen == 0) return(-1); /* a file must have a base name */
43
      if (elemlen == 0) return(-1); /* a file must have a base name */
56
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
44
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
57
      elemlen = 0;
45
      elemlen = 0;
58
      elemmaxlen = 3;
46
      elemmaxlen = 3;
59
      continue;
47
      continue;
60
    }
48
    }
61
    /* check that the element is not too long */
49
    /* check that the element is not too long */
62
    if (++elemlen > elemmaxlen) return(-3);
50
    if (++elemlen > elemmaxlen) return(-3);
63
    /* look for valid characters */
51
    /* look for valid characters */
64
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
52
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
65
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
53
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
66
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
54
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
67
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
55
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
68
    /* look for other valid characters */
56
    /* look for other valid characters */
69
    for (i2 = 0; validchars[i2] != 0; i2++) {
57
    for (i2 = 0; validchars[i2] != 0; i2++) {
70
      if (fname[i] == validchars[i2]) break;
58
      if (fname[i] == validchars[i2]) break;
71
    }
59
    }
72
    if (validchars[i2] != 0) continue;
60
    if (validchars[i2] != 0) continue;
73
    /* if we are here, then the character is invalid */
61
    /* if we are here, then the character is invalid */
74
    return(-4);
62
    return(-4);
75
  }
63
  }
76
  /* all checks passed */
64
  /* all checks passed */
77
  return(0);
65
  return(0);
78
}
66
}
79
 
67
 
80
 
68
 
81
/* processes a link file - that is, reads the target inside, and overwrite
-
 
82
 * the file with new content */
-
 
83
static void processlinkfile(const char *linkfile, const char *dosdir, const struct customdirs *dirlist, char *buff) {
-
 
84
  char origtarget[512];
-
 
85
  int x;
-
 
86
  char *shortfile;
-
 
87
  unsigned char comstub[] = { /* machine code of a COM stub launcher */
-
 
88
    0xBC,0x00,0x00,0xBB,0x00,0x10,0xB4,0x4A,0xCD,0x21,0xBB,0x2A,0x01,0x8C,0x5F,0x04,
-
 
89
    0x8C,0x5F,0x08,0x8C,0x5F,0x0C,0xB8,0x00,0x4B,0xBA,0x38,0x01,0xCD,0x21,0xB0,0x7F,
-
 
90
    0x72,0x04,0xB4,0x4D,0xCD,0x21,0xB4,0x4C,0xCD,0x21,0x00,0x00,0x80,0x00,0x00,0x00,
-
 
91
    0x5C,0x00,0x00,0x00,0x6C,0x00,0x00,0x00}; /* read comlink.asm for details */
-
 
92
  const unsigned stack_paras = 12;
-
 
93
  unsigned alloc_paras, alloc_bytes;
-
 
94
  FILE *fd;
-
 
95
  /* open the link file and read the original target */
-
 
96
  fd = fopen(linkfile, "r");
-
 
97
  if (fd == NULL) {
-
 
98
    kitten_printf(3, 21, "Error: Failed to open link file '%s' for read access.", linkfile);
-
 
99
    puts("");
-
 
100
    return;
-
 
101
  }
-
 
102
  x = fread(origtarget, 1, sizeof(origtarget) - 1, fd);
-
 
103
  origtarget[x] = 0;
-
 
104
  fclose(fd);
-
 
105
  /* validate the original target (ltrim to first \r or \n) */
-
 
106
  for (x = 0; origtarget[x] != 0; x++) {
-
 
107
    if ((origtarget[x] == '\r') || (origtarget[x] == '\n')) {
-
 
108
      origtarget[x] = 0;
-
 
109
      break;
-
 
110
    }
-
 
111
  }
-
 
112
  /* translate the original target to a local path */
-
 
113
  shortfile = computelocalpath(origtarget, buff, dosdir, dirlist);
-
 
114
  /* compute the amount of memory the stub should leave for itself:
-
 
115
	- 16 paragraphs needed for the PSP
-
 
116
	- sizeof(comstub) bytes needed for code and data
-
 
117
	- strlen(shortfile) + 1 for the command line
-
 
118
	- stack_paras paragraphs for the stack */
-
 
119
  alloc_paras = 16 + (sizeof(comstub) + strlen(shortfile) + 16) / 16 + stack_paras;
-
 
120
  alloc_bytes = 16 * alloc_paras;
-
 
121
  comstub[1] = alloc_bytes & 0xff;
-
 
122
  comstub[2] = alloc_bytes >> 8;
-
 
123
  comstub[4] = alloc_paras & 0xff;
-
 
124
  comstub[5] = alloc_paras >> 8;
-
 
125
  /* write new content into the link file */
-
 
126
  fd = fopen(linkfile, "wb");
-
 
127
  if (fd == NULL) {
-
 
128
    kitten_printf(3, 22, "Error: Failed to open link file '%s' for write access.", linkfile);
-
 
129
    puts("");
-
 
130
    return;
-
 
131
  }
-
 
132
  fwrite(comstub, 1, sizeof(comstub), fd);
-
 
133
  fprintf(fd, "%s%s%c", buff, shortfile, 0);
-
 
134
  fclose(fd);
-
 
135
}
-
 
136
 
-
 
137
 
-
 
138
/* returns 0 if pkgname is not installed, non-zero otherwise */
69
/* returns 0 if pkgname is not installed, non-zero otherwise */
139
int is_package_installed(const char *pkgname, const char *dosdir) {
70
int is_package_installed(const char *pkgname, const char *dosdir) {
140
  char fname[512];
71
  char fname[512];
141
  sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
72
  sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname);
142
  return(fileexists(fname)); /* file exists -> package is installed */
73
  return(fileexists(fname)); /* file exists -> package is installed */
143
}
74
}
144
 
75
 
145
 
76
 
146
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
77
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
147
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
78
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
148
  if (is_package_installed(pkgname, dosdir) != 0) {
79
  if (is_package_installed(pkgname, dosdir) != 0) {
149
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
80
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
150
    puts("");
81
    puts("");
151
    return(-1);
82
    return(-1);
152
  }
83
  }
153
  return(0);
84
  return(0);
154
}
85
}
155
 
86
 
156
 
87
 
157
/* find a filename in a flist linked list, and returns a pointer to it */
88
/* find a filename in a flist linked list, and returns a pointer to it */
158
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
89
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
159
  while (flist != NULL) {
90
  while (flist != NULL) {
160
    if (strcmp(flist->fname, fname) == 0) return(flist);
91
    if (strcasecmp(flist->fname, fname) == 0) return(flist);
161
    flist = flist->next;
92
    flist = flist->next;
162
  }
93
  }
163
  return(NULL);
94
  return(NULL);
164
}
95
}
165
 
96
 
166
 
97
 
167
/* prepare a package for installation. this is mandatory before actually installing it!
98
/* prepare a package for installation. this is mandatory before actually installing it!
168
 * returns a pointer to the zip file's index on success, NULL on failure. the **zipfd pointer is updated with a file descriptor to the open zip file to install. */
99
 * returns a pointer to the zip file's index on success, NULL on failure. the **zipfd pointer is updated with a file descriptor to the open zip file to install. */
169
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
100
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
170
  char fname[256];
101
  char fname[256];
171
  char appinfofile[256];
102
  char appinfofile[256];
172
  int appinfopresence;
103
  int appinfopresence;
173
  char *shortfile;
104
  char *shortfile;
174
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
105
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
175
  struct flist_t *flist = NULL;
106
  struct flist_t *flist = NULL;
176
 
107
 
177
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
108
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
178
 
109
 
179
  /* check if not already installed, if already here, print a message "you might want to use update instead"
110
  /* check if not already installed, if already here, print a message "you might want to use update instead"
180
   * of course this must not be done if we are in the process of upgrading said package */
111
   * of course this must not be done if we are in the process of upgrading said package */
181
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
112
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
182
    return(NULL);
113
    return(NULL);
183
  }
114
  }
184
 
115
 
185
  /* Now let's check the content of the zip file */
116
  /* Now let's check the content of the zip file */
186
 
117
 
187
  *zipfd = fopen(zipfile, "rb");
118
  *zipfd = fopen(zipfile, "rb");
188
  if (*zipfd == NULL) {
119
  if (*zipfd == NULL) {
189
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
120
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
190
    goto RAII;
121
    goto RAII;
191
  }
122
  }
192
  ziplinkedlist = zip_listfiles(*zipfd);
123
  ziplinkedlist = zip_listfiles(*zipfd);
193
  if (ziplinkedlist == NULL) {
124
  if (ziplinkedlist == NULL) {
194
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
125
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
195
    goto RAII;
126
    goto RAII;
196
  }
127
  }
197
  /* if updating, load the list of files belonging to the current package */
128
  /* if updating, load the list of files belonging to the current package */
198
  if ((flags & PKGINST_UPDATE) != 0) {
129
  if ((flags & PKGINST_UPDATE) != 0) {
199
    flist = pkg_loadflist(pkgname, dosdir);
130
    flist = pkg_loadflist(pkgname, dosdir);
200
  }
131
  }
201
  /* 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 */
132
  /* Verify that there's no collision with existing local files, look for the appinfo presence */
202
  appinfopresence = 0;
133
  appinfopresence = 0;
203
  prevzipnode = NULL;
134
  prevzipnode = NULL;
204
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
135
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
205
    /* change all slashes to backslashes, and switch into all-lowercase */
136
    /* change all slashes to backslashes, and switch into all-lowercase */
206
    slash2backslash(curzipnode->filename);
137
    slash2backslash(curzipnode->filename);
207
    strtolower(curzipnode->filename);
138
    strtolower(curzipnode->filename);
208
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
139
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
209
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
140
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
210
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
141
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
211
    }
142
    }
212
    /* is it a "link file"? */
143
    /* is it a "link file"? skip it - link files are no longer supported */
213
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
144
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
214
      /* skip links, if that's what the user wants */
-
 
215
      if ((flags & PKGINST_SKIPLINKS) != 0) {
-
 
216
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
145
      curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
217
      } else {
-
 
218
        /* if it's a *.BAT link, then rename it to *.COM */
-
 
219
        char *ext = getfext(curzipnode->filename);
-
 
220
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
-
 
221
      }
-
 
222
    }
146
    }
223
 
147
 
224
    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) */
148
    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) */
225
      if (prevzipnode == NULL) {  /* take the item out of the list */
149
      if (prevzipnode == NULL) {  /* take the item out of the list */
226
        ziplinkedlist = curzipnode->nextfile;
150
        ziplinkedlist = curzipnode->nextfile;
227
        free(curzipnode); /* free the item */
151
        free(curzipnode); /* free the item */
228
        curzipnode = ziplinkedlist;
152
        curzipnode = ziplinkedlist;
229
      } else {
153
      } else {
230
        prevzipnode->nextfile = curzipnode->nextfile;
154
        prevzipnode->nextfile = curzipnode->nextfile;
231
        free(curzipnode); /* free the item */
155
        free(curzipnode); /* free the item */
232
        curzipnode = prevzipnode->nextfile;
156
        curzipnode = prevzipnode->nextfile;
233
      }
157
      }
234
      continue; /* go to the next item */
158
      continue; /* go to the next item */
235
    }
159
    }
236
    /* validate that the file has a valid filename (8+3, no shady chars...) */
160
    /* validate that the file has a valid filename (8+3, no shady chars...) */
237
    if (validfilename(curzipnode->filename) != 0) {
161
    if (validfilename(curzipnode->filename) != 0) {
238
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
162
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
239
      printf(" %s\n", curzipnode->filename);
163
      printf(" %s\n", curzipnode->filename);
240
      goto RAII_ERR;
164
      goto RAII_ERR;
241
    }
165
    }
242
    /* look out for collisions with already existing files (unless we are
166
    /* look out for collisions with already existing files (unless we are
243
     * updating the package and the local file belongs to it */
167
     * updating the package and the local file belongs to it */
244
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
168
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
245
    strcat(fname, shortfile);
169
    strcat(fname, shortfile);
246
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
170
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
247
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
171
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
248
      printf(" %s\n", fname);
172
      printf(" %s\n", fname);
249
      goto RAII_ERR;
173
      goto RAII_ERR;
250
    }
174
    }
251
    /* abort if any entry is encrypted */
175
    /* abort if any entry is encrypted */
252
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
176
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
253
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
177
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
254
      puts("");
178
      puts("");
255
      printf(" %s\n", curzipnode->filename);
179
      printf(" %s\n", curzipnode->filename);
256
      goto RAII_ERR;
180
      goto RAII_ERR;
257
    }
181
    }
258
    /* abort if any file is compressed with an unsupported method */
182
    /* abort if any file is compressed with an unsupported method */
259
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
183
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
260
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
184
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
261
      puts("");
185
      puts("");
262
      printf(" %s\n", curzipnode->filename);
186
      printf(" %s\n", curzipnode->filename);
263
      goto RAII_ERR;
187
      goto RAII_ERR;
264
    }
188
    }
265
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
189
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
266
    prevzipnode = curzipnode;
190
    prevzipnode = curzipnode;
267
    curzipnode = curzipnode->nextfile;
191
    curzipnode = curzipnode->nextfile;
268
  }
192
  }
269
  /* if appinfo file not found, this is not a real FreeDOS package */
193
  /* if appinfo file not found, this is not a real FreeDOS package */
270
  if (appinfopresence != 1) {
194
  if (appinfopresence != 1) {
271
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
195
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
272
    puts("");
196
    puts("");
273
    goto RAII_ERR;
197
    goto RAII_ERR;
274
  }
198
  }
275
 
199
 
276
  goto RAII;
200
  goto RAII;
277
 
201
 
278
  RAII_ERR:
202
  RAII_ERR:
279
  zip_freelist(&ziplinkedlist);
203
  zip_freelist(&ziplinkedlist);
280
  ziplinkedlist = NULL;
204
  ziplinkedlist = NULL;
281
  if ((zipfd != NULL) && (*zipfd != NULL)) {
205
  if ((zipfd != NULL) && (*zipfd != NULL)) {
282
    fclose(*zipfd);
206
    fclose(*zipfd);
283
    *zipfd = NULL;
207
    *zipfd = NULL;
284
  }
208
  }
285
 
209
 
286
  RAII:
210
  RAII:
287
  pkg_freeflist(flist);
211
  pkg_freeflist(flist);
288
  return(ziplinkedlist);
212
  return(ziplinkedlist);
289
}
213
}
290
 
214
 
291
 
215
 
292
/* install a package that has been prepared already. returns 0 on success,
216
/* install a package that has been prepared already. returns 0 on success,
293
 * or a negative value on error, or a positive value on warning */
217
 * or a negative value on error, or a positive value on warning */
294
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
218
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
295
  char *buff;
219
  char *buff;
296
  char *fulldestfilename;
220
  char *fulldestfilename;
297
  char packageslst[64];
221
  char packageslst[64];
298
  char *shortfile;
222
  char *shortfile;
299
  long filesextractedsuccess = 0, filesextractedfailure = 0;
223
  long filesextractedsuccess = 0, filesextractedfailure = 0;
300
  struct ziplist *curzipnode;
224
  struct ziplist *curzipnode;
301
  FILE *lstfd;
225
  FILE *lstfd;
302
 
226
 
303
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
227
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
304
 
228
 
305
  buff = malloc(512);
229
  buff = malloc(512);
306
  fulldestfilename = malloc(1024);
230
  fulldestfilename = malloc(1024);
307
  if ((buff == NULL) || (fulldestfilename == NULL)) {
231
  if ((buff == NULL) || (fulldestfilename == NULL)) {
308
    kitten_puts(8, 0, "Out of memory!");
232
    kitten_puts(8, 0, "Out of memory!");
309
    zip_freelist(&ziplinkedlist);
233
    zip_freelist(&ziplinkedlist);
310
    free(buff);
234
    free(buff);
311
    free(fulldestfilename);
235
    free(fulldestfilename);
312
    return(-1);
236
    return(-1);
313
  }
237
  }
314
 
238
 
315
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
239
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
316
  sprintf(buff, "%s\\packages\\", dosdir);
240
  sprintf(buff, "%s\\packages\\", dosdir);
317
  mkpath(buff);
241
  mkpath(buff);
318
 
242
 
319
  /* open the lst file */
243
  /* open the lst file */
320
  sprintf(buff, "%s\\%s", dosdir, packageslst);
244
  sprintf(buff, "%s\\%s", dosdir, packageslst);
321
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
245
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
322
  if (lstfd == NULL) {
246
  if (lstfd == NULL) {
323
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
247
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
324
    puts("");
248
    puts("");
325
    zip_freelist(&ziplinkedlist);
249
    zip_freelist(&ziplinkedlist);
326
    free(buff);
250
    free(buff);
327
    free(fulldestfilename);
251
    free(fulldestfilename);
328
    return(-2);
252
    return(-2);
329
  }
253
  }
330
 
254
 
331
  /* write list of files in zip into the lst, and create the directories structure */
255
  /* write list of files in zip into the lst, and create the directories structure */
332
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
256
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
333
    int unzip_result;
257
    int unzip_result;
334
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
258
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
335
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
259
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
336
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
260
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
337
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
261
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
338
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
262
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
339
    /* create the path, just in case it doesn't exist yet */
263
    /* create the path, just in case it doesn't exist yet */
340
    mkpath(buff);
264
    mkpath(buff);
341
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
265
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
342
    /* Now unzip the file */
266
    /* Now unzip the file */
343
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
267
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
344
    if (unzip_result != 0) {
268
    if (unzip_result != 0) {
345
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
269
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
346
      printf(" [%d]\n", unzip_result);
270
      printf(" [%d]\n", unzip_result);
347
      filesextractedfailure += 1;
271
      filesextractedfailure += 1;
348
    } else {
272
    } else {
349
      printf(" %s -> %s\n", curzipnode->filename, buff);
273
      printf(" %s -> %s\n", curzipnode->filename, buff);
350
      filesextractedsuccess += 1;
274
      filesextractedsuccess += 1;
351
    }
275
    }
352
    /* if it's a LINK file, recompute a new content */
-
 
353
    if (islinkfile(curzipnode->filename) != 0) {
-
 
354
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
-
 
355
    }
-
 
356
  }
276
  }
357
  fclose(lstfd);
277
  fclose(lstfd);
358
 
278
 
359
  /* free the ziplist and close file descriptor */
279
  /* free the ziplist and close file descriptor */
360
  zip_freelist(&ziplinkedlist);
280
  zip_freelist(&ziplinkedlist);
361
  free(buff);
281
  free(buff);
362
  free(fulldestfilename);
282
  free(fulldestfilename);
363
 
283
 
364
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
284
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
365
  puts("");
285
  puts("");
366
  return(filesextractedfailure);
286
  return(filesextractedfailure);
367
}
287
}
368
 
288