summaryrefslogtreecommitdiff
path: root/audio/wavegain/files/patch-wavegain.c
blob: fdbddd05805693071e7fdd7709d2298dc8b0a0a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
--- wavegain.c	2005-11-27 16:58:46.000000000 +0800
+++ wavegain.c	2008-05-25 03:23:17.000000000 +0800
@@ -19,20 +19,8 @@
 #include <string.h>
 #include <ctype.h>
 
-#ifdef _WIN32
-#include <io.h>
-#else
-# ifndef __MACOSX__
-#  include <sys/io.h>
-# endif
-#endif
-
 #include <fcntl.h>
 
-#ifndef __MACOSX__
-#include <malloc.h>
-#endif
-
 #include "gain_analysis.h"
 #include "i18n.h"
 #include "config.h"
@@ -58,6 +46,14 @@
 #define ROUND64(x)   ( doubletmp = (x) + Dither.Add + (Int64_t)0x001FFFFD80000000L, *(Int64_t*)(&doubletmp) - (Int64_t)0x433FFFFD80000000L )
 #endif
 
+
+
+# include <errno.h>
+static int xrename(const char *oldpath, const char *newpath);
+
+
+
+
 extern int          write_log;
 dither_t            Dither;
 double              doubletmp;
@@ -584,16 +580,26 @@
 		close_audio_file(infile, aufile, wg_opts);
 		fclose(infile);
 
-		if (!settings->std_out) {
+	        if (!settings->std_out) {               
+		       		        
 			if (remove(filename) != 0) {
 				fprintf(stderr, " Error deleting old file '%s'\n", filename);
 				goto exit;
 			}
-    
-			if (rename(TEMP_NAME, filename) != 0) {
+						
+			/* 
+			* int rename(const char *old, const char *new);
+			* In POSIX, rename will fail if the 'old' and 'new' names are on different mounted file systems.
+			* ( From http://en.wikipedia.org/wiki/Rename_%28C%29 )
+			* Function 'xrename' from 'normalize-0.7.6' is one clever solution
+			*/
+								   
+			/*if (rename(TEMP_NAME, filename) != 0) { */			
+			if (xrename(TEMP_NAME, filename) != 0) {
 				fprintf(stderr, " Error renaming '" TEMP_NAME "' to '%s' (uh-oh)\n", filename);
 				goto exit;
 			}
+			
 		}
     
 		result = 1;
@@ -603,3 +609,65 @@
 }
 
 
+
+
+/* From normalize-0.7.6/nid3lib/write.c
+ * Move the file "oldpath" to "newpath", or copy and delete if they
+ * are on different filesystems.
+ */
+static int
+xrename(const char *oldpath, const char *newpath)
+{
+  FILE *in, *out;
+  char buf[4096];
+  size_t sz;
+
+  if (strcmp(oldpath, newpath) == 0)
+    return 0;
+
+#ifdef __EMX__
+  if (unlink(newpath) == -1 && errno != ENOENT)
+    return -1;
+#endif
+
+  if (rename(oldpath, newpath) == -1) {
+    if (errno == EXDEV) {
+      /* files are on different filesystems, so we have to copy */
+      if (unlink(newpath) == -1 && errno != ENOENT)
+	return -1;
+
+      in = fopen(oldpath, "rb");
+      if (in == NULL)
+	return -1;
+      out = fopen(newpath, "wb");
+      if (out == NULL) {
+	fclose(in);
+	return -1;
+      }
+
+      while ((sz = fread(buf, 1, 4096, in)) > 0)
+	fwrite(buf, 1, sz, out);
+
+      if (ferror(in) || ferror(out)) {
+	fclose(in);
+	fclose(out);
+	return -1;
+      }
+      if (fclose(in) == EOF) {
+	fclose(out);
+	return -1;
+      }
+      if (fclose(out) == EOF)
+	return -1;
+
+      if (unlink(oldpath) == -1)
+	return -1;
+    } else {
+      return -1;
+    }
+  }
+
+  return 0;
+}
+
+