1 /* Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file afh.c Paraslash's standalone audio format handler tool. */
16 /** Array of error strings. */
19 static struct lls_parse_result
*lpr
;
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)))
28 INIT_STDERR_LOGGING(loglevel
)
30 static inline bool tag_needs_update(bool given
, const char *tag
,
33 return given
&& (!tag
|| strcmp(tag
, arg
) != 0);
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
)
39 struct taginfo
*tags
= &afhi
->tags
;
40 bool modified
= false;
43 int output_fd
= -1, ret
;
46 arg
= OPT_STRING_VAL(YEAR
);
47 if (tag_needs_update(OPT_GIVEN(YEAR
), tags
->year
, arg
)) {
49 tags
->year
= para_strdup(arg
);
52 arg
= OPT_STRING_VAL(TITLE
);
53 if (tag_needs_update(OPT_GIVEN(TITLE
), tags
->title
, arg
)) {
55 tags
->title
= para_strdup(arg
);
58 arg
= OPT_STRING_VAL(ARTIST
);
59 if (tag_needs_update(OPT_GIVEN(ARTIST
), tags
->artist
, arg
)) {
61 tags
->artist
= para_strdup(arg
);
64 arg
= OPT_STRING_VAL(ALBUM
);
65 if (tag_needs_update(OPT_GIVEN(ALBUM
), tags
->album
, arg
)) {
67 tags
->album
= para_strdup(arg
);
70 arg
= OPT_STRING_VAL(COMMENT
);
71 if (tag_needs_update(OPT_GIVEN(COMMENT
), tags
->comment
, arg
)) {
73 tags
->comment
= para_strdup(arg
);
77 PARA_WARNING_LOG("no modifications necessary\n");
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.
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
);
90 tmp_name
= make_message("%s.XXXXXX", name
);
91 ret
= mkstemp(tmp_name
);
93 ret
= -ERRNO_TO_PARA_ERROR(errno
);
94 PARA_ERROR_LOG("could not create temporary file\n");
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
,
104 ret
= afh_rewrite_tags(audio_format_id
, map
, map_size
, tags
, output_fd
,
108 if (OPT_GIVEN(BACKUP
)) {
109 char *backup_name
= make_message("%s~", name
);
110 ret
= xrename(name
, backup_name
);
115 ret
= xrename(tmp_name
, name
);
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
;
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.
129 if (futimens(output_fd
, times
) < 0) {
130 ret
= -ERRNO_TO_PARA_ERROR(errno
);
135 if (ret
< 0 && output_fd
>= 0)
136 unlink(tmp_name
); /* ignore errors */
143 static void print_info(int audio_format_num
, struct afh_info
*afhi
)
147 afh_get_afhi_txt(audio_format_num
, afhi
, &msg
);
152 static void print_chunk_table(struct afh_info
*afhi
, int audio_format_id
,
153 const void *map
, size_t mapsize
)
158 for (i
= 0; i
< afhi
->chunks_total
; i
++) {
160 long unsigned from
, to
;
163 tv_scale(i
, &afhi
->chunk_tv
, &tv
);
165 tv_scale(i
+ 1, &afhi
->chunk_tv
, &tv
);
167 ret
= afh_get_chunk(i
, afhi
, audio_format_id
, map
, mapsize
,
170 PARA_ERROR_LOG("fatal: chunk %d: %s\n", i
,
171 para_strerror(-ret
));
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(" (%u)", len
);
183 afh_close(ctx
, audio_format_id
);
186 static void handle_help_flags(void)
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
);
198 printf("Supported audio formats\n %s\n", AUDIO_FORMAT_HANDLERS
);
203 * The main function of para_afh.
205 * \param argc Usual argument count.
206 * \param argv Usual argument vector.
208 * \return \p EXIT_FAILURE or \p EXIT_SUCCESS.
210 int main(int argc
, char **argv
)
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
;
218 ret
= lls(lls_parse(argc
, argv
, CMD_PTR
, &lpr
, &errctx
));
221 loglevel
= OPT_UINT32_VAL(LOGLEVEL
);
222 version_handle_flag("afh", OPT_GIVEN(VERSION
));
224 for (i
= 0; i
< lls_num_inputs(lpr
); i
++) {
226 const char *path
= lls_input(i
, lpr
);
227 ret
= mmap_full_file(path
, O_RDONLY
, &audio_file_data
,
228 &audio_file_size
, &fd
);
230 PARA_ERROR_LOG("failed to mmap \"%s\"\n", path
);
233 ret
= compute_afhi(path
, audio_file_data
, audio_file_size
,
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
);
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
);
250 ret2
= para_munmap(audio_file_data
, audio_file_size
);
251 if (ret2
< 0 && ret
>= 0)
258 PARA_ERROR_LOG("%s\n", errctx
);
260 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
261 lls_free_parse_result(lpr
, CMD_PTR
);
262 return ret
< 0? EXIT_FAILURE
: EXIT_SUCCESS
;