Subversion Repositories checksum-tools

Rev

Rev 13 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13 Rev 15
Line 1... Line 1...
1
#!/usr/bin/php
1
#!/usr/bin/php
2
<?php
2
<?php
3
 
3
 
4
/*
4
/*
5
   Copyright 2020-2021 Daniel Marschall, ViaThinkSoft
5
   Copyright 2020-2022 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
 
Line 18... Line 18...
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
-
 
24
 
-
 
25
function _rec($directory) {
23
function _rec($directory) {
26
        if (!is_dir($directory)) {
24
        if (!is_dir($directory)) {
27
                die("Invalid directory path $directory\n");
25
                fwrite(STDERR, "Invalid directory path '$directory'\n");
-
 
26
                return false;
28
        }
27
        }
29
 
28
 
30
        if ((basename($directory) == '.') || (basename($directory) == '..')) {
29
        if ((basename($directory) == '.') || (basename($directory) == '..')) {
31
                $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
30
                $md5_file = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.
32
                            basename(realpath($directory)).'.md5';
31
                            basename(realpath($directory)).'.md5';
Line 42... Line 41...
42
                $existing_files = array();
41
                $existing_files = array();
43
        }
42
        }
44
 
43
 
45
        $sd = @scandir($directory);
44
        $sd = @scandir($directory);
46
        if ($sd === false) {
45
        if ($sd === false) {
47
                echo "Error: Cannot scan directory $directory\n";
46
                fwrite(STDERR, "Error: Cannot scan directory $directory\n");
48
                return;
47
                return false;
49
        }
48
        }
50
 
49
 
51
        foreach ($sd as $file) {
50
        foreach ($sd as $file) {
52
                if ($file === '.') continue;
51
                if ($file === '.') continue;
53
                if ($file === '..') continue;
52
                if ($file === '..') continue;
-
 
53
                if (substr($file,0,1) === '.') continue;
54
                if (strtolower($file) === 'thumbs.db') continue;
54
                if (strtolower($file) === 'thumbs.db') continue;
55
                if (strtolower(substr($file, -4)) === '.md5') continue;
55
                if (strtolower(substr($file, -4)) === '.md5') continue;
56
                if (strtolower(substr($file, -4)) === '.sfv') continue;
56
                if (strtolower(substr($file, -4)) === '.sfv') continue;
57
 
57
 
58
                $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
58
                $fullpath = rtrim($directory,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$file;
Line 72... Line 72...
72
                                $md5 = strtolower(md5_file($fullpath));
72
                                $md5 = strtolower(md5_file($fullpath));
73
                                file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND);
73
                                file_put_contents($md5_file, "$md5 *$file\r\n", FILE_APPEND);
74
                        }
74
                        }
75
                } else {
75
                } else {
76
                        // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
76
                        // For some reason, some files on a NTFS volume are "FIFO" pipe files?!
77
                        echo "Warning: $fullpath is not a regular file!\n";
77
                        fwrite(STDERR, "Warning: $fullpath is not a regular file!\n");
78
                }
78
                }
79
        }
79
        }
-
 
80
 
-
 
81
        return true;
80
}
82
}
81
 
83
 
82
function md5_get_files($filename) {
84
function md5_get_files($filename) {
83
        $out = array();
85
        $out = array();
84
        $lines = file($filename);
86
        $lines = file($filename);
Line 99... Line 101...
99
 
101
 
100
# ---
102
# ---
101
 
103
 
102
$show_verbose = false;
104
$show_verbose = false;
103
$do_recursive = false;
105
$do_recursive = false;
104
$dir = '';
106
$dirs = array();
105
 
107
 
106
for ($i=1; $i<$argc; $i++) {
108
for ($i=1; $i<$argc; $i++) {
107
        if ($argv[$i] == '-v') {
109
        if ($argv[$i] == '-v') {
108
                $show_verbose = true;
110
                $show_verbose = true;
109
        } else if ($argv[$i] == '-r') {
111
        } else if ($argv[$i] == '-r') {
110
                $do_recursive = true;
112
                $do_recursive = true;
111
        } else {
113
        } else {
112
                $dir = $argv[$i];
114
                $dirs[] = $argv[$i];
113
        }
115
        }
114
}
116
}
115
 
117
 
116
if (empty($dir)) {
118
if (count($dirs) == 0) {
117
        echo "Syntax: $argv[0] [-v] [-r] <directory>\n";
119
        echo "Syntax: $argv[0] [-v] [-r] <directory> [<directory> [...]]\n";
118
        exit(2);
120
        exit(2);
119
}
121
}
120
 
122
 
-
 
123
$res = 0;
121
if (!is_dir($dir)) {
124
foreach ($dirs as $dir) {
122
        echo "Directory not found\n";
125
        if (!_rec($dir)) $res = 1;
123
        exit(1);
-
 
124
}
126
}
125
 
-
 
126
_rec($dir);
-
 
127
 
-
 
128
if ($show_verbose) echo "Done.\n";
127
if ($show_verbose) echo "Done.\n";
-
 
128
exit($res);
-
 
129