Subversion Repositories checksum-tools

Rev

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

Rev 2 Rev 4
Line 15... Line 15...
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
// TODO: make use of STDERR and return different exit codes
-
 
21
 
20
function testmd5($file) {
22
function testmd5($file) {
21
        // TODO: warn if an entry is multiple times (with different checksums) in a single file
23
        // TODO: warn if an entry is multiple times (with different checksums) in a single file
22
        if (!file_exists($file)) {
24
        if (!file_exists($file)) {
23
                echo "ERROR: File $file does not exist.\n";
25
                echo "ERROR: File $file does not exist.\n";
24
                return;
26
                return;
25
        }
27
        }
26
 
28
 
27
        $lines = file($file);
29
        $lines = file($file);
-
 
30
        $is_first_line = true;
-
 
31
        $force_utf8 = false;
28
        foreach ($lines as $line) {
32
        foreach ($lines as $line) {
-
 
33
                if ($is_first_line) {
-
 
34
                        $tmp = 0;
29
                $line = str_replace("\xEF\xBB\xBF",'',$line);
35
                        $line = str_replace("\xEF\xBB\xBF",'',$line,$tmp);
-
 
36
                        if ($tmp > 0) $force_utf8 = true;
-
 
37
                        $is_first_line = false;
-
 
38
                }
-
 
39
                $is_ansi = strstr(utf8_decode($line),'?') !== false; // Attention: This assumes that '?' is not part of the line!
-
 
40
                if (!$force_utf8 && $is_ansi) $line = utf8_encode($line);
-
 
41
 
30
                $line = trim($line);
42
                $line = trim($line);
31
                if ($line == '') continue;
43
                if ($line == '') continue;
32
                $line = str_replace('*', ' ', $line);
44
                $line = str_replace('*', ' ', $line);
33
                $line = str_replace("\t", ' ', $line);
45
                $line = str_replace("\t", ' ', $line);
34
                list($checksum, $origname) = explode(' ', $line, 2);
46
                list($checksum, $origname) = explode(' ', $line, 2);
Line 39... Line 51...
39
                } else {
51
                } else {
40
                        $checksum2 = md5_file($origname);
52
                        $checksum2 = md5_file($origname);
41
                        if (strtolower($checksum) != strtolower($checksum2)) {
53
                        if (strtolower($checksum) != strtolower($checksum2)) {
42
                                echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
54
                                echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
43
                        } else {
55
                        } else {
-
 
56
                                global $show_verbose;
44
                                //echo "OK: $origname\n";
57
                                if ($show_verbose) echo "OK: $origname\n";
45
                        }
58
                        }
46
                }
59
                }
47
                // TODO: Also warn about extra files which are not indexed
60
                // TODO: Also warn about extra files which are not indexed
48
        }
61
        }
49
}
62
}
50
 
63
 
51
function _rec($directory) {
64
function _rec($directory) {
-
 
65
        $directory = rtrim($directory, '/\\');
-
 
66
 
52
        if (!is_dir($directory)) {
67
        if (!is_dir($directory)) {
53
                exit("Invalid directory path $directory\n");
68
                exit("Invalid directory path $directory\n");
54
        }
69
        }
55
 
70
 
56
        if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
71
        if ($dont_add_files = count(glob("$directory/*.md5")) == 0) {
-
 
72
                global $show_verbose;
57
                // echo "Directory $directory has no MD5 file. Skipping.\n";
73
                if ($show_verbose) echo "Directory $directory has no MD5 file. Skipping.\n";
58
        } else {
74
        } else {
59
                $out = array();
75
                $out = array();
60
 
76
 
-
 
77
                global $show_verbose;
61
                // echo "Check $directory\n";
78
                if ($show_verbose) echo "Check directory $directory\n";
62
                $md5files = glob('*.md5');
79
                $md5files = glob($directory.'/*.md5');
63
                foreach ($md5files as $md5file) {
80
                foreach ($md5files as $md5file) {
64
                        testmd5($md5file);
81
                        testmd5($md5file);
65
                }
82
                }
66
        }
83
        }
67
 
84
 
-
 
85
        $sd = @scandir($directory);
-
 
86
        if ($sd === false) {
-
 
87
                echo "Error: Cannot scan directory $directory\n";
-
 
88
                return;
-
 
89
        }
-
 
90
 
68
        foreach (scandir($directory) as $file) {
91
        foreach ($sd as $file) {
69
                if ($file !== '.' && $file !== '..') {
92
                if ($file !== '.' && $file !== '..') {
70
                        $file = $directory . '/' . $file;
93
                        $file = $directory . '/' . $file;
71
                        if (is_dir($file)) {
94
                        if (is_dir($file)) {
72
                                _rec($file);
95
                                _rec($file);
73
                        }
96
                        }
Line 76... Line 99...
76
}
99
}
77
 
100
 
78
 
101
 
79
# ---
102
# ---
80
 
103
 
-
 
104
$show_verbose = false;
-
 
105
$dir = '';
-
 
106
 
-
 
107
for ($i=1; $i<$argc; $i++) {
81
if ($argc != 2) {
108
        if ($argv[$i] == '-v') {
-
 
109
                $show_verbose = true;
-
 
110
        } else {
-
 
111
                $dir = $argv[$i];
-
 
112
        }
-
 
113
}
-
 
114
 
-
 
115
if (empty($dir)) {
82
        echo "Syntax: $argv[0] <directory>\n";
116
        echo "Syntax: $argv[0] [-v] <directory>\n";
83
        exit(2);
117
        exit(2);
84
}
118
}
85
 
119
 
86
if (!is_dir($argv[1])) {
120
if (!is_dir($dir)) {
87
        echo "Directory not found\n";
121
        echo "Directory not found\n";
88
        exit(1);
122
        exit(1);
89
}
123
}
90
 
124
 
91
_rec($argv[1]);
125
_rec($dir);
92
 
126
 
93
echo "Done.\n";
-
 
94
127
if ($show_verbose) echo "Done.\n";
-
 
128