aft: Update status items on afs events.
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index 377740d132b831d126e0015f241ed65095c2e661..0e86b12d6c13ea1992c0c751dae61df43f78b226 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2013 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -72,6 +72,20 @@ enum ls_listing_mode {
        LS_MODE_PARSER,
 };
 
+/* Data about one audio file. Needed for ls and stat output. */
+struct ls_data {
+       /* Usual audio format handler information. */
+       struct afh_info afhi;
+       /* Audio file selector information. */
+       struct afs_info afsi;
+       /* The full path of the audio file. */
+       char *path;
+       /* The score value (if -a was given). */
+       long score;
+       /* The hash value of the audio file data. */
+       unsigned char *hash;
+};
+
 /** The flags accepted by the ls command. */
 enum ls_flags {
        /** -p */
@@ -87,10 +101,9 @@ enum ls_flags {
 /**
  * The size of the individual output fields of the ls command.
  *
- * These depend on the actual content being listed. If, for instance only files
- * with duration less than an hour are being listed, then the duration with is
- * made smaller because then the duration is listed as mm:ss rather than
- * hh:mm:ss.
+ * These depend on the content being listed. For example, if each listed file
+ * is shorter than an hour, the duration format is set to mm:ss. Otherwise it
+ * is set to hh:mm:ss.
  */
 struct ls_widths {
        /** size of the score field. */
@@ -163,7 +176,7 @@ enum afsi_offsets {
        AFSI_SIZE = 32
 };
 
-/**
+/*
  * Convert a struct afs_info to an osl object.
  *
  * \param afsi Pointer to the audio file info to be converted.
@@ -171,7 +184,7 @@ enum afsi_offsets {
  *
  * \sa load_afsi().
  */
-void save_afsi(struct afs_info *afsi, struct osl_object *obj)
+static void save_afsi(struct afs_info *afsi, struct osl_object *obj)
 {
        char *buf = obj->data;
 
@@ -186,17 +199,17 @@ void save_afsi(struct afs_info *afsi, struct osl_object *obj)
        memset(buf + AFSI_AUDIO_FORMAT_UNUSED_OFFSET, 0, 2);
 }
 
-/**
- *  Get the audio file selector info struct stored in an osl object.
+/*
+ * Get the audio file selector info struct stored in an osl object.
  *
  * \param afsi Points to the audio_file info structure to be filled in.
  * \param obj The osl object holding the data.
  *
- * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
+ * \return Standard.
  *
  * \sa save_afsi().
  */
-int load_afsi(struct afs_info *afsi, struct osl_object *obj)
+static int load_afsi(struct afs_info *afsi, struct osl_object *obj)
 {
        char *buf = obj->data;
        if (obj->size < AFSI_SIZE)
@@ -493,15 +506,16 @@ static int aft_get_row_of_hash(unsigned char *hash, struct osl_row **row)
        return osl(osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row));
 }
 
-/**
- * Get the osl object holding the audio file selector info of a row.
+/*
+ * Get the audio file selector info object of a row.
  *
  * \param row Pointer to a row in the audio file table.
  * \param obj Result pointer.
  *
  * \return Standard.
  */
-int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
+static int get_afsi_object_of_row(const struct osl_row *row,
+               struct osl_object *obj)
 {
        return osl(osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj));
 }
@@ -541,15 +555,15 @@ int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
        return load_afsi(afsi, &obj);
 }
 
-/**
+/*
  * Get the audio file selector info, given the path of an audio table.
  *
  * \param path The full path of the audio file.
  * \param afsi Result pointer.
  *
- * \return Positive on success, negative on errors.
+ * \return Standard.
  */
-int get_afsi_of_path(const char *path, struct afs_info *afsi)
+static int get_afsi_of_path(const char *path, struct afs_info *afsi)
 {
        struct osl_object obj;
        int ret = get_afsi_object_of_path(path, &obj);
@@ -755,7 +769,6 @@ static void get_duration_buf(int seconds, char *buf, struct ls_options *opts)
        }
 }
 
-
 static int write_attribute_items(struct para_buffer *b,
                const char *att_bitmap, struct afs_info *afsi)
 {
@@ -830,8 +843,8 @@ static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
        ret = aft_get_row_of_hash(d->hash, &aft_row);
        if (ret < 0)
                return ret;
-       ret = osl_open_disk_object(audio_file_table, aft_row,
-               AFTCOL_CHUNKS, &chunk_table_obj);
+       ret = osl(osl_open_disk_object(audio_file_table, aft_row,
+               AFTCOL_CHUNKS, &chunk_table_obj));
        if (ret < 0)
                return ret;
        ret = para_printf(b, "%s\n"
@@ -1027,17 +1040,11 @@ out:
        return ret;
 }
 
-static int make_status_items(struct audio_file_data *afd,
-               struct afs_info *afsi, char *path, long score,
-               unsigned char *hash)
+static struct ls_data status_item_ls_data;
+static struct osl_row *current_aft_row;
+
+static int make_status_items(void)
 {
-       struct ls_data d = {
-               .afhi = afd->afhi,
-               .afsi = *afsi,
-               .path = path,
-               .score = score,
-               .hash = hash
-       };
        struct ls_options opts = {
                .flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
                .mode = LS_MODE_VERBOSE,
@@ -1047,7 +1054,7 @@ static int make_status_items(struct audio_file_data *afd,
        int ret;
 
        time(&current_time);
-       ret = print_list_item(&d, &opts, &pb, current_time);
+       ret = print_list_item(&status_item_ls_data, &opts, &pb, current_time);
        if (ret < 0)
                return ret;
        free(status_items);
@@ -1055,7 +1062,7 @@ static int make_status_items(struct audio_file_data *afd,
        memset(&pb, 0, sizeof(pb));
        pb.max_size = shm_get_shmmax() - 1;
        pb.flags = PBF_SIZE_PREFIX;
-       ret = print_list_item(&d, &opts, &pb, current_time);
+       ret = print_list_item(&status_item_ls_data, &opts, &pb, current_time);
        if (ret < 0) {
                free(status_items);
                status_items = NULL;
@@ -1067,10 +1074,8 @@ static int make_status_items(struct audio_file_data *afd,
 }
 
 /**
- * Mmap the given audio file and update statistics.
+ * Open the audio file with highest score and set up an afd structure.
  *
- * \param aft_row Determines the audio file to be opened and updated.
- * \param score The score of the audio file.
  * \param afd Result pointer.
  *
  * On success, the numplayed field of the audio file selector info is increased
@@ -1079,67 +1084,75 @@ static int make_status_items(struct audio_file_data *afd,
  *
  * \return Positive shmid on success, negative on errors.
  */
-int open_and_update_audio_file(struct osl_row *aft_row, long score,
-       struct audio_file_data *afd)
+int open_and_update_audio_file(struct audio_file_data *afd)
 {
-       unsigned char *aft_hash, file_hash[HASH_SIZE];
+       unsigned char file_hash[HASH_SIZE];
        struct osl_object afsi_obj;
-       struct afs_info old_afsi, new_afsi;
-       int ret = get_hash_of_row(aft_row, &aft_hash);
+       struct afs_info new_afsi;
+       int ret;
        struct afsi_change_event_data aced;
        struct osl_object map, chunk_table_obj;
-       char *path;
-
+       struct ls_data *d = &status_item_ls_data;
+again:
+       ret = score_get_best(&current_aft_row, &d->score);
        if (ret < 0)
                return ret;
-       ret = get_audio_file_path_of_row(aft_row, &path);
+       ret = get_hash_of_row(current_aft_row, &d->hash);
        if (ret < 0)
                return ret;
-       PARA_NOTICE_LOG("%s\n", path);
-       ret = get_afsi_object_of_row(aft_row, &afsi_obj);
+       ret = get_audio_file_path_of_row(current_aft_row, &d->path);
        if (ret < 0)
                return ret;
-       ret = load_afsi(&old_afsi, &afsi_obj);
+       PARA_NOTICE_LOG("%s\n", d->path);
+       ret = get_afsi_object_of_row(current_aft_row, &afsi_obj);
        if (ret < 0)
                return ret;
-       ret = get_afhi_of_row(aft_row, &afd->afhi);
+       ret = load_afsi(&d->afsi, &afsi_obj);
        if (ret < 0)
                return ret;
-       afd->afhi.chunk_table = NULL;
-       ret = osl_open_disk_object(audio_file_table, aft_row,
-               AFTCOL_CHUNKS, &chunk_table_obj);
+       ret = get_afhi_of_row(current_aft_row, &afd->afhi);
        if (ret < 0)
-               goto err;
-       ret = mmap_full_file(path, O_RDONLY, &map.data,
-               &map.size, &afd->fd);
+               return ret;
+       d->afhi = afd->afhi;
+       d->afhi.chunk_table = afd->afhi.chunk_table = NULL;
+       ret = osl(osl_open_disk_object(audio_file_table, current_aft_row,
+               AFTCOL_CHUNKS, &chunk_table_obj));
+       if (ret < 0)
+               return ret;
+       ret = mmap_full_file(d->path, O_RDONLY, &map.data, &map.size, &afd->fd);
        if (ret < 0)
                goto err;
        hash_function(map.data, map.size, file_hash);
-       ret = hash_compare(file_hash, aft_hash);
+       ret = hash_compare(file_hash, d->hash);
        para_munmap(map.data, map.size);
        if (ret) {
                ret = -E_HASH_MISMATCH;
                goto err;
        }
-       new_afsi = old_afsi;
+       new_afsi = d->afsi;
        new_afsi.num_played++;
        new_afsi.last_played = time(NULL);
        save_afsi(&new_afsi, &afsi_obj); /* in-place update */
 
-       afd->audio_format_id = old_afsi.audio_format_id;
+       afd->audio_format_id = d->afsi.audio_format_id;
        load_chunk_table(&afd->afhi, chunk_table_obj.data);
-       ret = make_status_items(afd, &old_afsi, path, score, file_hash);
-       if (ret < 0)
-               goto err;
-       aced.aft_row = aft_row;
-       aced.old_afsi = &old_afsi;
+       aced.aft_row = current_aft_row;
+       aced.old_afsi = &d->afsi;
+       /*
+        * No need to update the status items as the AFSI_CHANGE event will
+        * recreate them.
+        */
        afs_event(AFSI_CHANGE, NULL, &aced);
        ret = save_afd(afd);
 err:
        free(afd->afhi.chunk_table);
        osl_close_disk_object(&chunk_table_obj);
-       if (ret < 0)
-               PARA_ERROR_LOG("%s: %s\n", path, para_strerror(-ret));
+       if (ret < 0) {
+               PARA_ERROR_LOG("%s: %s\n", d->path, para_strerror(-ret));
+               ret = score_delete(current_aft_row);
+               if (ret >= 0)
+                       goto again;
+       }
        return ret;
 }
 
@@ -1718,10 +1731,11 @@ static void com_add_callback(int fd, const struct osl_object *query)
                struct osl_object obj;
                if (pb) { /* hs trumps pb, remove pb */
                        if (flags & ADD_FLAG_VERBOSE) {
-                               ret = para_printf(&msg, "removing path brother\n");
+                               ret = para_printf(&msg, "removing %s\n", path);
                                if (ret < 0)
                                        goto out;
                        }
+                       afs_event(AUDIO_FILE_REMOVE, &msg, pb);
                        ret = osl(osl_del_row(audio_file_table, pb));
                        if (ret < 0)
                                goto out;
@@ -2006,7 +2020,6 @@ int com_add(struct command_context *cc)
                free(path);
        }
        return 1;
-
 }
 
 /**
@@ -2408,13 +2421,12 @@ static void com_cpsi_callback(int fd, const struct osl_object *query)
 out:
        if (ret < 0)
                para_printf(&cad.pb, "%s\n", para_strerror(-ret));
-       else if (cad.flags & CPSI_FLAG_VERBOSE) {
-               if (pmd.num_matches > 0)
+       else if (pmd.num_matches > 0) {
+               if (cad.flags & CPSI_FLAG_VERBOSE)
                        para_printf(&cad.pb, "copied requested afsi from %s "
                                "to %u files\n", source_path, pmd.num_matches);
-               else
-                       para_printf(&cad.pb, "nothing copied\n");
-       }
+       } else
+               para_printf(&cad.pb, "no matches - nothing copied\n");
        if (cad.pb.offset)
                pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
        free(cad.pb.buf);
@@ -2596,7 +2608,7 @@ static void afs_stat_callback(int fd, const struct osl_object *query)
  * is used to pass the status items from the afs process to the command handler
  * via a shared memory area and a pipe.
  *
- * \return The return value of the underyling call to \ref send_callback_request().
+ * \return The return value of the underlying call to \ref send_callback_request().
  */
 int send_afs_status(struct command_context *cc, int parser_friendly)
 {
@@ -2747,7 +2759,7 @@ static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
 {
        int ret;
 
-       switch(event) {
+       switch (event) {
        case ATTRIBUTE_REMOVE: {
                const struct rmatt_event_data *red = data;
                ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
@@ -2756,9 +2768,27 @@ static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
                if (ret < 0)
                        return ret;
                return audio_file_loop(data, clear_attribute);
-               }
-       default:
+       } case AFSI_CHANGE: {
+               struct afsi_change_event_data *aced = data;
+               uint64_t old_last_played = status_item_ls_data.afsi.last_played;
+               if (aced->aft_row != current_aft_row)
+                       return 0;
+               ret = get_afsi_of_row(aced->aft_row, &status_item_ls_data.afsi);
+               if (ret < 0)
+                       return ret;
+               status_item_ls_data.afsi.last_played = old_last_played;
+               make_status_items();
+               return 1;
+       } case AFHI_CHANGE: {
+               if (data != current_aft_row)
+                       return 0;
+               ret = get_afhi_of_row(data, &status_item_ls_data.afhi);
+               if (ret < 0)
+                       return ret;
+               make_status_items();
                return 1;
+       } default:
+               return 0;
        }
 }