Subversion Repositories SvarDOS

Rev

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

Rev 230 Rev 233
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 "fdnpkg.h"    /* PKGINST_SKIPLINKS... */
15
#include "fdnpkg.h"    /* 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(const char *pkgname, const char *dosdir) {
140
int is_package_installed(const char *pkgname, const char *dosdir) {
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
  return(fileexists(fname)); /* file exists -> package is installed */
143
  return(fileexists(fname)); /* file exists -> package is installed */
144
}
144
}
145
 
145
 
146
 
146
 
147
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
147
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
148
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
148
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
149
  if (is_package_installed(pkgname, dosdir) != 0) {
149
  if (is_package_installed(pkgname, dosdir) != 0) {
150
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
150
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
151
    puts("");
151
    puts("");
152
    return(-1);
152
    return(-1);
153
  }
153
  }
154
  return(0);
154
  return(0);
155
}
155
}
156
 
156
 
157
 
157
 
158
/* find a filename in a flist linked list, and returns a pointer to it */
158
/* find a filename in a flist linked list, and returns a pointer to it */
159
static struct flist_t *findfileinlist(struct flist_t *flist, char *fname) {
159
static struct flist_t *findfileinlist(struct flist_t *flist, char *fname) {
160
  while (flist != NULL) {
160
  while (flist != NULL) {
161
    if (strcmp(flist->fname, fname) == 0) return(flist);
161
    if (strcmp(flist->fname, fname) == 0) return(flist);
162
    flist = flist->next;
162
    flist = flist->next;
163
  }
163
  }
164
  return(NULL);
164
  return(NULL);
165
}
165
}
166
 
166
 
167
 
167
 
168
/* prepare a package for installation. this is mandatory before actually installing it!
168
/* prepare a package for installation. this is mandatory before actually installing it!
169
 * 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
 * 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. */
170
struct ziplist *pkginstall_preparepackage(char *pkgname, char *localfile, int flags, FILE **zipfd, char *dosdir, struct customdirs *dirlist, char *buffmem1k) {
170
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist, char *buffmem1k) {
171
  char *fname;
171
  char *fname;
172
  char *zipfile;
-
 
173
  char *appinfofile;
172
  char *appinfofile;
174
  int appinfopresence;
173
  int appinfopresence;
175
  char *shortfile;
174
  char *shortfile;
176
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
175
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
177
  struct flist_t *flist = NULL;
176
  struct flist_t *flist = NULL;
178
 
177
 
179
  fname = buffmem1k;
178
  fname = buffmem1k;
180
  zipfile = buffmem1k + 256;
-
 
181
  appinfofile = buffmem1k + 512;
179
  appinfofile = buffmem1k + 512;
182
 
180
 
183
  strtolower(pkgname); /* convert pkgname to lower case, because the http repo is probably case sensitive */
-
 
184
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
181
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
185
 
182
 
186
  /* check if not already installed, if already here, print a message "you might want to use update instead"
183
  /* check if not already installed, if already here, print a message "you might want to use update instead"
187
   * of course this must not be done if we are in the process of upgrading said package */
184
   * of course this must not be done if we are in the process of upgrading said package */
188
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
185
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
189
    return(NULL);
186
    return(NULL);
190
  }
187
  }
191
 
-
 
192
  if (localfile != NULL) {  /* if it's a local file, then we will have to skip all the network stuff */
-
 
193
    strcpy(zipfile, localfile);
-
 
194
  } else {
-
 
195
    zipfile[0] = 0;
-
 
196
  }
-
 
197
 
188
 
198
  /* Now let's check the content of the zip file */
189
  /* Now let's check the content of the zip file */
199
 
190
 
200
  *zipfd = fopen(zipfile, "rb");
191
  *zipfd = fopen(zipfile, "rb");
201
  if (*zipfd == NULL) {
192
  if (*zipfd == NULL) {
202
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
193
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
203
    goto RAII;
194
    goto RAII;
204
  }
195
  }
205
  ziplinkedlist = zip_listfiles(*zipfd);
196
  ziplinkedlist = zip_listfiles(*zipfd);
206
  if (ziplinkedlist == NULL) {
197
  if (ziplinkedlist == NULL) {
207
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
198
    kitten_puts(3, 8, "Error: Invalid zip archive! Package not installed.");
208
    goto RAII;
199
    goto RAII;
209
  }
200
  }
210
  /* if updating, load the list of files belonging to the current package */
201
  /* if updating, load the list of files belonging to the current package */
211
  if ((flags & PKGINST_UPDATE) != 0) {
202
  if ((flags & PKGINST_UPDATE) != 0) {
212
    flist = pkg_loadflist(pkgname, dosdir);
203
    flist = pkg_loadflist(pkgname, dosdir);
213
  }
204
  }
214
  /* 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 */
205
  /* 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 */
215
  appinfopresence = 0;
206
  appinfopresence = 0;
216
  prevzipnode = NULL;
207
  prevzipnode = NULL;
217
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
208
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
218
    /* change all slashes to backslashes, and switch into all-lowercase */
209
    /* change all slashes to backslashes, and switch into all-lowercase */
219
    slash2backslash(curzipnode->filename);
210
    slash2backslash(curzipnode->filename);
220
    strtolower(curzipnode->filename);
211
    strtolower(curzipnode->filename);
221
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
212
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
222
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
213
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
223
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
214
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
224
    }
215
    }
225
    /* is it a "link file"? */
216
    /* is it a "link file"? */
226
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
217
    if (fdnpkg_strcasestr(curzipnode->filename, "links\\") == curzipnode->filename) {
227
      /* skip links, if that's what the user wants */
218
      /* skip links, if that's what the user wants */
228
      if ((flags & PKGINST_SKIPLINKS) != 0) {
219
      if ((flags & PKGINST_SKIPLINKS) != 0) {
229
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
220
        curzipnode->filename[0] = 0; /* in fact, we just mark the file as 'empty' on the filename.. see later below */
230
      } else {
221
      } else {
231
        /* if it's a *.BAT link, then rename it to *.COM */
222
        /* if it's a *.BAT link, then rename it to *.COM */
232
        char *ext = getfext(curzipnode->filename);
223
        char *ext = getfext(curzipnode->filename);
233
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
224
        if (strcasecmp(ext, "bat") == 0) sprintf(ext, "com");
234
      }
225
      }
235
    }
226
    }
236
 
227
 
237
    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) */
228
    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) */
238
      if (prevzipnode == NULL) {  /* take the item out of the list */
229
      if (prevzipnode == NULL) {  /* take the item out of the list */
239
        ziplinkedlist = curzipnode->nextfile;
230
        ziplinkedlist = curzipnode->nextfile;
240
        free(curzipnode); /* free the item */
231
        free(curzipnode); /* free the item */
241
        curzipnode = ziplinkedlist;
232
        curzipnode = ziplinkedlist;
242
      } else {
233
      } else {
243
        prevzipnode->nextfile = curzipnode->nextfile;
234
        prevzipnode->nextfile = curzipnode->nextfile;
244
        free(curzipnode); /* free the item */
235
        free(curzipnode); /* free the item */
245
        curzipnode = prevzipnode->nextfile;
236
        curzipnode = prevzipnode->nextfile;
246
      }
237
      }
247
      continue; /* go to the next item */
238
      continue; /* go to the next item */
248
    }
239
    }
249
    /* validate that the file has a valid filename (8+3, no shady chars...) */
240
    /* validate that the file has a valid filename (8+3, no shady chars...) */
250
    if (validfilename(curzipnode->filename) != 0) {
241
    if (validfilename(curzipnode->filename) != 0) {
251
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
242
      kitten_puts(3, 23, "Error: Package contains an invalid filename:");
252
      printf(" %s\n", curzipnode->filename);
243
      printf(" %s\n", curzipnode->filename);
253
      goto RAII_ERR;
244
      goto RAII_ERR;
254
    }
245
    }
255
    /* look out for collisions with already existing files (unless we are
246
    /* look out for collisions with already existing files (unless we are
256
     * updating the package and the local file belongs to it */
247
     * updating the package and the local file belongs to it */
257
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
248
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
258
    strcat(fname, shortfile);
249
    strcat(fname, shortfile);
259
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
250
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
260
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
251
      kitten_puts(3, 9, "Error: Package contains a file that already exists locally:");
261
      printf(" %s\n", fname);
252
      printf(" %s\n", fname);
262
      goto RAII_ERR;
253
      goto RAII_ERR;
263
    }
254
    }
264
    /* abort if any entry is encrypted */
255
    /* abort if any entry is encrypted */
265
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
256
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
266
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
257
      kitten_printf(3, 20, "Error: Package contains an encrypted file:");
267
      puts("");
258
      puts("");
268
      printf(" %s\n", curzipnode->filename);
259
      printf(" %s\n", curzipnode->filename);
269
      goto RAII_ERR;
260
      goto RAII_ERR;
270
    }
261
    }
271
    /* abort if any file is compressed with an unsupported method */
262
    /* abort if any file is compressed with an unsupported method */
272
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
263
    if ((curzipnode->compmethod != 0/*store*/) && (curzipnode->compmethod != 8/*deflate*/)) { /* unsupported compression method */
273
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
264
      kitten_printf(8, 2, "Error: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
274
      puts("");
265
      puts("");
275
      printf(" %s\n", curzipnode->filename);
266
      printf(" %s\n", curzipnode->filename);
276
      goto RAII_ERR;
267
      goto RAII_ERR;
277
    }
268
    }
278
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
269
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
279
    prevzipnode = curzipnode;
270
    prevzipnode = curzipnode;
280
    curzipnode = curzipnode->nextfile;
271
    curzipnode = curzipnode->nextfile;
281
  }
272
  }
282
  /* if appinfo file not found, this is not a real FreeDOS package */
273
  /* if appinfo file not found, this is not a real FreeDOS package */
283
  if (appinfopresence != 1) {
274
  if (appinfopresence != 1) {
284
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
275
    kitten_printf(3, 12, "Error: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
285
    puts("");
276
    puts("");
286
    goto RAII_ERR;
277
    goto RAII_ERR;
287
  }
278
  }
288
 
279
 
289
  goto RAII;
280
  goto RAII;
290
 
281
 
291
  RAII_ERR:
282
  RAII_ERR:
292
  zip_freelist(&ziplinkedlist);
283
  zip_freelist(&ziplinkedlist);
293
  ziplinkedlist = NULL;
284
  ziplinkedlist = NULL;
294
  if ((zipfd != NULL) && (*zipfd != NULL)) {
285
  if ((zipfd != NULL) && (*zipfd != NULL)) {
295
    fclose(*zipfd);
286
    fclose(*zipfd);
296
    *zipfd = NULL;
287
    *zipfd = NULL;
297
  }
288
  }
298
 
289
 
299
  RAII:
290
  RAII:
300
  pkg_freeflist(flist);
291
  pkg_freeflist(flist);
301
  return(ziplinkedlist);
292
  return(ziplinkedlist);
302
}
293
}
303
 
294
 
304
 
295
 
305
/* install a package that has been prepared already. returns 0 on success,
296
/* install a package that has been prepared already. returns 0 on success,
306
 * or a negative value on error, or a positive value on warning */
297
 * or a negative value on error, or a positive value on warning */
307
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
298
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
308
  char *buff;
299
  char *buff;
309
  char *fulldestfilename;
300
  char *fulldestfilename;
310
  char packageslst[64];
301
  char packageslst[64];
311
  char *shortfile;
302
  char *shortfile;
312
  long filesextractedsuccess = 0, filesextractedfailure = 0;
303
  long filesextractedsuccess = 0, filesextractedfailure = 0;
313
  struct ziplist *curzipnode;
304
  struct ziplist *curzipnode;
314
  FILE *lstfd;
305
  FILE *lstfd;
315
 
306
 
316
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
307
  sprintf(packageslst, "packages\\%s.lst", pkgname); /* Prepare the packages/xxxx.lst filename string for later use */
317
 
308
 
318
  buff = malloc(512);
309
  buff = malloc(512);
319
  fulldestfilename = malloc(1024);
310
  fulldestfilename = malloc(1024);
320
  if ((buff == NULL) || (fulldestfilename == NULL)) {
311
  if ((buff == NULL) || (fulldestfilename == NULL)) {
321
    kitten_puts(8, 0, "Out of memory!");
312
    kitten_puts(8, 0, "Out of memory!");
322
    zip_freelist(&ziplinkedlist);
313
    zip_freelist(&ziplinkedlist);
323
    free(buff);
314
    free(buff);
324
    free(fulldestfilename);
315
    free(fulldestfilename);
325
    return(-1);
316
    return(-1);
326
  }
317
  }
327
 
318
 
328
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
319
  /* create the %DOSDIR%/packages directory, just in case it doesn't exist yet */
329
  sprintf(buff, "%s\\packages\\", dosdir);
320
  sprintf(buff, "%s\\packages\\", dosdir);
330
  mkpath(buff);
321
  mkpath(buff);
331
 
322
 
332
  /* open the lst file */
323
  /* open the lst file */
333
  sprintf(buff, "%s\\%s", dosdir, packageslst);
324
  sprintf(buff, "%s\\%s", dosdir, packageslst);
334
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
325
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
335
  if (lstfd == NULL) {
326
  if (lstfd == NULL) {
336
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
327
    kitten_printf(3, 10, "Error: Could not create %s!", buff);
337
    puts("");
328
    puts("");
338
    zip_freelist(&ziplinkedlist);
329
    zip_freelist(&ziplinkedlist);
339
    free(buff);
330
    free(buff);
340
    free(fulldestfilename);
331
    free(fulldestfilename);
341
    return(-2);
332
    return(-2);
342
  }
333
  }
343
 
334
 
344
  /* write list of files in zip into the lst, and create the directories structure */
335
  /* write list of files in zip into the lst, and create the directories structure */
345
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
336
  for (curzipnode = ziplinkedlist; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
346
    int unzip_result;
337
    int unzip_result;
347
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
338
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) continue; /* skip directories */
348
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
339
    if (strcasecmp(curzipnode->filename, packageslst) == 0) continue; /* skip silently the package.lst file, if found */
349
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
340
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */
350
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
341
    /* log the filename to packages\pkg.lst (with original, unmapped drive) */
351
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
342
    fprintf(lstfd, "%s%s\r\n", buff, shortfile);
352
    /* create the path, just in case it doesn't exist yet */
343
    /* create the path, just in case it doesn't exist yet */
353
    mkpath(buff);
344
    mkpath(buff);
354
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
345
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
355
    /* Now unzip the file */
346
    /* Now unzip the file */
356
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
347
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
357
    if (unzip_result != 0) {
348
    if (unzip_result != 0) {
358
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
349
      kitten_printf(8, 3, "Error while extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
359
      printf(" [%d]\n", unzip_result);
350
      printf(" [%d]\n", unzip_result);
360
      filesextractedfailure += 1;
351
      filesextractedfailure += 1;
361
    } else {
352
    } else {
362
      printf(" %s -> %s\n", curzipnode->filename, buff);
353
      printf(" %s -> %s\n", curzipnode->filename, buff);
363
      filesextractedsuccess += 1;
354
      filesextractedsuccess += 1;
364
    }
355
    }
365
    /* if it's a LINK file, recompute a new content */
356
    /* if it's a LINK file, recompute a new content */
366
    if (islinkfile(curzipnode->filename) != 0) {
357
    if (islinkfile(curzipnode->filename) != 0) {
367
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
358
      processlinkfile(fulldestfilename, dosdir, dirlist, buff);
368
    }
359
    }
369
  }
360
  }
370
  fclose(lstfd);
361
  fclose(lstfd);
371
 
362
 
372
  /* free the ziplist and close file descriptor */
363
  /* free the ziplist and close file descriptor */
373
  zip_freelist(&ziplinkedlist);
364
  zip_freelist(&ziplinkedlist);
374
  free(buff);
365
  free(buff);
375
  free(fulldestfilename);
366
  free(fulldestfilename);
376
 
367
 
377
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
368
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
378
  puts("");
369
  puts("");
379
  return(filesextractedfailure);
370
  return(filesextractedfailure);
380
}
371
}
381
 
372