Subversion Repositories checksum-tools

Rev

Rev 2 | Rev 5 | 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
 
38
        foreach (scandir($directory) as $file) {
39
                if ($file === '.') continue;
40
                if ($file === '..') continue;
41
                if (strtolower($file) === 'thumbs.db') continue;
42
                if (substr($file, -4) === '.md5') continue;
43
                if (substr($file, -4) === '.sfv') continue;
44
 
45
                $fullpath = $directory . '/' . $file;
46
                if (is_dir($fullpath)) {
47
                        _rec($fullpath);
48
                } else {
4 daniel-mar 49
                        global $show_verbose;
50
                        if ($show_verbose) echo "$fullpath\n";
2 daniel-mar 51
                        $dir = pathinfo($fullpath, PATHINFO_DIRNAME);
52
 
53
                        if (!file_exists($md5_file)) {
54
                                file_put_contents($md5_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
55
                        }
56
 
57
                        if (!in_array($file, $existing_files)) {
58
                                $md5 = strtolower(md5_file($fullpath));
59
                                file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND);
60
                        }
61
                }
62
        }
63
}
64
 
65
function md5_get_files($filename) {
66
        $out = array();
67
        $lines = file($filename);
68
        foreach ($lines as $line) {
69
                $line = str_replace("\xEF\xBB\xBF",'',$line);
70
                $line = trim($line);
71
                if ($line == '') continue;
72
                $line = str_replace('*', ' ', $line);
73
                $line = str_replace("\t", ' ', $line);
74
                list($checksum, $origname) = explode(' ', $line, 2);
75
                $origname = dirname($filename) . '/' . trim($origname);
76
                $checksum = trim($checksum);
77
                $out[] = $origname;
78
        }
79
 
80
        return $out;
81
}
82
 
83
# ---
84
 
4 daniel-mar 85
$show_verbose = false;
86
$dir = '';
87
 
88
for ($i=1; $i<$argc; $i++) {
89
        if ($argv[$i] == '-v') {
90
                $show_verbose = true;
91
        } else {
92
                $dir = $argv[$i];
93
        }
94
}
95
 
96
if (empty($dir)) {
97
        echo "Syntax: $argv[0] [-v] <directory>\n";
2 daniel-mar 98
        exit(2);
99
}
100
 
4 daniel-mar 101
if (!is_dir($dir)) {
2 daniel-mar 102
        echo "Directory not found\n";
103
        exit(1);
104
}
105
 
4 daniel-mar 106
_rec($dir);
2 daniel-mar 107
 
4 daniel-mar 108
if ($show_verbose) echo "Done.\n";