Merge branch 'refs/heads/t/lopsub'
[paraslash.git] / afh.c
1 /*
2 * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
8
9 #include <regex.h>
10 #include <lopsub.h>
11
12 #include "afh.lsg.h"
13 #include "para.h"
14 #include "string.h"
15 #include "fd.h"
16 #include "afh.h"
17 #include "error.h"
18 #include "version.h"
19
20 /** Array of error strings. */
21 DEFINE_PARA_ERRLIST;
22
23 struct lls_parse_result *lpr;
24
25 #define CMD_PTR (lls_cmd(0, afh_suite))
26 #define OPT_RESULT(_name) (lls_opt_result(LSG_AFH_PARA_AFH_OPT_ ## _name, lpr))
27 #define OPT_GIVEN(_name) (lls_opt_given(OPT_RESULT(_name)))
28 #define OPT_STRING_VAL(_name) (lls_string_val(0, OPT_RESULT(_name)))
29 #define OPT_UINT32_VAL(_name) (lls_uint32_val(0, OPT_RESULT(_name)))
30
31 static int loglevel;
32 INIT_STDERR_LOGGING(loglevel)
33
34 static inline bool tag_needs_update(bool given, const char *tag,
35 const char *arg)
36 {
37 return given && (!tag || strcmp(tag, arg) != 0);
38 }
39
40 static int rewrite_tags(const char *name, int input_fd, void *map,
41 size_t map_size, int audio_format_id, struct afh_info *afhi)
42 {
43 struct taginfo *tags = &afhi->tags;
44 bool modified = false;
45 char *tmp_name;
46 const char *arg;
47 int output_fd = -1, ret;
48 struct stat sb;
49
50 arg = OPT_STRING_VAL(YEAR);
51 if (tag_needs_update(OPT_GIVEN(YEAR), tags->year, arg)) {
52 free(tags->year);
53 tags->year = para_strdup(arg);
54 modified = true;
55 }
56 arg = OPT_STRING_VAL(TITLE);
57 if (tag_needs_update(OPT_GIVEN(TITLE), tags->title, arg)) {
58 free(tags->title);
59 tags->title = para_strdup(arg);
60 modified = true;
61 }
62 arg = OPT_STRING_VAL(ARTIST);
63 if (tag_needs_update(OPT_GIVEN(ARTIST), tags->artist, arg)) {
64 free(tags->artist);
65 tags->artist = para_strdup(arg);
66 modified = true;
67 }
68 arg = OPT_STRING_VAL(ALBUM);
69 if (tag_needs_update(OPT_GIVEN(ALBUM), tags->album, arg)) {
70 free(tags->album);
71 tags->album = para_strdup(arg);
72 modified = true;
73 }
74 arg = OPT_STRING_VAL(COMMENT);
75 if (tag_needs_update(OPT_GIVEN(COMMENT), tags->comment, arg)) {
76 free(tags->comment);
77 tags->comment = para_strdup(arg);
78 modified = true;
79 }
80 if (!modified) {
81 PARA_WARNING_LOG("no modifications necessary\n");
82 return 0;
83 }
84 /*
85 * mkstmp() creates the temporary file with permissions 0600, but we
86 * like it to have the same permissions as the original file, so we
87 * have to get this information.
88 */
89 if (fstat(input_fd, &sb) < 0) {
90 ret = -ERRNO_TO_PARA_ERROR(errno);
91 PARA_ERROR_LOG("failed to fstat fd %d (%s)\n", input_fd, name);
92 return ret;
93 }
94 tmp_name = make_message("%s.XXXXXX", name);
95 ret = mkstemp(tmp_name);
96 if (ret < 0) {
97 ret = -ERRNO_TO_PARA_ERROR(errno);
98 PARA_ERROR_LOG("could not create temporary file\n");
99 goto out;
100 }
101 output_fd = ret;
102 if (fchmod(output_fd, sb.st_mode) < 0) {
103 ret = -ERRNO_TO_PARA_ERROR(errno);
104 PARA_ERROR_LOG("failed to fchmod fd %d (%s)\n", output_fd,
105 tmp_name);
106 goto out;
107 }
108 ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
109 tmp_name);
110 if (ret < 0)
111 goto out;
112 if (OPT_GIVEN(BACKUP)) {
113 char *backup_name = make_message("%s~", name);
114 ret = xrename(name, backup_name);
115 free(backup_name);
116 if (ret < 0)
117 goto out;
118 }
119 ret = xrename(tmp_name, name);
120 out:
121 if (ret < 0 && output_fd >= 0)
122 unlink(tmp_name); /* ignore errors */
123 free(tmp_name);
124 if (output_fd >= 0)
125 close(output_fd);
126 return ret;
127 }
128
129 static void print_info(int audio_format_num, struct afh_info *afhi)
130 {
131 char *msg;
132
133 afh_get_afhi_txt(audio_format_num, afhi, &msg);
134 printf("%s", msg);
135 free(msg);
136 }
137
138 static void print_chunk_table(struct afh_info *afhi, int audio_format_id,
139 const void *map, size_t mapsize)
140 {
141 int i, ret;
142 void *ctx = NULL;
143
144 for (i = 0; i < afhi->chunks_total; i++) {
145 struct timeval tv;
146 long unsigned from, to;
147 const char *buf;
148 size_t len;
149 tv_scale(i, &afhi->chunk_tv, &tv);
150 from = tv2ms(&tv);
151 tv_scale(i + 1, &afhi->chunk_tv, &tv);
152 to = tv2ms(&tv);
153 ret = afh_get_chunk(i, afhi, audio_format_id, map, mapsize,
154 &buf, &len, &ctx);
155 if (ret < 0) {
156 PARA_ERROR_LOG("fatal: chunk %d: %s\n", i,
157 para_strerror(-ret));
158 return;
159 }
160 if (!OPT_GIVEN(PARSER_FRIENDLY))
161 printf("%d [%lu.%03lu - %lu.%03lu] ", i, from / 1000,
162 from % 1000, to / 1000, to % 1000);
163 printf("%td - %td", buf - (const char *)map,
164 buf + len - (const char *)map);
165 if (!OPT_GIVEN(PARSER_FRIENDLY))
166 printf(" (%zu)", len);
167 printf("\n");
168 }
169 afh_close(ctx, audio_format_id);
170 }
171
172 static void handle_help_flags(void)
173 {
174 char *help;
175
176 if (OPT_GIVEN(DETAILED_HELP))
177 help = lls_long_help(CMD_PTR);
178 else if (OPT_GIVEN(HELP) || lls_num_inputs(lpr) == 0)
179 help = lls_short_help(CMD_PTR);
180 else
181 return;
182 printf("%s", help);
183 free(help);
184 printf("Supported audio formats\n %s\n", AUDIO_FORMAT_HANDLERS);
185 exit(EXIT_SUCCESS);
186 }
187
188 /**
189 * The main function of para_afh.
190 *
191 * \param argc Usual argument count.
192 * \param argv Usual argument vector.
193 *
194 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
195 */
196 int main(int argc, char **argv)
197 {
198 int i, ret = 0, audio_format_num, fd;
199 void *audio_file_data;
200 size_t audio_file_size;
201 struct afh_info afhi;
202 char *errctx;
203
204 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
205 if (ret < 0)
206 goto out;
207 loglevel = OPT_UINT32_VAL(LOGLEVEL);
208 version_handle_flag("afh", OPT_GIVEN(VERSION));
209 handle_help_flags();
210 afh_init();
211 for (i = 0; i < lls_num_inputs(lpr); i++) {
212 int ret2;
213 const char *path = lls_input(i, lpr);
214 ret = mmap_full_file(path, O_RDONLY, &audio_file_data,
215 &audio_file_size, &fd);
216 if (ret < 0) {
217 PARA_ERROR_LOG("failed to mmap \"%s\"\n", path);
218 goto out;
219 }
220 ret = compute_afhi(path, audio_file_data, audio_file_size,
221 fd, &afhi);
222 if (ret >= 0) {
223 audio_format_num = ret;
224 if (OPT_GIVEN(MODIFY)) {
225 ret = rewrite_tags(path, fd, audio_file_data,
226 audio_file_size, audio_format_num, &afhi);
227 } else {
228 printf("File %d: %s\n", i + 1, path);
229 print_info(audio_format_num, &afhi);
230 if (OPT_GIVEN(CHUNK_TABLE))
231 print_chunk_table(&afhi, audio_format_num,
232 audio_file_data, audio_file_size);
233 }
234 clear_afhi(&afhi);
235 }
236 close(fd);
237 ret2 = para_munmap(audio_file_data, audio_file_size);
238 if (ret2 < 0 && ret >= 0)
239 ret = ret2;
240 if (ret < 0)
241 break;
242 }
243 out:
244 if (errctx)
245 PARA_ERROR_LOG("%s\n", errctx);
246 if (ret < 0)
247 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
248 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
249 }