Subversion Repositories SvarDOS

Rev

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