Subversion Repositories SvarDOS

Rev

Rev 314 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
184 mateuszvis 1
/*
185 mateuszvis 2
  SvarDOS repo index builder
562 mateuszvis 3
  Copyright (C) Mateusz Viste 2012-2022
184 mateuszvis 4
 
185 mateuszvis 5
  buildidx computes an index tsv file for the SvarDOS repository.
6
  it must be executed pointing to a directory that stores packages (zip)
7
  files. buildidx will generate the index file and save it into the package
8
  repository.
184 mateuszvis 9
 
314 mateuszvis 10
  13 feb 2021: 'title' LSM field is no longer looked after
292 mateuszvis 11
  11 feb 2021: lsm headers are no longer checked, so it is compatible with the simpler lsm format used by SvarDOS
185 mateuszvis 12
  13 jan 2021: removed the identification line, changed CRC32 to bsum, not creating the listing.txt file and stopped compressing index
184 mateuszvis 13
  23 apr 2017: uncompressed index is no longer created, added CRC32 of zib (bin only) files, if present
14
  28 aug 2016: listing.txt is always written inside the repo dir (instead of inside current dir)
15
  27 aug 2016: accepting full paths to repos (starting with /...)
16
  07 dec 2013: rewritten buildidx in ANSI C89
17
  19 aug 2013: add a compressed version of the index file to repos (index.gz)
18
  22 jul 2013: creating a listing.txt file with list of packages
19
  18 jul 2013: writing the number of packaged into the first line of the lst file
20
  11 jul 2013: added a switch to 7za to make it case insensitive when extracting lsm files
21
  10 jul 2013: changed unzip calls to 7za (to handle cases when appinfo is compressed with lzma)
22
  04 feb 2013: added CRC32 support
23
  22 sep 2012: forked 1st version from FDUPDATE builder
24
*/
25
 
26
#include <errno.h>
27
#include <stdio.h>   /* fopen, fclose... */
185 mateuszvis 28
#include <stdint.h>
184 mateuszvis 29
#include <stdlib.h>  /* system() */
30
#include <string.h>  /* strcasecmp() */
31
#include <time.h>    /* time(), ctime() */
32
#include <unistd.h>  /* read() */
33
#include <dirent.h>
34
#include <sys/types.h>
35
 
562 mateuszvis 36
#define pVer "2022-01-20"
184 mateuszvis 37
 
38
 
185 mateuszvis 39
/* computes the BSD sum of a file and returns it. returns 0 on error. */
186 mateuszvis 40
static uint16_t file2bsum(const char *filename) {
185 mateuszvis 41
  uint16_t result = 0;
42
  unsigned char buff[1024 * 1024];
43
  size_t i, buffread;
184 mateuszvis 44
  FILE *fd;
45
  fd = fopen(filename, "rb");
46
  if (fd == NULL) return(0);
47
  while ((buffread = fread(buff, 1, sizeof(buff), fd)) > 0) {
185 mateuszvis 48
    for (i = 0; i < buffread; i++) {
49
      /* rotr */
50
      result = (result >> 1) | (result << 15);
51
      /* */
52
      result += buff[i];
53
    }
184 mateuszvis 54
  }
55
  fclose(fd);
56
  return(result);
57
}
58
 
59
 
60
static void trim(char *str) {
61
  int x, y, firstchar = -1, lastchar = -1;
62
  for (x = 0; str[x] != 0; x++) {
63
    switch (str[x]) {
64
      case ' ':
65
      case '\t':
66
      case '\n':
67
      case '\r':
68
        break;
69
      default:
70
        if (firstchar < 0) firstchar = x;
71
        lastchar = x;
72
        break;
73
    }
74
  }
75
  str[lastchar + 1] = 0; /* right trim */
76
  if (firstchar > 0) { /* left trim (shift to the left ) */
77
    y = 0;
78
    for (x = firstchar; str[x] != 0; x++) str[y++] = str[x];
79
    str[y] = 0;
80
  }
81
}
82
 
83
 
84
/* reads a line from a file descriptor, and writes it to *line, the *line array is filled no more than maxlen bytes. returns the number of byte read on success, or a negative value on failure (reaching EOF is considered an error condition) */
85
static int readline_fromfile(FILE *fd, char *line, int maxlen) {
86
  int bytebuff, linelen = 0;
87
  for (;;) {
88
    bytebuff = fgetc(fd);
89
    if (bytebuff == EOF) {
90
      line[linelen] = 0;
91
      if (linelen == 0) return(-1);
92
      return(linelen);
93
    }
94
    if (bytebuff < 0) return(-1);
95
    if (bytebuff == '\r') continue; /* ignore CR */
96
    if (bytebuff == '\n') {
97
      line[linelen] = 0;
98
      return(linelen);
99
    }
100
    if (linelen < maxlen) line[linelen++] = bytebuff;
101
  }
102
}
103
 
314 mateuszvis 104
static int readlsm(const char *filename, char *version, char *description) {
184 mateuszvis 105
  char linebuff[1024];
106
  char *valuestr;
107
  int x;
108
  FILE *fd;
109
  /* reset fields to be read to empty values */
110
  version[0] = 0;
111
  description[0] = 0;
112
  /* open the file */
113
  fd = fopen(filename, "rb");
114
  if (fd == NULL) return(-1);
115
  /* read the LSM file line by line */
116
  while (readline_fromfile(fd, linebuff, 127) >= 0) {
117
    for (x = 0;; x++) {
118
      if (linebuff[x] == 0) {
119
        x = -1;
120
        break;
121
      } else if (linebuff[x] == ':') {
122
        break;
123
      }
124
    }
125
    if (x > 0) {
126
      linebuff[x] = 0;
127
      valuestr = linebuff + x + 1;
128
      trim(linebuff);
129
      trim(valuestr);
130
      if (strcasecmp(linebuff, "version") == 0) {
314 mateuszvis 131
        sprintf(version, "%s", valuestr);
132
      } else if (strcasecmp(linebuff, "description") == 0) {
133
        sprintf(description, "%s", valuestr);
184 mateuszvis 134
      }
135
    }
136
  }
137
  fclose(fd);
138
  return(0);
139
}
140
 
141
 
142
static int cmpstring(const void *p1, const void *p2) {
143
    /* The actual arguments to this function are "pointers to
144
       pointers to char", but strcmp(3) arguments are "pointers
145
       to char", hence the following cast plus dereference */
146
   return(strcasecmp(* (char * const *) p1, * (char * const *) p2));
147
}
148
 
149
 
186 mateuszvis 150
static void GenIndexes(const char *repodir) {
314 mateuszvis 151
  char *LsmFileList[4096]; /* TODO dynamic sizing, otherwise will crash if there are 4096+ packages */
184 mateuszvis 152
  char tmpbuf[64];
314 mateuszvis 153
  char *LsmFile, LSMpackage[64], LSMversion[128], LSMdescription[1024];
184 mateuszvis 154
  int LsmCount = 0, x;
185 mateuszvis 155
  FILE *idx;
184 mateuszvis 156
  DIR *dir;
157
  struct dirent *diritem;
158
 
159
  dir = opendir("appinfo");
160
  if (dir == NULL) {
161
    printf("ERROR: Unable to open directory '%s' (%s)\n", repodir, strerror(errno));
162
    return;
163
  }
164
 
165
  /* load the content of the appinfo directory */
166
  while ((diritem = readdir(dir)) != NULL) {
167
    if (strstr(diritem->d_name, ".lsm") == NULL) continue; /* skip files that do not contain '.lsm' */
168
    LsmFileList[LsmCount] = strdup(diritem->d_name);
169
    /* printf("Loaded LSM file: %s\n", LsmFileList[LsmCount]); */
170
    LsmCount += 1;
171
  }
172
 
173
  closedir(dir);
174
 
175
  printf("Found %d LSM files\n", LsmCount);
176
 
177
  /* sort the entries */
178
  qsort(&LsmFileList[0], LsmCount, sizeof(char *), cmpstring);
179
 
180
  /* Create the index file */
185 mateuszvis 181
  sprintf(tmpbuf, "%s/index.tsv", repodir);
184 mateuszvis 182
  idx = fopen(tmpbuf, "wb");
183
 
184
  /* Read every LSM */
185
  for (x = 0; x < LsmCount; x++) {
185 mateuszvis 186
    uint16_t bsum;
184 mateuszvis 187
    LsmFile = LsmFileList[x];
188
    sprintf(LSMpackage, "%s", LsmFile);
189
    LSMpackage[strlen(LSMpackage) - 4] = 0;
190
 
185 mateuszvis 191
    /* compute the BSD sum of the zip package */
184 mateuszvis 192
    sprintf(tmpbuf, "%s/%s.zip", repodir, LSMpackage);
185 mateuszvis 193
    bsum = file2bsum(tmpbuf);
184 mateuszvis 194
 
185 mateuszvis 195
    printf("Processing %s... BSUM %04X\n", LsmFile, bsum);
184 mateuszvis 196
 
197
    sprintf(tmpbuf, "appinfo/%s", LsmFile);
314 mateuszvis 198
    readlsm(tmpbuf, LSMversion, LSMdescription);
184 mateuszvis 199
 
200
    if (strlen(LSMpackage) > 8) printf("Warning: %s.zip is not in 8.3 format!\n", LSMpackage);
201
    if (LSMversion[0] == 0) printf("Warning: no LSM version for %s.zip!\n", LSMpackage);
202
    if (LSMdescription[0] == 0) printf("Warning: no LSM description for %s.zip!\n", LSMpackage);
185 mateuszvis 203
    fprintf(idx, "%s\t%s\t%s\t%u\n", LSMpackage, LSMversion, LSMdescription, bsum);
184 mateuszvis 204
  }
205
  fclose(idx);
206
  printf("%d packages found.\n", LsmCount);
207
}
208
 
209
 
210
 
211
int main(int argc, char **argv) {
212
  char *repodir;
186 mateuszvis 213
  char cmdbuff[256];
184 mateuszvis 214
 
186 mateuszvis 215
  puts("SvarDOS repository index generator ver " pVer);
184 mateuszvis 216
 
217
  if (argc != 2) {
218
    puts("Usage: buildidx repodir");
219
    return(1);
220
  }
221
 
222
  if ((argv[1][0] == '?') || (argv[1][0] == '-') || (argv[1][0]) == 0) {
223
    puts("Usage: buildidx repodir");
224
    return(1);
225
  }
226
 
227
  repodir = argv[1];
228
 
229
  printf("Will build index for %s directory.\n", repodir);
230
  puts("Recreating the appinfo directory...");
231
 
232
  system("rm -rf appinfo");
233
  system("mkdir appinfo");
234
 
235
  puts("Populating appinfo with LSM files from archives...");
186 mateuszvis 236
  snprintf(cmdbuff, sizeof(cmdbuff), "unzip -C -j -L -o '%s/*.zip' 'appinfo/*.lsm' -d appinfo", repodir);
184 mateuszvis 237
  system(cmdbuff);
238
 
239
  puts("Generating the index file...");
240
  GenIndexes(repodir);
241
 
242
  system("rm -rf appinfo");
243
  return(0);
244
}