Merge branch 'refs/heads/t/xz'
[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
11 #include "para.h"
12 #include "string.h"
13 #include "afh.cmdline.h"
14 #include "fd.h"
15 #include "afh.h"
16 #include "error.h"
17 #include "version.h"
18 #include "ggo.h"
19
20 /** Array of error strings. */
21 DEFINE_PARA_ERRLIST;
22
23 static struct afh_args_info conf;
24
25 static int loglevel;
26 INIT_STDERR_LOGGING(loglevel)
27
28 static inline bool tag_needs_update(bool given, const char *tag,
29                 const char *arg)
30 {
31         return given && (!tag || strcmp(tag, arg) != 0);
32 }
33
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)
36 {
37         struct taginfo *tags = &afhi->tags;
38         bool modified = false;
39         char *tmp_name;
40         int output_fd = -1, ret;
41         struct stat sb;
42
43         if (tag_needs_update(conf.year_given, tags->year, conf.year_arg)) {
44                 free(tags->year);
45                 tags->year = para_strdup(conf.year_arg);
46                 modified = true;
47         }
48         if (tag_needs_update(conf.title_given, tags->title, conf.title_arg)) {
49                 free(tags->title);
50                 tags->title = para_strdup(conf.title_arg);
51                 modified = true;
52         }
53         if (tag_needs_update(conf.artist_given, tags->artist,
54                         conf.artist_arg)) {
55                 free(tags->artist);
56                 tags->artist = para_strdup(conf.artist_arg);
57                 modified = true;
58         }
59         if (tag_needs_update(conf.album_given, tags->album, conf.album_arg)) {
60                 free(tags->album);
61                 tags->album = para_strdup(conf.album_arg);
62                 modified = true;
63         }
64         if (tag_needs_update(conf.comment_given, tags->comment,
65                         conf.comment_arg)) {
66                 free(tags->comment);
67                 tags->comment = para_strdup(conf.comment_arg);
68                 modified = true;
69         }
70         if (!modified) {
71                 PARA_WARNING_LOG("no modifications necessary\n");
72                 return 0;
73         }
74         /*
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.
78          */
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);
82                 return ret;
83         }
84         tmp_name = make_message("%s.XXXXXX", name);
85         ret = mkstemp(tmp_name);
86         if (ret < 0) {
87                 ret = -ERRNO_TO_PARA_ERROR(errno);
88                 PARA_ERROR_LOG("could not create temporary file\n");
89                 goto out;
90         }
91         output_fd = ret;
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,
95                         tmp_name);
96                 goto out;
97         }
98         ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
99                 tmp_name);
100         if (ret < 0)
101                 goto out;
102         if (conf.backup_given) {
103                 char *backup_name = make_message("%s~", name);
104                 ret = xrename(name, backup_name);
105                 free(backup_name);
106                 if (ret < 0)
107                         goto out;
108         }
109         ret = xrename(tmp_name, name);
110 out:
111         if (ret < 0 && output_fd >= 0)
112                 unlink(tmp_name); /* ignore errors */
113         free(tmp_name);
114         if (output_fd >= 0)
115                 close(output_fd);
116         return ret;
117 }
118
119 static void print_info(int audio_format_num, struct afh_info *afhi)
120 {
121         char *msg;
122
123         afh_get_afhi_txt(audio_format_num, afhi, &msg);
124         printf("%s", msg);
125         free(msg);
126 }
127
128 static void print_chunk_table(struct afh_info *afhi, int audio_format_id,
129                 const void *map, size_t mapsize)
130 {
131         int i, ret;
132         void *ctx = NULL;
133
134         for (i = 0; i < afhi->chunks_total; i++) {
135                 struct timeval tv;
136                 long unsigned from, to;
137                 const char *buf;
138                 size_t len;
139                 tv_scale(i, &afhi->chunk_tv, &tv);
140                 from = tv2ms(&tv);
141                 tv_scale(i + 1, &afhi->chunk_tv, &tv);
142                 to = tv2ms(&tv);
143                 ret = afh_get_chunk(i, afhi, audio_format_id, map, mapsize,
144                         &buf, &len, &ctx);
145                 if (ret < 0) {
146                         PARA_ERROR_LOG("fatal: chunk %d: %s\n", i,
147                                 para_strerror(-ret));
148                         return;
149                 }
150                 if (!conf.parser_friendly_given)
151                         printf("%d [%lu.%03lu - %lu.%03lu] ", i, from / 1000,
152                                 from % 1000, to / 1000, to % 1000);
153                 printf("%td - %td", buf - (const char *)map,
154                         buf + len - (const char *)map);
155                 if (!conf.parser_friendly_given)
156                         printf(" (%zu)", len);
157                 printf("\n");
158         }
159         afh_close(ctx, audio_format_id);
160 }
161
162 __noreturn static void print_help_and_die(void)
163 {
164         struct ggo_help h = DEFINE_GGO_HELP(afh);
165         int d = conf.detailed_help_given;
166         unsigned flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
167
168         ggo_print_help(&h, flags);
169         printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
170         exit(EXIT_SUCCESS);
171 }
172
173 /**
174  * The main function of para_afh.
175  *
176  * \param argc Usual argument count.
177  * \param argv Usual argument vector.
178  *
179  * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
180  */
181 int main(int argc, char **argv)
182 {
183         int i, ret = 0, audio_format_num, fd;
184         void *audio_file_data;
185         size_t audio_file_size;
186         struct afh_info afhi;
187
188         afh_cmdline_parser(argc, argv, &conf);
189         loglevel = get_loglevel_by_name(conf.loglevel_arg);
190         version_handle_flag("afh", conf.version_given);
191         if (conf.help_given || conf.detailed_help_given || conf.inputs_num == 0)
192                 print_help_and_die();
193         afh_init();
194         for (i = 0; i < conf.inputs_num; i++) {
195                 int ret2;
196                 ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
197                         &audio_file_size, &fd);
198                 if (ret < 0) {
199                         PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
200                         goto out;
201                 }
202                 ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
203                         fd, &afhi);
204                 if (ret >= 0) {
205                         audio_format_num = ret;
206                         if (conf.modify_given) {
207                                 ret = rewrite_tags(conf.inputs[i], fd, audio_file_data,
208                                         audio_file_size, audio_format_num, &afhi);
209                         } else {
210                                 printf("File %d: %s\n", i + 1, conf.inputs[i]);
211                                 print_info(audio_format_num, &afhi);
212                                 if (conf.chunk_table_given)
213                                         print_chunk_table(&afhi, audio_format_num,
214                                                 audio_file_data, audio_file_size);
215                         }
216                         clear_afhi(&afhi);
217                 }
218                 close(fd);
219                 ret2 = para_munmap(audio_file_data, audio_file_size);
220                 if (ret2 < 0 && ret >= 0)
221                         ret = ret2;
222                 if (ret < 0)
223                         break;
224         }
225 out:
226         if (ret < 0)
227                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
228         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
229 }