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 testsfv($file) {
22
function testsfv($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 = rtrim($line);
42
                $line = rtrim($line);
31
                if ($line == '') continue;
43
                if ($line == '') continue;
32
                $checksum = substr($line,-8);
44
                $checksum = substr($line,-8);
33
                $origname = rtrim(substr($line,0,strlen($line)-8));
45
                $origname = rtrim(substr($line,0,strlen($line)-8));
34
                $origname = dirname($file) . '/' . trim($origname);
46
                $origname = dirname($file) . '/' . trim($origname);
Line 37... Line 49...
37
                } else {
49
                } else {
38
                        $checksum2 = crc32_file($origname);
50
                        $checksum2 = crc32_file($origname);
39
                        if (strtolower($checksum) != strtolower($checksum2)) {
51
                        if (strtolower($checksum) != strtolower($checksum2)) {
40
                                echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
52
                                echo "CHECKSUM FAIL: $origname (expected $checksum, but is $checksum2)\n";
41
                        } else {
53
                        } else {
-
 
54
                                global $show_verbose;
42
                                //echo "OK: $origname\n";
55
                                if ($show_verbose) echo "OK: $origname\n";
43
                        }
56
                        }
44
                }
57
                }
45
                // TODO: Also warn about extra files which are not indexed
58
                // TODO: Also warn about extra files which are not indexed
46
        }
59
        }
47
}
60
}
Line 59... Line 72...
59
        }
72
        }
60
        return $out;
73
        return $out;
61
}
74
}
62
 
75
 
63
function _rec($directory) {
76
function _rec($directory) {
-
 
77
        $directory = rtrim($directory, '/\\');
-
 
78
 
64
        if (!is_dir($directory)) {
79
        if (!is_dir($directory)) {
65
                exit("Invalid directory path $directory\n");
80
                exit("Invalid directory path $directory\n");
66
        }
81
        }
67
 
82
 
68
        if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) {
83
        if ($dont_add_files = count(glob("$directory/*.sfv")) == 0) {
-
 
84
                global $show_verbose;
69
                // echo "Directory $directory has no SFV file. Skipping.\n";
85
                if ($show_verbose) echo "Directory $directory has no SFV file. Skipping.\n";
70
        } else {
86
        } else {
71
                $out = array();
87
                $out = array();
72
 
88
 
-
 
89
                global $show_verbose;
73
                // echo "Check $directory\n";
90
                if ($show_verbose) echo "Check directory $directory\n";
74
                $sfvfiles = glob('*.sfv');
91
                $sfvfiles = glob($directory.'/*.sfv');
75
                foreach ($sfvfiles as $sfvfile) {
92
                foreach ($sfvfiles as $sfvfile) {
76
                        testsfv($sfvfile);
93
                        testsfv($sfvfile);
77
                }
94
                }
78
        }
95
        }
79
 
96
 
-
 
97
        $sd = @scandir($directory);
-
 
98
        if ($sd === false) {
-
 
99
                echo "Error: Cannot scan directory $directory\n";
-
 
100
                return;
-
 
101
        }
-
 
102
 
80
        foreach (scandir($directory) as $file) {
103
        foreach ($sd as $file) {
81
                if ($file !== '.' && $file !== '..') {
104
                if ($file !== '.' && $file !== '..') {
82
                        $file = $directory . '/' . $file;
105
                        $file = $directory . '/' . $file;
83
                        if (is_dir($file)) {
106
                        if (is_dir($file)) {
84
                                _rec($file);
107
                                _rec($file);
85
                        }
108
                        }
Line 88... Line 111...
88
}
111
}
89
 
112
 
90
 
113
 
91
# ---
114
# ---
92
 
115
 
-
 
116
$show_verbose = false;
-
 
117
$dir = '';
-
 
118
 
-
 
119
for ($i=1; $i<$argc; $i++) {
93
if ($argc != 2) {
120
        if ($argv[$i] == '-v') {
-
 
121
                $show_verbose = true;
-
 
122
        } else {
-
 
123
                $dir = $argv[$i];
-
 
124
        }
-
 
125
}
-
 
126
 
-
 
127
if (empty($dir)) {
94
        echo "Syntax: $argv[0] <directory>\n";
128
        echo "Syntax: $argv[0] [-v] <directory>\n";
95
        exit(2);
129
        exit(2);
96
}
130
}
97
 
131
 
98
if (!is_dir($argv[1])) {
132
if (!is_dir($dir)) {
99
        echo "Directory not found\n";
133
        echo "Directory not found\n";
100
        exit(1);
134
        exit(1);
101
}
135
}
102
 
136
 
103
_rec($argv[1]);
137
_rec($dir);
104
 
138
 
105
echo "Done.\n";
-
 
106
139
if ($show_verbose) echo "Done.\n";
-
 
140