Don't fail miserably when some file doesn't exist

This commit is contained in:
King_DuckZ 2014-09-26 14:01:51 +02:00
parent 464e7a44ac
commit 1a644a543b

View file

@ -21,11 +21,30 @@ import std.file : isDir;
import std.conv;
import std.string;
import tiger;
import std.exception;
import std.format;
import std.array : appender;
private void writeError (string parFile, string parMessage) {
stderr.writefln("tigersum: %s: %s", parFile, parMessage);
}
private void writeWarning (string parMessage) {
writeError("WARNING", parMessage);
}
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);
File curr_file;
try {
curr_file = File(curr_path, parFileMode);
}
catch (ErrnoException err) {
writeError(file_name, "unexisting file or directory");
continue;
}
if (!isDir(curr_path)) {
const string hash = tigerToString(getTiger(curr_file, parTiger2));
if (parBsdStyle) {
@ -36,7 +55,7 @@ void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, stri
}
}
else {
writefln("tigersum: %s: is a directory", file_name);
writeError(file_name, "is a directory");
}
}
}
@ -46,6 +65,9 @@ bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parF
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+(.+)$";
int unmatched_lines;
int mismatches;
int failed_open;
foreach (line; curr_list.byLine()) {
auto m = match(line, reg_bsd);
@ -65,15 +87,46 @@ bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parF
writef("%s: ", capt_filename);
const string curr_path = parCWD ~ capt_filename;
File curr_file = File(curr_path, parFileMode);
File curr_file;
try {
curr_file = File(curr_path, parFileMode);
}
catch (ErrnoException err) {
++failed_open;
writeln("opening or reading FAILED");
writeError(capt_filename, "unexisting file or directory");
continue;
}
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)
if (check_tiger == curr_tiger) {
writeln("OK");
else
}
else {
++mismatches;
writeln("FAILED");
}
}
else {
++unmatched_lines;
}
}
if (unmatched_lines) {
auto writer = appender!string();
formattedWrite(writer, "%d line%s not formatted correctly", unmatched_lines, (unmatched_lines == 1 ? "" : "s"));
writeWarning(writer.data);
}
if (failed_open) {
auto writer = appender!string();
formattedWrite(writer, "%d listed file%s can't be read", failed_open, (failed_open == 1 ? "" : "s"));
writeWarning(writer.data);
}
if (mismatches) {
auto writer = appender!string();
formattedWrite(writer, "%d calculated checksum%s match", mismatches, (mismatches == 1 ? " DOESN'T" : "s DON'T"));
writeWarning(writer.data);
}
return ret_val;
}