File: 30dont_overwrite_files.diff

package info (click to toggle)
crip 3.9-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 540 kB
  • sloc: perl: 7,419; sh: 297; makefile: 6
file content (227 lines) | stat: -rw-r--r-- 7,764 bytes parent folder | download | duplicates (5)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#! /bin/sh /usr/share/dpatch/dpatch-run
## 30_dont_overwrite_files.dpatch by Timo Schneider <timo.schneider@s2004.tu-chemnitz.de>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: A patch which prevents overwriting files

@DPATCH@

--- a/crip
+++ b/crip
@@ -532,24 +532,30 @@
 use CDDB_get qw( get_cddb get_discids );
 
 # Check for existing *.wav, *.tag, *.ogg, *.flac or filenames.txt files
-$out = `ls *.wav *.ogg *.flac *.tag 2>/dev/null`;
-if ($out ne "") {
-	print "You currently have *.wav, *.ogg, *.flac, or *.tag files in the current\n";
-	print "working directory.  These are subject to being overwritten.\n\n";
-	print "Press <enter> to proceed (or ctrl-C to abort): ";
-	$inp = <STDIN>;
-}
-if (-e "filenames.txt") {
-	print "You currently have a filenames.txt file in the current working\n";
-	print "working directory.  This will be deleted!\n\n";
-	print "Press <enter> to proceed (or ctrl-C to abort): ";
-	$inp = <STDIN>;
-	# Go ahead and delete the file now so that if the script is stopped and
-	#  restarted this question won't keep bugging the user.
-	print "Deleting filenames.txt\n\n";
-	`rm filenames.txt`;
+
+
+#  We don't need this any more, even if there are *.wav, *.tag ... files, they won't be overwritten :-)
+#
+#
+#$out = `ls *.wav *.ogg *.flac *.tag 2>/dev/null`;
+#if ($out ne "") {
+#	print "You currently have *.wav, *.ogg, *.flac, or *.tag files in the current\n";
+#	print "working directory.  These are subject to being overwritten.\n\n";
+#	print "Press <enter> to proceed (or ctrl-C to abort): ";
+#	$inp = <STDIN>;
+#}
+#
+
+# check if "filenames.txt" already exists, if so, take filenames.1.txt...
+
+$titlefile = "filenames.txt";
+my $counter=1;
+while (-e $titlefile) {
+	$titlefile=~s/(filenames).*(txt)/$1.$counter.$2/;
+	$counter++;
 }
 
+
 # End of check components section.
 
 
@@ -1095,7 +1101,7 @@
 if ($editnames eq "on") {
 	print "Here are the computer-chosen filenames for the tracks that you've picked:\n";
 	# Create the filenames.txt file now
-	open(NAMEFILE, ">filenames.txt");
+	open(NAMEFILE, ">$titlefile");
 	$listnum = 0;
 	while ($listnum <= $#list) {
 		$index = 0;
@@ -1112,7 +1118,7 @@
 	do {
 		# Print out the filenames.txt file to the screen
 		print "-------------------------------------------------------------------------------\n";
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, "$titlefile");
 		while ($line = <NAMEFILE>) {
 			# Process $line just like the filenames would be before showing them
 			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
@@ -1138,13 +1144,13 @@
 		if ($inp eq "") { $inp = $default; }
 		$inp =~ s/\s//g;
 		if ($inp =~ m/^\s*y/i) {
-			system "$editor filenames.txt";
+			system "$editor $titlefile";
 			$firstpass = 0;
 		}
 		# Process filenames.txt into $oname[][] and $name[][]
 		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
 		$warning = 0;
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		$listnum = 0;
 		while ($listnum <= $#list) {
 			$index = 0;
@@ -1387,7 +1393,7 @@
 if ($editnames eq "both") {
 	print "\nHere are the filenames you put:\n";
 	# Create the filenames.txt file now
-	open(NAMEFILE, ">filenames.txt");
+	open(NAMEFILE, ">$titlefile");
 	$listnum = 0;
 	while ($listnum <= $#list) {
 		$index = 0;
@@ -1404,7 +1410,7 @@
 	do {
 		# Print out the filenames.txt file to the screen
 		print "-------------------------------------------------------------------------------\n";
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		while ($line = <NAMEFILE>) {
 			# Process $line just like the filenames would be before showing them
 			chop $line;  $line =~ s/^\s*//;  $line =~ s/\s*$//;
@@ -1430,13 +1436,13 @@
 		if ($inp eq "") { $inp = $default; }
 		$inp =~ s/\s//g;
 		if ($inp =~ m/^\s*y/i) {
-			system "$editor filenames.txt";
+			system "$editor $titlefile";
 			$firstpass = 0;
 		}
 		# Process filenames.txt into $oname[][] and $name[][]
 		$broken = 0;	# Will set this to 1 if the file is discovered to be broken
 		$warning = 0;
-		open(NAMEFILE, "filenames.txt");
+		open(NAMEFILE, $titlefile);
 		$listnum = 0;
 		while ($listnum <= $#list) {
 			$index = 0;
@@ -1484,6 +1490,57 @@
 
 print "\n\n";
 
+# At this point the user has definetly chosen the filenames he wants
+# for the tracks. We have to check if they are OK or if older files
+# exist which have the same name. They would be overwritten. This is a
+# very bad thing, so we change the "dangerous" track/filenames "foo"
+# into "foo.n" where n is the smallest possible natural positive number
+# to let the filename become unique (so that there is no old file with
+# the same name in the current working directory). But we also have to
+# take care that all the new files we create have unique names.
+# Another problem is the cdda.wav file cdparanoia creates as a default
+# output file. We don't want to mess up an older cdda.wav file lying 
+# around.
+
+# Perls -e doesn't support wildcards, but we need them for the filetest 
+# because a track named foo may bring up a bunch of files during ripping:
+# foo.wav, foo.rev.wav, foo.ogg etc.
+# And whe have to check that all the track titles on the list are unique. 
+# So we just write our own -e:
+
+sub dash_e {
+	@i = glob $_[0];
+	return 1 if (scalar @i || -e $_[0]);
+	$_[0]=~m/^(.*)\.\*/;
+	return 1 if ($unique{$1} >= 1);
+	return 0; 
+}
+%unique = ();
+$listnum = 0;
+while ($listnum <= $#list) {
+	$index = 0;
+	while ($index <= $#{$list[$listnum]}) {
+		if (dash_e("$name[$listnum][$index].*")) {
+			my $i = 1;
+			while (dash_e("$name[$listnum][$index].$i.*")) { $i++; }
+			$name[$listnum][$index] = "$name[$listnum][$index].$i";
+			$oname[$listnum][$index] = "$oname[$listnum][$index].$i";
+			$unique{"$name[$listnum][$index].$i"}++;
+		} else { $unique{"$name[$listnum][$index]"}++; }
+		$index++;
+	}
+	$listnum++;
+}
+
+# Let's take care of the cdparanoia outfile:
+
+$cdparanoia_outfile="cdda";
+if (-e "$cdparanoia_outfile.wav") {
+	$i=1;
+	while (-e "$cdparanoia_outfile.$i.wav") {$i++;}
+	$cdparanoia_outfile = "$cdparanoia_outfile.$i.wav";
+}
+
 
 # Note:  $oname[][] (original name) is the same as $name[][] with the
 #  exception that $name includes an escape "\" on characters used to
@@ -1809,7 +1866,7 @@
 			do {
 				print "\n---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----\n";
 				print "Ripping track ", $list[$listnum][$index], "...\n\n";
-				$cdpcmd="cdparanoia $tmpcdflags -d $cddevice $list[$listnum][$index]";
+				$cdpcmd="cdparanoia $tmpcdflags -d $cddevice $list[$listnum][$index] $cdparanoia_outfile";
 				print "Command: $cdpcmd\n";
 				$cdpout = system "$cdpcmd";
 				if ($cdpout != 0) {
@@ -1860,8 +1917,8 @@
 			# ...but there is currently no way to do it!
 
 			# Rename cdda.wav to $name[][].wav
-			print "Moving cdda.wav to $name[$listnum][$index].wav\n";
-			`mv -- cdda.wav $name[$listnum][$index].wav`;
+			print "Moving $cdparanoia_outfile to $name[$listnum][$index].wav\n";
+			`mv -- $cdparanoia_outfile $name[$listnum][$index].wav`;
 		} # End if ($skiprip eq "on)
 
 	    $index++;
@@ -2204,12 +2261,17 @@
 
 # Delete the filenames.txt file
 if ($delfiles eq "on") {
-	print "Deleting filenames.txt\n";
-	`rm filenames.txt`;
-	if (-e "filenames.txt~") {
-		print "Deleting filenames.txt~\n";
-		`rm filenames.txt~`;
-	}
+	print "Deleting $titlefile\n";
+	`rm $titlefile`;
+
+# Why are we doing this? We didn't create any *~ files, so we won't delete them.
+# If there are editors which create such files without deleting them afterwards,
+# it would be better to fix the editor than providing a dangerous workaraound.
+
+#	if (-e "$titlefile~") {
+#		print "Deleting $titlefile~\n";
+#		`rm $titlefile~`;
+#	}
 }