Rev 5 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5 | Rev 15 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #!/usr/bin/php |
1 | #!/usr/bin/php |
2 | <?php |
2 | <?php |
3 | 3 | ||
4 | /* |
4 | /* |
5 | Copyright 2020 Daniel Marschall, ViaThinkSoft |
5 | Copyright 2020-2022 Daniel Marschall, ViaThinkSoft |
6 | 6 | ||
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with 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 |
9 | You may obtain a copy of the License at |
10 | 10 | ||
Line 15... | Line 15... | ||
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
17 | limitations under the License. |
18 | */ |
18 | */ |
19 | 19 | ||
20 | // TODO: make use of STDERR and return different exit codes |
- | |
21 | // TODO: On Windows file systems, accept file names case insensitively |
20 | // TODO: On Windows file systems, accept file names case insensitively |
22 | 21 | ||
23 | function utf8_normalize($str) { |
22 | function utf8_normalize($str) { |
24 | // This helps to handle decomposite Unicode endpoints (E.g. German Umlauts have different representations) |
23 | // This helps to handle decomposite Unicode endpoints (E.g. German Umlauts have different representations) |
25 | // Requires php-intl |
24 | // Requires php-intl |
Line 37... | Line 36... | ||
37 | } |
36 | } |
38 | 37 | ||
39 | function testsfv($file) { |
38 | function testsfv($file) { |
40 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
39 | // TODO: warn if an entry is multiple times (with different checksums) in a single file |
41 | if (!file_exists($file)) { |
40 | if (!file_exists($file)) { |
42 | echo "ERROR: File $file does not exist.\n"; |
41 | fwrite(STDERR, "ERROR: File $file does not exist.\n"); |
43 | return; |
42 | return; |
44 | } |
43 | } |
45 | 44 | ||
46 | $files_checked = array(); |
45 | $files_checked = array(); |
47 | 46 | ||
Line 63... | Line 62... | ||
63 | if ($line == '') continue; |
62 | if ($line == '') continue; |
64 | $checksum = substr($line,-8); |
63 | $checksum = substr($line,-8); |
65 | $origname = rtrim(substr($line,0,strlen($line)-8)); |
64 | $origname = rtrim(substr($line,0,strlen($line)-8)); |
66 | $origname = dirname($file) . '/' . rtrim($origname); |
65 | $origname = dirname($file) . '/' . rtrim($origname); |
67 | if (!file_exists($origname)) { |
66 | if (!file_exists($origname)) { |
68 | echo "WARNING: File vanished : $origname\n"; |
67 | fwrite(STDERR, "WARNING: File vanished : $origname\n"); |
69 | } else { |
68 | } else { |
70 | if (is_file($origname)) { |
69 | if (is_file($origname)) { |
71 | $checksum2 = crc32_file($origname); |
70 | $checksum2 = crc32_file($origname); |
72 | if (strtolower($checksum) != strtolower($checksum2)) { |
71 | if (strtolower($checksum) != strtolower($checksum2)) { |
73 | echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"; |
72 | fwrite(STDERR, "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n"); |
74 | } else { |
73 | } else { |
75 | global $show_verbose; |
74 | global $show_verbose; |
76 | if ($show_verbose) echo "OK: $origname\n"; |
75 | if ($show_verbose) echo "OK: $origname\n"; |
77 | } |
76 | } |
78 | } else { |
77 | } else { |
79 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
78 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
80 | echo "Warning: $origname is not a regular file!\n"; |
79 | fwrite(STDERR, "Warning: $origname is not a regular file!\n"); |
81 | } |
80 | } |
82 | } |
81 | } |
83 | 82 | ||
84 | $origname = utf8_normalize(basename($origname)); |
83 | $origname = utf8_normalize(basename($origname)); |
85 | $files_checked[] = dirname($file) . '/' . $origname; |
84 | $files_checked[] = dirname($file) . '/' . $origname; |
Line 87... | Line 86... | ||
87 | 86 | ||
88 | // Now check if files have vanished! |
87 | // Now check if files have vanished! |
89 | $directory = dirname($file); |
88 | $directory = dirname($file); |
90 | $sd = @scandir($directory); |
89 | $sd = @scandir($directory); |
91 | if ($sd === false) { |
90 | if ($sd === false) { |
92 | echo "Error: Cannot scan directory $directory\n"; |
91 | fwrite(STDERR, "Error: Cannot scan directory $directory\n"); |
93 | } else { |
92 | } else { |
94 | foreach ($sd as $file) { |
93 | foreach ($sd as $file) { |
95 | if ($file === '.') continue; |
94 | if ($file === '.') continue; |
96 | if ($file === '..') continue; |
95 | if ($file === '..') continue; |
- | 96 | if (substr($file,0,1) === '.') continue; |
|
97 | if (strtolower($file) === 'thumbs.db') continue; |
97 | if (strtolower($file) === 'thumbs.db') continue; |
98 | if (strtolower(substr($file, -4)) === '.md5') continue; |
98 | if (strtolower(substr($file, -4)) === '.md5') continue; |
99 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
99 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
100 | $fullpath = $directory . '/' . $file; |
100 | $fullpath = $directory . '/' . $file; |
101 | if (!is_dir($fullpath)) { |
101 | if (!is_dir($fullpath)) { |
102 | $fullpath = utf8_normalize($fullpath); |
102 | $fullpath = utf8_normalize($fullpath); |
103 | if (!in_array($fullpath,$files_checked)) { |
103 | if (!in_array($fullpath,$files_checked)) { |
104 | echo "Warning: File not in SFV checksum file: $fullpath\n"; |
104 | fwrite(STDERR, "Warning: File not in SFV checksum file: $fullpath\n"); |
105 | } |
105 | } |
106 | } |
106 | } |
107 | } |
107 | } |
108 | } |
108 | } |
109 | } |
109 | } |
Line 124... | Line 124... | ||
124 | 124 | ||
125 | function _rec($directory) { |
125 | function _rec($directory) { |
126 | $directory = rtrim($directory, '/\\'); |
126 | $directory = rtrim($directory, '/\\'); |
127 | 127 | ||
128 | if (!is_dir($directory)) { |
128 | if (!is_dir($directory)) { |
129 | exit("Invalid directory path $directory\n"); |
129 | fwrite(STDERR, "Invalid directory path $directory\n"); |
- | 130 | return false; |
|
130 | } |
131 | } |
131 | 132 | ||
132 | if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) { |
133 | if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) { |
133 | global $show_verbose; |
134 | global $show_verbose; |
134 | if ($show_verbose) echo "Directory $directory has no SFV file. Skipping.\n"; |
135 | if ($show_verbose) echo "Directory $directory has no SFV file. Skipping.\n"; |
Line 143... | Line 144... | ||
143 | } |
144 | } |
144 | } |
145 | } |
145 | 146 | ||
146 | $sd = @scandir($directory); |
147 | $sd = @scandir($directory); |
147 | if ($sd === false) { |
148 | if ($sd === false) { |
148 | echo "Error: Cannot scan directory $directory\n"; |
149 | fwrite(STDERR, "Error: Cannot scan directory $directory\n"); |
149 | return; |
150 | return false; |
150 | } |
151 | } |
151 | 152 | ||
152 | foreach ($sd as $file) { |
153 | foreach ($sd as $file) { |
153 | if ($file !== '.' && $file !== '..') { |
154 | if ($file !== '.' && $file !== '..') { |
154 | $file = $directory . '/' . $file; |
155 | $file = $directory . '/' . $file; |
155 | if (is_dir($file)) { |
156 | if (is_dir($file)) { |
156 | _rec($file); |
157 | _rec($file); |
157 | } |
158 | } |
158 | } |
159 | } |
159 | } |
160 | } |
- | 161 | ||
- | 162 | return true; |
|
160 | } |
163 | } |
161 | 164 | ||
162 | 165 | ||
163 | # --- |
166 | # --- |
164 | 167 | ||
165 | $show_verbose = false; |
168 | $show_verbose = false; |
166 | $dir = ''; |
169 | $dirs = array(); |
167 | 170 | ||
168 | for ($i=1; $i<$argc; $i++) { |
171 | for ($i=1; $i<$argc; $i++) { |
169 | if ($argv[$i] == '-v') { |
172 | if ($argv[$i] == '-v') { |
170 | $show_verbose = true; |
173 | $show_verbose = true; |
171 | } else { |
174 | } else { |
172 | $dir = $argv[$i]; |
175 | $dirs[] = $argv[$i]; |
173 | } |
176 | } |
174 | } |
177 | } |
175 | 178 | ||
176 | if (empty($dir)) { |
179 | if (count($dirs) == 0) { |
177 | echo "Syntax: $argv[0] [-v] <directory>\n"; |
180 | echo "Syntax: $argv[0] [-v] <directory> [<directory> [...]]\n"; |
178 | exit(2); |
181 | exit(2); |
179 | } |
182 | } |
180 | 183 | ||
- | 184 | $res = 0; |
|
181 | if (!is_dir($dir)) { |
185 | foreach ($dirs as $dir) { |
182 | echo "Directory not found\n"; |
186 | if (!_rec($dir)) $res = 1; |
183 | exit(1); |
- | |
184 | } |
187 | } |
185 | - | ||
186 | _rec($dir); |
- | |
187 | - | ||
188 | if ($show_verbose) echo "Done.\n"; |
188 | if ($show_verbose) echo "Done.\n"; |
- | 189 | exit($res); |
|
- | 190 |