Subversion Repositories SvarDOS

Rev

Rev 562 | Rev 650 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
562 mateuszvis 1
<?php /*
2
 
3
  SvarDOS repo index builder
4
  Copyright (C) Mateusz Viste 2012-2022
5
 
6
  buildidx computes an index tsv file for the SvarDOS repository.
7
  it must be executed pointing to a directory that stores packages (zip)
8
  files. buildidx will generate the index file and save it into the package
9
  repository.
10
 
11
  requires php-zip
12
 
13
  20 jan 2022: rewritten the code from ANSI C to PHP for easier maintenance
14
  13 feb 2021: 'title' LSM field is no longer looked after
15
  11 feb 2021: lsm headers are no longer checked, so it is compatible with the simpler lsm format used by SvarDOS
16
  13 jan 2021: removed the identification line, changed CRC32 to bsum, not creating the listing.txt file and stopped compressing index
17
  23 apr 2017: uncompressed index is no longer created, added CRC32 of zib (bin only) files, if present
18
  28 aug 2016: listing.txt is always written inside the repo dir (instead of inside current dir)
19
  27 aug 2016: accepting full paths to repos (starting with /...)
20
  07 dec 2013: rewritten buildidx in ANSI C89
21
  19 aug 2013: add a compressed version of the index file to repos (index.gz)
22
  22 jul 2013: creating a listing.txt file with list of packages
23
  18 jul 2013: writing the number of packaged into the first line of the lst file
24
  11 jul 2013: added a switch to 7za to make it case insensitive when extracting lsm files
25
  10 jul 2013: changed unzip calls to 7za (to handle cases when appinfo is compressed with lzma)
26
  04 feb 2013: added CRC32 support
27
  22 sep 2012: forked 1st version from FDUPDATE builder
28
*/
29
 
30
$PVER = "20220120";
31
 
32
 
33
// computes the BSD sum of a file and returns it
34
function file2bsum($fname) {
35
  $result = 0;
36
 
37
  $fd = fopen($fname, 'rb');
38
  if ($fd === false) return(0);
39
 
40
  while (!feof($fd)) {
41
 
42
    $buff = fread($fd, 1024 * 1024);
43
 
563 mateuszvis 44
    $slen = strlen($buff);
45
    for ($i = 0; $i < $slen; $i++) {
562 mateuszvis 46
      // rotr
47
      $result = ($result >> 1) | ($result << 15);
48
      // add and truncate to 16 bits
563 mateuszvis 49
      $result += ord($buff[$i]);
562 mateuszvis 50
      $result &= 0xffff;
51
    }
52
  }
53
 
54
  fclose($fd);
55
  return($result);
56
}
57
 
58
 
59
// reads file fil from zip archive z and returns its content, or false on error
60
function read_file_from_zip($z, $fil) {
61
  $zip = new ZipArchive;
62
  if ($zip->open($z, ZipArchive::RDONLY) !== true) {
63
    echo "ERROR: failed to open zip file '{$z}'\n";
64
    return(false);
65
  }
66
 
67
  // load the appinfo/pkgname.lsm file
68
  $res = $zip->getFromName($fil, 8192, ZipArchive::FL_NOCASE);
69
 
70
  $zip->close();
71
  return($res);
72
}
73
 
74
 
75
// reads a LSM string and returns it in the form of an array
76
function parse_lsm($s) {
77
  $res = array();
78
  for ($l = strtok($s, "\n"); $l !== false; $l = strtok("\n")) {
79
    // the line is "token: value", let's find the colon
80
    $colpos = strpos($l, ':');
81
    if (($colpos === false) || ($colpos === 0)) continue;
82
    $tok = strtolower(trim(substr($l, 0, $colpos)));
83
    $val = trim(substr($l, $colpos + 1));
84
    $res[$tok] = $val;
85
  }
86
  return($res);
87
}
88
 
89
 
90
// ***************** MAIN ROUTINE *********************************************
91
 
92
echo "SvarDOS repository index generator ver {$PVER}\n";
93
 
94
if (($_SERVER['argc'] != 2) || ($_SERVER['argv'][1][0] == '-')) {
95
  echo "usage: php buildidx.php repodir\n";
96
  exit(1);
97
}
98
 
99
$repodir = $_SERVER['argv'][1];
100
 
101
echo "building index for directory {$repodir}...\n";
102
 
103
$pkgfiles = scandir($repodir);
104
$pkglist = '';
105
$pkgcount = 0;
106
 
107
// iterate over each zip file
108
foreach ($pkgfiles as $zipfile) {
109
  if (!preg_match('/.zip$/i', $zipfile)) continue; // skip non-zip files
110
  if (strchr($zipfile, '-')) {
111
    echo "skipping: {$zipfile}\n";
112
    continue; // skip alt vers (like dosmid-0.9.2.zip)
113
  }
114
 
115
  $path_parts = pathinfo($zipfile);
116
  $pkg = strtolower($path_parts['filename']);
117
 
118
  $zipfile_fullpath = realpath($repodir . '/' . $zipfile);
119
 
120
  $lsm = read_file_from_zip($zipfile_fullpath, "appinfo/{$pkg}.lsm");
121
  if ($lsm == false) {
122
    echo "ERROR: pkg {$z} does not contain an LSM file at the expected location\n";
123
    exit(1);
124
  }
125
 
126
  $lsmarray = parse_lsm($lsm);
127
  if (empty($lsmarray['version'])) {
128
    echo "ERROR: lsm file in {$zipfile} does not contain a version\n";
129
    var_dump($lsmarray);
130
    exit(1);
131
  }
132
  if (empty($lsmarray['description'])) {
133
    echo "ERROR: lsm file in {$zipfile} does not contain a description\n";
134
    exit(1);
135
  }
136
 
137
  $pkglist .= "{$pkg}\t{$lsmarray['version']}\t{$lsmarray['description']}\t" . file2bsum($zipfile_fullpath) . "\n";
138
  $pkgcount++;
139
 
140
}
141
 
142
echo "DONE - processed " . $pkgcount . " zip files\n";
143
 
144
file_put_contents($repodir . '/index.tsv', $pkglist);
145
 
146
exit(0);
147
 
148
?>