Subversion Repositories checksum-tools

Rev

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