run: return better status

This commit is contained in:
Frédéric Mangano-Tarumi 2018-11-18 11:42:33 -05:00
parent ddb838ac81
commit 8949094203
2 changed files with 16 additions and 22 deletions

View File

@ -315,20 +315,17 @@ static bool same_file(const std::string& path_in, const std::string& path_out)
*/
ot::status ot::run(ot::options& opt)
{
if (!opt.path_out.empty() && same_file(opt.path_in, opt.path_out)) {
fputs("error: the input and output files are the same\n", stderr);
return ot::st::fatal_error;
}
if (!opt.path_out.empty() && same_file(opt.path_in, opt.path_out))
return {ot::st::fatal_error, "Input and output files are the same"};
std::unique_ptr<FILE, decltype(&fclose)> input(nullptr, &fclose);
if (opt.path_in == "-") {
input.reset(stdin);
} else {
FILE* input_file = fopen(opt.path_in.c_str(), "r");
if (input_file == nullptr) {
perror("fopen");
return ot::st::fatal_error;
}
if (input_file == nullptr)
return {ot::st::standard_error,
"could not open '" + opt.path_in + "' for reading: " + strerror(errno)};
input.reset(input_file);
}
@ -336,15 +333,13 @@ ot::status ot::run(ot::options& opt)
if (opt.path_out == "-") {
output.reset(stdout);
} else if (!opt.path_out.empty()) {
if (!opt.overwrite && access(opt.path_out.c_str(), F_OK) == 0) {
fprintf(stderr, "'%s' already exists (use -y to overwrite)\n", opt.path_out.c_str());
return ot::st::fatal_error;
}
if (!opt.overwrite && access(opt.path_out.c_str(), F_OK) == 0)
return {ot::st::fatal_error,
"'" + opt.path_out + "' already exists (use -y to overwrite)"};
FILE* output_file = fopen(opt.path_out.c_str(), "w");
if (output_file == nullptr) {
perror("fopen");
return ot::st::fatal_error;
}
if (output_file == nullptr)
return {ot::st::standard_error,
"could not open '" + opt.path_out + "' for writing: " + strerror(errno)};
output.reset(output_file);
}
@ -368,10 +363,9 @@ ot::status ot::run(ot::options& opt)
}
if (opt.inplace) {
if (rename(opt.path_out.c_str(), opt.path_in.c_str()) == -1) {
perror("rename");
return ot::st::fatal_error;
}
if (rename(opt.path_out.c_str(), opt.path_in.c_str()) == -1)
return {ot::st::fatal_error,
"could not move the result to '" + opt.path_in + "': " + strerror(errno)};
}
return ot::st::ok;

View File

@ -99,12 +99,12 @@ is(md5('out.opus'), '111a483596ac32352fbce4d14d16abd2', 'the copy is faithful');
# empty out.opus
{ my $fh; open($fh, '>', 'out.opus') and close($fh) or die }
is_deeply(opustags(qw(gobble.opus -o out.opus)), ['', <<'EOF', 256], 'refuse to override');
'out.opus' already exists (use -y to overwrite)
error: 'out.opus' already exists (use -y to overwrite)
EOF
is(md5('out.opus'), 'd41d8cd98f00b204e9800998ecf8427e', 'the output wasn\'t written');
is_deeply(opustags(qw(out.opus -o out.opus)), ['', <<'EOF', 256], 'output and input can\'t be the same');
error: the input and output files are the same
error: Input and output files are the same
EOF
is_deeply(opustags(qw(gobble.opus -o out.opus --overwrite)), ['', '', 0], 'overwrite');