2 * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file afh.c Paraslash's standalone audio format handler tool. */
20 /** Array of error strings. */
23 static struct lls_parse_result *lpr;
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)))
32 INIT_STDERR_LOGGING(loglevel)
34 static inline bool tag_needs_update(bool given, const char *tag,
37 return given && (!tag || strcmp(tag, arg) != 0);
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)
43 struct taginfo *tags = &afhi->tags;
44 bool modified = false;
47 int output_fd = -1, ret;
50 arg = OPT_STRING_VAL(YEAR);
51 if (tag_needs_update(OPT_GIVEN(YEAR), tags->year, arg)) {
53 tags->year = para_strdup(arg);
56 arg = OPT_STRING_VAL(TITLE);
57 if (tag_needs_update(OPT_GIVEN(TITLE), tags->title, arg)) {
59 tags->title = para_strdup(arg);
62 arg = OPT_STRING_VAL(ARTIST);
63 if (tag_needs_update(OPT_GIVEN(ARTIST), tags->artist, arg)) {
65 tags->artist = para_strdup(arg);
68 arg = OPT_STRING_VAL(ALBUM);
69 if (tag_needs_update(OPT_GIVEN(ALBUM), tags->album, arg)) {
71 tags->album = para_strdup(arg);
74 arg = OPT_STRING_VAL(COMMENT);
75 if (tag_needs_update(OPT_GIVEN(COMMENT), tags->comment, arg)) {
77 tags->comment = para_strdup(arg);
81 PARA_WARNING_LOG("no modifications necessary\n");
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.
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);
94 tmp_name = make_message("%s.XXXXXX", name);
95 ret = mkstemp(tmp_name);
97 ret = -ERRNO_TO_PARA_ERROR(errno);
98 PARA_ERROR_LOG("could not create temporary file\n");
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,
108 ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
112 if (OPT_GIVEN(BACKUP)) {
113 char *backup_name = make_message("%s~", name);
114 ret = xrename(name, backup_name);
119 ret = xrename(tmp_name, name);
121 if (ret < 0 && output_fd >= 0)
122 unlink(tmp_name); /* ignore errors */
129 static void print_info(int audio_format_num, struct afh_info *afhi)
133 afh_get_afhi_txt(audio_format_num, afhi, &msg);
138 static void print_chunk_table(struct afh_info *afhi, int audio_format_id,
139 const void *map, size_t mapsize)
144 for (i = 0; i < afhi->chunks_total; i++) {
146 long unsigned from, to;
149 tv_scale(i, &afhi->chunk_tv, &tv);
151 tv_scale(i + 1, &afhi->chunk_tv, &tv);
153 ret = afh_get_chunk(i, afhi, audio_format_id, map, mapsize,
156 PARA_ERROR_LOG("fatal: chunk %d: %s\n", i,
157 para_strerror(-ret));
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);
169 afh_close(ctx, audio_format_id);
172 static void handle_help_flags(void)
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);
184 printf("Supported audio formats\n %s\n", AUDIO_FORMAT_HANDLERS);
189 * The main function of para_afh.
191 * \param argc Usual argument count.
192 * \param argv Usual argument vector.
194 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
196 int main(int argc, char **argv)
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;
204 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
207 loglevel = OPT_UINT32_VAL(LOGLEVEL);
208 version_handle_flag("afh", OPT_GIVEN(VERSION));
211 for (i = 0; i < lls_num_inputs(lpr); i++) {
213 const char *path = lls_input(i, lpr);
214 ret = mmap_full_file(path, O_RDONLY, &audio_file_data,
215 &audio_file_size, &fd);
217 PARA_ERROR_LOG("failed to mmap \"%s\"\n", path);
220 ret = compute_afhi(path, audio_file_data, audio_file_size,
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);
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);
237 ret2 = para_munmap(audio_file_data, audio_file_size);
238 if (ret2 < 0 && ret >= 0)
245 PARA_ERROR_LOG("%s\n", errctx);
247 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
248 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;