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