Added code to check saved checksums.

The code is very brittle, no error checking is performed at all
so if anything goes wrong the program will misbehave.
Output is still incomplete.
This commit is contained in:
King_DuckZ 2014-09-25 17:09:46 +02:00
parent d1c01a340b
commit c085388163
3 changed files with 97 additions and 20 deletions

View file

@ -25,4 +25,5 @@ add_executable(${PROJECT_NAME}
tiger.c
main.d
tiger.d
sums.d
)

29
main.d
View file

@ -17,31 +17,12 @@
import std.stdio;
import std.file;
import tiger;
import std.array;
import std.getopt;
import sums;
mixin(import("tigersumConfig.d"));
void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) {
foreach (file_name; parFiles) {
const auto curr_path = parCWD ~ file_name;
File curr_file = File(curr_path, parFileMode);
if (!isDir(curr_path)) {
const string hash = tigerToString(getTiger(curr_file, parTiger2));
if (parBsdStyle) {
writefln("tiger (%s) = %s", file_name, hash);
}
else {
writefln("%s %s", hash, file_name);
}
}
else {
writefln("tigersum: %s: is a directory", file_name);
}
}
}
int main (string[] parArgs) {
if (parArgs.length == 1)
return 0;
@ -77,9 +58,13 @@ int main (string[] parArgs) {
"binary|b", &fileModeHandler,
"text|t", &fileModeHandler,
"tag", &bsd_style
//"quiet", &switch_quiet,
//"status", &switch_status,
//"warn|w", &switch_warn
);
if (check_mode) {
checkSums(args[1 .. $], working_dir, tiger_version == 2, file_mode);
}
else if (show_help) {
writefln("Usage: %s [OPTION]... [FILE]...", PROGRAM_NAME);
@ -89,6 +74,10 @@ int main (string[] parArgs) {
writeln(" -t, --text read in text mode (default)");
writefln(" -i n, --tiger=n set the tiger version that should be used; valid values are 1 or 2, %d is default", TIGER_VERSION_DEF);
writeln();
//writeln("The following three options are useful only when verifying checksums:");
//writeln(" --quiet don't print OK for each successfully verified file");
//writeln(" --status don't output anything, status code shows success");
//writeln(" -w, --warn warn about improperly formatted checksum lines");
writeln();
writeln(" -h, --help display this help and exit");
writeln(" -v, --version display version informations and exit");

87
sums.d Normal file
View file

@ -0,0 +1,87 @@
/* tigersum - a command line utility to calculate the tiger hash from input.
Copyright (C) 2014 Michele "King_DuckZ" Santullo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import std.regex;
import std.stdio;
import std.file : isDir;
import std.conv;
import std.string;
import tiger;
void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) {
foreach (file_name; parFiles) {
const auto curr_path = parCWD ~ file_name;
File curr_file = File(curr_path, parFileMode);
if (!isDir(curr_path)) {
const string hash = tigerToString(getTiger(curr_file, parTiger2));
if (parBsdStyle) {
writefln("tiger (%s) = %s", file_name, hash);
}
else {
writefln("%s %s", hash, file_name);
}
}
else {
writefln("tigersum: %s: is a directory", file_name);
}
}
}
bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parFileMode) {
File curr_list = File(parFile, "r");
bool ret_val = true;
const auto reg_bsd = r"^tiger \((.+)\)\s*=\s*([a-fA-F0-9]{48})$";
const auto reg_normal = r"^([a-fA-F0-9]{48})\s+(.+)$";
foreach (line; curr_list.byLine()) {
auto m = match(line, reg_bsd);
int index_hash, index_file;
if (m) {
index_hash = 2;
index_file = 1;
}
else {
m = match(line, reg_normal);
index_hash = 1;
index_file = 2;
}
if (m) {
const string capt_filename = to!string(m.captures[index_file]);
writef("%s: ", capt_filename);
const string curr_path = parCWD ~ capt_filename;
File curr_file = File(curr_path, parFileMode);
const string curr_tiger = tigerToString(getTiger(curr_file, parTiger2));
const string check_tiger = to!string(m.captures[index_hash]).toLower;
if (check_tiger == curr_tiger)
writeln("OK");
else
writeln("FAILED");
}
}
return ret_val;
}
bool checkSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode) {
bool ret_val = true;
foreach (file_name; parFiles) {
const auto curr_path = parCWD ~ file_name;
ret_val &= checkSums(curr_path, parCWD, parTiger2, parFileMode);
}
return ret_val;
}