8 int mp3_get_file_info(char *map, size_t numbytes,
9 struct audio_format_info *afi); /* FXIME */
11 #define AFS_AUDIO_FILE_DIR "/home/mp3"
13 static void *audio_file_table;
16 * Describes the structure of the mmapped-afs info struct.
18 * \sa struct afs_info.
21 /** Where .last_played is stored. */
22 AFSI_LAST_PLAYED_OFFSET = 0,
23 /** Storage position of the attributes bitmap. */
24 AFSI_ATTRIBUTES_OFFSET = 8,
25 /** Storage position of the .num_played field. */
26 AFSI_NUM_PLAYED_OFFSET = 16,
27 /** Storage position of the .image_id field. */
28 AFSI_IMAGE_ID_OFFSET = 20,
29 /** Storage position of the .lyrics_id field. */
30 AFSI_LYRICS_ID_OFFSET = 24,
31 /** Storage position of the .audio_format_id field. */
32 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
33 /** On-disk storage space needed. */
38 * Convert a struct afs_info to an osl object.
40 * \param afsi Pointer to the audio file info to be converted.
41 * \param obj Result pointer.
45 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
47 struct afs_info default_afs_info = {
48 .last_played = time(NULL) - 365 * 24 * 60 * 60,
53 .audio_format_id = 5, /* FIXME */
55 char *buf = obj->data;
58 afsi = &default_afs_info;
60 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
61 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
62 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
63 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
64 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
65 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
66 afsi->audio_format_id);
70 * Get the audio file selector info struct stored in an osl object.
72 * \param afsi Points to the audio_file info structure to be filled in.
73 * \param obj The osl object holding the data.
75 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
79 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
81 char *buf = obj->data;
82 if (obj->size < AFSI_SIZE)
84 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
85 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
86 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
87 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
88 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
89 afsi->audio_format_id = read_u8(buf +
90 AFSI_AUDIO_FORMAT_ID_OFFSET);
94 /** The columns of the audio file table. */
95 enum audio_file_table_columns {
96 /** The hash on the content of the audio file. */
98 /** The full path in the filesystem. */
100 /** The audio file selector info. */
102 /** The audio format handler info. */
104 /** The chunk table info and the chunk table of the audio file. */
106 /** The number of columns of this table. */
110 static struct osl_column_description aft_cols[] = {
112 .storage_type = OSL_MAPPED_STORAGE,
113 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
115 .compare_function = osl_hash_compare,
116 .data_size = HASH_SIZE
119 .storage_type = OSL_MAPPED_STORAGE,
120 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
122 .compare_function = string_compare,
125 .storage_type = OSL_MAPPED_STORAGE,
126 .storage_flags = OSL_FIXED_SIZE,
128 .data_size = AFSI_SIZE
131 .storage_type = OSL_MAPPED_STORAGE,
135 .storage_type = OSL_DISK_STORAGE,
140 static const struct osl_table_description audio_file_table_desc = {
142 .name = "audio_files",
143 .num_columns = NUM_AFT_COLUMNS,
144 .flags = OSL_LARGE_TABLE,
145 .column_descriptions = aft_cols
148 static char *prefix_path(const char *prefix, int len, const char *path)
178 /* Remove last component of the prefix */
183 } while (len && prefix[len-1] != '/');
187 return para_strdup(path);
188 speclen = strlen(path);
189 n = para_malloc(speclen + len + 1);
190 memcpy(n, prefix, len);
191 memcpy(n + len, path, speclen+1);
196 * We fundamentally don't like some paths: we don't want
197 * dot or dot-dot anywhere.
199 * Also, we don't want double slashes or slashes at the
200 * end that can make pathnames ambiguous.
202 static int verify_dotfile(const char *rest)
205 * The first character was '.', but that has already been discarded, we
209 /* "." is not allowed */
214 if (rest[1] == '\0' || rest[1] == '/')
220 static int verify_path(const char *path, char **resolved_path)
222 const char *orig_path = path;
224 const char prefix[] = AFS_AUDIO_FILE_DIR "/";
225 const size_t prefix_len = strlen(prefix);
227 PARA_DEBUG_LOG("path: %s\n", path);
240 if (verify_dotfile(path) > 0)
243 *resolved_path = NULL;
248 if (*orig_path == '/')
249 *resolved_path = prefix_path("", 0, orig_path);
251 *resolved_path = prefix_path(prefix, prefix_len, orig_path);
252 PARA_DEBUG_LOG("resolved: %s\n", *resolved_path);
253 return *resolved_path? 1: -E_BAD_PATH;
257 AFHI_SECONDS_TOTAL_OFFSET = 0,
258 AFHI_BITRATE_OFFSET = 4,
259 AFHI_FREQUENCY_OFFSET = 8,
260 AFHI_CHANNELS_OFFSET = 12,
261 AFHI_INFO_STRING_OFFSET = 13,
265 static unsigned sizeof_afhi_buf(const struct audio_format_info *afhi)
269 return strlen(afhi->info_string) + MIN_AFHI_SIZE;
272 static void save_afhi(struct audio_format_info *afhi, char *buf)
276 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET,
277 afhi->seconds_total);
278 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
279 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
280 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
281 strcpy(buf + AFHI_INFO_STRING_OFFSET, afhi->info_string); /* OK */
282 PARA_DEBUG_LOG("last byte written: %p\n", buf + AFHI_INFO_STRING_OFFSET + strlen(afhi->info_string));
285 static void load_afhi(const char *buf, struct audio_format_info *afhi)
287 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
288 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
289 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
290 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
291 strcpy(afhi->info_string, buf + AFHI_INFO_STRING_OFFSET);
294 static unsigned sizeof_chunk_info_buf(struct audio_format_info *afhi)
298 return 4 * afhi->chunks_total + 20;
302 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
303 enum chunk_info_offsets {
304 /** The total number of chunks (4 bytes). */
305 CHUNKS_TOTAL_OFFSET = 0,
306 /** The length of the audio file header (4 bytes). */
307 HEADER_LEN_OFFSET = 4,
308 /** The start of the audio file header (4 bytes). */
309 HEADER_OFFSET_OFFSET = 8,
310 /** The seconds part of the chunk time (4 bytes). */
311 CHUNK_TV_TV_SEC_OFFSET = 12,
312 /** The microseconds part of the chunk time (4 bytes). */
313 CHUNK_TV_TV_USEC = 16,
314 /** Chunk table entries start here. */
315 CHUNK_TABLE_OFFSET = 20,
318 /* TODO: audio format handlers could just produce this */
319 static void save_chunk_info(struct audio_format_info *afhi, char *buf)
325 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
326 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
327 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
328 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
329 write_u32(buf + CHUNK_TV_TV_USEC, afhi->chunk_tv.tv_usec);
330 for (i = 0; i < afhi->chunks_total; i++)
331 write_u32(buf + CHUNK_TABLE_OFFSET + 4 * i, afhi->chunk_table[i]);
334 static int load_chunk_info(struct osl_object *obj, struct audio_format_info *afhi)
336 char *buf = obj->data;
339 if (obj->size < CHUNK_TABLE_OFFSET)
340 return -E_BAD_DATA_SIZE;
342 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
343 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
344 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
345 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
346 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC);
348 if (afhi->chunks_total * 4 + CHUNK_TABLE_OFFSET > obj->size)
349 return -E_BAD_DATA_SIZE;
350 afhi->chunk_table = para_malloc(afhi->chunks_total * sizeof(size_t));
351 for (i = 0; i < afhi->chunks_total; i++)
352 afhi->chunk_table[i] = read_u32(buf + CHUNK_TABLE_OFFSET + 4 * i);
357 * Get the row of the audio file table corresponding to the given path.
359 * \param path The full path of the audio file.
360 * \param row Result pointer.
362 * \return The return value of the underlying call to osl_get_row().
364 int aft_get_row_of_path(char *path, struct osl_row **row)
366 struct osl_object obj = {.data = path, .size = strlen(path) + 1};
367 return osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row);
371 * Get the row of the audio file table corresponding to the given hash value.
373 * \param hash The hash value of the desired audio file.
374 * \param row resul pointer.
376 * \return The return value of the underlying call to osl_get_row().
378 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
380 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
381 return osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row);
385 * Get the osl object holding the audio file selector info of a row.
387 * \param row Pointer to a row in the audio file table.
388 * \param obj Result pointer.
390 * \return The return value of the underlying call to osl_get_object().
392 int get_afsi_object_of_row(const void *row, struct osl_object *obj)
394 return osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj);
398 * Get the osl object holding the audio file selector info, given a path.
401 * \param path The full path of the audio file.
402 * \param obj Result pointer.
404 * \return Positive on success, negative on errors.
406 int get_afsi_object_of_path(char *path, struct osl_object *obj)
409 int ret = aft_get_row_of_path(path, &row);
412 return get_afsi_object_of_row(row, obj);
416 * Get the audio file selector info, given a row of the audio file table.
418 * \param row Pointer to a row in the audio file table.
419 * \param afsi Result pointer.
421 * \return Positive on success, negative on errors.
423 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
425 struct osl_object obj;
426 int ret = get_afsi_object_of_row(row, &obj);
429 return load_afsi(afsi, &obj);
433 * Get the path of an audio file, given a row of the audio file table.
435 * \param row Pointer to a row in the audio file table.
436 * \param path Result pointer.
438 * \return Positive on success, negative on errors.
440 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
442 struct osl_object path_obj;
443 int ret = osl_get_object(audio_file_table, row, AFTCOL_PATH,
447 *path = path_obj.data;
452 * Get the object containing the hash value of an audio file, given a row.
454 * \param row Pointer to a row of the audio file table.
455 * \param obj Result pointer.
457 * \return The return value of the underlying call to osl_get_object().
459 * \sa get_hash_of_row().
461 int get_hash_object_of_aft_row(const void *row, struct osl_object *obj)
463 return osl_get_object(audio_file_table, row, AFTCOL_HASH, obj);
467 * Get the hash value of an audio file, given a row of the audio file table.
469 * \param row Pointer to a row of the audio file table.
470 * \param hash Result pointer.
472 * \a hash points to mapped data and must not be freed by the caller.
474 * \return The return value of the underlying call to
475 * get_hash_object_of_aft_row().
477 static int get_hash_of_row(const void *row, HASH_TYPE **hash)
479 struct osl_object obj;
480 int ret = get_hash_object_of_aft_row(row, &obj);
489 * Get the audio format handler info, given a row of the audio file table.
491 * \param row Pointer to a row of the audio file table.
492 * \param afhi Result pointer.
494 * \return The return value of the underlying call to osl_get_object().
496 * \sa get_chunk_table_of_row().
498 int get_afhi_of_row(const void *row, struct audio_format_info *afhi)
500 struct osl_object obj;
501 int ret = osl_get_object(audio_file_table, row, AFTCOL_AFHI,
505 load_afhi(obj.data, afhi);
510 * Get the chunk table of an audio file, given a row of the audio file table.
512 * \param row Pointer to a row of the audio file table.
513 * \param afhi Result pointer.
515 * \return The return value of the underlying call to osl_open_disk_object().
517 * \sa get_afhi_of_row().
519 int get_chunk_table_of_row(const void *row, struct audio_format_info *afhi)
521 struct osl_object obj;
522 int ret = osl_open_disk_object(audio_file_table, row, AFTCOL_CHUNKS,
526 ret = load_chunk_info(&obj, afhi);
527 osl_close_disk_object(&obj);
532 * Mmap the given audio file and update statistics.
534 * \param aft_row Determines the audio file to be opened and updated.
535 * \param afd Result pointer.
537 * On success, the numplayed field of the audio file selector info is increased
538 * and the lastplayed time is set to the current time. Finally, the score of
539 * the audio file is updated.
541 * \return Positive on success, negative on errors.
543 int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd)
545 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
546 struct osl_object afsi_obj;
547 struct afs_info new_afsi;
548 int ret = get_hash_of_row(aft_row, &aft_hash);
552 ret = get_audio_file_path_of_row(aft_row, &afd->path);
555 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
558 ret = load_afsi(&afd->afsi, &afsi_obj);
561 ret = get_afhi_of_row(aft_row, &afd->afhi);
564 ret = get_chunk_table_of_row(aft_row, &afd->afhi);
567 ret = mmap_full_file(afd->path, O_RDONLY, &afd->map);
570 hash_function(afd->map.data, afd->map.size, file_hash);
571 ret = -E_HASH_MISMATCH;
572 if (hash_compare(file_hash, aft_hash))
574 new_afsi = afd->afsi;
575 new_afsi.num_played++;
576 new_afsi.last_played = time(NULL);
577 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
578 if (afd->current_play_mode == PLAY_MODE_PLAYLIST)
579 ret = playlist_update_audio_file(aft_row);
581 ret = mood_update_audio_file(aft_row, &afd->afsi);
584 free(afd->afhi.chunk_table);
590 static int get_local_time(uint64_t *seconds, char *buf, size_t size)
594 if (!localtime_r((time_t *)seconds, &t))
596 if (*seconds + 6 * 30 * 24 * 3600 > now) {
597 if (!strftime(buf, size, "%b %e %k:%M", &t))
601 if (!strftime(buf, size, "%b %e %Y", &t))
606 #define GET_NUM_DIGITS(x, num) { \
607 typeof((x)) _tmp = PARA_ABS(x); \
610 while ((_tmp) > 9) { \
616 static short unsigned get_duration(int seconds_total, char *buf, short unsigned max_width)
618 short unsigned width;
619 int s = seconds_total;
620 unsigned hours = s / 3600, mins = (s % 3600) / 60, secs = s % 60;
622 if (s < 3600) { /* less than one hour => m:ss or mm:ss */
623 GET_NUM_DIGITS(mins, &width); /* 1 or 2 */
624 width += 3; /* 4 or 5 */
626 sprintf(buf, "%*u:%02u", max_width - width + 1, mins, secs);
629 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
630 GET_NUM_DIGITS(hours, &width);
633 sprintf(buf, "%*u:%02u:%02u", max_width - width + 1, hours, mins, secs);
637 static char *make_attribute_line(const char *att_bitmap, struct afs_info *afsi)
639 char *att_text, *att_line;
641 get_attribute_text(&afsi->attributes, " ", &att_text);
643 return para_strdup(att_bitmap);
644 att_line = make_message("%s (%s)", att_bitmap, att_text);
649 static char *make_lyrics_line(struct afs_info *afsi)
652 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
654 return make_message("%u", afsi->lyrics_id);
655 return make_message("%u (%s)", afsi->lyrics_id, lyrics_name);
658 static char *make_image_line(struct afs_info *afsi)
661 img_get_name_by_id(afsi->image_id, &image_name);
663 return make_message("%u", afsi->image_id);
664 return make_message("%u (%s)", afsi->image_id, image_name);
667 static int print_list_item(struct ls_data *d, struct ls_options *opts,
668 struct para_buffer *b)
672 char last_played_time[30];
673 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
674 char score_buf[30] = "";
675 struct afs_info *afsi = &d->afsi;
676 struct audio_format_info *afhi = &d->afhi;
677 struct ls_widths *w = &opts->widths;
678 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
680 if (opts->mode == LS_MODE_SHORT) {
681 para_printf(b, "%s\n", d->path);
684 get_attribute_bitmap(&afsi->attributes, att_buf);
685 ret = get_local_time(&afsi->last_played, last_played_time,
686 sizeof(last_played_time));
689 get_duration(afhi->seconds_total, duration_buf, w->duration_width);
691 if (opts->mode == LS_MODE_LONG)
692 sprintf(score_buf, "%*li ", w->score_width, d->score);
694 sprintf(score_buf, "%li ", d->score);
697 if (opts->mode == LS_MODE_LONG) {
700 "%s " /* attributes */
701 "%*d " /* image_id */
702 "%*d " /* lyrics_id */
704 "%s " /* audio format */
705 "%*d " /* frequency */
708 "%*d " /* num_played */
709 "%s " /* last_played */
713 w->image_id_width, afsi->image_id,
714 w->lyrics_id_width, afsi->lyrics_id,
715 w->bitrate_width, afhi->bitrate,
717 w->frequency_width, afhi->frequency,
720 w->num_played_width, afsi->num_played,
726 if (opts->mode == LS_MODE_VERBOSE) {
727 HASH_TYPE asc_hash[2 * HASH_SIZE + 1];
728 char *att_line, *lyrics_line, *image_line;
730 hash_to_asc(d->hash, asc_hash);
731 att_line = make_attribute_line(att_buf, afsi);
732 lyrics_line = make_lyrics_line(afsi);
733 image_line = make_image_line(afsi);
735 "%s: %s\n" /* path */
741 "bitrate: %dkbit/s\n"
747 "last_played: %s\n\n",
748 (opts->flags & LS_FLAG_FULL_PATH)?
749 "path" : "file", d->path,
750 have_score? "score: " : "", score_buf,
751 have_score? "\n" : "",
772 static int ls_audio_format_compare(const void *a, const void *b)
774 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
775 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
778 static int ls_duration_compare(const void *a, const void *b)
780 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
781 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
784 static int ls_bitrate_compare(const void *a, const void *b)
786 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
787 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
790 static int ls_lyrics_id_compare(const void *a, const void *b)
792 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
793 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
796 static int ls_image_id_compare(const void *a, const void *b)
798 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
799 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
802 static int ls_channels_compare(const void *a, const void *b)
804 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
805 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
808 static int ls_frequency_compare(const void *a, const void *b)
810 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
811 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
814 static int ls_num_played_compare(const void *a, const void *b)
816 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
817 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
820 static int ls_last_played_compare(const void *a, const void *b)
822 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
823 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
826 static int ls_score_compare(const void *a, const void *b)
828 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
829 return NUM_COMPARE(d1->score, d2->score);
832 static int ls_path_compare(const void *a, const void *b)
834 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
835 return strcmp(d1->path, d2->path);
838 static int sort_matching_paths(struct ls_options *options)
840 size_t nmemb = options->num_matching_paths;
841 size_t size = sizeof(uint32_t);
842 int (*compar)(const void *, const void *);
845 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
846 for (i = 0; i < nmemb; i++)
847 options->data_ptr[i] = options->data + i;
849 /* In these cases the array is already sorted */
850 if (options->sorting == LS_SORT_BY_PATH
851 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
852 && (options->flags & LS_FLAG_FULL_PATH))
854 if (options->sorting == LS_SORT_BY_SCORE &&
855 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
858 switch (options->sorting) {
859 case LS_SORT_BY_PATH:
860 compar = ls_path_compare; break;
861 case LS_SORT_BY_SCORE:
862 compar = ls_score_compare; break;
863 case LS_SORT_BY_LAST_PLAYED:
864 compar = ls_last_played_compare; break;
865 case LS_SORT_BY_NUM_PLAYED:
866 compar = ls_num_played_compare; break;
867 case LS_SORT_BY_FREQUENCY:
868 compar = ls_frequency_compare; break;
869 case LS_SORT_BY_CHANNELS:
870 compar = ls_channels_compare; break;
871 case LS_SORT_BY_IMAGE_ID:
872 compar = ls_image_id_compare; break;
873 case LS_SORT_BY_LYRICS_ID:
874 compar = ls_lyrics_id_compare; break;
875 case LS_SORT_BY_BITRATE:
876 compar = ls_bitrate_compare; break;
877 case LS_SORT_BY_DURATION:
878 compar = ls_duration_compare; break;
879 case LS_SORT_BY_AUDIO_FORMAT:
880 compar = ls_audio_format_compare; break;
884 qsort(options->data_ptr, nmemb, size, compar);
888 /* row is either an aft_row or a row of the score table */
889 /* TODO: Only compute widths if we need them */
890 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
893 struct ls_options *options = ls_opts;
896 unsigned short num_digits;
898 struct osl_row *aft_row;
902 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
903 ret = get_score_and_aft_row(row, &score, &aft_row);
908 ret = get_audio_file_path_of_row(aft_row, &path);
911 if (!(options->flags & LS_FLAG_FULL_PATH)) {
912 char *p = strrchr(path, '/');
916 if (options->num_patterns) {
917 for (i = 0; i < options->num_patterns; i++) {
918 ret = fnmatch(options->patterns[i], path, FNM_PATHNAME);
921 if (ret == FNM_NOMATCH)
925 if (i >= options->num_patterns) /* no match */
928 tmp = options->num_matching_paths++;
929 if (options->num_matching_paths > options->array_size) {
930 options->array_size++;
931 options->array_size *= 2;
932 options->data = para_realloc(options->data, options->array_size
933 * sizeof(*options->data));
935 d = options->data + tmp;
936 ret = get_afsi_of_row(aft_row, &d->afsi);
939 ret = get_afhi_of_row(aft_row, &d->afhi);
943 ret = get_hash_of_row(aft_row, &d->hash);
946 w = &options->widths;
947 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
948 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
949 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
950 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
951 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
952 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
953 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
954 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
955 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
956 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
957 /* just get the number of chars to print this amount of time */
958 tmp = get_duration(d->afhi.seconds_total, NULL, 0);
959 w->duration_width = PARA_MAX(w->duration_width, tmp);
960 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
961 GET_NUM_DIGITS(score, &num_digits);
962 num_digits++; /* add one for the sign (space or "-") */
963 w->score_width = PARA_MAX(w->score_width, num_digits);
969 static int com_ls_callback(const struct osl_object *query,
970 struct osl_object *ls_output)
972 struct ls_options *opts = query->data;
973 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
974 struct para_buffer b = {.buf = NULL, .size = 0};
977 PARA_NOTICE_LOG("%d patterns\n", opts->num_patterns);
978 if (opts->num_patterns) {
979 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
980 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
981 opts->patterns[i] = p;
983 PARA_NOTICE_LOG("pattern %d: %s\n", i, opts->patterns[i]);
986 opts->patterns = NULL;
987 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
988 ret = admissible_file_loop(opts, prepare_ls_row);
990 ret = osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
994 ret = opts->num_patterns? -E_NO_MATCH : 1;
995 if (!opts->num_matching_paths) {
996 PARA_NOTICE_LOG("no match, ret: %d\n", ret);
999 ret = sort_matching_paths(opts);
1002 if (opts->flags & LS_FLAG_REVERSE)
1003 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1004 ret = print_list_item(opts->data_ptr[i], opts, &b);
1009 for (i = 0; i < opts->num_matching_paths; i++) {
1010 ret = print_list_item(opts->data_ptr[i], opts, &b);
1016 ls_output->data = b.buf;
1017 ls_output->size = b.size;
1019 free(opts->data_ptr);
1020 free(opts->patterns);
1025 * TODO: flags -h (sort by hash)
1027 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1028 * full list: list everything, including afsi, afhi, atts as clear text
1031 int com_afs_ls(__a_unused int fd, int argc, const char **argv)
1035 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1036 enum ls_listing_mode mode = LS_MODE_SHORT;
1037 struct ls_options opts = {.patterns = NULL};
1038 struct osl_object query = {.data = &opts, .size = sizeof(opts)},
1041 for (i = 1; i < argc; i++) {
1042 const char *arg = argv[i];
1045 if (!strcmp(arg, "--")) {
1049 if (!strncmp(arg, "-l", 2)) {
1051 mode = LS_MODE_LONG;
1055 return -E_AFT_SYNTAX;
1056 switch(*(arg + 2)) {
1058 mode = LS_MODE_SHORT;
1061 mode = LS_MODE_LONG;
1064 mode = LS_MODE_VERBOSE;
1067 mode = LS_MODE_MBOX;
1070 return -E_AFT_SYNTAX;
1073 if (!strcmp(arg, "-p")) {
1074 flags |= LS_FLAG_FULL_PATH;
1077 if (!strcmp(arg, "-a")) {
1078 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1081 if (!strcmp(arg, "-r")) {
1082 flags |= LS_FLAG_REVERSE;
1085 if (!strncmp(arg, "-s", 2)) {
1086 if (!*(arg + 2) || *(arg + 3))
1087 return -E_AFT_SYNTAX;
1088 switch(*(arg + 2)) {
1090 sort = LS_SORT_BY_PATH;
1092 case 's': /* -ss implies -a */
1093 sort = LS_SORT_BY_SCORE;
1094 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1097 sort = LS_SORT_BY_LAST_PLAYED;
1100 sort = LS_SORT_BY_NUM_PLAYED;
1103 sort = LS_SORT_BY_FREQUENCY;
1106 sort = LS_SORT_BY_CHANNELS;
1109 sort = LS_SORT_BY_IMAGE_ID;
1112 sort = LS_SORT_BY_LYRICS_ID;
1115 sort = LS_SORT_BY_BITRATE;
1118 sort = LS_SORT_BY_DURATION;
1121 sort = LS_SORT_BY_AUDIO_FORMAT;
1124 return -E_AFT_SYNTAX;
1127 return -E_AFT_SYNTAX;
1131 opts.sorting = sort;
1133 opts.num_patterns = argc - i;
1134 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1135 argv + i, com_ls_callback, &ls_output);
1136 if (ret >= 0 && ls_output.data) {
1137 printf("%s\n", (char *)ls_output.data);
1138 free(ls_output.data);
1144 * Call the given function for each file in the audio file table.
1146 * \param private_data An arbitrary data pointer, passed to \a func.
1147 * \param func The custom function to be called.
1149 * \return The return value of the underlying call to osl_rbtree_loop().
1151 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1153 return osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1157 static void *find_hash_sister(HASH_TYPE *hash)
1159 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1160 struct osl_row *row;
1162 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1166 #define AFTROW_HEADER_SIZE 4
1168 enum aft_row_offsets {
1169 AFTROW_AFHI_OFFSET_POS = 0,
1170 AFTROW_CHUNKS_OFFSET_POS = 2,
1171 AFTROW_HASH_OFFSET = AFTROW_HEADER_SIZE,
1172 AFTROW_FLAGS_OFFSET = (AFTROW_HASH_OFFSET + HASH_SIZE),
1173 AFTROW_PATH_OFFSET = (AFTROW_FLAGS_OFFSET + 4)
1176 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1177 * In this case, afhi won't be stored in the buffer */
1178 static void save_audio_file_info(HASH_TYPE *hash, const char *path,
1179 struct audio_format_info *afhi,
1180 uint32_t flags, struct osl_object *obj)
1182 size_t path_len = strlen(path) + 1;
1183 size_t afhi_size = sizeof_afhi_buf(afhi);
1184 size_t size = AFTROW_PATH_OFFSET + path_len + afhi_size
1185 + sizeof_chunk_info_buf(afhi);
1186 char *buf = para_malloc(size);
1189 memcpy(buf + AFTROW_HASH_OFFSET, hash, HASH_SIZE);
1190 write_u32(buf + AFTROW_FLAGS_OFFSET, flags);
1191 strcpy(buf + AFTROW_PATH_OFFSET, path);
1193 pos = AFTROW_PATH_OFFSET + path_len;
1194 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1195 PARA_DEBUG_LOG("last afhi byte: %p, pos %d\n", buf + pos + afhi_size - 1,
1196 pos + afhi_size - 1);
1197 write_u16(buf + AFTROW_AFHI_OFFSET_POS, pos);
1198 save_afhi(afhi, buf + pos);
1201 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1202 write_u16(buf + AFTROW_CHUNKS_OFFSET_POS, pos);
1203 save_chunk_info(afhi, buf + pos);
1204 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1218 AFHI: whether afhi and chunk table are computed and sent
1219 ACTION: table modifications to be performed
1221 +---+----+-----+------+---------------------------------------------------+
1222 | HS | PB | F | AFHI | ACTION
1223 +---+----+-----+------+---------------------------------------------------+
1224 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1225 | | update path, keep afsi
1226 +---+----+-----+------+---------------------------------------------------+
1227 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1228 | | otherwise: remove PB, HS: update path, keep afhi,
1230 +---+----+-----+------+---------------------------------------------------+
1231 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1233 +---+----+-----+------+---------------------------------------------------+
1234 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1235 +---+----+-----+------+---------------------------------------------------+
1236 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1237 | | (force has no effect)
1238 +---+----+-----+------+---------------------------------------------------+
1239 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1240 +---+----+-----+------+---------------------------------------------------+
1241 | N | N | Y | Y | (new file) create new entry (force has no effect)
1242 +---+----+-----+------+---------------------------------------------------+
1243 | N | N | N | Y | (new file) create new entry
1244 +---+----+-----+------+---------------------------------------------------+
1246 afhi <=> force or no HS
1251 #define ADD_FLAG_LAZY 1
1252 #define ADD_FLAG_FORCE 2
1253 #define ADD_FLAG_VERBOSE 4
1255 static int com_add_callback(const struct osl_object *query,
1256 __a_unused struct osl_object *result)
1258 char *buf = query->data, *path;
1259 struct osl_row *pb, *aft_row;
1260 const struct osl_row *hs;
1261 struct osl_object objs[NUM_AFT_COLUMNS];
1263 char asc[2 * HASH_SIZE + 1];
1265 char afsi_buf[AFSI_SIZE];
1266 uint32_t flags = read_u32(buf + AFTROW_FLAGS_OFFSET);
1268 hash = buf + AFTROW_HASH_OFFSET;
1269 hash_to_asc(hash, asc);;
1270 objs[AFTCOL_HASH].data = buf + AFTROW_HASH_OFFSET;
1271 objs[AFTCOL_HASH].size = HASH_SIZE;
1273 path = buf + AFTROW_PATH_OFFSET;
1274 objs[AFTCOL_PATH].data = path;
1275 objs[AFTCOL_PATH].size = strlen(path) + 1;
1277 PARA_DEBUG_LOG("request to add %s with hash %s\n", path, asc);
1278 hs = find_hash_sister(hash);
1279 ret = aft_get_row_of_path(path, &pb);
1280 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1282 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1283 if (flags & ADD_FLAG_VERBOSE)
1284 PARA_NOTICE_LOG("ignoring duplicate %p\n", path);
1287 if (hs && hs != pb) {
1288 struct osl_object obj;
1289 if (pb) { /* hs trumps pb, remove pb */
1290 if (flags & ADD_FLAG_VERBOSE)
1291 PARA_NOTICE_LOG("removing path brother\n");
1292 ret = osl_del_row(audio_file_table, pb);
1297 /* file rename, update hs' path */
1298 ret = osl_get_object(audio_file_table, hs, AFTCOL_PATH, &obj);
1299 if (flags & ADD_FLAG_VERBOSE)
1300 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj.data, path);
1301 ret = osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1302 &objs[AFTCOL_PATH]);
1305 if (!(flags & ADD_FLAG_FORCE))
1308 /* no hs or force mode, child must have sent afhi */
1309 uint16_t afhi_offset = read_u16(buf + AFTROW_AFHI_OFFSET_POS);
1310 uint16_t chunks_offset = read_u16(buf + AFTROW_CHUNKS_OFFSET_POS);
1312 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1313 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1314 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1316 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1317 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1318 if (pb && !hs) { /* update pb's hash */
1319 char old_asc[2 * HASH_SIZE + 1];
1320 HASH_TYPE *old_hash;
1321 ret = get_hash_of_row(pb, &old_hash);
1324 hash_to_asc(old_hash, old_asc);
1325 if (flags & ADD_FLAG_VERBOSE)
1326 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path,
1328 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1329 &objs[AFTCOL_HASH]);
1333 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1334 const void *row = pb? pb : hs;
1335 /* update afhi and chunk_table */
1336 if (flags & ADD_FLAG_VERBOSE)
1337 PARA_NOTICE_LOG("updating audio format handler info (%zd bytes)\n",
1338 objs[AFTCOL_AFHI].size);
1339 ret = osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1340 &objs[AFTCOL_AFHI]);
1343 if (flags & ADD_FLAG_VERBOSE)
1344 PARA_NOTICE_LOG("updating chunk table\n");
1345 ret = osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1346 &objs[AFTCOL_CHUNKS]);
1349 ret = mood_update_audio_file(row, NULL);
1353 /* new entry, use default afsi */
1354 if (flags & ADD_FLAG_VERBOSE)
1355 PARA_NOTICE_LOG("adding %s\n", path);
1356 objs[AFTCOL_AFSI].data = &afsi_buf;
1357 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1358 save_afsi(NULL, &objs[AFTCOL_AFSI]);
1359 ret = osl_add_and_get_row(audio_file_table, objs, &aft_row);
1362 return mood_update_audio_file(aft_row, NULL);
1365 struct private_add_data {
1370 static int add_one_audio_file(const char *arg, const void *private_data)
1373 const struct private_add_data *pad = private_data;
1374 struct audio_format_info afhi, *afhi_ptr = NULL;
1375 struct osl_row *pb, *hs; /* path brother/hash sister */
1376 struct osl_object map, obj = {.data = NULL};
1378 HASH_TYPE hash[HASH_SIZE];
1380 afhi.header_offset = 0;
1381 afhi.header_len = 0;
1382 ret = verify_path(arg, &path);
1385 ret = aft_get_row_of_path(path, &pb);
1386 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1389 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1390 if (pad->flags & ADD_FLAG_VERBOSE)
1391 PARA_NOTICE_LOG("lazy-ignore: %s\n", path);
1394 /* we still want to add this file. Compute its hash and look it up */
1395 ret = mmap_full_file(path, O_RDONLY, &map);
1398 hash_function(map.data, map.size, hash);
1399 hs = find_hash_sister(hash);
1401 * return success if we're pretty sure that we already know this file
1404 if (pb && hs && hs == pb && (!(pad->flags & ADD_FLAG_FORCE))) {
1405 if (pad->flags & ADD_FLAG_VERBOSE)
1406 PARA_NOTICE_LOG("not forcing update: %s\n", path);
1410 * we won't recalculate the audio format info and the chunk table if
1411 * there is a hash sister unless in FORCE mode.
1413 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1414 ret = mp3_get_file_info(map.data, map.size, &afhi);
1416 PARA_WARNING_LOG("audio format of %s not recognized, skipping\n", path);
1422 munmap(map.data, map.size);
1423 save_audio_file_info(hash, path, afhi_ptr, pad->flags, &obj);
1424 /* ask parent to consider this entry for adding */
1425 ret = send_callback_request(com_add_callback, &obj, NULL);
1429 munmap(map.data, map.size);
1434 free(afhi_ptr->chunk_table);
1438 int com_add(int fd, int argc, const char **argv)
1441 struct private_add_data pad = {.fd = fd, .flags = 0};
1442 struct stat statbuf;
1444 for (i = 1; i < argc; i++) {
1445 const char *arg = argv[i];
1448 if (!strcmp(arg, "--")) {
1452 if (!strcmp(arg, "-l")) {
1453 pad.flags |= ADD_FLAG_LAZY;
1456 if (!strcmp(arg, "-f")) {
1457 pad.flags |= ADD_FLAG_FORCE;
1460 if (!strcmp(arg, "-v")) {
1461 pad.flags |= ADD_FLAG_VERBOSE;
1466 return -E_AFT_SYNTAX;
1467 for (; i < argc; i++) {
1468 ret = stat(argv[i], &statbuf);
1471 if (S_ISDIR(statbuf.st_mode)) {
1472 ret = for_each_file_in_dir(argv[i],
1473 add_one_audio_file, &pad);
1478 ret = add_one_audio_file(argv[i], &pad);
1488 struct com_touch_options {
1495 static int com_touch_callback(const struct osl_object *query,
1496 __a_unused struct osl_object *result)
1498 struct com_touch_options *cto = query->data;
1499 char *p = (char *)query->data + sizeof(*cto);
1501 int ret, no_options = cto->num_played < 0 && cto->last_played < 0 &&
1502 cto->lyrics_id < 0 && cto->image_id < 0;
1504 for (;p < (char *)query->data + query->size; p += len + 1) {
1505 struct afs_info old_afsi, new_afsi;
1506 struct osl_object obj;
1507 struct osl_row *row;
1510 ret = aft_get_row_of_path(p, &row);
1513 ret = get_afsi_object_of_row(row, &obj);
1516 ret = load_afsi(&old_afsi, &obj);
1519 new_afsi = old_afsi;
1521 new_afsi.num_played++;
1522 new_afsi.last_played = time(NULL);
1524 if (cto->lyrics_id >= 0)
1525 new_afsi.lyrics_id = cto->lyrics_id;
1526 if (cto->image_id >= 0)
1527 new_afsi.image_id = cto->image_id;
1528 if (cto->num_played >= 0)
1529 new_afsi.num_played = cto->num_played;
1530 if (cto->last_played >= 0)
1531 new_afsi.last_played = cto->last_played;
1533 save_afsi(&new_afsi, &obj); /* in-place update */
1534 ret = mood_update_audio_file(row, &old_afsi);
1541 int com_touch(__a_unused int fd, int argc, const char **argv)
1543 struct com_touch_options cto = {
1549 struct osl_object options = {.data = &cto, .size = sizeof(cto)};
1553 for (i = 1; i < argc; i++) {
1554 const char *arg = argv[i];
1557 if (!strcmp(arg, "--")) {
1561 if (!strncmp(arg, "-n", 2)) {
1562 ret = para_atol(arg + 2, &cto.num_played);
1567 if (!strncmp(arg, "-l", 2)) {
1568 ret = para_atol(arg + 2, &cto.last_played);
1573 if (!strncmp(arg, "-y", 2)) {
1574 ret = para_atol(arg + 2, &cto.lyrics_id);
1579 if (!strncmp(arg, "-i", 2)) {
1580 ret = para_atol(arg + 2, &cto.image_id);
1586 ret = -E_AFT_SYNTAX;
1589 return send_option_arg_callback_request(&options, argc - i,
1590 argv + i, com_touch_callback, NULL);
1595 struct com_rm_options {
1599 static int com_rm_callback(const struct osl_object *query,
1600 __a_unused struct osl_object *result)
1602 struct com_rm_options *cro = query->data;
1603 char *p = (char *)query->data + sizeof(*cro);
1607 for (;p < (char *)query->data + query->size; p += len + 1) {
1608 struct osl_row *row;
1611 ret = aft_get_row_of_path(p, &row);
1614 ret = mood_delete_audio_file(row);
1617 ret = osl_del_row(audio_file_table, row);
1625 * TODO options: -v verbose, -f dont stop if file not found
1626 * -h remove by hash, use fnmatch
1630 int com_afs_rm(__a_unused int fd, int argc, const char **argv)
1632 struct com_rm_options cro = {.flags = 0};
1633 struct osl_object options = {.data = &cro, .size = sizeof(cro)};
1636 for (i = 1; i < argc; i++) {
1637 const char *arg = argv[i];
1640 if (!strcmp(arg, "--")) {
1645 ret = -E_AFT_SYNTAX;
1648 return send_option_arg_callback_request(&options, argc - i,
1649 argv + i, com_rm_callback, NULL);
1655 * Close the audio file table.
1657 * \param flags Ususal flags that are passed to osl_close_table().
1659 * \sa osl_close_table().
1661 void aft_shutdown(enum osl_close_flags flags)
1663 osl_close_table(audio_file_table, flags);
1667 * Open the audio file table.
1669 * \param ti Gets initialized by this function
1671 * \return Positive on success, negative on errors.
1673 * \sa osl_open_table().
1675 int aft_init(struct table_info *ti)
1679 ti->desc = &audio_file_table_desc;
1680 ret = osl_open_table(ti->desc, &ti->table);
1683 audio_file_table = ti->table;
1684 osl_get_num_rows(audio_file_table, &num);
1685 PARA_INFO_LOG("audio file table contains %d files\n", num);
1688 audio_file_table = NULL;
1689 return ret == -E_NOENT? 1 : ret;