Subversion Repositories checksum-tools

Rev

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