-
Notifications
You must be signed in to change notification settings - Fork 55
/
rip_music_unix.cpp
298 lines (194 loc) · 5.76 KB
/
rip_music_unix.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
_________ __ __
/ _____// |_____________ _/ |______ ____ __ __ ______
\_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
/ \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
/_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
\/ \/ \//_____/ \/
______________________ ______________________
T H E W A R B E G I N S
Stratagus - A free fantasy real time strategy game engine
rip_music_unix.c - rip audio CD on Unix with cdparanoia
Copyright (C) 2011 Pali Rohár <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <libgen.h>
#if defined(__linux__)
# include <mntent.h>
#elif defined(__FreeBSD__)
# include <sys/param.h>
# include <sys/mount.h>
#elif defined(__APPLE__)
# define __dev_t dev_t
# define __ino_t ino_t
#endif
#include "rip_music.h"
static char * find_mnt_dir(const char * dir) {
char save_cwd[PATH_MAX];
char last_cwd[PATH_MAX];
char cwd[PATH_MAX];
char * mnt_dir = NULL;
struct stat st;
__dev_t last_dev;
__ino_t last_ino;
if ( ! getcwd(save_cwd, sizeof(save_cwd)) ) {
fprintf(stderr, "Error: Cannot store working directory: %s\n", strerror(errno));
return NULL;
}
if ( stat(dir, &st) != 0 ) {
fprintf(stderr, "Error: Cannot stat %s: %s\n", dir, strerror(errno));
return NULL;
}
if ( chdir(dir) == -1 ) {
fprintf(stderr, "Error: Cannot change directory to %s: %s\n", dir, strerror(errno));
return NULL;
}
if ( ! getcwd(last_cwd, sizeof(last_cwd)) ) {
fprintf(stderr, "Error: Cannot get working directory: %s\n", strerror(errno));
if ( chdir(save_cwd) == -1 )
fprintf(stderr, "Error: Cannot restore working directory: %s\n", strerror(errno));
return NULL;
}
last_dev = st.st_dev;
last_ino = st.st_ino;
while ( 1 ) {
if ( chdir("..") == -1 ) {
fprintf(stderr, "Error: Cannot change working directory: %s\n", strerror(errno));
break;
}
if ( ! getcwd(cwd, sizeof(cwd)) ) {
fprintf(stderr, "Error: Cannot get working directory: %s\n", strerror(errno));
break;
}
if ( stat(cwd, &st) != 0 ) {
fprintf(stderr, "Error: Cannot stat %s: %s\n", cwd, strerror(errno));
break;
}
if ( last_dev != st.st_dev || last_ino == st.st_ino ) {
mnt_dir = strdup(last_cwd);
break;
}
if ( strcmp(cwd, "/") == 0 ) {
mnt_dir = strdup("/");
break;
}
last_dev = st.st_dev;
last_ino = st.st_ino;
strcpy(last_cwd, cwd);
}
if ( chdir(save_cwd) == -1 )
fprintf(stderr, "Error: Cannot restore working directory: %s\n", strerror(errno));
return mnt_dir;
}
static char * find_dev(const char * mnt_dir) {
#if defined(__linux__)
struct mntent * mnt;
char * dev = NULL;
FILE * file;
file = setmntent("/proc/self/mounts", "r");
if ( ! file ) {
fprintf(stderr, "Error: Cannot open file /proc/self/mounts: %s\n", strerror(errno));
return NULL;
}
while ( ( mnt = getmntent(file) ) ) {
if ( strcmp(mnt->mnt_dir, mnt_dir) == 0 ) {
dev = strdup(mnt->mnt_fsname);
break;
}
}
endmntent(file);
return dev;
#elif defined(__FreeBSD__)
struct statfs sfs;
if ( statfs(mnt_dir, &sfs) != 0 ) {
fprintf(stderr, "Error: Cannot get mounted device: %s\n", strerror(errno));
return NULL;
}
return strdup(sfs.f_mntfromname);
#else
return NULL;
#endif
}
static int spawnvp(const char * file, char * const argv[]) {
int status;
pid_t pid = fork();
if ( pid < 0 ) {
fprintf(stderr, "Error: Cannot fork: %s\n", strerror(errno));
return -errno;
}
if ( pid == 0 ) {
execvp(file, argv);
fprintf(stderr, "Error: Cannot exec: %s\n", strerror(errno));
_exit(errno);
}
if ( waitpid(pid, &status, 0) == pid )
return WEXITSTATUS(status);
else
return -1;
}
int RipMusic(int expansion_cd, const char * data_dir, const char * dest_dir) {
struct stat st;
char * args[6] = { (char *)"cdparanoia", (char *)"-d", NULL, (char *)"-v", (char *)"-Q", NULL };
char * mnt_dir;
char * dev;
int count = 0;
int i;
if ( stat(data_dir, &st) != 0 ) {
fprintf(stderr, "Error: Cannot stat %s: %s\n", data_dir, strerror(errno));
return 1;
}
mnt_dir = find_mnt_dir(data_dir);
if ( ! mnt_dir ) {
fprintf(stderr, "Error: Cannot find mount dir\n");
return 1;
}
dev = find_dev(mnt_dir);
if ( ! dev ) {
fprintf(stderr, "Error: Cannot find device for mount dir %s\n", mnt_dir);
free(mnt_dir);
return 1;
}
free(mnt_dir);
printf("Found CD-ROM device: %s\n", dev);
fflush(stdout);
args[2] = dev;
if ( spawnvp(args[0], args) != 0 ) {
free(dev);
return 1;
}
for ( i = 0; MusicNames[i]; ++i ) {
char num[3];
char file[PATH_MAX];
if ( ! expansion_cd && ! MusicNames[i+1] )
break;
snprintf(num, sizeof(num), "%d", i+2);
snprintf(file, sizeof(file), "%s/%s.wav", dest_dir, MusicNames[i]);
args[3] = num;
args[4] = file;
if ( spawnvp(args[0], args) == 0 )
++count;
}
free(dev);
if ( count == 0 )
return 1;
else
return 0;
}