Commit 78cb9212 authored by Michael Smith's avatar Michael Smith

Fix bug 655: if we errored out, don't rename our temporary output file to the

input file, instead just remove the temp file.


svn path=/trunk/vorbis-tools/; revision=9935
parent d21f1cbe
...@@ -63,7 +63,7 @@ param_t *new_param(void); ...@@ -63,7 +63,7 @@ param_t *new_param(void);
void free_param(param_t *param); void free_param(param_t *param);
void parse_options(int argc, char *argv[], param_t *param); void parse_options(int argc, char *argv[], param_t *param);
void open_files(param_t *p); void open_files(param_t *p);
void close_files(param_t *p); void close_files(param_t *p, int output_written);
/********** /**********
...@@ -107,7 +107,7 @@ int main(int argc, char **argv) ...@@ -107,7 +107,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, _("Failed to open file as vorbis: %s\n"), fprintf(stderr, _("Failed to open file as vorbis: %s\n"),
vcedit_error(state)); vcedit_error(state));
close_files(param); close_files(param, 0);
free_param(param); free_param(param);
vcedit_clear(state); vcedit_clear(state);
return 1; return 1;
...@@ -120,7 +120,7 @@ int main(int argc, char **argv) ...@@ -120,7 +120,7 @@ int main(int argc, char **argv)
/* done */ /* done */
vcedit_clear(state); vcedit_clear(state);
close_files(param); close_files(param, 0);
free_param(param); free_param(param);
return 0; return 0;
} }
...@@ -133,7 +133,7 @@ int main(int argc, char **argv) ...@@ -133,7 +133,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, _("Failed to open file as vorbis: %s\n"), fprintf(stderr, _("Failed to open file as vorbis: %s\n"),
vcedit_error(state)); vcedit_error(state));
close_files(param); close_files(param, 0);
free_param(param); free_param(param);
vcedit_clear(state); vcedit_clear(state);
return 1; return 1;
...@@ -174,7 +174,7 @@ int main(int argc, char **argv) ...@@ -174,7 +174,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, _("Failed to write comments to output file: %s\n"), fprintf(stderr, _("Failed to write comments to output file: %s\n"),
vcedit_error(state)); vcedit_error(state));
close_files(param); close_files(param, 0);
free_param(param); free_param(param);
vcedit_clear(state); vcedit_clear(state);
return 1; return 1;
...@@ -183,7 +183,7 @@ int main(int argc, char **argv) ...@@ -183,7 +183,7 @@ int main(int argc, char **argv)
/* done */ /* done */
vcedit_clear(state); vcedit_clear(state);
close_files(param); close_files(param, 1);
free_param(param); free_param(param);
return 0; return 0;
} }
...@@ -527,23 +527,31 @@ void open_files(param_t *p) ...@@ -527,23 +527,31 @@ void open_files(param_t *p)
***********/ ***********/
void close_files(param_t *p) void close_files(param_t *p, int output_written)
{ {
if (p->in != NULL && p->in != stdin) fclose(p->in); if (p->in != NULL && p->in != stdin) fclose(p->in);
if (p->out != NULL && p->out != stdout) fclose(p->out); if (p->out != NULL && p->out != stdout) fclose(p->out);
if (p->com != NULL && p->com != stdout && p->com != stdin) fclose(p->com); if (p->com != NULL && p->com != stdout && p->com != stdin) fclose(p->com);
if(p->tempoutfile) { if(p->tempoutfile) {
/* Some platforms fail to rename a file if the new name already exists, if(output_written) {
* so we need to remove, then rename. How stupid. /* Some platforms fail to rename a file if the new name already
*/ * exists, so we need to remove, then rename. How stupid.
if(rename(p->outfilename, p->infilename)) { */
if(remove(p->infilename)) if(rename(p->outfilename, p->infilename)) {
fprintf(stderr, _("Error removing old file %s\n"), p->infilename); if(remove(p->infilename))
else if(rename(p->outfilename, p->infilename)) fprintf(stderr, _("Error removing old file %s\n"), p->infilename);
fprintf(stderr, _("Error renaming %s to %s\n"), p->outfilename, else if(rename(p->outfilename, p->infilename))
p->infilename); fprintf(stderr, _("Error renaming %s to %s\n"), p->outfilename,
} p->infilename);
}
}
else {
if(remove(p->outfilename)) {
fprintf(stderr, _("Error removing erroneous temporary file %s\n"),
p->outfilename);
}
} }
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment