Rev 13 | 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 | /* |
||
15 | daniel-mar | 5 | Copyright 2020-2022 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 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 | // This script generates MD5 files |
||
21 | // If there is already an MD5 file existing, only new files get appended to the existing MD5 files. |
||
22 | |||
23 | function _rec($directory) { |
||
24 | if (!is_dir($directory)) { |
||
15 | daniel-mar | 25 | fwrite(STDERR, "Invalid directory path '$directory'\n"); |
26 | return false; |
||
2 | daniel-mar | 27 | } |
28 | |||
11 | daniel-mar | 29 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
30 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
||
31 | basename(realpath($directory)).'.md5'; |
||
2 | daniel-mar | 32 | |
11 | daniel-mar | 33 | } else { |
34 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
||
35 | basename($directory).'.md5'; |
||
36 | } |
||
37 | |||
2 | daniel-mar | 38 | if (file_exists($md5_file)) { |
39 | $existing_files = md5_get_files($md5_file); |
||
40 | } else { |
||
41 | $existing_files = array(); |
||
42 | } |
||
43 | |||
5 | daniel-mar | 44 | $sd = @scandir($directory); |
45 | if ($sd === false) { |
||
15 | daniel-mar | 46 | fwrite(STDERR, "Error: Cannot scan directory $directory\n"); |
47 | return false; |
||
5 | daniel-mar | 48 | } |
49 | |||
50 | foreach ($sd as $file) { |
||
2 | daniel-mar | 51 | if ($file === '.') continue; |
52 | if ($file === '..') continue; |
||
15 | daniel-mar | 53 | if (substr($file,0,1) === '.') continue; |
2 | daniel-mar | 54 | if (strtolower($file) === 'thumbs.db') continue; |
5 | daniel-mar | 55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
||
2 | daniel-mar | 57 | |
12 | daniel-mar | 58 | $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
2 | daniel-mar | 59 | if (is_dir($fullpath)) { |
13 | daniel-mar | 60 | global $do_recursive; |
61 | if ($do_recursive) _rec($fullpath); |
||
5 | daniel-mar | 62 | } else if (is_file($fullpath)) { |
4 | daniel-mar | 63 | global $show_verbose; |
64 | if ($show_verbose) echo "$fullpath\n"; |
||
2 | daniel-mar | 65 | $dir = pathinfo($fullpath, PATHINFO_DIRNAME); |
66 | |||
67 | if (!file_exists($md5_file)) { |
||
68 | file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
||
69 | } |
||
70 | |||
71 | if (!in_array($file, $existing_files)) { |
||
72 | $md5 = strtolower(md5_file($fullpath)); |
||
73 | file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND); |
||
74 | } |
||
5 | daniel-mar | 75 | } else { |
76 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
||
15 | daniel-mar | 77 | fwrite(STDERR, "Warning: $fullpath is not a regular file!\n"); |
2 | daniel-mar | 78 | } |
79 | } |
||
15 | daniel-mar | 80 | |
81 | return true; |
||
2 | daniel-mar | 82 | } |
83 | |||
84 | function md5_get_files($filename) { |
||
85 | $out = array(); |
||
86 | $lines = file($filename); |
||
87 | foreach ($lines as $line) { |
||
88 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
||
89 | $line = trim($line); |
||
90 | if ($line == '') continue; |
||
91 | $line = str_replace('*', ' ', $line); |
||
92 | $line = str_replace("\t", ' ', $line); |
||
93 | list($checksum, $origname) = explode(' ', $line, 2); |
||
94 | $origname = dirname($filename) . '/' . trim($origname); |
||
95 | $checksum = trim($checksum); |
||
96 | $out[] = $origname; |
||
97 | } |
||
98 | |||
99 | return $out; |
||
100 | } |
||
101 | |||
102 | # --- |
||
103 | |||
4 | daniel-mar | 104 | $show_verbose = false; |
13 | daniel-mar | 105 | $do_recursive = false; |
15 | daniel-mar | 106 | $dirs = array(); |
4 | daniel-mar | 107 | |
108 | for ($i=1; $i<$argc; $i++) { |
||
109 | if ($argv[$i] == '-v') { |
||
110 | $show_verbose = true; |
||
13 | daniel-mar | 111 | } else if ($argv[$i] == '-r') { |
112 | $do_recursive = true; |
||
4 | daniel-mar | 113 | } else { |
15 | daniel-mar | 114 | $dirs[] = $argv[$i]; |
4 | daniel-mar | 115 | } |
116 | } |
||
117 | |||
15 | daniel-mar | 118 | if (count($dirs) == 0) { |
119 | echo "Syntax: $argv[0] [-v] [-r] <directory> [<directory> [...]]\n"; |
||
2 | daniel-mar | 120 | exit(2); |
121 | } |
||
122 | |||
15 | daniel-mar | 123 | $res = 0; |
124 | foreach ($dirs as $dir) { |
||
125 | if (!_rec($dir)) $res = 1; |
||
2 | daniel-mar | 126 | } |
15 | daniel-mar | 127 | if ($show_verbose) echo "Done.\n"; |
128 | exit($res); |
||
2 | daniel-mar | 129 |