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 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)
142 if (OPT_GIVEN(PARSER_FRIENDLY)) {
143 printf("chunk_table: ");
144 for (i = 0; i <= afhi->chunks_total; i++)
145 printf("%u ", afhi->chunk_table[i]);
149 for (i = 1; i <= afhi->chunks_total; i++) {
151 long unsigned from, to;
152 tv_scale(i - 1, &afhi->chunk_tv, &tv);
154 tv_scale(i, &afhi->chunk_tv, &tv);
156 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
157 from / 1000, from % 1000, to / 1000, to % 1000,
158 afhi->chunk_table[i - 1], afhi->chunk_table[i],
159 afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
163 static void handle_help_flags(void)
167 if (OPT_GIVEN(DETAILED_HELP))
168 help = lls_long_help(CMD_PTR);
169 else if (OPT_GIVEN(HELP) || lls_num_inputs(lpr) == 0)
170 help = lls_short_help(CMD_PTR);
175 printf("Supported audio formats\n %s\n", AUDIO_FORMAT_HANDLERS);
180 * The main function of para_afh.
182 * \param argc Usual argument count.
183 * \param argv Usual argument vector.
185 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
187 int main(int argc, char **argv)
189 int i, ret = 0, audio_format_num, fd;
190 void *audio_file_data;
191 size_t audio_file_size;
192 struct afh_info afhi;
195 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
198 loglevel = OPT_UINT32_VAL(LOGLEVEL);
199 version_handle_flag("afh", OPT_GIVEN(VERSION));
202 for (i = 0; i < lls_num_inputs(lpr); i++) {
204 const char *path = lls_input(i, lpr);
205 ret = mmap_full_file(path, O_RDONLY, &audio_file_data,
206 &audio_file_size, &fd);
208 PARA_ERROR_LOG("failed to mmap \"%s\"\n", path);
211 ret = compute_afhi(path, audio_file_data, audio_file_size,
214 audio_format_num = ret;
215 if (OPT_GIVEN(MODIFY)) {
216 ret = rewrite_tags(path, fd, audio_file_data,
217 audio_file_size, audio_format_num, &afhi);
219 printf("File %d: %s\n", i + 1, path);
220 print_info(audio_format_num, &afhi);
221 if (OPT_GIVEN(CHUNK_TABLE))
222 print_chunk_table(&afhi);
228 ret2 = para_munmap(audio_file_data, audio_file_size);
229 if (ret2 < 0 && ret >= 0)
236 PARA_ERROR_LOG("%s\n", errctx);
238 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
239 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;