Subversion Repositories SvarDOS

Rev

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

Rev 219 Rev 228
1
/*
1
/*
2
 * This file is part of FDNPKG
2
 * This file is part of PKGINST (SvarDOS)
3
 * Copyright (C) 2013-2017 Mateusz Viste. All rights reserved.
3
 * Copyright (C) 2013-2021 Mateusz Viste. All rights reserved.
4
 */
4
 */
5
 
5
 
6
#include <stdio.h>
6
#include <stdio.h>
7
#include <ctype.h>    /* tolower() */
7
#include <ctype.h>    /* tolower() */
8
#include <stdlib.h>   /* atoi(), qsort() - not using it after all, redefining it manually later */
8
#include <stdlib.h>   /* atoi(), qsort() - not using it after all, redefining it manually later */
9
#include <string.h>   /* strlen() */
9
#include <string.h>   /* strlen() */
10
#include <sys/types.h>
10
#include <sys/types.h>
11
 
-
 
12
/* opendir() and friends */
11
#include <direct.h> /* opendir() and friends */
13
#ifdef __WATCOMC__
-
 
14
  #include <direct.h>
-
 
15
#else
-
 
16
  #include <dirent.h>
-
 
17
#endif
-
 
18
 
12
 
19
#include "fdnpkg.h"   /* PKGINST_UPDATE */
13
#include "fdnpkg.h"   /* PKGINST_UPDATE */
20
#include "fileexst.h"
14
#include "fileexst.h"
21
#include "getdelim.h"
15
#include "getdelim.h"
22
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
16
#include "helpers.h"  /* fdnpkg_strcasestr(), slash2backslash()... */
23
#include "kprintf.h"
17
#include "kprintf.h"
24
#include "libunzip.h"  /* zip_freelist()... */
18
#include "libunzip.h"  /* zip_freelist()... */
25
#include "lsm.h"
19
#include "lsm.h"
26
#include "pkgdb.h"
20
#include "pkgdb.h"
27
#include "pkginst.h"
21
#include "pkginst.h"
28
#include "pkgrem.h"
22
#include "pkgrem.h"
29
#include "rtrim.h"
23
#include "rtrim.h"
30
#include "showinst.h"  /* include self for control */
24
#include "showinst.h"  /* include self for control */
31
#include "version.h"
25
#include "version.h"
32
 
26
 
33
 
27
 
34
/* this is a wrapper around strcasecmp(), to be used by qsort() */
28
/* this is a wrapper around strcasecmp(), to be used by qsort() */
35
static int strcompare(const void *str1, const void *str2) {
29
static int strcompare(const void *str1, const void *str2) {
36
  char **s1 = (char **)str1;
30
  char **s1 = (char **)str1;
37
  char **s2 = (char **)str2;
31
  char **s2 = (char **)str2;
38
  return(strcasecmp(*s1, *s2));
32
  return(strcasecmp(*s1, *s2));
39
}
33
}
40
 
34
 
41
 
35
 
42
/* clears current line */
-
 
43
static void clrline(void) {
-
 
44
  printf("\r                                                                            \r");
-
 
45
}
-
 
46
 
-
 
47
 
-
 
48
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, char *filterstr, char *dosdir) {
36
static int loadinstpkglist(char **packagelist, char **packagelist_ver, int packagelist_maxlen, char *filterstr, char *dosdir) {
49
  DIR *dp;
37
  DIR *dp;
50
  int packagelist_len = 0, x;
38
  int packagelist_len = 0, x;
51
  struct dirent *ep;
39
  struct dirent *ep;
52
  #define verstr_maxlen 64
40
  #define verstr_maxlen 64
53
  char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
41
  char pkgdir[512], lsmfilename[1024], verstr[verstr_maxlen];
54
  sprintf(pkgdir, "%s\\packages", dosdir);
42
  sprintf(pkgdir, "%s\\packages", dosdir);
55
  dp = opendir(pkgdir);
43
  dp = opendir(pkgdir);
56
  if (dp != NULL) {
44
  if (dp != NULL) {
57
    while ((ep = readdir(dp)) != NULL) {
45
    while ((ep = readdir(dp)) != NULL) {
58
      if (ep->d_name[0] != '.') { /* ignore '.', '..', and hidden directories */
46
      if (ep->d_name[0] != '.') { /* ignore '.', '..', and hidden directories */
59
        if (strlen(ep->d_name) > 4) {
47
        if (strlen(ep->d_name) > 4) {
60
          int tlen = strlen(ep->d_name);
48
          int tlen = strlen(ep->d_name);
61
          if ((ep->d_name[tlen - 4] != '.') || (tolower(ep->d_name[tlen - 3]) != 'l') || (tolower(ep->d_name[tlen - 2]) != 's') || (tolower(ep->d_name[tlen - 1]) != 't')) continue;  /* if it's not an .lst file, skip it silently */
49
          if ((ep->d_name[tlen - 4] != '.') || (tolower(ep->d_name[tlen - 3]) != 'l') || (tolower(ep->d_name[tlen - 2]) != 's') || (tolower(ep->d_name[tlen - 1]) != 't')) continue;  /* if it's not an .lst file, skip it silently */
62
 
50
 
63
          if (filterstr != NULL) {
51
          if (filterstr != NULL) {
64
            if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
52
            if (fdnpkg_strcasestr(ep->d_name, filterstr) == NULL) continue; /* if it's not matching the non-NULL filter, skip it */
65
          }
53
          }
66
          if (packagelist_len >= packagelist_maxlen) {
54
          if (packagelist_len >= packagelist_maxlen) {
67
            closedir(dp);
55
            closedir(dp);
68
            return(-1); /* if not enough place in the list - return an error */
56
            return(-1); /* if not enough place in the list - return an error */
69
          }
57
          }
70
          packagelist[packagelist_len] = strdup(ep->d_name);
58
          packagelist[packagelist_len] = strdup(ep->d_name);
71
          packagelist[packagelist_len][strlen(packagelist[packagelist_len]) - 4] = 0; /* cut out the .lst extension */
59
          packagelist[packagelist_len][strlen(packagelist[packagelist_len]) - 4] = 0; /* cut out the .lst extension */
72
          packagelist_len += 1;
60
          packagelist_len += 1;
73
        }
61
        }
74
      }
62
      }
75
    }
63
    }
76
    closedir(dp);
64
    closedir(dp);
77
    qsort(packagelist, packagelist_len, sizeof (char **), strcompare); /* sort the package list */
65
    qsort(packagelist, packagelist_len, sizeof (char **), strcompare); /* sort the package list */
78
    for (x = 0; x < packagelist_len; x++) {
66
    for (x = 0; x < packagelist_len; x++) {
79
      /* for each package, load the metadata from %DOSDIR\APPINFO\*.lsm */
67
      /* for each package, load the metadata from %DOSDIR\APPINFO\*.lsm */
80
      sprintf(lsmfilename, "%s\\appinfo\\%s.lsm", dosdir, packagelist[x]);
68
      sprintf(lsmfilename, "%s\\appinfo\\%s.lsm", dosdir, packagelist[x]);
81
      if (readlsm(lsmfilename, verstr, verstr_maxlen) != 0) sprintf(verstr, "(unknown version)");
69
      if (readlsm(lsmfilename, verstr, verstr_maxlen) != 0) sprintf(verstr, "(unknown version)");
82
      packagelist_ver[x] = strdup(verstr);
70
      packagelist_ver[x] = strdup(verstr);
83
    }
71
    }
84
    return(packagelist_len);
72
    return(packagelist_len);
85
  } else {
73
  } else {
86
    kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
74
    kitten_printf(9, 0, "Error: Could not access the %s directory.", pkgdir);
87
    puts("");
75
    puts("");
88
    return(-1);
76
    return(-1);
89
  }
77
  }
90
}
78
}
91
 
79
 
92
#define packagelist_maxlen 1024
80
#define packagelist_maxlen 1024
93
 
81
 
94
void showinstalledpkgs(char *filterstr, char *dosdir) {
82
void showinstalledpkgs(char *filterstr, char *dosdir) {
95
  char *packagelist[packagelist_maxlen];
83
  char *packagelist[packagelist_maxlen];
96
  char *packagelist_ver[packagelist_maxlen];
84
  char *packagelist_ver[packagelist_maxlen];
97
  int packagelist_len, x;
85
  int packagelist_len, x;
98
 
86
 
99
  /* load the list of packages */
87
  /* load the list of packages */
100
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir);   /* Populate the packages list */
88
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, filterstr, dosdir);   /* Populate the packages list */
101
  if (packagelist_len < 0) return;
89
  if (packagelist_len < 0) return;
102
  if (packagelist_len == 0) {
90
  if (packagelist_len == 0) {
103
    kitten_puts(5, 0, "No package matched the search.");
91
    kitten_puts(5, 0, "No package matched the search.");
104
    return;
92
    return;
105
  }
93
  }
106
 
94
 
107
  /* iterate through all packages */
95
  /* iterate through all packages */
108
  for (x = 0; x < packagelist_len; x++) {
96
  for (x = 0; x < packagelist_len; x++) {
109
    /* print the package/version couple on screen */
97
    /* print the package/version couple on screen */
110
    printf("%s %s\n", packagelist[x], packagelist_ver[x]);
98
    printf("%s %s\n", packagelist[x], packagelist_ver[x]);
111
  }
99
  }
112
}
-
 
113
 
-
 
114
 
-
 
115
/* displays the list of available updates for local packages (or a single package if pkg is not NULL). Returns 0 if some updates are found, non zero otherwise. */
-
 
116
int checkupdates(char *dosdir, struct pkgdb *pkgdb, char **repolist, char *pkg, char *tempdir, int flags, struct customdirs *dirlist, char *proxy, int proxyport, char *downloadingstring, char *mapdrv) {
-
 
117
  struct pkgdb *curpkg;
-
 
118
  struct pkgrepo *currepo;
-
 
119
  char *packagelist[packagelist_maxlen];
-
 
120
  char *packagelist_ver[packagelist_maxlen];
-
 
121
  int packagelist_len, x, foundupdate = 0, totalupdatesfound = 0;
-
 
122
  int packages_updated = 0, packages_updatefailed = 0;
-
 
123
  packagelist_len = loadinstpkglist(packagelist, packagelist_ver, packagelist_maxlen, NULL, dosdir);
-
 
124
  for (x = 0; x < packagelist_len; x++) {
-
 
125
    if (pkg != NULL) {
-
 
126
      if (strcasecmp(pkg, packagelist[x]) != 0) continue; /* if we got asked for a specific package, skip all other packages. */
-
 
127
    } else { /* else display a progress so slower PCs don't think we are freezed */
-
 
128
      long percprogress = x; /* long, just in case we have more than 3'200 packages installed... */
-
 
129
      percprogress *= 100;
-
 
130
      percprogress /= packagelist_len;
-
 
131
      kitten_printf(10, 7, "Looking for updates...");
-
 
132
      printf(" %ld%% (%s)        \r", percprogress, packagelist[x]); /* empty spaces trailing for flushing the previous content on the line */
-
 
133
    }
-
 
134
    for (curpkg = pkgdb; curpkg != NULL; curpkg = curpkg->nextpkg) { /* iterate through packages */
-
 
135
      if (strcasecmp(curpkg->name, packagelist[x]) == 0) {
-
 
136
        foundupdate = 0;
-
 
137
        for (currepo = curpkg->repolist; currepo != NULL; currepo = currepo->nextrepo) { /* check for possible newer version in every repo */
-
 
138
          if (isversionnewer(packagelist_ver[x], currepo->version) > 0) { /* isversionnewer() returns 1 if version is newer */
-
 
139
            if (foundupdate == 0) {
-
 
140
              foundupdate = 1;
-
 
141
              totalupdatesfound += 1;
-
 
142
              if (pkg == NULL) {
-
 
143
                clrline();
-
 
144
                kitten_printf(10, 0, "%s (local version: %s)", packagelist[x], packagelist_ver[x]);
-
 
145
                puts("");
-
 
146
              }
-
 
147
            }
-
 
148
            if (pkg == NULL) {
-
 
149
              clrline();
-
 
150
              printf("  ");
-
 
151
              kitten_printf(10, 1, "version %s at %s", currepo->version, repolist[currepo->repo]);
-
 
152
              puts("");
-
 
153
            }
-
 
154
          }
-
 
155
        }
-
 
156
        /* actually upgrade the package, if requested so */
-
 
157
        if (foundupdate != 0) {
-
 
158
          if (flags & PKGINST_UPDATE) {
-
 
159
            FILE *zipfilefd;
-
 
160
            struct ziplist *zipfileidx;
-
 
161
            char buffmem1k[1024];
-
 
162
            clrline();
-
 
163
            kitten_printf(10, 3, "An update of '%s' has been found. Update in progress...", packagelist[x]);
-
 
164
            puts("");
-
 
165
            packages_updatefailed += 1; /* increment the updatefailed counter - later we will decrement it if we're okay */
-
 
166
            zipfileidx = pkginstall_preparepackage(pkgdb, packagelist[x], tempdir, NULL, flags, repolist, &zipfilefd, proxy, proxyport, downloadingstring, dosdir, dirlist, buffmem1k, mapdrv);
-
 
167
            if (zipfileidx != NULL) {
-
 
168
              if (pkgrem(packagelist[x], dosdir, mapdrv) != 0) {
-
 
169
                /* ooops, package removal failed... */
-
 
170
                zip_freelist(&zipfileidx);
-
 
171
              } else {  /* pkgrem == 0 */
-
 
172
                pkginstall_installpackage(packagelist[x], dosdir, dirlist, zipfileidx, zipfilefd, mapdrv);
-
 
173
                packages_updated += 1;
-
 
174
                packages_updatefailed -= 1; /* decrement the updatefailed counter to leverage the fact we incremented it without reason earlier */
-
 
175
              }
-
 
176
              fclose(zipfilefd);
-
 
177
            }
-
 
178
          }
-
 
179
          puts(""); /* add a line feed to visually separate packages */
-
 
180
        }
-
 
181
        break; /* get out of the loop to get over the next local package */
-
 
182
      }
-
 
183
    }
-
 
184
  }
-
 
185
  if (pkg == NULL) {
-
 
186
    clrline();
-
 
187
    kitten_printf(10, 5, "%d package update(s) found.", totalupdatesfound);
-
 
188
    puts("");
-
 
189
    if (flags & PKGINST_UPDATE) {
-
 
190
      kitten_printf(10, 4, "%d package(s) updated, %d package(s) failed.", packages_updated, packages_updatefailed);
-
 
191
      puts("");
-
 
192
    }
-
 
193
  }
-
 
194
  if (foundupdate != 0) {
-
 
195
    return(0);
-
 
196
  } else {
-
 
197
    return(-1);
-
 
198
  }
-
 
199
}
100
}
200
 
101
 
201
 
102
 
202
/* frees a linked list of filenames */
103
/* frees a linked list of filenames */
203
void pkg_freeflist(struct flist_t *flist) {
104
void pkg_freeflist(struct flist_t *flist) {
204
  while (flist != NULL) {
105
  while (flist != NULL) {
205
    struct flist_t *victim = flist;
106
    struct flist_t *victim = flist;
206
    flist = flist->next;
107
    flist = flist->next;
207
    free(victim);
108
    free(victim);
208
  }
109
  }
209
}
110
}
210
 
111
 
211
 
112
 
212
/* returns a linked list of the files that belong to package pkgname */
113
/* returns a linked list of the files that belong to package pkgname */
213
struct flist_t *pkg_loadflist(char *pkgname, char *dosdir) {
114
struct flist_t *pkg_loadflist(char *pkgname, char *dosdir) {
214
  struct flist_t *res = NULL, *newnode;
115
  struct flist_t *res = NULL, *newnode;
215
  FILE *fd;
116
  FILE *fd;
216
  char *lineptr;
117
  char *lineptr;
217
  char buff[512];
118
  char buff[512];
218
  int getdelimlen, fnamelen;
119
  int getdelimlen, fnamelen;
219
  size_t getdelimcount = 0;
120
  size_t getdelimcount = 0;
220
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
121
  sprintf(buff, "%s\\packages\\%s.lst", dosdir, pkgname);
221
  if (fileexists(buff) == 0) { /* file does not exist */
122
  if (fileexists(buff) == 0) { /* file does not exist */
222
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
123
    kitten_printf(9, 1, "Error: Local package %s not found.", pkgname);
223
    puts("");
124
    puts("");
224
    return(NULL);
125
    return(NULL);
225
  }
126
  }
226
  fd = fopen(buff, "rb");
127
  fd = fopen(buff, "rb");
227
  if (fd == NULL) {
128
  if (fd == NULL) {
228
    kitten_puts(4, 1, "Error opening lst file!");
129
    kitten_puts(4, 1, "Error opening lst file!");
229
    return(NULL);
130
    return(NULL);
230
  }
131
  }
231
  /* iterate through all lines of the file */
132
  /* iterate through all lines of the file */
232
  for (;;) {
133
  for (;;) {
233
    lineptr = NULL;
134
    lineptr = NULL;
234
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
135
    getdelimlen = getdelim(&lineptr, &getdelimcount, '\n', fd);
235
    if (getdelimlen < 0) { /* EOF */
136
    if (getdelimlen < 0) { /* EOF */
236
      free(lineptr);
137
      free(lineptr);
237
      break;
138
      break;
238
    }
139
    }
239
    rtrim(lineptr);  /* right-trim the filename */
140
    rtrim(lineptr);  /* right-trim the filename */
240
    slash2backslash(lineptr); /* change all / to \ */
141
    slash2backslash(lineptr); /* change all / to \ */
241
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
142
    if ((lineptr[0] == 0) || (lineptr[0] == '\r') || (lineptr[0] == '\n')) continue; /* skip empty lines */
242
    if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
143
    if (lineptr[strlen(lineptr) - 1] == '\\') continue; /* skip directories */
243
    if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
144
    if ((lineptr[0] == '\\') || (lineptr[1] == ':')) { /* this is an absolute path */
244
      fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
145
      fnamelen = snprintf(buff, sizeof(buff), "%s", lineptr);
245
    } else { /* else it's a relative path starting at %dosdir% */
146
    } else { /* else it's a relative path starting at %dosdir% */
246
      fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
147
      fnamelen = snprintf(buff, sizeof(buff), "%s\\%s\n", dosdir, lineptr);
247
    }
148
    }
248
    free(lineptr); /* free the memory occupied by the line */
149
    free(lineptr); /* free the memory occupied by the line */
249
    /* add the new node to the result */
150
    /* add the new node to the result */
250
    newnode = malloc(sizeof(struct flist_t) + fnamelen);
151
    newnode = malloc(sizeof(struct flist_t) + fnamelen);
251
    if (newnode == NULL) {
152
    if (newnode == NULL) {
252
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
153
      kitten_printf(2, 14, "Out of memory! (%s)", "malloc failure");
253
      continue;
154
      continue;
254
    }
155
    }
255
    newnode->next = res;
156
    newnode->next = res;
256
    strcpy(newnode->fname, buff);
157
    strcpy(newnode->fname, buff);
257
    res = newnode;
158
    res = newnode;
258
  }
159
  }
259
  fclose(fd);
160
  fclose(fd);
260
  return(res);
161
  return(res);
261
}
162
}
262
 
163
 
263
 
164
 
264
/* Prints files owned by a package */
165
/* Prints files owned by a package */
265
void listfilesofpkg(char *pkgname, char *dosdir) {
166
void listfilesofpkg(char *pkgname, char *dosdir) {
266
  struct flist_t *flist, *flist_ptr;
167
  struct flist_t *flist, *flist_ptr;
267
  /* load the list of files belonging to pkgname */
168
  /* load the list of files belonging to pkgname */
268
  flist = pkg_loadflist(pkgname, dosdir);
169
  flist = pkg_loadflist(pkgname, dosdir);
269
  /* display each filename on screen */
170
  /* display each filename on screen */
270
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
171
  for (flist_ptr = flist; flist_ptr != NULL; flist_ptr = flist_ptr->next) {
271
    printf("%s\n", flist_ptr->fname);
172
    printf("%s\n", flist_ptr->fname);
272
  }
173
  }
273
  /* free the list of files */
174
  /* free the list of files */
274
  pkg_freeflist(flist);
175
  pkg_freeflist(flist);
275
}
176
}
276
 
177