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. */
13 #include "afh.cmdline.h"
20 /** Array of error strings. */
23 static struct afh_args_info conf;
26 INIT_STDERR_LOGGING(loglevel)
28 static inline bool tag_needs_update(bool given, const char *tag,
31 return given && (!tag || strcmp(tag, arg) != 0);
34 static int rewrite_tags(const char *name, int input_fd, void *map,
35 size_t map_size, int audio_format_id, struct afh_info *afhi)
37 struct taginfo *tags = &afhi->tags;
38 bool modified = false;
40 int output_fd = -1, ret;
43 if (tag_needs_update(conf.year_given, tags->year, conf.year_arg)) {
45 tags->year = para_strdup(conf.year_arg);
48 if (tag_needs_update(conf.title_given, tags->title, conf.title_arg)) {
50 tags->title = para_strdup(conf.title_arg);
53 if (tag_needs_update(conf.artist_given, tags->artist,
56 tags->artist = para_strdup(conf.artist_arg);
59 if (tag_needs_update(conf.album_given, tags->album, conf.album_arg)) {
61 tags->album = para_strdup(conf.album_arg);
64 if (tag_needs_update(conf.comment_given, tags->comment,
67 tags->comment = para_strdup(conf.comment_arg);
71 PARA_WARNING_LOG("no modifications necessary\n");
75 * mkstmp() creates the temporary file with permissions 0600, but we
76 * like it to have the same permissions as the original file, so we
77 * have to get this information.
79 if (fstat(input_fd, &sb) < 0) {
80 ret = -ERRNO_TO_PARA_ERROR(errno);
81 PARA_ERROR_LOG("failed to fstat fd %d (%s)\n", input_fd, name);
84 tmp_name = make_message("%s.XXXXXX", name);
85 ret = mkstemp(tmp_name);
87 ret = -ERRNO_TO_PARA_ERROR(errno);
88 PARA_ERROR_LOG("could not create temporary file\n");
92 if (fchmod(output_fd, sb.st_mode) < 0) {
93 ret = -ERRNO_TO_PARA_ERROR(errno);
94 PARA_ERROR_LOG("failed to fchmod fd %d (%s)\n", output_fd,
98 ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
102 if (conf.backup_given) {
103 char *backup_name = make_message("%s~", name);
104 ret = xrename(name, backup_name);
109 ret = xrename(tmp_name, name);
111 if (ret < 0 && output_fd >= 0)
112 unlink(tmp_name); /* ignore errors */
119 static void print_info(int audio_format_num, struct afh_info *afhi)
123 afh_get_afhi_txt(audio_format_num, afhi, &msg);
128 static void print_chunk_table(struct afh_info *afhi)
132 if (conf.parser_friendly_given) {
133 printf("chunk_table: ");
134 for (i = 0; i <= afhi->chunks_total; i++)
135 printf("%u ", afhi->chunk_table[i]);
139 for (i = 1; i <= afhi->chunks_total; i++) {
141 long unsigned from, to;
142 tv_scale(i - 1, &afhi->chunk_tv, &tv);
144 tv_scale(i, &afhi->chunk_tv, &tv);
146 printf("%d [%lu.%03lu - %lu.%03lu] %u - %u (%u)\n", i - 1,
147 from / 1000, from % 1000, to / 1000, to % 1000,
148 afhi->chunk_table[i - 1], afhi->chunk_table[i],
149 afhi->chunk_table[i] - afhi->chunk_table[i - 1]);
153 __noreturn static void print_help_and_die(void)
155 struct ggo_help h = DEFINE_GGO_HELP(afh);
156 int d = conf.detailed_help_given;
157 unsigned flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
159 ggo_print_help(&h, flags);
160 printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
165 * The main function of para_afh.
167 * \param argc Usual argument count.
168 * \param argv Usual argument vector.
170 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
172 int main(int argc, char **argv)
174 int i, ret = 0, audio_format_num, fd;
175 void *audio_file_data;
176 size_t audio_file_size;
177 struct afh_info afhi;
179 afh_cmdline_parser(argc, argv, &conf);
180 loglevel = get_loglevel_by_name(conf.loglevel_arg);
181 version_handle_flag("afh", conf.version_given);
182 if (conf.help_given || conf.detailed_help_given || conf.inputs_num == 0)
183 print_help_and_die();
185 for (i = 0; i < conf.inputs_num; i++) {
187 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
188 &audio_file_size, &fd);
190 PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
193 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
196 audio_format_num = ret;
197 if (conf.modify_given) {
198 ret = rewrite_tags(conf.inputs[i], fd, audio_file_data,
199 audio_file_size, audio_format_num, &afhi);
201 printf("File %d: %s\n", i + 1, conf.inputs[i]);
202 print_info(audio_format_num, &afhi);
203 if (conf.chunk_table_given)
204 print_chunk_table(&afhi);
210 ret2 = para_munmap(audio_file_data, audio_file_size);
211 if (ret2 < 0 && ret >= 0)
218 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
219 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;