Subversion Repositories checksum-tools

Rev

Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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