Subversion Repositories checksum-tools

Rev

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

Rev 5 Rev 11
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 SFV files
20
// This script generates SFV files
21
// If there is already an SFV file existing, only new files get appended to the existing SFV files.
21
// If there is already an SFV file existing, only new files get appended to the existing SFV 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
        if ((basename($directory) == '.') || (basename($directory) == '..')) {
-
 
31
                $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
-
 
32
                            basename(realpath($directory)).'.sfv';
-
 
33
 
-
 
34
        } else {
-
 
35
                $sfv_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
30
        $sfv_file = $directory.'/'.basename($directory).'.sfv';
36
                            basename($directory).'.sfv';
-
 
37
        }
31
 
38
 
32
        if (file_exists($sfv_file)) {
39
        if (file_exists($sfv_file)) {
33
                $existing_files = sfv_get_files($sfv_file);
40
                $existing_files = sfv_get_files($sfv_file);
34
        } else {
41
        } else {
35
                $existing_files = array();
42
                $existing_files = array();
36
        }
43
        }
37
 
44
 
38
        $sd = @scandir($directory);
45
        $sd = @scandir($directory);
39
        if ($sd === false) {
46
        if ($sd === false) {
40
                echo "Error: Cannot scan directory $directory\n";
47
                echo "Error: Cannot scan directory $directory\n";
41
                return;
48
                return;
42
        }
49
        }
43
 
50
 
44
        foreach ($sd as $file) {
51
        foreach ($sd as $file) {
45
                if ($file === '.') continue;
52
                if ($file === '.') continue;
46
                if ($file === '..') continue;
53
                if ($file === '..') continue;
47
                if (strtolower($file) === 'thumbs.db') continue;
54
                if (strtolower($file) === 'thumbs.db') continue;
48
                if (strtolower(substr($file, -4)) === '.md5') continue;
55
                if (strtolower(substr($file, -4)) === '.md5') continue;
49
                if (strtolower(substr($file, -4)) === '.sfv') continue;
56
                if (strtolower(substr($file, -4)) === '.sfv') continue;
50
 
57
 
51
                $fullpath = $directory . '/' . $file;
58
                $fullpath = $directory . '/' . $file;
52
                if (is_dir($fullpath)) {
59
                if (is_dir($fullpath)) {
53
                        _rec($fullpath);
60
                        _rec($fullpath);
54
                } else if (is_file($fullpath)) {
61
                } else if (is_file($fullpath)) {
55
                        global $show_verbose;
62
                        global $show_verbose;
56
                        if ($show_verbose) echo "$fullpath\n";
63
                        if ($show_verbose) echo "$fullpath\n";
57
                        $dir = pathinfo($fullpath, PATHINFO_DIRNAME);
64
                        $dir = pathinfo($fullpath, PATHINFO_DIRNAME);
58
 
65
 
59
                        if (!file_exists($sfv_file)) {
66
                        if (!file_exists($sfv_file)) {
60
                                file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
67
                                file_put_contents($sfv_file, "; Generated by ViaThinkSoft\r\n"); // TODO: BOM
61
                        }
68
                        }
62
 
69
 
63
                        if (!in_array($file, $existing_files)) {
70
                        if (!in_array($file, $existing_files)) {
64
                                $crc32 = strtoupper(crc32_file($fullpath));
71
                                $crc32 = strtoupper(crc32_file($fullpath));
65
                                file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND);
72
                                file_put_contents($sfv_file, "$file $crc32\r\n", FILE_APPEND);
66
                        }
73
                        }
67
                } else {
74
                } else {
68
                        // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
75
                        // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
69
                        echo "Warning: $fullpath is not a regular file!\n";
76
                        echo "Warning: $fullpath is not a regular file!\n";
70
                }
77
                }
71
        }
78
        }
72
}
79
}
73
 
80
 
74
function sfv_get_files($filename) {
81
function sfv_get_files($filename) {
75
        $out = array();
82
        $out = array();
76
        $lines = file($filename);
83
        $lines = file($filename);
77
        foreach ($lines as $line) {
84
        foreach ($lines as $line) {
78
                $line = rtrim($line);
85
                $line = rtrim($line);
79
                if ($line == '') continue;
86
                if ($line == '') continue;
80
                if (substr($line,0,1) == ';') continue;
87
                if (substr($line,0,1) == ';') continue;
81
                $out[] = substr($line, 0, strrpos($line, ' '));
88
                $out[] = substr($line, 0, strrpos($line, ' '));
82
 
89
 
83
        }
90
        }
84
        return $out;
91
        return $out;
85
}
92
}
86
 
93
 
87
function swapEndianness($hex) {
94
function swapEndianness($hex) {
88
        return implode('', array_reverse(str_split($hex, 2)));
95
        return implode('', array_reverse(str_split($hex, 2)));
89
}
96
}
90
 
97
 
91
function crc32_file($filename, $rawOutput = false) {
98
function crc32_file($filename, $rawOutput = false) {
92
        $out = bin2hex(hash_file ('crc32b', $filename , true));
99
        $out = bin2hex(hash_file ('crc32b', $filename , true));
93
        if (hash('crc32b', 'TEST') == 'b893eaee') {
100
        if (hash('crc32b', 'TEST') == 'b893eaee') {
94
                // hash_file() in PHP 5.2 has the wrong Endianess!
101
                // hash_file() in PHP 5.2 has the wrong Endianess!
95
                // https://bugs.php.net/bug.php?id=47467
102
                // https://bugs.php.net/bug.php?id=47467
96
                $out = swapEndianness($out);
103
                $out = swapEndianness($out);
97
        }
104
        }
98
        return $out;
105
        return $out;
99
}
106
}
100
 
107
 
101
# ---
108
# ---
102
 
109
 
103
$show_verbose = false;
110
$show_verbose = false;
104
$dir = '';
111
$dir = '';
105
 
112
 
106
for ($i=1; $i<$argc; $i++) {
113
for ($i=1; $i<$argc; $i++) {
107
        if ($argv[$i] == '-v') {
114
        if ($argv[$i] == '-v') {
108
                $show_verbose = true;
115
                $show_verbose = true;
109
        } else {
116
        } else {
110
                $dir = $argv[$i];
117
                $dir = $argv[$i];
111
        }
118
        }
112
}
119
}
113
 
120
 
114
if (empty($dir)) {
121
if (empty($dir)) {
115
        echo "Syntax: $argv[0] [-v] <directory>\n";
122
        echo "Syntax: $argv[0] [-v] <directory>\n";
116
        exit(2);
123
        exit(2);
117
}
124
}
118
 
125
 
119
if (!is_dir($dir)) {
126
if (!is_dir($dir)) {
120
        echo "Directory not found\n";
127
        echo "Directory not found\n";
121
        exit(1);
128
        exit(1);
122
}
129
}
123
 
130
 
124
_rec($dir);
131
_rec($dir);
125
 
132
 
126
if ($show_verbose) echo "Done.\n";
133
if ($show_verbose) echo "Done.\n";
127
 
134