Subversion Repositories checksum-tools

Rev

Rev 4 | Rev 11 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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