-/**
- * Mmap the given audio file and update statistics.
- *
- * \param aft_row Determines the audio file to be opened and updated.
- * \param afd Result pointer.
- *
- * On success, the numplayed field of the audio file selector info is increased
- * and the lastplayed time is set to the current time. Finally, the score of
- * the audio file is updated.
- *
- * \return Positive on success, negative on errors.
- */
-int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd)
-{
- HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
- struct osl_object afsi_obj;
- struct afs_info new_afsi;
- int ret = get_hash_of_row(aft_row, &aft_hash);
- struct afsi_change_event_data aced;
- struct osl_object map, chunk_table_obj;
- char *tmp, *path;
-
- if (ret < 0)
- return ret;
- ret = get_audio_file_path_of_row(aft_row, &path);
- if (ret < 0)
- return ret;
- strncpy(afd->path, path, sizeof(afd->path) - 1);
- afd->path[sizeof(afd->path) - 1] = '\0';
- ret = get_afsi_object_of_row(aft_row, &afsi_obj);
- if (ret < 0)
- return ret;
- ret = load_afsi(&afd->afsi, &afsi_obj);
- if (ret < 0)
- return ret;
- ret = get_afhi_of_row(aft_row, &afd->afhi);
- if (ret < 0)
- return ret;
- ret = osl_open_disk_object(audio_file_table, aft_row,
- AFTCOL_CHUNKS, &chunk_table_obj);
- if (ret < 0)
- return ret;
- ret = mmap_full_file(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);
- para_munmap(map.data, map.size);
- if (ret) {
- ret = -E_HASH_MISMATCH;
- goto err;
- }
- new_afsi = afd->afsi;
- new_afsi.num_played++;
- new_afsi.last_played = time(NULL);
- save_afsi(&new_afsi, &afsi_obj); /* in-place update */
-
- ret = load_chunk_info(&chunk_table_obj, &afd->afhi);
- if (ret < 0)
- goto err;
- ret = get_attribute_text(&afd->afsi.attributes, " ", &tmp);
- if (ret < 0)
- goto err;
- assert(tmp);
- strncpy(afd->attributes_string, tmp, sizeof(afd->attributes_string));
- afd->attributes_string[sizeof(afd->attributes_string) - 1] = '\0';
- free(tmp);
-
- {
- struct ls_data d = {
- .afhi = afd->afhi,
- .afsi = afd->afsi,
- .path = path,
- .score = afd->score,
- .hash = file_hash
- };
- struct para_buffer pb = {.buf = NULL};
- ret = make_status_items(&d, &pb);
- if (ret < 0)
- goto err;
- strncpy(afd->afs_status_info, pb.buf, AFS_STATUS_INFO_SIZE);
- afd->afs_status_info[AFS_STATUS_INFO_SIZE] = '\0';
- free(pb.buf);
- }
- aced.aft_row = aft_row;
- aced.old_afsi = &afd->afsi;
- afs_event(AFSI_CHANGE, NULL, &aced);
- ret = save_afd(afd);
- if (ret < 0)
- goto err;
- free(afd->afhi.chunk_table);
-err:
- osl_close_disk_object(&chunk_table_obj);
- return ret;
-}
-