Subversion Repositories SvarDOS

Rev

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

Rev 1683 Rev 1711
1
/*
1
/*
2
 * This file is part of pkg (SvarDOS)
2
 * This file is part of pkg (SvarDOS)
3
 * Copyright (C) 2012-2024 Mateusz Viste
3
 * Copyright (C) 2012-2024 Mateusz Viste
4
 */
4
 */
5
 
5
 
6
#include <ctype.h>     /* toupper() */
6
#include <ctype.h>     /* toupper() */
7
#include <stdio.h>
7
#include <stdio.h>
8
#include <stdlib.h>    /* system() */
8
#include <stdlib.h>    /* system() */
9
#include <string.h>    /* strcpy() */
9
#include <string.h>    /* strcpy() */
10
#include <unistd.h>    /* read() */
10
#include <unistd.h>    /* read() */
11
#include <sys/types.h> /* struct utimbuf */
11
#include <sys/types.h> /* struct utimbuf */
12
 
12
 
13
#include "helpers.h"   /* slash2backslash() */
13
#include "helpers.h"   /* slash2backslash() */
14
#include "fileexst.h"
14
#include "fileexst.h"
15
#include "kprintf.h"
15
#include "kprintf.h"
16
#include "libunzip.h"  /* zip_listfiles()... */
16
#include "libunzip.h"  /* zip_listfiles()... */
17
#include "showinst.h"  /* pkg_loadflist() */
17
#include "showinst.h"  /* pkg_loadflist() */
18
#include "svarlang.lib\svarlang.h"
18
#include "svarlang.lib\svarlang.h"
19
 
19
 
20
#include "pkginst.h"   /* include self for control */
20
#include "pkginst.h"   /* include self for control */
21
 
21
 
22
 
22
 
23
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
23
/* validate a filename (8+3, no weird characters, etc). returns 0 on success,
24
 * nonzero otherwise. */
24
 * nonzero otherwise. */
25
static int validfilename(const char *fname) {
25
static int validfilename(const char *fname) {
26
  int i, i2;
26
  int i, i2;
27
  char *validchars = "!#$%&'()-@^_`{}~";
27
  char *validchars = "!#$%&'()-@^_`{}~";
28
  int elemlen = 0;
28
  int elemlen = 0;
29
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
29
  int elemmaxlen = 8; /* switches from 8 to 3 depending wheter I am analyzing
30
                         a filename or an extension */
30
                         a filename or an extension */
31
  /* look for invalid chars in the entire string, and check the length of the
31
  /* look for invalid chars in the entire string, and check the length of the
32
   * element at the same time */
32
   * element at the same time */
33
  for (i = 0; fname[i] != 0; i++) {
33
  for (i = 0; fname[i] != 0; i++) {
34
    /* director separators are threated specially */
34
    /* director separators are threated specially */
35
    if (fname[i] == '\\') {
35
    if (fname[i] == '\\') {
36
      elemlen = 0;
36
      elemlen = 0;
37
      elemmaxlen = 8;
37
      elemmaxlen = 8;
38
      continue;
38
      continue;
39
    }
39
    }
40
    /* '.' switches the check into extension mode */
40
    /* '.' switches the check into extension mode */
41
    if (fname[i] == '.') {
41
    if (fname[i] == '.') {
42
      if (elemlen == 0) return(-1); /* a file must have a base name */
42
      if (elemlen == 0) return(-1); /* a file must have a base name */
43
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
43
      if (elemmaxlen == 3) return(-2); /* a file cannot have two extensions */
44
      elemlen = 0;
44
      elemlen = 0;
45
      elemmaxlen = 3;
45
      elemmaxlen = 3;
46
      continue;
46
      continue;
47
    }
47
    }
48
    /* check that the element is not too long */
48
    /* check that the element is not too long */
49
    if (++elemlen > elemmaxlen) return(-3);
49
    if (++elemlen > elemmaxlen) return(-3);
50
    /* look for valid characters */
50
    /* look for valid characters */
51
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
51
    if ((fname[i] >= 'a') && (fname[i] <= 'z')) continue;
52
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
52
    if ((fname[i] >= 'A') && (fname[i] <= 'Z')) continue;
53
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
53
    if ((fname[i] >= '0') && (fname[i] <= '9')) continue;
54
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
54
    if ((fname[i] & 128) != 0) continue; /* high bytes are okay (NLS) */
55
    /* look for other valid characters */
55
    /* look for other valid characters */
56
    for (i2 = 0; validchars[i2] != 0; i2++) {
56
    for (i2 = 0; validchars[i2] != 0; i2++) {
57
      if (fname[i] == validchars[i2]) break;
57
      if (fname[i] == validchars[i2]) break;
58
    }
58
    }
59
    if (validchars[i2] != 0) continue;
59
    if (validchars[i2] != 0) continue;
60
    /* if we are here, then the character is invalid */
60
    /* if we are here, then the character is invalid */
61
    return(-4);
61
    return(-4);
62
  }
62
  }
63
  /* all checks passed */
63
  /* all checks passed */
64
  return(0);
64
  return(0);
65
}
65
}
66
 
66
 
67
 
67
 
68
/* returns 0 if pkgname is not installed, non-zero otherwise */
68
/* returns 0 if pkgname is not installed, non-zero otherwise */
69
int is_package_installed(const char *pkgname, const char *dosdir) {
69
int is_package_installed(const char *pkgname, const char *dosdir) {
70
  char fname[256];
70
  char fname[256];
71
  sprintf(fname, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
71
  sprintf(fname, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
72
  return(fileexists(fname)); /* file exists -> package is installed */
72
  return(fileexists(fname)); /* file exists -> package is installed */
73
}
73
}
74
 
74
 
75
 
75
 
76
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
76
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */
77
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
77
static int validate_package_not_installed(const char *pkgname, const char *dosdir) {
78
  if (is_package_installed(pkgname, dosdir) != 0) {
78
  if (is_package_installed(pkgname, dosdir) != 0) {
79
    kitten_printf(3, 18, pkgname); /* "Package %s is already installed! You might want to use the 'update' action." */
79
    kitten_printf(3, 18, pkgname); /* "Package %s is already installed! You might want to use the 'update' action." */
80
    puts("");
80
    puts("");
81
    return(-1);
81
    return(-1);
82
  }
82
  }
83
  return(0);
83
  return(0);
84
}
84
}
85
 
85
 
86
 
86
 
87
/* find a filename in a flist linked list, and returns a pointer to it */
87
/* find a filename in a flist linked list, and returns a pointer to it */
88
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
88
static struct flist_t *findfileinlist(struct flist_t *flist, const char *fname) {
89
  while (flist != NULL) {
89
  while (flist != NULL) {
90
    if (strcasecmp(flist->fname, fname) == 0) return(flist);
90
    if (strcasecmp(flist->fname, fname) == 0) return(flist);
91
    flist = flist->next;
91
    flist = flist->next;
92
  }
92
  }
93
  return(NULL);
93
  return(NULL);
94
}
94
}
95
 
95
 
96
 
96
 
97
/* prepare a package for installation. this is mandatory before installing it!
97
/* prepare a package for installation. this is mandatory before installing it!
98
 * returns a pointer to the zip file's index on success, NULL on failure.
98
 * returns a pointer to the zip file's index on success, NULL on failure.
99
 * the **zipfd pointer is updated with file descriptor of the open (to be
99
 * the **zipfd pointer is updated with file descriptor of the open (to be
100
 * installed) zip file.
100
 * installed) zip file.
101
 * the returned ziplist is guaranteed to have the APPINFO file as first node
101
 * the returned ziplist is guaranteed to have the APPINFO file as first node
102
 * the ziplist is also guaranteed not to contain any directory entries */
102
 * the ziplist is also guaranteed not to contain any directory entries */
103
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
103
struct ziplist *pkginstall_preparepackage(const char *pkgname, const char *zipfile, int flags, FILE **zipfd, const char *dosdir, const struct customdirs *dirlist) {
104
  char fname[256];
104
  char fname[256];
105
  char appinfofile[32];
105
  char appinfofile[32];
106
  struct ziplist *appinfoptr;
106
  struct ziplist *appinfoptr;
107
  char *shortfile;
107
  char *shortfile;
108
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
108
  struct ziplist *ziplinkedlist = NULL, *curzipnode, *prevzipnode;
109
  struct flist_t *flist = NULL;
109
  struct flist_t *flist = NULL;
110
 
110
 
111
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
111
  sprintf(appinfofile, "appinfo\\%s.lsm", pkgname); /* Prepare the appinfo/xxxx.lsm filename string for later use */
112
 
112
 
113
  /* check if not already installed, if already here, print a message "you might want to use update instead"
113
  /* check if not already installed, if already here, print a message "you might want to use update instead"
114
   * of course this must not be done if we are in the process of upgrading said package */
114
   * of course this must not be done if we are in the process of upgrading said package */
115
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
115
  if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) {
116
    return(NULL);
116
    return(NULL);
117
  }
117
  }
118
 
118
 
119
  /* copy zip filename into fname */
119
  /* copy zip filename into fname */
120
  strcpy(fname, zipfile);
120
  strcpy(fname, zipfile);
121
 
121
 
122
  /* append an .SVP extension if not present */
122
  /* append an .SVP extension if not present */
123
  {
123
  {
124
    int slen = strlen(fname);
124
    int slen = strlen(fname);
125
    if ((slen < 4) || (fname[slen - 4] != '.')) strcat(fname, ".SVP");
125
    if ((slen < 4) || (fname[slen - 4] != '.')) strcat(fname, ".SVP");
126
  }
126
  }
127
 
127
 
128
  /* now let's check the content of the zip file */
128
  /* now let's check the content of the zip file */
129
 
129
 
130
  *zipfd = fopen(fname, "rb");
130
  *zipfd = fopen(fname, "rb");
131
  if (*zipfd == NULL) {
131
  if (*zipfd == NULL) {
132
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
132
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
133
    goto RAII;
133
    goto RAII;
134
  }
134
  }
135
  ziplinkedlist = zip_listfiles(*zipfd);
135
  ziplinkedlist = zip_listfiles(*zipfd);
136
  if (ziplinkedlist == NULL) {
136
  if (ziplinkedlist == NULL) {
137
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
137
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
138
    goto RAII;
138
    goto RAII;
139
  }
139
  }
140
  /* if updating, load the list of files belonging to the current package */
140
  /* if updating, load the list of files belonging to the current package */
141
  if ((flags & PKGINST_UPDATE) != 0) {
141
  if ((flags & PKGINST_UPDATE) != 0) {
142
    flist = pkg_loadflist(pkgname, dosdir);
142
    flist = pkg_loadflist(pkgname, dosdir);
143
  }
143
  }
144
 
144
 
145
  /* Verify that there's no collision with existing local files, look for the appinfo presence */
145
  /* Verify that there's no collision with existing local files, look for the appinfo presence */
146
  appinfoptr = NULL;
146
  appinfoptr = NULL;
147
  prevzipnode = NULL;
147
  prevzipnode = NULL;
148
 
148
 
149
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
149
  for (curzipnode = ziplinkedlist; curzipnode != NULL;) {
150
    /* change all slashes to backslashes, and switch into all-lowercase */
150
    /* change all slashes to backslashes, and switch into all-lowercase */
151
    slash2backslash(curzipnode->filename);
151
    slash2backslash(curzipnode->filename);
152
    strlwr(curzipnode->filename);
152
    strlwr(curzipnode->filename);
153
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
153
    /* remove 'directory' ZIP entries to avoid false alerts about directory already existing */
154
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
154
    if ((curzipnode->flags & ZIP_FLAG_ISADIR) != 0) {
155
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
155
      curzipnode->filename[0] = 0; /* mark it "empty", will be removed in a short moment */
156
    }
156
    }
157
    /* is it a "link file"? skip it - link files are no longer supported */
157
    /* is it a "link file"? skip it - link files are no longer supported */
158
    if (strstr(curzipnode->filename, "links\\") == curzipnode->filename) {
158
    if (strstr(curzipnode->filename, "links\\") == curzipnode->filename) {
159
      curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
159
      curzipnode->filename[0] = 0; /* in fact, I just mark the file as 'empty' on the filename - see later below */
160
    }
160
    }
161
 
161
 
162
    /* is it the appinfo file? detach it from the list for now */
162
    /* is it the appinfo file? detach it from the list for now */
163
    if (strcmp(curzipnode->filename, appinfofile) == 0) {
163
    if (strcmp(curzipnode->filename, appinfofile) == 0) {
164
      appinfoptr = curzipnode;
164
      appinfoptr = curzipnode;
165
      curzipnode = curzipnode->nextfile;
165
      curzipnode = curzipnode->nextfile;
166
      if (prevzipnode == NULL) {
166
      if (prevzipnode == NULL) {
167
        ziplinkedlist = curzipnode;
167
        ziplinkedlist = curzipnode;
168
      } else {
168
      } else {
169
        prevzipnode->nextfile = curzipnode;
169
        prevzipnode->nextfile = curzipnode;
170
      }
170
      }
171
      continue;
171
      continue;
172
    }
172
    }
173
 
173
 
174
    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) */
174
    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) */
175
      if (prevzipnode == NULL) {  /* take the item out of the list */
175
      if (prevzipnode == NULL) {  /* take the item out of the list */
176
        ziplinkedlist = curzipnode->nextfile;
176
        ziplinkedlist = curzipnode->nextfile;
177
        free(curzipnode); /* free the item */
177
        free(curzipnode); /* free the item */
178
        curzipnode = ziplinkedlist;
178
        curzipnode = ziplinkedlist;
179
      } else {
179
      } else {
180
        prevzipnode->nextfile = curzipnode->nextfile;
180
        prevzipnode->nextfile = curzipnode->nextfile;
181
        free(curzipnode); /* free the item */
181
        free(curzipnode); /* free the item */
182
        curzipnode = prevzipnode->nextfile;
182
        curzipnode = prevzipnode->nextfile;
183
      }
183
      }
184
      continue; /* go to the next item */
184
      continue; /* go to the next item */
185
    }
185
    }
186
 
186
 
187
    /* validate that the file has a valid filename (8+3, no shady chars...) */
187
    /* validate that the file has a valid filename (8+3, no shady chars...) */
188
    if (validfilename(curzipnode->filename) != 0) {
188
    if (validfilename(curzipnode->filename) != 0) {
189
      puts(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
189
      puts(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
190
      printf(" %s\n", curzipnode->filename);
190
      printf(" %s\n", curzipnode->filename);
191
      goto RAII_ERR;
191
      goto RAII_ERR;
192
    }
192
    }
193
 
193
 
194
    /* look out for collisions with already existing files (unless we are
194
    /* look out for collisions with already existing files (unless we are
195
     * updating the package and the local file belongs to it */
195
     * updating the package and the local file belongs to it */
196
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
196
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
197
    strcat(fname, shortfile);
197
    strcat(fname, shortfile);
198
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
198
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
199
      puts(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
199
      puts(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
200
      printf(" %s\n", fname);
200
      printf(" %s\n", fname);
201
      goto RAII_ERR;
201
      goto RAII_ERR;
202
    }
202
    }
203
 
203
 
204
    /* abort if any entry is encrypted */
204
    /* abort if any entry is encrypted */
205
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
205
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
206
      puts(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
206
      puts(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
207
      printf(" %s\n", curzipnode->filename);
207
      printf(" %s\n", curzipnode->filename);
208
      goto RAII_ERR;
208
      goto RAII_ERR;
209
    }
209
    }
210
 
210
 
211
    /* abort if any file is compressed with an unsupported method */
211
    /* abort if any file is compressed with an unsupported method */
212
    if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
212
    if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
213
      kitten_printf(8, 2, curzipnode->compmethod); /* "ERROR: Package contains a file compressed with an unsupported method (%d):" */
213
      kitten_printf(8, 2, curzipnode->compmethod); /* "ERROR: Package contains a file compressed with an unsupported method (%d):" */
214
      puts("");
214
      puts("");
215
      printf(" %s\n", curzipnode->filename);
215
      printf(" %s\n", curzipnode->filename);
216
      goto RAII_ERR;
216
      goto RAII_ERR;
217
    }
217
    }
218
 
218
 
219
    /* add node to list */
219
    /* add node to list */
220
    prevzipnode = curzipnode;
220
    prevzipnode = curzipnode;
221
    curzipnode = curzipnode->nextfile;
221
    curzipnode = curzipnode->nextfile;
222
  }
222
  }
223
 
223
 
224
  /* if appinfo file not found, this is not a real SvarDOS package */
224
  /* if appinfo file not found, this is not a real SvarDOS package */
225
  if (appinfoptr == NULL) {
225
  if (appinfoptr == NULL) {
226
    kitten_printf(3, 12, appinfofile); /* "ERROR: Package do not contain the %s file! Not a valid SvarDOS package." */
226
    kitten_printf(3, 12, appinfofile); /* "ERROR: Package do not contain the %s file! Not a valid SvarDOS package." */
227
    puts("");
227
    puts("");
228
    goto RAII_ERR;
228
    goto RAII_ERR;
229
  }
229
  }
230
 
230
 
231
  /* attach the appinfo node to the top of the list (installation second stage
231
  /* attach the appinfo node to the top of the list (installation second stage
232
   * relies on this) */
232
   * relies on this) */
233
  appinfoptr->nextfile = ziplinkedlist;
233
  appinfoptr->nextfile = ziplinkedlist;
234
  ziplinkedlist = appinfoptr;
234
  ziplinkedlist = appinfoptr;
235
 
235
 
236
  goto RAII;
236
  goto RAII;
237
 
237
 
238
  RAII_ERR:
238
  RAII_ERR:
239
  zip_freelist(&ziplinkedlist);
239
  zip_freelist(&ziplinkedlist);
240
  ziplinkedlist = NULL;
240
  ziplinkedlist = NULL;
241
  if ((zipfd != NULL) && (*zipfd != NULL)) {
241
  if ((zipfd != NULL) && (*zipfd != NULL)) {
242
    fclose(*zipfd);
242
    fclose(*zipfd);
243
    *zipfd = NULL;
243
    *zipfd = NULL;
244
  }
244
  }
245
 
245
 
246
  RAII:
246
  RAII:
247
  pkg_freeflist(flist);
247
  pkg_freeflist(flist);
248
  return(ziplinkedlist);
248
  return(ziplinkedlist);
249
}
249
}
250
 
250
 
251
 
251
 
252
/* look for a "warn" field in the package's LSM file and display it if found */
252
/* look for a "warn" field in the package's LSM file and display it if found */
253
static void display_warn_if_exists(const char *pkgname, const char *dosdir, char *buff, size_t buffsz) {
253
static void display_warn_if_exists(const char *pkgname, const char *dosdir, char *buff, size_t buffsz) {
254
  FILE *fd;
254
  FILE *fd;
255
  char *msgptr;
255
  char *msgptr;
256
  int warncount = 0, i;
256
  int warncount = 0, i;
257
 
257
 
258
  sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
258
  sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
259
  fd = fopen(buff, "rb");
259
  fd = fopen(buff, "rb");
260
  if (fd == NULL) return;
260
  if (fd == NULL) return;
261
 
261
 
262
  while (freadtokval(fd, buff, buffsz, &msgptr, ':') == 0) {
262
  while (freadtokval(fd, buff, buffsz, &msgptr, ':') == 0) {
263
    if (msgptr != NULL) {
263
    if (msgptr != NULL) {
264
      if (strcasecmp(buff, "warn") == 0) {
264
      if (strcasecmp(buff, "warn") == 0) {
265
        /* print a visual delimiter */
265
        /* print a visual delimiter */
266
        if (warncount == 0) {
266
        if (warncount == 0) {
267
          puts("");
267
          puts("");
268
          for (i = 0; i < 79; i++) putchar('*');
268
          for (i = 0; i < 79; i++) putchar('*');
269
          puts("");
269
          puts("");
270
        }
270
        }
271
        /* there may be more than one "warn" line */
271
        /* there may be more than one "warn" line */
272
        puts(msgptr);
272
        puts(msgptr);
273
        warncount++;
273
        warncount++;
274
      }
274
      }
275
    }
275
    }
276
  }
276
  }
277
 
277
 
278
  fclose(fd);
278
  fclose(fd);
279
 
279
 
280
  /* if one or more warn lines have been displayed then close with a delimiter again */
280
  /* if one or more warn lines have been displayed then close with a delimiter again */
281
  if (warncount > 0) {
281
  if (warncount > 0) {
282
    for (i = 0; i < 79; i++) putchar('*');
282
    for (i = 0; i < 79; i++) putchar('*');
283
    puts("");
283
    puts("");
284
  }
284
  }
285
 
285
 
286
}
286
}
287
 
287
 
288
 
288
 
289
/* install a package that has been prepared already. returns 0 on success,
289
/* install a package that has been prepared already. returns 0 on success,
290
 * or a negative value on error, or a positive value on warning */
290
 * or a negative value on error, or a positive value on warning */
291
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
291
int pkginstall_installpackage(const char *pkgname, const char *dosdir, const struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) {
292
  char buff[256];
292
  char buff[256];
293
  char fulldestfilename[256];
293
  char fulldestfilename[256];
294
  char *shortfile;
294
  char *shortfile;
295
  long filesextractedsuccess = 0, filesextractedfailure = 0;
295
  long filesextractedsuccess = 0, filesextractedfailure = 0;
296
  struct ziplist *curzipnode;
296
  struct ziplist *curzipnode;
297
  FILE *lsmfd;
297
  FILE *lsmfd;
298
  int unzip_result;
298
  int unzip_result;
299
 
299
 
300
  /* create the %DOSDIR%/APPINFO directory, just in case it doesn't exist yet */
300
  /* create the %DOSDIR%/APPINFO directory, just in case it doesn't exist yet */
301
  sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
301
  sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, pkgname);
302
  mkpath(buff);
302
  mkpath(buff);
303
 
303
 
304
  /* start by extracting the APPINFO (LSM) file - I need it so I can append the
304
  /* start by extracting the APPINFO (LSM) file - I need it so I can append the
305
   * list of files belonging to the packages later */
305
   * list of files belonging to the packages later */
306
  unzip_result = zip_unzip(zipfd, ziplinkedlist, buff);
306
  unzip_result = zip_unzip(zipfd, ziplinkedlist, buff);
307
  if (unzip_result != 0) {
307
  if (unzip_result != 0) {
308
    kitten_printf(8, 3, ziplinkedlist, buff); /* "ERROR: failed extracting '%s' to '%s'!" */
308
    kitten_printf(8, 3, ziplinkedlist, buff); /* "ERROR: failed extracting '%s' to '%s'!" */
309
    printf(" [%d]\n", unzip_result);
309
    printf(" [%d]\n", unzip_result);
310
    return(-1);
310
    return(-1);
311
  }
311
  }
312
  printf(" %s -> %s\n", ziplinkedlist->filename, buff);
312
  printf(" %s -> %s\n", ziplinkedlist->filename, buff);
313
  filesextractedsuccess++;
313
  filesextractedsuccess++;
314
 
314
 
315
  /* open the (freshly created) LSM file */
315
  /* open the (freshly created) LSM file */
316
  lsmfd = fopen(buff, "ab"); /* opening in APPEND mode so I do not loose the LSM content */
316
  lsmfd = fopen(buff, "ab"); /* opening in APPEND mode so I do not loose the LSM content */
317
  if (lsmfd == NULL) {
317
  if (lsmfd == NULL) {
318
    kitten_printf(3, 10, buff); /* "ERROR: Could not create %s!" */
318
    kitten_printf(3, 10, buff); /* "ERROR: Could not create %s!" */
319
    puts("");
319
    puts("");
320
    return(-2);
320
    return(-2);
321
  }
321
  }
322
  fprintf(lsmfd, "\r\n"); /* in case the LSM does not end with a clear line already */
322
  fprintf(lsmfd, "\r\n"); /* in case the LSM does not end with a clear line already */
323
 
323
 
324
  /* write list of files in zip into the lst, and create the directories structure */
324
  /* write list of files in zip into the lst, and create the directories structure */
325
  for (curzipnode = ziplinkedlist->nextfile; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
325
  for (curzipnode = ziplinkedlist->nextfile; curzipnode != NULL; curzipnode = curzipnode->nextfile) {
326
 
326
 
327
    /* substitute paths to custom dirs */
327
    /* substitute paths to custom dirs */
328
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist);
328
    shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist);
329
 
329
 
330
    /* log the filename to LSM metadata file + its CRC */
330
    /* log the filename to LSM metadata file + its CRC */
331
    fprintf(lsmfd, "%s%s?%08lX\r\n", buff, shortfile, curzipnode->crc32);
331
    fprintf(lsmfd, "%s%s?%08lX\r\n", buff, shortfile, curzipnode->crc32);
332
 
332
 
333
    /* create the path, just in case it doesn't exist yet */
333
    /* create the path, just in case it doesn't exist yet */
334
    mkpath(buff);
334
    mkpath(buff);
335
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
335
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
336
 
336
 
337
    /* Now unzip the file */
337
    /* Now unzip the file */
338
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
338
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
339
    if (unzip_result != 0) {
339
    if (unzip_result != 0) {
340
      kitten_printf(8, 3, curzipnode->filename, fulldestfilename); /* "ERROR: failed extracting '%s' to '%s'!" */
340
      kitten_printf(8, 3, curzipnode->filename, fulldestfilename); /* "ERROR: failed extracting '%s' to '%s'!" */
341
      printf(" [%d]\n", unzip_result);
341
      printf(" [%d]\n", unzip_result);
342
      filesextractedfailure += 1;
342
      filesextractedfailure += 1;
343
    } else {
343
    } else {
344
      printf(" %s -> %s\n", curzipnode->filename, buff);
344
      printf(" %s -> %s\n", curzipnode->filename, buff);
345
      filesextractedsuccess += 1;
345
      filesextractedsuccess += 1;
346
    }
346
    }
347
  }
347
  }
348
  fclose(lsmfd);
348
  fclose(lsmfd);
349
 
349
 
350
  kitten_printf(3, 19, pkgname, filesextractedsuccess, filesextractedfailure); /* "Package %s installed: %ld files extracted, %ld errors." */
350
  kitten_printf(3, 19, pkgname, filesextractedfailure, filesextractedsuccess); /* "Package %s installed: %ld errors, %ld files extracted." */
351
  puts("");
351
  puts("");
352
 
352
 
353
  /* scan the LSM file for a "warn" message to display */
353
  /* scan the LSM file for a "warn" message to display */
354
  display_warn_if_exists(pkgname, dosdir, buff, sizeof(buff));
354
  display_warn_if_exists(pkgname, dosdir, buff, sizeof(buff));
355
 
355
 
356
  return(filesextractedfailure);
356
  return(filesextractedfailure);
357
}
357
}
358
 
358