Rev 2 | Rev 5 | Go to most recent revision | Details | Compare with Previous | 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 | |||
4 | daniel-mar | 20 | // TODO: make use of STDERR and return different exit codes |
21 | |||
2 | daniel-mar | 22 | function testmd5($file) { |
23 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
||
24 | if (!file_exists($file)) { |
||
25 | echo "ERROR: File $file does not exist.\n"; |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | $lines = file($file); |
||
4 | daniel-mar | 30 | $is_first_line = true; |
31 | $force_utf8 = false; |
||
2 | daniel-mar | 32 | foreach ($lines as $line) { |
4 | daniel-mar | 33 | if ($is_first_line) { |
34 | $tmp = 0; |
||
35 | $line = str_replace("\xEF\xBB\xBF",'',$line,$tmp); |
||
36 | if ($tmp > 0) $force_utf8 = true; |
||
37 | $is_first_line = false; |
||
38 | } |
||
39 | $is_ansi = strstr(utf8_decode($line),'?') !== false; // Attention: This assumes that '?' is not part of the line! |
||
40 | if (!$force_utf8 && $is_ansi) $line = utf8_encode($line); |
||
41 | |||
2 | daniel-mar | 42 | $line = trim($line); |
43 | if ($line == '') continue; |
||
44 | $line = str_replace('*', ' ', $line); |
||
45 | $line = str_replace("\t", ' ', $line); |
||
46 | list($checksum, $origname) = explode(' ', $line, 2); |
||
47 | $origname = dirname($file) . '/' . trim($origname); |
||
48 | $checksum = trim($checksum); |
||
49 | if (!file_exists($origname)) { |
||
50 | echo "WARNING: File vanished : $origname\n"; |
||
51 | } else { |
||
52 | $checksum2 = md5_file($origname); |
||
53 | if (strtolower($checksum) != strtolower($checksum2)) { |
||
54 | echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"; |
||
55 | } else { |
||
4 | daniel-mar | 56 | global $show_verbose; |
57 | if ($show_verbose) echo "OK: $origname\n"; |
||
2 | daniel-mar | 58 | } |
59 | } |
||
60 | // TODO: Also warn about extra files which are not indexed |
||
61 | } |
||
62 | } |
||
63 | |||
64 | function _rec($directory) { |
||
4 | daniel-mar | 65 | $directory = rtrim($directory, '/\\'); |
66 | |||
2 | daniel-mar | 67 | if (!is_dir($directory)) { |
68 | exit("Invalid directory path $directory\n"); |
||
69 | } |
||
70 | |||
71 | if ($dont_add_files = count(glob("$directory/*.md5")) == 0) { |
||
4 | daniel-mar | 72 | global $show_verbose; |
73 | if ($show_verbose) echo "Directory $directory has no MD5 file. Skipping.\n"; |
||
2 | daniel-mar | 74 | } else { |
75 | $out = array(); |
||
76 | |||
4 | daniel-mar | 77 | global $show_verbose; |
78 | if ($show_verbose) echo "Check directory $directory\n"; |
||
79 | $md5files = glob($directory.'/*.md5'); |
||
2 | daniel-mar | 80 | foreach ($md5files as $md5file) { |
81 | testmd5($md5file); |
||
82 | } |
||
83 | } |
||
84 | |||
4 | daniel-mar | 85 | $sd = @scandir($directory); |
86 | if ($sd === false) { |
||
87 | echo "Error: Cannot scan directory $directory\n"; |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | foreach ($sd as $file) { |
||
2 | daniel-mar | 92 | if ($file !== '.' && $file !== '..') { |
93 | $file = $directory . '/' . $file; |
||
94 | if (is_dir($file)) { |
||
95 | _rec($file); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | |||
102 | # --- |
||
103 | |||
4 | daniel-mar | 104 | $show_verbose = false; |
105 | $dir = ''; |
||
106 | |||
107 | for ($i=1; $i<$argc; $i++) { |
||
108 | if ($argv[$i] == '-v') { |
||
109 | $show_verbose = true; |
||
110 | } else { |
||
111 | $dir = $argv[$i]; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if (empty($dir)) { |
||
116 | echo "Syntax: $argv[0] [-v] <directory>\n"; |
||
2 | daniel-mar | 117 | exit(2); |
118 | } |
||
119 | |||
4 | daniel-mar | 120 | if (!is_dir($dir)) { |
2 | daniel-mar | 121 | echo "Directory not found\n"; |
122 | exit(1); |
||
123 | } |
||
124 | |||
4 | daniel-mar | 125 | _rec($dir); |
2 | daniel-mar | 126 | |
4 | daniel-mar | 127 | if ($show_verbose) echo "Done.\n"; |