/pkginst/helpers.c |
---|
1,6 → 1,6 |
/* |
* This file is part of pkginst |
* Copyright (C) 2012-2021 Mateusz Viste |
* This file is part of fdnpkg |
* Copyright (C) 2012-2017 Mateusz Viste |
* |
* It contains a few helper function... |
*/ |
193,6 → 193,37 |
} |
/* remap drive, if needed (hack for situations where fdinst is used to |
* install an OS to a drive that will change its letter post-install) */ |
void mapdrives(char *s, char *mapdrv) { |
int i = 0; |
if (mapdrv == NULL) return; |
if ((s == NULL) || (s[0] == 0) || (s[1] != ':')) return; |
while (mapdrv[i] != 0) { |
if (toupper(mapdrv[i]) == toupper(s[0])) { |
s[0] = toupper(mapdrv[i + 1]); |
return; |
} |
i += 2; |
} |
} |
/* */ |
void unmapdrives(char *s, char *mapdrv) { |
int i = 0; |
if (mapdrv == NULL) return; |
if ((s == NULL) || (s[0] == 0) || (s[1] != ':')) return; |
while (mapdrv[i] != 0) { |
if (toupper(mapdrv[i + 1]) == toupper(s[0])) { |
s[0] = toupper(mapdrv[i]); |
return; |
} |
i += 2; |
} |
} |
/* returns a pointer to the start of the filename, out of a path\to\file string, and |
fills respath with the local folder where the file should be placed. */ |
char *computelocalpath(char *longfilename, char *respath, char *dosdir, struct customdirs *dirlist) { |
/pkginst/helpers.h |
---|
1,6 → 1,6 |
/* |
* This file is part of pkginst |
* Copyright (C) 2012-2021 Mateusz Viste |
* This file is part of fdnpkg |
* Copyright (C) 2012-2017 Mateusz Viste |
* |
* It contains a few helper function... |
*/ |
14,6 → 14,8 |
void strtolower(char *mystring); |
char *fdnpkg_strcasestr(const char *s, const char *find); |
void mkpath(char *dirs); |
void mapdrives(char *s, char *mapdrv); |
void unmapdrives(char *s, char *mapdrv); |
char *computelocalpath(char *longfilename, char *respath, char *dosdir, struct customdirs *dirlist); |
void removeDoubleBackslashes(char *str); |
int detect_localpath(char *url); |
/pkginst/loadconf.c |
---|
20,8 → 20,11 |
#include "version.h" |
void freeconf(struct customdirs **dirlist) { |
void freeconf(char **repolist, int repscount, struct customdirs **dirlist) { |
int x; |
struct customdirs *curpos; |
/* free repolist */ |
for (x = 0; x < repscount; x++) free(repolist[x]); |
/* free the linked list of custom dirs */ |
while (*dirlist != NULL) { |
curpos = *dirlist; |
34,6 → 37,21 |
} |
static int checkfordoubledrepos(char **repolist, int repocount) { |
int x, y; |
for (x = 0; x < (repocount - 1); x++) { |
for (y = x + 1; y < repocount; y++) { |
if (strcmp(repolist[x], repolist[y]) == 0) { |
kitten_printf(7, 14, "Error: repository '%s' is listed twice!", repolist[x]); |
puts(""); |
return(-1); |
} |
} |
} |
return(0); |
} |
static int checkfordoubledirlist(struct customdirs *dirlist) { |
struct customdirs *curpos; |
for (; dirlist != NULL; dirlist = dirlist->next) { |
107,14 → 125,18 |
} |
int loadconf(char *cfgfile, struct customdirs **dirlist, int *flags) { |
int loadconf(char *cfgfile, char **repolist, int maxreps, unsigned long *crc32val, long *maxcachetime, struct customdirs **dirlist, int *flags, char **proxy, int *proxyport, char **mapdrv) { |
int bytebuff, parserstate = 0; |
FILE *fd; |
#define BUFFSIZE 1024 |
unsigned char *fbuff; |
#define maxtok 16 |
char token[maxtok]; |
#define maxval 1024 |
char value[maxval]; |
int curtok = 0, curval = 0, nline = 1; |
int repocount = 0; |
int buffread; |
fd = fopen(cfgfile, "r"); |
if (fd == NULL) { |
123,6 → 145,26 |
return(-1); |
} |
/* compute the CRC32 of the configuration file (if crc32val not NULL) */ |
if (crc32val != NULL) { |
fbuff = malloc(BUFFSIZE); |
if (fbuff == NULL) { |
fclose(fd); |
kitten_printf(2, 14, "Out of memory! (%s)", "fbuff malloc"); |
puts(""); |
puts(""); |
return(-1); |
} |
*crc32val = crc32_init(); |
while ((buffread = fread(fbuff, sizeof(char), BUFFSIZE, fd)) > 0) { |
if (buffread > 0) crc32_feed(crc32val, fbuff, buffread); |
} |
crc32_finish(crc32val); |
free(fbuff); |
} |
/* rewind the file, to start reading it again */ |
rewind(fd); |
/* read the config file line by line */ |
do { |
bytebuff = fgetc(fd); |
188,10 → 230,64 |
} |
/* Interpret the token/value pair now! */ |
/* printf("token='%s' ; value = '%s'\n", token, value); */ |
if (strcasecmp(token, "SKIPLINKS") == 0) { |
if (strcasecmp(token, "REPO") == 0) { /* Repository declaration */ |
if (maxreps == 0) { |
/* simply ignore if the app explicitely wishes to load no repositories */ |
} else if (repocount >= maxreps) { |
kitten_printf(7, 6, "Dropped a repository: too many configured (max=%d)", maxreps); |
puts(""); |
} else { |
char pathdelimchar; |
/* add a trailing path delimiter (slash or backslash) to the url if not there already */ |
if (detect_localpath(value) != 0) { |
pathdelimchar = '\\'; |
} else { |
pathdelimchar = '/'; |
} |
if ((value[curval - 1] != '/') && (value[curval - 1] != '\\')) { |
value[curval++] = pathdelimchar; |
value[curval] = 0; |
} |
/* copy the value into the repository list */ |
repolist[repocount] = strdup(value); |
if (repolist[repocount] == NULL) { |
kitten_printf(2, 14, "Out of memory! (%s)", "repolist malloc"); |
puts(""); |
freeconf(repolist, repocount, dirlist); |
fclose(fd); |
return(-1); |
} |
repocount += 1; |
} |
} else if (strcasecmp(token, "MAPDRIVES") == 0) { |
*mapdrv = strdup(value); |
if ((*mapdrv != NULL) && strlen(*mapdrv) & 1) { |
free(*mapdrv); |
*mapdrv = NULL; |
} |
if (*mapdrv == NULL) *mapdrv = ""; |
} else if (strcasecmp(token, "MAXCACHETIME") == 0) { |
long tmpint = atol(value); |
if ((tmpint >= 0) && (tmpint < 1209600l)) { /* min 0s / max 2 weeks */ |
if (maxcachetime != NULL) *maxcachetime = tmpint; |
} else { |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "maxcachetime", nline); |
puts(""); |
} |
} else if (strcasecmp(token, "INSTALLSOURCES") == 0) { |
int tmpint = atoi(value); /* must be 0/1 */ |
if (tmpint == 0) { |
*flags |= PKGINST_NOSOURCE; |
} else if (tmpint == 1) { |
/* do nothing */ |
} else { |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "installsources", nline); |
puts(""); |
} |
} else if (strcasecmp(token, "SKIPLINKS") == 0) { |
int tmpint = atoi(value); /* must be 0/1 */ |
if (tmpint == 0) { |
/* do nothing */ |
} else if (tmpint == 1) { |
*flags |= PKGINST_SKIPLINKS; |
} else { |
198,6 → 294,21 |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline); |
puts(""); |
} |
} else if (strcasecmp(token, "HTTP_PROXY") == 0) { |
if (value[0] != 0) { |
if (proxy != NULL) *proxy = strdup(value); |
} else { |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "http_proxy", nline); |
puts(""); |
} |
} else if (strcasecmp(token, "HTTP_PROXYPORT") == 0) { |
int tmpint = atoi(value); |
if (tmpint != 0) { |
if (proxyport != NULL) *proxyport = tmpint; |
} else { |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "http_proxyport", nline); |
puts(""); |
} |
} else if (strcasecmp(token, "DIR") == 0) { /* custom repository entry */ |
char *argv[2], *evar, *evar_content, *realLocation; |
#define realLocation_len 512 |
210,7 → 321,7 |
if (realLocation == NULL) { |
kitten_printf(2, 14, "Out of memory! (%s)", "malloc realLocation"); |
puts(""); |
freeconf(dirlist); |
freeconf(repolist, repocount, dirlist); |
fclose(fd); |
return(-1); |
} |
226,7 → 337,7 |
if (y + 1 > realLocation_len) { |
kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline); |
puts(""); |
freeconf(dirlist); |
freeconf(repolist, repocount, dirlist); |
free(realLocation); |
fclose(fd); |
return(-1); |
242,7 → 353,7 |
if (evar_content == NULL) { |
kitten_printf(7, 13, "Error: Found inexisting environnement variable '%s' at line #%d", evar, nline); |
puts(""); |
freeconf(dirlist); |
freeconf(repolist, repocount, dirlist); |
free(realLocation); |
fclose(fd); |
return(-1); |
250,7 → 361,7 |
if (strlen(evar_content) + y + 1 > realLocation_len) { |
kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline); |
puts(""); |
freeconf(dirlist); |
freeconf(repolist, repocount, dirlist); |
free(realLocation); |
fclose(fd); |
return(-1); |
268,7 → 379,7 |
if (addnewdir(dirlist, argv[0], realLocation) != 0) { |
kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir"); |
puts(""); |
freeconf(dirlist); |
freeconf(repolist, repocount, dirlist); |
free(realLocation); |
fclose(fd); |
return(-1); |
302,9 → 413,10 |
} while (bytebuff != EOF); |
fclose(fd); |
/* perform some validations */ |
/* Look out for doubled repositories */ |
if (checkfordoubledrepos(repolist, repocount) != 0) return(-1); |
if (checkfordoubledirlist(*dirlist) != 0) return(-1); |
if (validatedirlist(*dirlist) != 0) return(-1); |
return(0); |
return(repocount); |
} |
/pkginst/loadconf.h |
---|
1,7 → 1,7 |
/* |
* This file is part of pkginst. |
* This file is part of FDNPKG. |
* |
* Copyright (C) 2012-2021 Mateusz Viste |
* Copyright (C) 2012-2016 Mateusz Viste |
*/ |
#ifndef loadrepolist_sentinel |
13,11 → 13,11 |
struct customdirs *next; |
}; |
/* Loads the list of custom directories from the config file specified in %FDNPKG%. |
* Returns 0 on success, or -1 on failure. */ |
int loadconf(char *cfgfile, struct customdirs **dirlist, int *flags); |
/* Loads the list of repositories from the config file specified in %FDNPKG%. |
* Returns the amount of repositories found (and loaded) on success, or -1 on failure. */ |
int loadconf(char *cfgfile, char **repolist, int maxreps, unsigned long *crc32val, long *maxcachetime, struct customdirs **dirlist, int *nosourceflag, char **proxy, int *proxyport, char **mapdrv); |
/* Free the memory allocated at configuration load. */ |
void freeconf(struct customdirs **dirlist); |
void freeconf(char **repolist, int repscount, struct customdirs **dirlist); |
#endif |
/pkginst/main.c |
---|
85,7 → 85,7 |
} |
static int pkginst(char *file, int flags, char *dosdir, char *tempdir, struct customdirs *dirlist) { |
static int pkginst(char *file, int flags, char *dosdir, char *tempdir, struct customdirs *dirlist, char *mapdrv) { |
char pkgname[32]; |
int t, lastpathdelim = -1, u = 0; |
char *buffmem1k; |
111,11 → 111,11 |
return(1); |
} |
/* prepare the zip file and install it */ |
zipfileidx = pkginstall_preparepackage(pkgname, file, flags, &zipfilefd, dosdir, dirlist, buffmem1k); |
zipfileidx = pkginstall_preparepackage(NULL, pkgname, tempdir, file, flags, NULL, &zipfilefd, NULL, 0, NULL, dosdir, dirlist, buffmem1k, mapdrv); |
free(buffmem1k); |
if (zipfileidx != NULL) { |
int res = 0; |
if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd) != 0) res = 1; |
if (pkginstall_installpackage(pkgname, dosdir, dirlist, zipfileidx, zipfilefd, mapdrv) != 0) res = 1; |
fclose(zipfilefd); |
return(res); |
} else { |
130,6 → 130,7 |
enum ACTIONTYPES action; |
char *dosdir, *tempdir, *cfgfile; |
struct customdirs *dirlist; |
char *mapdrv = ""; |
action = parsearg(argc, argv); |
if (action == ACTION_HELP) return(showhelp()); |
150,7 → 151,7 |
/* load configuration */ |
flags = 0; |
dirlist = NULL; |
if (loadconf(cfgfile, &dirlist, &flags) < 0) return(5); |
if (loadconf(cfgfile, NULL, 0, NULL, NULL, &dirlist, &flags, NULL, NULL, &mapdrv) < 0) return(5); |
/* free the cfgfile buffer, I won't need the config file's location any more */ |
free(cfgfile); |
158,10 → 159,10 |
switch (action) { |
case ACTION_INSTALL: |
res = pkginst(argv[2], flags, dosdir, tempdir, dirlist); |
res = pkginst(argv[2], flags, dosdir, tempdir, dirlist, mapdrv); |
break; |
case ACTION_REMOVE: |
res = pkgrem(argv[2], dosdir); |
res = pkgrem(argv[2], dosdir, mapdrv); |
break; |
default: |
res = showhelp(); |
/pkginst/pkginst.c |
---|
137,9 → 137,10 |
/* returns 0 if pkgname is not installed, non-zero otherwise */ |
int is_package_installed(char *pkgname, char *dosdir) { |
int is_package_installed(char *pkgname, char *dosdir, char *mapdrv) { |
char fname[512]; |
sprintf(fname, "%s\\packages\\%s.lst", dosdir, pkgname); |
mapdrives(fname, mapdrv); |
if (fileexists(fname) != 0) { /* file exists -> package is installed */ |
return(1); |
} else { |
149,8 → 150,8 |
/* checks that pkgname is NOT installed. return 0 on success, non-zero otherwise. */ |
int validate_package_not_installed(char *pkgname, char *dosdir) { |
if (is_package_installed(pkgname, dosdir) != 0) { |
int validate_package_not_installed(char *pkgname, char *dosdir, char *mapdrv) { |
if (is_package_installed(pkgname, dosdir, mapdrv) != 0) { |
kitten_printf(3, 18, "Package %s is already installed! You might want to use the 'update' action.", pkgname); |
puts(""); |
return(-1); |
171,7 → 172,7 |
/* prepare a package for installation. this is mandatory before actually installing it! |
* returns a pointer to the zip file's index on success, NULL on failure. the *zipfile pointer is updated with a file descriptor to the open zip file to install. */ |
struct ziplist *pkginstall_preparepackage(char *pkgname, char *localfile, int flags, FILE **zipfd, char *dosdir, struct customdirs *dirlist, char *buffmem1k) { |
struct ziplist *pkginstall_preparepackage(struct pkgdb *pkgdb, char *pkgname, char *tempdir, char *localfile, int flags, char **repolist, FILE **zipfd, char *proxy, int proxyport, char *downloadingstring, char *dosdir, struct customdirs *dirlist, char *buffmem1k, char *mapdrv) { |
char *fname; |
char *zipfile; |
char *appinfofile; |
189,7 → 190,7 |
/* check if not already installed, if already here, print a message "you might want to use update instead" |
* of course this must not be done if we are in the process of upgrading said package */ |
if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir) != 0)) { |
if (((flags & PKGINST_UPDATE) == 0) && (validate_package_not_installed(pkgname, dosdir, mapdrv) != 0)) { |
return(NULL); |
} |
270,6 → 271,7 |
/* look out for collisions with already existing files (unless we are |
* updating the package and the local file belongs to it */ |
shortfile = computelocalpath(curzipnode->filename, fname, dosdir, dirlist); |
mapdrives(fname, mapdrv); |
strcat(fname, shortfile); |
if ((findfileinlist(flist, fname) == NULL) && (fileexists(fname) != 0)) { |
kitten_puts(3, 9, "Error: Package contains a file that already exists locally:"); |
315,7 → 317,7 |
/* install a package that has been prepared already. returns 0 on success, |
* or a negative value on error, or a positive value on warning */ |
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd) { |
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd, char *mapdrv) { |
char *buff; |
char *fulldestfilename; |
char packageslst[64]; |
342,6 → 344,7 |
/* open the lst file */ |
sprintf(buff, "%s\\%s", dosdir, packageslst); |
mapdrives(buff, mapdrv); |
lstfd = fopen(buff, "wb"); /* opening it in binary mode, because I like to have control over line terminators (CR/LF) */ |
if (lstfd == NULL) { |
kitten_printf(3, 10, "Error: Could not create %s!", buff); |
360,6 → 363,8 |
shortfile = computelocalpath(curzipnode->filename, buff, dosdir, dirlist); /* substitute paths to custom dirs */ |
/* log the filename to packages\pkg.lst (with original, unmapped drive) */ |
fprintf(lstfd, "%s%s\r\n", buff, shortfile); |
/* remap drive letter, if needed (AFTER writing to lstfd) */ |
mapdrives(buff, mapdrv); |
/* create the path, just in case it doesn't exist yet */ |
mkpath(buff); |
sprintf(fulldestfilename, "%s%s", buff, shortfile); |
375,6 → 380,7 |
} |
/* if it's a LINK file, recompute a new content */ |
if (islinkfile(curzipnode->filename) != 0) { |
unmapdrives(buff, mapdrv); |
processlinkfile(fulldestfilename, dosdir, dirlist, buff); |
} |
} |
/pkginst/pkginst.h |
---|
1,6 → 1,6 |
/* |
* This file is part of SvarDOS |
* Copyright (C) 2012-2021 Mateusz Viste |
* This file is part of FDNPKG |
* Copyright (C) 2012-2017 Mateusz Viste |
*/ |
#ifndef pkginst_sentinel |
8,9 → 8,9 |
#include "loadconf.h" /* required for struct customdirs */ |
int is_package_installed(char *pkgname, char *dosdir); |
struct ziplist *pkginstall_preparepackage(char *pkgname, char *localfile, int nosourceflag, FILE **zipfd, char *dosdir, struct customdirs *dirlist, char *buffmem1k); |
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd); |
int validate_package_not_installed(char *pkgname, char *dosdir); |
int is_package_installed(char *pkgname, char *dosdir, char *mapdrv); |
struct ziplist *pkginstall_preparepackage(struct pkgdb *pkgdb, char *pkgname, char *tempdir, char *localfile, int nosourceflag, char **repolist, FILE **zipfd, char *proxy, int proxyport, char *downloadingstring, char *dosdir, struct customdirs *dirlist, char *buffmem1k, char *mapdrv); |
int pkginstall_installpackage(char *pkgname, char *dosdir, struct customdirs *dirlist, struct ziplist *ziplinkedlist, FILE *zipfd, char *mapdrv); |
int validate_package_not_installed(char *pkgname, char *dosdir, char *mapdrv); |
#endif |
/pkginst/pkgrem.c |
---|
69,7 → 69,7 |
/* removes a package from the system. Returns 0 on success, non-zero otherwise */ |
int pkgrem(char *pkgname, char *dosdir) { |
int pkgrem(char *pkgname, char *dosdir, char *mapdrv) { |
char fpath[512]; |
char shellcmd[512]; |
char *lineptr; |
83,6 → 83,7 |
/* Check if the file %DOSDIR%\packages\pkgname.lst exists (if not, the package is not installed) */ |
sprintf(fpath, "%s\\packages\\%s.lst", dosdir, pkgname); |
mapdrives(fpath, mapdrv); |
if (fileexists(fpath) == 0) { /* file does not exist */ |
kitten_printf(4, 0, "Package %s is not installed, so not removed.", pkgname); |
puts(""); |
113,6 → 114,8 |
free(lineptr); /* free the memory occupied by the line */ |
continue; /* skip empty lines */ |
} |
/* remap drive */ |
mapdrives(lineptr, mapdrv); |
/* remember the path part for removal later */ |
lastdirsep = -1; |
for (x = 1; lineptr[x] != 0; x++) { |
/pkginst/pkgrem.h |
---|
1,9 → 1,9 |
/* |
* This file is part of the SvarDOS project. |
* Copyright (C) Mateusz Viste 2012-2021. All rights reserved. |
* This file is part of the FDNPKG project. |
* Copyright (C) Mateusz Viste 2012-2016. All rights reserved. |
*/ |
#ifndef pkgrem_sentinel |
#define pkgrem_sentinel |
int pkgrem(char *pkgname, char *dosdir); |
int pkgrem(char *pkgname, char *dosdir, char *mapdrv); |
#endif |