Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | /* |
||
5 | Copyright 2020 Daniel Marschall, ViaThinkSoft |
||
6 | |||
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | you may not use this file except in compliance with the License. |
||
9 | You may obtain a copy of the License at |
||
10 | |||
11 | http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | |||
13 | Unless required by applicable law or agreed to in writing, software |
||
14 | distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | See the License for the specific language governing permissions and |
||
17 | limitations under the License. |
||
18 | */ |
||
19 | |||
20 | function testsfv($file) { |
||
21 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
||
22 | if (!file_exists($file)) { |
||
23 | echo "ERROR: File $file does not exist.\n"; |
||
24 | return; |
||
25 | } |
||
26 | |||
27 | $lines = file($file); |
||
28 | foreach ($lines as $line) { |
||
29 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
||
30 | $line = rtrim($line); |
||
31 | if ($line == '') continue; |
||
32 | $checksum = substr($line,-8); |
||
33 | $origname = rtrim(substr($line,0,strlen($line)-8)); |
||
34 | $origname = dirname($file) . '/' . trim($origname); |
||
35 | if (!file_exists($origname)) { |
||
36 | echo "WARNING: File vanished : $origname\n"; |
||
37 | } else { |
||
38 | $checksum2 = crc32_file($origname); |
||
39 | if (strtolower($checksum) != strtolower($checksum2)) { |
||
40 | echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"; |
||
41 | } else { |
||
42 | //echo "OK: $origname\n"; |
||
43 | } |
||
44 | } |
||
45 | // TODO: Also warn about extra files which are not indexed |
||
46 | } |
||
47 | } |
||
48 | |||
49 | function swapEndianness($hex) { |
||
50 | return implode('', array_reverse(str_split($hex, 2))); |
||
51 | } |
||
52 | |||
53 | function crc32_file($filename, $rawOutput = false) { |
||
54 | $out = bin2hex(hash_file ('crc32b', $filename , true)); |
||
55 | if (hash('crc32b', 'TEST') == 'b893eaee') { |
||
56 | // hash_file() in PHP 5.2 has the wrong Endianess! |
||
57 | // https://bugs.php.net/bug.php?id=47467 |
||
58 | $out = swapEndianness($out); |
||
59 | } |
||
60 | return $out; |
||
61 | } |
||
62 | |||
63 | function _rec($directory) { |
||
64 | if (!is_dir($directory)) { |
||
65 | exit("Invalid directory path $directory\n"); |
||
66 | } |
||
67 | |||
68 | if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) { |
||
69 | // echo "Directory $directory has no SFV file. Skipping.\n"; |
||
70 | } else { |
||
71 | $out = array(); |
||
72 | |||
73 | // echo "Check $directory\n"; |
||
74 | $sfvfiles = glob('*.sfv'); |
||
75 | foreach ($sfvfiles as $sfvfile) { |
||
76 | testsfv($sfvfile); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | foreach (scandir($directory) as $file) { |
||
81 | if ($file !== '.' && $file !== '..') { |
||
82 | $file = $directory . '/' . $file; |
||
83 | if (is_dir($file)) { |
||
84 | _rec($file); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | |||
91 | # --- |
||
92 | |||
93 | if ($argc != 2) { |
||
94 | echo "Syntax: $argv[0] <directory>\n"; |
||
95 | exit(2); |
||
96 | } |
||
97 | |||
98 | if (!is_dir($argv[1])) { |
||
99 | echo "Directory not found\n"; |
||
100 | exit(1); |
||
101 | } |
||
102 | |||
103 | _rec($argv[1]); |
||
104 | |||
105 | echo "Done.\n"; |