Don't fail miserably when some file doesn't exist
This commit is contained in:
parent
464e7a44ac
commit
1a644a543b
1 changed files with 58 additions and 5 deletions
63
src/sums.d
63
src/sums.d
|
@ -21,11 +21,30 @@ import std.file : isDir;
|
||||||
import std.conv;
|
import std.conv;
|
||||||
import std.string;
|
import std.string;
|
||||||
import tiger;
|
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) {
|
void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) {
|
||||||
foreach (file_name; parFiles) {
|
foreach (file_name; parFiles) {
|
||||||
const auto curr_path = parCWD ~ file_name;
|
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)) {
|
if (!isDir(curr_path)) {
|
||||||
const string hash = tigerToString(getTiger(curr_file, parTiger2));
|
const string hash = tigerToString(getTiger(curr_file, parTiger2));
|
||||||
if (parBsdStyle) {
|
if (parBsdStyle) {
|
||||||
|
@ -36,7 +55,7 @@ void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, stri
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
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;
|
bool ret_val = true;
|
||||||
const auto reg_bsd = r"^tiger \((.+)\)\s*=\s*([a-fA-F0-9]{48})$";
|
const auto reg_bsd = r"^tiger \((.+)\)\s*=\s*([a-fA-F0-9]{48})$";
|
||||||
const auto reg_normal = r"^([a-fA-F0-9]{48})\s+(.+)$";
|
const auto reg_normal = r"^([a-fA-F0-9]{48})\s+(.+)$";
|
||||||
|
int unmatched_lines;
|
||||||
|
int mismatches;
|
||||||
|
int failed_open;
|
||||||
|
|
||||||
foreach (line; curr_list.byLine()) {
|
foreach (line; curr_list.byLine()) {
|
||||||
auto m = match(line, reg_bsd);
|
auto m = match(line, reg_bsd);
|
||||||
|
@ -65,14 +87,45 @@ bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parF
|
||||||
writef("%s: ", capt_filename);
|
writef("%s: ", capt_filename);
|
||||||
|
|
||||||
const string curr_path = parCWD ~ 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 curr_tiger = tigerToString(getTiger(curr_file, parTiger2));
|
||||||
const string check_tiger = to!string(m.captures[index_hash]).toLower;
|
const string check_tiger = to!string(m.captures[index_hash]).toLower;
|
||||||
if (check_tiger == curr_tiger)
|
if (check_tiger == curr_tiger) {
|
||||||
writeln("OK");
|
writeln("OK");
|
||||||
else
|
}
|
||||||
|
else {
|
||||||
|
++mismatches;
|
||||||
writeln("FAILED");
|
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;
|
return ret_val;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue