Subversion Repositories SvarDOS

Rev

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