Merge branch 'refs/heads/t/gui-improvements'
[paraslash.git] / afh.c
diff --git a/afh.c b/afh.c
index b66e260..195b378 100644 (file)
--- a/afh.c
+++ b/afh.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008-2010 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -7,8 +7,6 @@
 /** \file afh.c Paraslash's standalone audio format handler tool. */
 
 #include <regex.h>
-#include <dirent.h>
-#include <sys/time.h>
 
 #include "para.h"
 #include "string.h"
 #include "fd.h"
 #include "afh.h"
 #include "error.h"
+#include "version.h"
+#include "ggo.h"
 
 static struct afh_args_info conf;
-/** The list of all status items */
-const char *status_item_list[] = {STATUS_ITEM_ARRAY};
-
 INIT_AFH_ERRLISTS;
 
 static int loglevel;
 INIT_STDERR_LOGGING(loglevel)
 
+static inline bool tag_needs_update(bool given, const char *tag,
+               const char *arg)
+{
+       return given && (!tag || strcmp(tag, arg) != 0);
+}
+
+static int rewrite_tags(const char *name, int input_fd, void *map,
+               size_t map_size, int audio_format_id, struct afh_info *afhi)
+{
+       struct taginfo *tags = &afhi->tags;
+       bool modified = false;
+       char *tmp_name;
+       int output_fd = -1, ret;
+       struct stat sb;
+
+       if (tag_needs_update(conf.year_given, tags->year, conf.year_arg)) {
+               free(tags->year);
+               tags->year = para_strdup(conf.year_arg);
+               modified = true;
+       }
+       if (tag_needs_update(conf.title_given, tags->title, conf.title_arg)) {
+               free(tags->title);
+               tags->title = para_strdup(conf.title_arg);
+               modified = true;
+       }
+       if (tag_needs_update(conf.artist_given, tags->artist,
+                       conf.artist_arg)) {
+               free(tags->artist);
+               tags->artist = para_strdup(conf.artist_arg);
+               modified = true;
+       }
+       if (tag_needs_update(conf.album_given, tags->album, conf.album_arg)) {
+               free(tags->album);
+               tags->album = para_strdup(conf.album_arg);
+               modified = true;
+       }
+       if (tag_needs_update(conf.comment_given, tags->comment,
+                       conf.comment_arg)) {
+               free(tags->comment);
+               tags->comment = para_strdup(conf.comment_arg);
+               modified = true;
+       }
+       if (!modified) {
+               PARA_WARNING_LOG("no modifications necessary\n");
+               return 0;
+       }
+       /*
+        * mkstmp() creates the temporary file with permissions 0600, but we
+        * like it to have the same permissions as the original file, so we
+        * have to get this information.
+        */
+       if (fstat(input_fd, &sb) < 0) {
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+               PARA_ERROR_LOG("failed to fstat fd %d (%s)\n", input_fd, name);
+               return ret;
+       }
+       tmp_name = make_message("%s.XXXXXX", name);
+       ret = mkstemp(tmp_name);
+       if (ret < 0) {
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+               PARA_ERROR_LOG("could not create temporary file\n");
+               goto out;
+       }
+       output_fd = ret;
+       if (fchmod(output_fd, sb.st_mode) < 0) {
+               ret = -ERRNO_TO_PARA_ERROR(errno);
+               PARA_ERROR_LOG("failed to fchmod fd %d (%s)\n", output_fd,
+                       tmp_name);
+               goto out;
+       }
+       ret = afh_rewrite_tags(audio_format_id, map, map_size, tags, output_fd,
+               tmp_name);
+       if (ret < 0)
+               goto out;
+       if (conf.backup_given) {
+               char *backup_name = make_message("%s~", name);
+               ret = xrename(name, backup_name);
+               free(backup_name);
+               if (ret < 0)
+                       goto out;
+       }
+       ret = xrename(tmp_name, name);
+out:
+       if (ret < 0 && output_fd >= 0)
+               unlink(tmp_name); /* ignore errors */
+       free(tmp_name);
+       if (output_fd >= 0)
+               close(output_fd);
+       return ret;
+}
+
 static void print_info(int audio_format_num, struct afh_info *afhi)
 {
-       printf("%s: %dkbit/s\n" /* bitrate */
-               "%s: %s\n" /* format */
-               "%s: %dHz\n" /* frequency */
-               "%s: %d\n" /* channels */
-               "%s: %lu\n" /* seconds total */
-               "%s: %lu: %lu\n" /* chunk time */
-               "%s: %lu\n" /* num chunks */
-               "%s: %s\n" /* techinfo */
-               "%s: %s\n" /* artist */
-               "%s: %s\n" /* title */
-               "%s: %s\n" /* year */
-               "%s: %s\n" /* album */
-               "%s: %s\n", /* comment */
-               status_item_list[SI_BITRATE], afhi->bitrate,
-               status_item_list[SI_FORMAT], audio_format_name(audio_format_num),
-               status_item_list[SI_FREQUENCY], afhi->frequency,
-               status_item_list[SI_CHANNELS], afhi->channels,
-               status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
-               status_item_list[SI_CHUNK_TIME], (long unsigned)afhi->chunk_tv.tv_sec,
-                       (long unsigned)afhi->chunk_tv.tv_usec,
-               status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
-               status_item_list[SI_TECHINFO], afhi->techinfo? afhi->techinfo : "",
-               status_item_list[SI_ARTIST], afhi->tags.artist? afhi->tags.artist : "",
-               status_item_list[SI_TITLE], afhi->tags.title? afhi->tags.title : "",
-               status_item_list[SI_YEAR], afhi->tags.year? afhi->tags.year : "",
-               status_item_list[SI_ALBUM], afhi->tags.album? afhi->tags.album : "",
-               status_item_list[SI_COMMENT], afhi->tags.comment? afhi->tags.comment : ""
-       );
+       char *msg;
+
+       afh_get_afhi_txt(audio_format_num, afhi, &msg);
+       printf("%s", msg);
+       free(msg);
 }
 
 static void print_chunk_table(struct afh_info *afhi)
 {
        int i;
 
-       if (!conf.human_given) {
+       if (conf.parser_friendly_given) {
                printf("chunk_table: ");
                for (i = 0; i <= afhi->chunks_total; i++)
                        printf("%u ", afhi->chunk_table[i]);
@@ -83,71 +148,15 @@ static void print_chunk_table(struct afh_info *afhi)
        }
 }
 
-static int cat_file(void *audio_file_data, struct afh_info *afhi)
+__noreturn static void print_help_and_die(void)
 {
-       int ret;
-       struct timeval stream_start;
-       long unsigned i, first_chunk, last_chunk;
-       const char *buf;
-       size_t size;
-
-
-       if (conf.begin_chunk_arg < 0) {
-               if (-conf.begin_chunk_arg > afhi->chunks_total)
-                       return -ERRNO_TO_PARA_ERROR(EINVAL);
-               first_chunk = afhi->chunks_total + conf.begin_chunk_arg;
-       } else
-               first_chunk = conf.begin_chunk_arg;
-       if (conf.end_chunk_given) {
-               if (conf.end_chunk_arg < 0) {
-                       if (-conf.end_chunk_arg > afhi->chunks_total)
-                               return -ERRNO_TO_PARA_ERROR(EINVAL);
-                       last_chunk = afhi->chunks_total + conf.end_chunk_arg;
-               } else {
-                       if (conf.end_chunk_arg >= afhi->chunks_total)
-                               return -ERRNO_TO_PARA_ERROR(EINVAL);
-                       last_chunk = conf.end_chunk_arg;
-               }
-       } else
-               last_chunk = afhi->chunks_total - 1;
-       if (first_chunk >= last_chunk)
-               return -ERRNO_TO_PARA_ERROR(EINVAL);
-       if (!afhi->chunks_total)
-               return 1;
-       afh_get_header(afhi, audio_file_data, &buf, &size);
-       if (size && first_chunk && !conf.no_header_given) {
-               PARA_INFO_LOG("writing audio file header (%zu bytes)\n", size);
-               ret = write(STDOUT_FILENO, buf, size);
-               if (ret < 0)
-                       return ret;
-               if (ret != size)
-                       return -E_AFH_SHORT_WRITE;
-       }
-       PARA_NOTICE_LOG("writing chunks %lu - %lu\n", first_chunk, last_chunk);
-       gettimeofday(&stream_start, NULL);
-       for (i = first_chunk; i <= last_chunk; i++) {
-               struct timeval now, diff, next_chunk;
-               afh_get_chunk(i, afhi, audio_file_data, &buf, &size);
-               PARA_DEBUG_LOG("chunk %lu: size %zu\n", i, size);
-               if (conf.just_in_time_given) {
-                       compute_chunk_time(i - first_chunk, &afhi->chunk_tv,
-                               &stream_start, &next_chunk);
-                       gettimeofday(&now, NULL);
-                       ret = tv_diff(&next_chunk, &now, &diff);
-                       if (ret > 0) {
-                               ret = para_select(1, NULL, NULL, &diff);
-                               if (ret < 0)
-                                       return ret;
-                       }
-               }
-               if (!size)
-                       continue;
-               PARA_INFO_LOG("writing chunk %lu\n", i);
-               ret = write_all(STDOUT_FILENO, buf, &size);
-               if (ret < 0)
-                       return ret;
-       }
-       return 1;
+       struct ggo_help h = DEFINE_GGO_HELP(afh);
+       int d = conf.detailed_help_given;
+       unsigned flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
+
+       ggo_print_help(&h, flags);
+       printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
+       exit(EXIT_SUCCESS);
 }
 
 /**
@@ -166,41 +175,39 @@ int main(int argc, char **argv)
        struct afh_info afhi;
 
        afh_cmdline_parser(argc, argv, &conf);
-       HANDLE_VERSION_FLAG("afh", conf);
        loglevel = get_loglevel_by_name(conf.loglevel_arg);
+       version_handle_flag("afh", conf.version_given);
+       if (conf.help_given || conf.detailed_help_given)
+               print_help_and_die();
+       afh_init();
        ret = -E_AFH_SYNTAX;
        if (conf.inputs_num == 0)
                goto out;
-       if (conf.stream_given && conf.inputs_num != 1)
-               goto out;
-       afh_init();
        for (i = 0; i < conf.inputs_num; i++) {
                int ret2;
                ret = mmap_full_file(conf.inputs[i], O_RDONLY, &audio_file_data,
                        &audio_file_size, &fd);
-               if (ret < 0)
+               if (ret < 0) {
+                       PARA_ERROR_LOG("failed to mmap \"%s\"\n", conf.inputs[i]);
                        goto out;
+               }
                ret = compute_afhi(conf.inputs[i], audio_file_data, audio_file_size,
                        fd, &afhi);
-               if (ret < 0)
-                       goto out;
-               audio_format_num = ret;
-               if (conf.stream_given)
-                       ret = cat_file(audio_file_data, &afhi);
-               else {
-                       printf("File %d: %s\n", i + 1, conf.inputs[i]);
-                       print_info(audio_format_num, &afhi);
-                       if (conf.chunk_table_given)
-                               print_chunk_table(&afhi);
-                       free(afhi.techinfo);
-                       free(afhi.tags.artist);
-                       free(afhi.tags.title);
-                       free(afhi.tags.year);
-                       free(afhi.tags.album);
-                       free(afhi.tags.comment);
-                       free(afhi.chunk_table);
-                       printf("\n");
+               if (ret >= 0) {
+                       audio_format_num = ret;
+                       if (conf.modify_given) {
+                               ret = rewrite_tags(conf.inputs[i], fd, audio_file_data,
+                                       audio_file_size, audio_format_num, &afhi);
+                       } else {
+                               printf("File %d: %s\n", i + 1, conf.inputs[i]);
+                               print_info(audio_format_num, &afhi);
+                               if (conf.chunk_table_given)
+                                       print_chunk_table(&afhi);
+                               printf("\n");
+                       }
+                       clear_afhi(&afhi);
                }
+               close(fd);
                ret2 = para_munmap(audio_file_data, audio_file_size);
                if (ret2 < 0 && ret >= 0)
                        ret = ret2;