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