[xiph-commits] r9935 - trunk/vorbis-tools/vorbiscomment

msmith at svn.xiph.org msmith at svn.xiph.org
Fri Sep 2 12:50:37 PDT 2005


Author: msmith
Date: 2005-09-02 12:50:35 -0700 (Fri, 02 Sep 2005)
New Revision: 9935

Modified:
   trunk/vorbis-tools/vorbiscomment/vcomment.c
Log:
Fix bug 655: if we errored out, don't rename our temporary output file to the
input file, instead just remove the temp file.



Modified: trunk/vorbis-tools/vorbiscomment/vcomment.c
===================================================================
--- trunk/vorbis-tools/vorbiscomment/vcomment.c	2005-09-02 14:20:02 UTC (rev 9934)
+++ trunk/vorbis-tools/vorbiscomment/vcomment.c	2005-09-02 19:50:35 UTC (rev 9935)
@@ -63,7 +63,7 @@
 void free_param(param_t *param);
 void parse_options(int argc, char *argv[], param_t *param);
 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 @@
 		{
 			fprintf(stderr, _("Failed to open file as vorbis: %s\n"), 
 					vcedit_error(state));
-            close_files(param);
+            close_files(param, 0);
             free_param(param);
             vcedit_clear(state);
 			return 1;
@@ -120,7 +120,7 @@
 		/* done */
 		vcedit_clear(state);
 
-		close_files(param);
+		close_files(param, 0);
         free_param(param);
 		return 0;		
 	}
@@ -133,7 +133,7 @@
 		{
 			fprintf(stderr, _("Failed to open file as vorbis: %s\n"), 
 					vcedit_error(state));
-            close_files(param);
+            close_files(param, 0);
             free_param(param);
             vcedit_clear(state);
 			return 1;
@@ -174,7 +174,7 @@
 		{
 			fprintf(stderr, _("Failed to write comments to output file: %s\n"), 
 					vcedit_error(state));
-            close_files(param);
+            close_files(param, 0);
             free_param(param);
             vcedit_clear(state);
 			return 1;
@@ -183,7 +183,7 @@
 		/* done */
 		vcedit_clear(state);
 		
-		close_files(param);
+		close_files(param, 1);
         free_param(param);
 		return 0;
 	}
@@ -527,23 +527,31 @@
 
 ***********/
 
-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->out != NULL && p->out != stdout) fclose(p->out);
-	if (p->com != NULL && p->com != stdout && p->com != stdin) fclose(p->com);
+  if (p->in != NULL && p->in != stdin) fclose(p->in);
+  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->tempoutfile) {
-        /* 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))
-                fprintf(stderr, _("Error removing old file %s\n"), p->infilename);
-            else if(rename(p->outfilename, p->infilename)) 
-                fprintf(stderr, _("Error renaming %s to %s\n"), p->outfilename, 
-                        p->infilename);
-        }
+  if(p->tempoutfile) {
+    if(output_written) {
+      /* 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))
+          fprintf(stderr, _("Error removing old file %s\n"), p->infilename);
+        else if(rename(p->outfilename, 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);
+      }
+    }
+  }
 }
 



More information about the commits mailing list