Rev 11 | Rev 13 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 11 | Rev 12 | ||
---|---|---|---|
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-2021 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 | ||
11 | http://www.apache.org/licenses/LICENSE-2.0 |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | 12 | ||
13 | Unless required by applicable law or agreed to in writing, software |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
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 | // This script generates MD5 files |
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. |
21 | // If there is already an MD5 file existing, only new files get appended to the existing MD5 files. |
22 | 22 | ||
23 | // TODO: make use of STDERR and return different exit codes |
23 | // TODO: make use of STDERR and return different exit codes |
24 | 24 | ||
25 | function _rec($directory) { |
25 | function _rec($directory) { |
26 | if (!is_dir($directory)) { |
26 | if (!is_dir($directory)) { |
27 | die("Invalid directory path $directory\n"); |
27 | die("Invalid directory path $directory\n"); |
28 | } |
28 | } |
29 | 29 | ||
30 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
30 | if ((basename($directory) == '.') || (basename($directory) == '..')) { |
31 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
31 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
32 | basename(realpath($directory)).'.md5'; |
32 | basename(realpath($directory)).'.md5'; |
33 | 33 | ||
34 | } else { |
34 | } else { |
35 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
35 | $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR. |
36 | basename($directory).'.md5'; |
36 | basename($directory).'.md5'; |
37 | } |
37 | } |
38 | 38 | ||
39 | if (file_exists($md5_file)) { |
39 | if (file_exists($md5_file)) { |
40 | $existing_files = md5_get_files($md5_file); |
40 | $existing_files = md5_get_files($md5_file); |
41 | } else { |
41 | } else { |
42 | $existing_files = array(); |
42 | $existing_files = array(); |
43 | } |
43 | } |
44 | 44 | ||
45 | $sd = @scandir($directory); |
45 | $sd = @scandir($directory); |
46 | if ($sd === false) { |
46 | if ($sd === false) { |
47 | echo "Error: Cannot scan directory $directory\n"; |
47 | echo "Error: Cannot scan directory $directory\n"; |
48 | return; |
48 | return; |
49 | } |
49 | } |
50 | 50 | ||
51 | foreach ($sd as $file) { |
51 | foreach ($sd as $file) { |
52 | if ($file === '.') continue; |
52 | if ($file === '.') continue; |
53 | if ($file === '..') continue; |
53 | if ($file === '..') continue; |
54 | if (strtolower($file) === 'thumbs.db') continue; |
54 | if (strtolower($file) === 'thumbs.db') continue; |
55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
55 | if (strtolower(substr($file, -4)) === '.md5') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
56 | if (strtolower(substr($file, -4)) === '.sfv') continue; |
57 | 57 | ||
58 | $fullpath = $directory . '/' . $file; |
58 | $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file; |
59 | if (is_dir($fullpath)) { |
59 | if (is_dir($fullpath)) { |
60 | _rec($fullpath); |
60 | _rec($fullpath); |
61 | } else if (is_file($fullpath)) { |
61 | } else if (is_file($fullpath)) { |
62 | global $show_verbose; |
62 | global $show_verbose; |
63 | if ($show_verbose) echo "$fullpath\n"; |
63 | if ($show_verbose) echo "$fullpath\n"; |
64 | $dir = pathinfo($fullpath, PATHINFO_DIRNAME); |
64 | $dir = pathinfo($fullpath, PATHINFO_DIRNAME); |
65 | 65 | ||
66 | if (!file_exists($md5_file)) { |
66 | if (!file_exists($md5_file)) { |
67 | file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
67 | file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM |
68 | } |
68 | } |
69 | 69 | ||
70 | if (!in_array($file, $existing_files)) { |
70 | if (!in_array($file, $existing_files)) { |
71 | $md5 = strtolower(md5_file($fullpath)); |
71 | $md5 = strtolower(md5_file($fullpath)); |
72 | file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND); |
72 | file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND); |
73 | } |
73 | } |
74 | } else { |
74 | } else { |
75 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
75 | // For some reason, some files on a NTFS volume are "FIFO" pipe files?! |
76 | echo "Warning: $fullpath is not a regular file!\n"; |
76 | echo "Warning: $fullpath is not a regular file!\n"; |
77 | } |
77 | } |
78 | } |
78 | } |
79 | } |
79 | } |
80 | 80 | ||
81 | function md5_get_files($filename) { |
81 | function md5_get_files($filename) { |
82 | $out = array(); |
82 | $out = array(); |
83 | $lines = file($filename); |
83 | $lines = file($filename); |
84 | foreach ($lines as $line) { |
84 | foreach ($lines as $line) { |
85 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
85 | $line = str_replace("\xEF\xBB\xBF",'',$line); |
86 | $line = trim($line); |
86 | $line = trim($line); |
87 | if ($line == '') continue; |
87 | if ($line == '') continue; |
88 | $line = str_replace('*', ' ', $line); |
88 | $line = str_replace('*', ' ', $line); |
89 | $line = str_replace("\t", ' ', $line); |
89 | $line = str_replace("\t", ' ', $line); |
90 | list($checksum, $origname) = explode(' ', $line, 2); |
90 | list($checksum, $origname) = explode(' ', $line, 2); |
91 | $origname = dirname($filename) . '/' . trim($origname); |
91 | $origname = dirname($filename) . '/' . trim($origname); |
92 | $checksum = trim($checksum); |
92 | $checksum = trim($checksum); |
93 | $out[] = $origname; |
93 | $out[] = $origname; |
94 | } |
94 | } |
95 | 95 | ||
96 | return $out; |
96 | return $out; |
97 | } |
97 | } |
98 | 98 | ||
99 | # --- |
99 | # --- |
100 | 100 | ||
101 | $show_verbose = false; |
101 | $show_verbose = false; |
102 | $dir = ''; |
102 | $dir = ''; |
103 | 103 | ||
104 | for ($i=1; $i<$argc; $i++) { |
104 | for ($i=1; $i<$argc; $i++) { |
105 | if ($argv[$i] == '-v') { |
105 | if ($argv[$i] == '-v') { |
106 | $show_verbose = true; |
106 | $show_verbose = true; |
107 | } else { |
107 | } else { |
108 | $dir = $argv[$i]; |
108 | $dir = $argv[$i]; |
109 | } |
109 | } |
110 | } |
110 | } |
111 | 111 | ||
112 | if (empty($dir)) { |
112 | if (empty($dir)) { |
113 | echo "Syntax: $argv[0] [-v] <directory>\n"; |
113 | echo "Syntax: $argv[0] [-v] <directory>\n"; |
114 | exit(2); |
114 | exit(2); |
115 | } |
115 | } |
116 | 116 | ||
117 | if (!is_dir($dir)) { |
117 | if (!is_dir($dir)) { |
118 | echo "Directory not found\n"; |
118 | echo "Directory not found\n"; |
119 | exit(1); |
119 | exit(1); |
120 | } |
120 | } |
121 | 121 | ||
122 | _rec($dir); |
122 | _rec($dir); |
123 | 123 | ||
124 | if ($show_verbose) echo "Done.\n"; |
124 | if ($show_verbose) echo "Done.\n"; |
125 | 125 |