Subversion Repositories SvarDOS

Rev

Rev 295 | Rev 614 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 295 Rev 613
Line 1... Line 1...
1
/*
1
/*
2
 * This file is part of pkg (SvarDOS)
2
 * This file is part of pkg (SvarDOS)
3
 * Copyright (C) 2012-2021 Mateusz Viste
3
 * Copyright (C) 2012-2022 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() */
Line 13... Line 13...
13
#include "helpers.h"   /* slash2backslash(), strtolower() */
13
#include "helpers.h"   /* slash2backslash(), strtolower() */
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
 
19
 
19
#include "pkginst.h"   /* include self for control */
20
#include "pkginst.h"   /* include self for control */
20
 
21
 
21
 
22
 
22
/* 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,
Line 73... Line 74...
73
 
74
 
74
 
75
 
75
/* 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. */
76
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) {
77
  if (is_package_installed(pkgname, dosdir) != 0) {
78
  if (is_package_installed(pkgname, dosdir) != 0) {
78
    kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname);
79
    kitten_printf(3, 18, pkgname); /* "Package %s is already installed! You might want to use the 'update' action." */
79
    puts("");
80
    puts("");
80
    return(-1);
81
    return(-1);
81
  }
82
  }
82
  return(0);
83
  return(0);
83
}
84
}
Line 113... Line 114...
113
 
114
 
114
  /* Now let's check the content of the zip file */
115
  /* Now let's check the content of the zip file */
115
 
116
 
116
  *zipfd = fopen(zipfile, "rb");
117
  *zipfd = fopen(zipfile, "rb");
117
  if (*zipfd == NULL) {
118
  if (*zipfd == NULL) {
118
    kitten_puts(3, 8, "ERROR: Invalid zip archive! Package not installed.");
119
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
119
    goto RAII;
120
    goto RAII;
120
  }
121
  }
121
  ziplinkedlist = zip_listfiles(*zipfd);
122
  ziplinkedlist = zip_listfiles(*zipfd);
122
  if (ziplinkedlist == NULL) {
123
  if (ziplinkedlist == NULL) {
123
    kitten_puts(3, 8, "ERROR: Invalid zip archive! Package not installed.");
124
    puts(svarlang_str(3, 8)); /* "ERROR: Invalid zip archive! Package not installed." */
124
    goto RAII;
125
    goto RAII;
125
  }
126
  }
126
  /* if updating, load the list of files belonging to the current package */
127
  /* if updating, load the list of files belonging to the current package */
127
  if ((flags & PKGINST_UPDATE) != 0) {
128
  if ((flags & PKGINST_UPDATE) != 0) {
128
    flist = pkg_loadflist(pkgname, dosdir);
129
    flist = pkg_loadflist(pkgname, dosdir);
Line 155... Line 156...
155
      }
156
      }
156
      continue; /* go to the next item */
157
      continue; /* go to the next item */
157
    }
158
    }
158
    /* validate that the file has a valid filename (8+3, no shady chars...) */
159
    /* validate that the file has a valid filename (8+3, no shady chars...) */
159
    if (validfilename(curzipnode->filename) != 0) {
160
    if (validfilename(curzipnode->filename) != 0) {
160
      kitten_puts(3, 23, "ERROR: Package contains an invalid filename:");
161
      puts(svarlang_str(3, 23)); /* "ERROR: Package contains an invalid filename:" */
161
      printf(" %s\n", curzipnode->filename);
162
      printf(" %s\n", curzipnode->filename);
162
      goto RAII_ERR;
163
      goto RAII_ERR;
163
    }
164
    }
164
    /* look out for collisions with already existing files (unless we are
165
    /* look out for collisions with already existing files (unless we are
165
     * updating the package and the local file belongs to it */
166
     * updating the package and the local file belongs to it */
166
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
167
    shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist);
167
    strcat(fname, shortfile);
168
    strcat(fname, shortfile);
168
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
169
    if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) {
169
      kitten_puts(3, 9, "ERROR: Package contains a file that already exists locally:");
170
      puts(svarlang_str(3, 9)); /* "ERROR: Package contains a file that already exists locally:" */
170
      printf(" %s\n", fname);
171
      printf(" %s\n", fname);
171
      goto RAII_ERR;
172
      goto RAII_ERR;
172
    }
173
    }
173
    /* abort if any entry is encrypted */
174
    /* abort if any entry is encrypted */
174
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
175
    if ((curzipnode->flags & ZIP_FLAG_ENCRYPTED) != 0) {
175
      kitten_printf(3, 20, "ERROR: Package contains an encrypted file:");
176
      puts(svarlang_str(3, 20)); /* "ERROR: Package contains an encrypted file:" */
176
      puts("");
-
 
177
      printf(" %s\n", curzipnode->filename);
177
      printf(" %s\n", curzipnode->filename);
178
      goto RAII_ERR;
178
      goto RAII_ERR;
179
    }
179
    }
180
    /* abort if any file is compressed with an unsupported method */
180
    /* abort if any file is compressed with an unsupported method */
181
    if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
181
    if ((curzipnode->compmethod != ZIP_METH_STORE) && (curzipnode->compmethod != ZIP_METH_DEFLATE)) { /* unsupported compression method */
182
      kitten_printf(8, 2, "ERROR: Package contains a file compressed with an unsupported method (%d):", curzipnode->compmethod);
182
      kitten_printf(8, 2, curzipnode->compmethod); /* "ERROR: Package contains a file compressed with an unsupported method (%d):" */
183
      puts("");
183
      puts("");
184
      printf(" %s\n", curzipnode->filename);
184
      printf(" %s\n", curzipnode->filename);
185
      goto RAII_ERR;
185
      goto RAII_ERR;
186
    }
186
    }
187
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
187
    if (strcmp(curzipnode->filename, appinfofile) == 0) appinfopresence = 1;
188
    prevzipnode = curzipnode;
188
    prevzipnode = curzipnode;
189
    curzipnode = curzipnode->nextfile;
189
    curzipnode = curzipnode->nextfile;
190
  }
190
  }
191
  /* if appinfo file not found, this is not a real FreeDOS package */
191
  /* if appinfo file not found, this is not a real FreeDOS package */
192
  if (appinfopresence != 1) {
192
  if (appinfopresence != 1) {
193
    kitten_printf(3, 12, "ERROR: Package do not contain the %s file! Not a valid FreeDOS package.", appinfofile);
193
    kitten_printf(3, 12, appinfofile); /* "ERROR: Package do not contain the %s file! Not a valid SvarDOS package." */
194
    puts("");
194
    puts("");
195
    goto RAII_ERR;
195
    goto RAII_ERR;
196
  }
196
  }
197
 
197
 
198
  goto RAII;
198
  goto RAII;
Line 230... Line 230...
230
 
230
 
231
  /* open the lst file */
231
  /* open the lst file */
232
  sprintf(buff, "%s\\%s", dosdir, packageslst);
232
  sprintf(buff, "%s\\%s", dosdir, packageslst);
233
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
233
  lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */
234
  if (lstfd == NULL) {
234
  if (lstfd == NULL) {
235
    kitten_printf(3, 10, "ERROR: Could not create %s!", buff);
235
    kitten_printf(3, 10, buff); /* "ERROR: Could not create %s!" */
236
    puts("");
236
    puts("");
237
    return(-2);
237
    return(-2);
238
  }
238
  }
239
 
239
 
240
  /* write list of files in zip into the lst, and create the directories structure */
240
  /* write list of files in zip into the lst, and create the directories structure */
Line 248... Line 248...
248
    mkpath(buff);
248
    mkpath(buff);
249
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
249
    sprintf(fulldestfilename, "%s%s", buff, shortfile);
250
    /* Now unzip the file */
250
    /* Now unzip the file */
251
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
251
    unzip_result = zip_unzip(zipfd, curzipnode, fulldestfilename);
252
    if (unzip_result != 0) {
252
    if (unzip_result != 0) {
253
      kitten_printf(8, 3, "ERROR: failed extracting '%s' to '%s'!", curzipnode->filename, fulldestfilename);
253
      kitten_printf(8, 3, curzipnode->filename, fulldestfilename); /* "ERROR: failed extracting '%s' to '%s'!" */
254
      printf(" [%d]\n", unzip_result);
254
      printf(" [%d]\n", unzip_result);
255
      filesextractedfailure += 1;
255
      filesextractedfailure += 1;
256
    } else {
256
    } else {
257
      printf(" %s -> %s\n", curzipnode->filename, buff);
257
      printf(" %s -> %s\n", curzipnode->filename, buff);
258
      filesextractedsuccess += 1;
258
      filesextractedsuccess += 1;
259
    }
259
    }
260
  }
260
  }
261
  fclose(lstfd);
261
  fclose(lstfd);
262
 
262
 
263
  kitten_printf(3, 19, "Package %s installed: %ld files extracted, %ld errors.", pkgname, filesextractedsuccess, filesextractedfailure);
263
  kitten_printf(3, 19, pkgname, filesextractedsuccess, filesextractedfailure); /* "Package %s installed: %ld files extracted, %ld errors." */
264
  puts("");
264
  puts("");
265
  return(filesextractedfailure);
265
  return(filesextractedfailure);
266
}
266
}