afs: Simplify open_next_audio_file().
[paraslash.git] / aft.c
diff --git a/aft.c b/aft.c
index a73537c74446b3cdb49e405f7918701d837a8e8f..8a633298ead4d05d6c11b8a7a429b0ec7974ed80 100644 (file)
--- a/aft.c
+++ b/aft.c
@@ -1080,10 +1080,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
@@ -1092,17 +1090,22 @@ 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)
 {
+       struct osl_row *aft_row;
+       long score;
        unsigned char *aft_hash, 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);
+       int ret;
        struct afsi_change_event_data aced;
        struct osl_object map, chunk_table_obj;
        char *path;
-
+again:
+       ret = score_get_best(&aft_row, &score);
+       if (ret < 0)
+               return ret;
+       ret = get_hash_of_row(aft_row, &aft_hash);
        if (ret < 0)
                return ret;
        ret = get_audio_file_path_of_row(aft_row, &path);
@@ -1150,8 +1153,12 @@ int open_and_update_audio_file(struct osl_row *aft_row, long score,
 err:
        free(afd->afhi.chunk_table);
        osl_close_disk_object(&chunk_table_obj);
-       if (ret < 0)
+       if (ret < 0) {
                PARA_ERROR_LOG("%s: %s\n", path, para_strerror(-ret));
+               ret = score_delete(aft_row);
+               if (ret >= 0)
+                       goto again;
+       }
        return ret;
 }