2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
19 #define AFS_AUDIO_FILE_DIR "/home/mp3"
21 static struct osl_table *audio_file_table;
23 enum ls_listing_mode {
31 LS_FLAG_FULL_PATH = 1,
32 LS_FLAG_ADMISSIBLE_ONLY = 2,
37 unsigned short score_width;
38 unsigned short image_id_width;
39 unsigned short lyrics_id_width;
40 unsigned short bitrate_width;
41 unsigned short frequency_width;
42 unsigned short duration_width;
43 unsigned short num_played_width;
47 struct audio_format_info afhi;
56 enum ls_sorting_method sorting;
57 enum ls_listing_mode mode;
60 struct ls_widths widths;
62 uint32_t num_matching_paths;
64 struct ls_data **data_ptr;
68 * Describes the layout of the mmapped-afs info struct.
70 * \sa struct afs_info.
73 /** Where .last_played is stored. */
74 AFSI_LAST_PLAYED_OFFSET = 0,
75 /** Storage position of the attributes bitmap. */
76 AFSI_ATTRIBUTES_OFFSET = 8,
77 /** Storage position of the .num_played field. */
78 AFSI_NUM_PLAYED_OFFSET = 16,
79 /** Storage position of the .image_id field. */
80 AFSI_IMAGE_ID_OFFSET = 20,
81 /** Storage position of the .lyrics_id field. */
82 AFSI_LYRICS_ID_OFFSET = 24,
83 /** Storage position of the .audio_format_id field. */
84 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
85 /** On-disk storage space needed. */
90 * Convert a struct afs_info to an osl object.
92 * \param afsi Pointer to the audio file info to be converted.
93 * \param obj Result pointer.
97 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
99 char *buf = obj->data;
101 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
102 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
103 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
104 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
105 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
106 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
107 afsi->audio_format_id);
111 * Get the audio file selector info struct stored in an osl object.
113 * \param afsi Points to the audio_file info structure to be filled in.
114 * \param obj The osl object holding the data.
116 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
120 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
122 char *buf = obj->data;
123 if (obj->size < AFSI_SIZE)
125 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
126 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
127 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
128 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
129 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
130 afsi->audio_format_id = read_u8(buf +
131 AFSI_AUDIO_FORMAT_ID_OFFSET);
135 /** The columns of the audio file table. */
136 enum audio_file_table_columns {
137 /** The hash on the content of the audio file. */
139 /** The full path in the filesystem. */
141 /** The audio file selector info. */
143 /** The audio format handler info. */
145 /** The chunk table info and the chunk table of the audio file. */
147 /** The number of columns of this table. */
151 static struct osl_column_description aft_cols[] = {
153 .storage_type = OSL_MAPPED_STORAGE,
154 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
156 .compare_function = osl_hash_compare,
157 .data_size = HASH_SIZE
160 .storage_type = OSL_MAPPED_STORAGE,
161 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
163 .compare_function = string_compare,
166 .storage_type = OSL_MAPPED_STORAGE,
167 .storage_flags = OSL_FIXED_SIZE,
169 .data_size = AFSI_SIZE
172 .storage_type = OSL_MAPPED_STORAGE,
176 .storage_type = OSL_DISK_STORAGE,
181 static struct osl_table_description audio_file_table_desc = {
182 .name = "audio_files",
183 .num_columns = NUM_AFT_COLUMNS,
184 .flags = OSL_LARGE_TABLE,
185 .column_descriptions = aft_cols
188 static char *prefix_path(const char *prefix, int len, const char *path)
218 /* Remove last component of the prefix */
223 } while (len && prefix[len-1] != '/');
227 return para_strdup(path);
228 speclen = strlen(path);
229 n = para_malloc(speclen + len + 1);
230 memcpy(n, prefix, len);
231 memcpy(n + len, path, speclen+1);
236 * We fundamentally don't like some paths: we don't want
237 * dot or dot-dot anywhere.
239 * Also, we don't want double slashes or slashes at the
240 * end that can make pathnames ambiguous.
242 static int verify_dotfile(const char *rest)
245 * The first character was '.', but that has already been discarded, we
249 /* "." is not allowed */
254 if (rest[1] == '\0' || rest[1] == '/')
260 static int verify_path(const char *orig_path, char **resolved_path)
263 const char prefix[] = AFS_AUDIO_FILE_DIR "/";
264 const char *path = orig_path;
265 const size_t prefix_len = strlen(prefix);
276 case '/': /* double slash */
279 if (verify_dotfile(path) < 0)
285 if (*orig_path != '/')
286 *resolved_path = prefix_path(prefix, prefix_len, orig_path);
288 *resolved_path = para_strdup(orig_path);
295 AFHI_SECONDS_TOTAL_OFFSET = 0,
296 AFHI_BITRATE_OFFSET = 4,
297 AFHI_FREQUENCY_OFFSET = 8,
298 AFHI_CHANNELS_OFFSET = 12,
299 AFHI_INFO_STRING_OFFSET = 13,
303 static unsigned sizeof_afhi_buf(const struct audio_format_info *afhi)
307 return strlen(afhi->info_string) + MIN_AFHI_SIZE;
310 static void save_afhi(struct audio_format_info *afhi, char *buf)
314 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET,
315 afhi->seconds_total);
316 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
317 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
318 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
319 strcpy(buf + AFHI_INFO_STRING_OFFSET, afhi->info_string); /* OK */
320 PARA_DEBUG_LOG("last byte written: %p\n", buf + AFHI_INFO_STRING_OFFSET + strlen(afhi->info_string));
323 static void load_afhi(const char *buf, struct audio_format_info *afhi)
325 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
326 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
327 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
328 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
329 strcpy(afhi->info_string, buf + AFHI_INFO_STRING_OFFSET);
332 static unsigned sizeof_chunk_info_buf(struct audio_format_info *afhi)
336 return 4 * afhi->chunks_total + 20;
340 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
341 enum chunk_info_offsets{
342 /** The total number of chunks (4 bytes). */
343 CHUNKS_TOTAL_OFFSET = 0,
344 /** The length of the audio file header (4 bytes). */
345 HEADER_LEN_OFFSET = 4,
346 /** The start of the audio file header (4 bytes). */
347 HEADER_OFFSET_OFFSET = 8,
348 /** The seconds part of the chunk time (4 bytes). */
349 CHUNK_TV_TV_SEC_OFFSET = 12,
350 /** The microseconds part of the chunk time (4 bytes). */
351 CHUNK_TV_TV_USEC = 16,
352 /** Chunk table entries start here. */
353 CHUNK_TABLE_OFFSET = 20,
356 /* TODO: audio format handlers could just produce this */
357 static void save_chunk_info(struct audio_format_info *afhi, char *buf)
363 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
364 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
365 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
366 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
367 write_u32(buf + CHUNK_TV_TV_USEC, afhi->chunk_tv.tv_usec);
368 for (i = 0; i < afhi->chunks_total; i++)
369 write_u32(buf + CHUNK_TABLE_OFFSET + 4 * i, afhi->chunk_table[i]);
372 static int load_chunk_info(struct osl_object *obj, struct audio_format_info *afhi)
374 char *buf = obj->data;
377 if (obj->size < CHUNK_TABLE_OFFSET)
378 return -E_BAD_DATA_SIZE;
380 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
381 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
382 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
383 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
384 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC);
386 if (afhi->chunks_total * 4 + CHUNK_TABLE_OFFSET > obj->size)
387 return -E_BAD_DATA_SIZE;
388 afhi->chunk_table = para_malloc(afhi->chunks_total * sizeof(size_t));
389 for (i = 0; i < afhi->chunks_total; i++)
390 afhi->chunk_table[i] = read_u32(buf + CHUNK_TABLE_OFFSET + 4 * i);
395 * Get the row of the audio file table corresponding to the given path.
397 * \param path The full path of the audio file.
398 * \param row Result pointer.
400 * \return The return value of the underlying call to osl_get_row().
402 int aft_get_row_of_path(char *path, struct osl_row **row)
404 struct osl_object obj = {.data = path, .size = strlen(path) + 1};
406 return osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row);
410 * Get the row of the audio file table corresponding to the given hash value.
412 * \param hash The hash value of the desired audio file.
413 * \param row resul pointer.
415 * \return The return value of the underlying call to osl_get_row().
417 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
419 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
420 return osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row);
424 * Get the osl object holding the audio file selector info of a row.
426 * \param row Pointer to a row in the audio file table.
427 * \param obj Result pointer.
429 * \return The return value of the underlying call to osl_get_object().
431 int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
433 return osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj);
437 * Get the osl object holding the audio file selector info, given a path.
440 * \param path The full path of the audio file.
441 * \param obj Result pointer.
443 * \return Positive on success, negative on errors.
445 int get_afsi_object_of_path(char *path, struct osl_object *obj)
448 int ret = aft_get_row_of_path(path, &row);
451 return get_afsi_object_of_row(row, obj);
455 * Get the audio file selector info, given a row of the audio file table.
457 * \param row Pointer to a row in the audio file table.
458 * \param afsi Result pointer.
460 * \return Positive on success, negative on errors.
462 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
464 struct osl_object obj;
465 int ret = get_afsi_object_of_row(row, &obj);
468 return load_afsi(afsi, &obj);
472 * Get the path of an audio file, given a row of the audio file table.
474 * \param row Pointer to a row in the audio file table.
475 * \param path Result pointer.
477 * \return Positive on success, negative on errors.
479 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
481 struct osl_object path_obj;
482 int ret = osl_get_object(audio_file_table, row, AFTCOL_PATH,
486 *path = path_obj.data;
491 * Get the object containing the hash value of an audio file, given a row.
493 * \param row Pointer to a row of the audio file table.
494 * \param obj Result pointer.
496 * \return The return value of the underlying call to osl_get_object().
498 * \sa get_hash_of_row().
500 static int get_hash_object_of_aft_row(const struct osl_row *row, struct osl_object *obj)
502 return osl_get_object(audio_file_table, row, AFTCOL_HASH, obj);
506 * Get the hash value of an audio file, given a row of the audio file table.
508 * \param row Pointer to a row of the audio file table.
509 * \param hash Result pointer.
511 * \a hash points to mapped data and must not be freed by the caller.
513 * \return The return value of the underlying call to
514 * get_hash_object_of_aft_row().
516 static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash)
518 struct osl_object obj;
519 int ret = get_hash_object_of_aft_row(row, &obj);
528 * Get the audio format handler info, given a row of the audio file table.
530 * \param row Pointer to a row of the audio file table.
531 * \param afhi Result pointer.
533 * \return The return value of the underlying call to osl_get_object().
535 * \sa get_chunk_table_of_row().
537 static int get_afhi_of_row(const struct osl_row *row, struct audio_format_info *afhi)
539 struct osl_object obj;
540 int ret = osl_get_object(audio_file_table, row, AFTCOL_AFHI,
544 load_afhi(obj.data, afhi);
549 * Get the chunk table of an audio file, given a row of the audio file table.
551 * \param row Pointer to a row of the audio file table.
552 * \param afhi Result pointer.
554 * \return The return value of the underlying call to osl_open_disk_object().
556 * \sa get_afhi_of_row().
558 static int get_chunk_table_of_row(const struct osl_row *row, struct audio_format_info *afhi)
560 struct osl_object obj;
561 int ret = osl_open_disk_object(audio_file_table, row, AFTCOL_CHUNKS,
565 ret = load_chunk_info(&obj, afhi);
566 osl_close_disk_object(&obj);
571 * Mmap the given audio file and update statistics.
573 * \param aft_row Determines the audio file to be opened and updated.
574 * \param afd Result pointer.
576 * On success, the numplayed field of the audio file selector info is increased
577 * and the lastplayed time is set to the current time. Finally, the score of
578 * the audio file is updated.
580 * \return Positive on success, negative on errors.
582 int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd)
584 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
585 struct osl_object afsi_obj;
586 struct afs_info new_afsi;
587 int ret = get_hash_of_row(aft_row, &aft_hash);
591 ret = get_audio_file_path_of_row(aft_row, &afd->path);
594 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
597 ret = load_afsi(&afd->afsi, &afsi_obj);
600 ret = get_afhi_of_row(aft_row, &afd->afhi);
603 ret = get_chunk_table_of_row(aft_row, &afd->afhi);
606 ret = mmap_full_file(afd->path, O_RDONLY, &afd->map);
609 hash_function(afd->map.data, afd->map.size, file_hash);
610 ret = -E_HASH_MISMATCH;
611 if (hash_compare(file_hash, aft_hash))
613 new_afsi = afd->afsi;
614 new_afsi.num_played++;
615 new_afsi.last_played = time(NULL);
616 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
617 if (afd->current_play_mode == PLAY_MODE_PLAYLIST)
618 ret = playlist_update_audio_file(aft_row);
620 ret = mood_update_audio_file(aft_row, &afd->afsi);
623 free(afd->afhi.chunk_table);
627 static int get_local_time(uint64_t *seconds, char *buf, size_t size,
632 if (!localtime_r((time_t *)seconds, &t))
634 if (*seconds + 6 * 30 * 24 * 3600 > current_time) {
635 if (!strftime(buf, size, "%b %e %k:%M", &t))
639 if (!strftime(buf, size, "%b %e %Y", &t))
644 #define GET_NUM_DIGITS(x, num) { \
645 typeof((x)) _tmp = PARA_ABS(x); \
648 while ((_tmp) > 9) { \
654 static short unsigned get_duration_width(int seconds)
656 short unsigned width;
657 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
659 if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
660 return 4 + (mins > 9);
661 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
662 GET_NUM_DIGITS(hours, &width);
666 static void get_duration_buf(int seconds, char *buf, short unsigned max_width)
668 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
670 if (!hours) /* m:ss or mm:ss */
671 sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
672 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
673 sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
677 static char *make_attribute_line(const char *att_bitmap, struct afs_info *afsi)
679 char *att_text, *att_line;
681 get_attribute_text(&afsi->attributes, " ", &att_text);
683 return para_strdup(att_bitmap);
684 att_line = make_message("%s (%s)", att_bitmap, att_text);
689 static char *make_lyrics_line(struct afs_info *afsi)
692 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
694 return make_message("%u", afsi->lyrics_id);
695 return make_message("%u (%s)", afsi->lyrics_id, lyrics_name);
698 static char *make_image_line(struct afs_info *afsi)
701 img_get_name_by_id(afsi->image_id, &image_name);
703 return make_message("%u", afsi->image_id);
704 return make_message("%u (%s)", afsi->image_id, image_name);
707 static int print_list_item(struct ls_data *d, struct ls_options *opts,
708 struct para_buffer *b, time_t current_time)
712 char last_played_time[30];
713 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
714 char score_buf[30] = "";
715 struct afs_info *afsi = &d->afsi;
716 struct audio_format_info *afhi = &d->afhi;
717 struct ls_widths *w = &opts->widths;
718 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
720 if (opts->mode == LS_MODE_SHORT) {
721 para_printf(b, "%s\n", d->path);
724 get_attribute_bitmap(&afsi->attributes, att_buf);
725 ret = get_local_time(&afsi->last_played, last_played_time,
726 sizeof(last_played_time), current_time);
729 get_duration_buf(afhi->seconds_total, duration_buf, w->duration_width);
731 if (opts->mode == LS_MODE_LONG)
732 sprintf(score_buf, "%*li ", w->score_width, d->score);
734 sprintf(score_buf, "%li ", d->score);
737 PARA_NOTICE_LOG("id: %s, %d\n", d->path, afsi->audio_format_id);
738 if (opts->mode == LS_MODE_LONG) {
741 "%s " /* attributes */
742 "%*d " /* image_id */
743 "%*d " /* lyrics_id */
745 "%s " /* audio format */
746 "%*d " /* frequency */
749 "%*d " /* num_played */
750 "%s " /* last_played */
754 w->image_id_width, afsi->image_id,
755 w->lyrics_id_width, afsi->lyrics_id,
756 w->bitrate_width, afhi->bitrate,
757 audio_format_name(afsi->audio_format_id),
758 w->frequency_width, afhi->frequency,
761 w->num_played_width, afsi->num_played,
767 if (opts->mode == LS_MODE_VERBOSE) {
768 char asc_hash[2 * HASH_SIZE + 1];
769 char *att_line, *lyrics_line, *image_line;
771 hash_to_asc(d->hash, asc_hash);
772 att_line = make_attribute_line(att_buf, afsi);
773 lyrics_line = make_lyrics_line(afsi);
774 image_line = make_image_line(afsi);
776 "%s: %s\n" /* path */
782 "bitrate: %dkbit/s\n"
788 "last_played: %s\n\n",
789 (opts->flags & LS_FLAG_FULL_PATH)?
790 "path" : "file", d->path,
791 have_score? "score: " : "", score_buf,
792 have_score? "\n" : "",
798 audio_format_name(afsi->audio_format_id),
813 static int ls_audio_format_compare(const void *a, const void *b)
815 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
816 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
819 static int ls_duration_compare(const void *a, const void *b)
821 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
822 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
825 static int ls_bitrate_compare(const void *a, const void *b)
827 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
828 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
831 static int ls_lyrics_id_compare(const void *a, const void *b)
833 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
834 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
837 static int ls_image_id_compare(const void *a, const void *b)
839 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
840 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
843 static int ls_channels_compare(const void *a, const void *b)
845 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
846 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
849 static int ls_frequency_compare(const void *a, const void *b)
851 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
852 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
855 static int ls_num_played_compare(const void *a, const void *b)
857 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
858 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
861 static int ls_last_played_compare(const void *a, const void *b)
863 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
864 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
867 static int ls_score_compare(const void *a, const void *b)
869 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
870 return NUM_COMPARE(d1->score, d2->score);
873 static int ls_path_compare(const void *a, const void *b)
875 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
876 return strcmp(d1->path, d2->path);
879 static int sort_matching_paths(struct ls_options *options)
881 size_t nmemb = options->num_matching_paths;
882 size_t size = sizeof(*options->data_ptr);
883 int (*compar)(const void *, const void *);
886 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
887 for (i = 0; i < nmemb; i++)
888 options->data_ptr[i] = options->data + i;
890 /* In these cases the array is already sorted */
891 if (options->sorting == LS_SORT_BY_PATH
892 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
893 && (options->flags & LS_FLAG_FULL_PATH))
895 if (options->sorting == LS_SORT_BY_SCORE &&
896 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
899 switch (options->sorting) {
900 case LS_SORT_BY_PATH:
901 compar = ls_path_compare; break;
902 case LS_SORT_BY_SCORE:
903 compar = ls_score_compare; break;
904 case LS_SORT_BY_LAST_PLAYED:
905 compar = ls_last_played_compare; break;
906 case LS_SORT_BY_NUM_PLAYED:
907 compar = ls_num_played_compare; break;
908 case LS_SORT_BY_FREQUENCY:
909 compar = ls_frequency_compare; break;
910 case LS_SORT_BY_CHANNELS:
911 compar = ls_channels_compare; break;
912 case LS_SORT_BY_IMAGE_ID:
913 compar = ls_image_id_compare; break;
914 case LS_SORT_BY_LYRICS_ID:
915 compar = ls_lyrics_id_compare; break;
916 case LS_SORT_BY_BITRATE:
917 compar = ls_bitrate_compare; break;
918 case LS_SORT_BY_DURATION:
919 compar = ls_duration_compare; break;
920 case LS_SORT_BY_AUDIO_FORMAT:
921 compar = ls_audio_format_compare; break;
925 qsort(options->data_ptr, nmemb, size, compar);
929 /* row is either an aft_row or a row of the score table */
930 /* TODO: Only compute widths if we need them */
931 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
934 struct ls_options *options = ls_opts;
937 unsigned short num_digits;
939 struct osl_row *aft_row;
943 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
944 ret = get_score_and_aft_row(row, &score, &aft_row);
949 ret = get_audio_file_path_of_row(aft_row, &path);
952 if (!(options->flags & LS_FLAG_FULL_PATH)) {
953 char *p = strrchr(path, '/');
957 if (options->num_patterns) {
958 for (i = 0; i < options->num_patterns; i++) {
959 ret = fnmatch(options->patterns[i], path, FNM_PATHNAME);
962 if (ret == FNM_NOMATCH)
966 if (i >= options->num_patterns) /* no match */
969 tmp = options->num_matching_paths++;
970 if (options->num_matching_paths > options->array_size) {
971 options->array_size++;
972 options->array_size *= 2;
973 options->data = para_realloc(options->data, options->array_size
974 * sizeof(*options->data));
976 d = options->data + tmp;
977 ret = get_afsi_of_row(aft_row, &d->afsi);
980 ret = get_afhi_of_row(aft_row, &d->afhi);
984 ret = get_hash_of_row(aft_row, &d->hash);
987 w = &options->widths;
988 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
989 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
990 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
991 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
992 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
993 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
994 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
995 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
996 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
997 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
998 /* get the number of chars to print this amount of time */
999 tmp = get_duration_width(d->afhi.seconds_total);
1000 w->duration_width = PARA_MAX(w->duration_width, tmp);
1001 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1002 GET_NUM_DIGITS(score, &num_digits);
1003 num_digits++; /* add one for the sign (space or "-") */
1004 w->score_width = PARA_MAX(w->score_width, num_digits);
1010 static int com_ls_callback(const struct osl_object *query,
1011 struct osl_object *ls_output)
1013 struct ls_options *opts = query->data;
1014 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
1015 struct para_buffer b = {.buf = NULL, .size = 0};
1017 time_t current_time;
1020 PARA_NOTICE_LOG("%d patterns\n", opts->num_patterns);
1021 if (opts->num_patterns) {
1022 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
1023 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
1024 opts->patterns[i] = p;
1026 PARA_NOTICE_LOG("pattern %d: %s\n", i, opts->patterns[i]);
1029 opts->patterns = NULL;
1030 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
1031 ret = admissible_file_loop(opts, prepare_ls_row);
1033 ret = osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
1037 ret = opts->num_patterns? -E_NO_MATCH : 0;
1038 if (!opts->num_matching_paths) {
1039 PARA_NOTICE_LOG("no match, ret: %d\n", ret);
1042 ret = sort_matching_paths(opts);
1045 time(¤t_time);
1046 if (opts->flags & LS_FLAG_REVERSE)
1047 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1048 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1053 for (i = 0; i < opts->num_matching_paths; i++) {
1054 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1060 ls_output->data = b.buf;
1061 PARA_NOTICE_LOG("ls_outoute.data: %p\n", ls_output->data);
1062 ls_output->size = b.size;
1064 free(opts->data_ptr);
1065 free(opts->patterns);
1070 * TODO: flags -h (sort by hash) -lm (list in mbox format)
1072 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1073 * full list: list everything, including afsi, afhi, atts as clear text
1076 int com_afs_ls(int fd, int argc, char * const * const argv)
1080 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1081 enum ls_listing_mode mode = LS_MODE_SHORT;
1082 struct ls_options opts = {.patterns = NULL};
1083 struct osl_object query = {.data = &opts, .size = sizeof(opts)},
1086 for (i = 1; i < argc; i++) {
1087 const char *arg = argv[i];
1090 if (!strcmp(arg, "--")) {
1094 if (!strncmp(arg, "-l", 2)) {
1096 mode = LS_MODE_LONG;
1100 return -E_AFT_SYNTAX;
1101 switch(*(arg + 2)) {
1103 mode = LS_MODE_SHORT;
1106 mode = LS_MODE_LONG;
1109 mode = LS_MODE_VERBOSE;
1112 mode = LS_MODE_MBOX;
1115 return -E_AFT_SYNTAX;
1118 if (!strcmp(arg, "-p")) {
1119 flags |= LS_FLAG_FULL_PATH;
1122 if (!strcmp(arg, "-a")) {
1123 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1126 if (!strcmp(arg, "-r")) {
1127 flags |= LS_FLAG_REVERSE;
1130 if (!strncmp(arg, "-s", 2)) {
1131 if (!*(arg + 2) || *(arg + 3))
1132 return -E_AFT_SYNTAX;
1133 switch(*(arg + 2)) {
1135 sort = LS_SORT_BY_PATH;
1137 case 's': /* -ss implies -a */
1138 sort = LS_SORT_BY_SCORE;
1139 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1142 sort = LS_SORT_BY_LAST_PLAYED;
1145 sort = LS_SORT_BY_NUM_PLAYED;
1148 sort = LS_SORT_BY_FREQUENCY;
1151 sort = LS_SORT_BY_CHANNELS;
1154 sort = LS_SORT_BY_IMAGE_ID;
1157 sort = LS_SORT_BY_LYRICS_ID;
1160 sort = LS_SORT_BY_BITRATE;
1163 sort = LS_SORT_BY_DURATION;
1166 sort = LS_SORT_BY_AUDIO_FORMAT;
1169 return -E_AFT_SYNTAX;
1172 return -E_AFT_SYNTAX;
1175 opts.sorting = sort;
1177 opts.num_patterns = argc - i;
1178 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1179 argv + i, com_ls_callback, &ls_output);
1181 ret = send_buffer(fd, (char *)ls_output.data);
1182 free(ls_output.data);
1188 * Call the given function for each file in the audio file table.
1190 * \param private_data An arbitrary data pointer, passed to \a func.
1191 * \param func The custom function to be called.
1193 * \return The return value of the underlying call to osl_rbtree_loop().
1195 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1197 return osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1201 static struct osl_row *find_hash_sister(HASH_TYPE *hash)
1203 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1204 struct osl_row *row;
1206 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1210 enum aft_row_offsets {
1211 AFTROW_AFHI_OFFSET_POS = 0,
1212 AFTROW_CHUNKS_OFFSET_POS = 2,
1213 AFTROW_AUDIO_FORMAT_OFFSET = 4,
1214 AFTROW_FLAGS_OFFSET = 5,
1215 AFTROW_HASH_OFFSET = 9,
1216 AFTROW_PATH_OFFSET = (AFTROW_HASH_OFFSET + HASH_SIZE),
1219 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1220 * In this case, afhi won't be stored in the buffer */
1221 static void save_audio_file_info(HASH_TYPE *hash, const char *path,
1222 struct audio_format_info *afhi, uint32_t flags,
1223 uint8_t audio_format_num, struct osl_object *obj)
1225 size_t path_len = strlen(path) + 1;
1226 size_t afhi_size = sizeof_afhi_buf(afhi);
1227 size_t size = AFTROW_PATH_OFFSET + path_len + afhi_size
1228 + sizeof_chunk_info_buf(afhi);
1229 char *buf = para_malloc(size);
1232 write_u8(buf + AFTROW_AUDIO_FORMAT_OFFSET, audio_format_num);
1233 write_u32(buf + AFTROW_FLAGS_OFFSET, flags);
1235 memcpy(buf + AFTROW_HASH_OFFSET, hash, HASH_SIZE);
1236 strcpy(buf + AFTROW_PATH_OFFSET, path);
1238 pos = AFTROW_PATH_OFFSET + path_len;
1239 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1240 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1241 pos + afhi_size - 1);
1242 write_u16(buf + AFTROW_AFHI_OFFSET_POS, pos);
1243 save_afhi(afhi, buf + pos);
1246 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1247 write_u16(buf + AFTROW_CHUNKS_OFFSET_POS, pos);
1248 save_chunk_info(afhi, buf + pos);
1249 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1263 AFHI: whether afhi and chunk table are computed and sent
1264 ACTION: table modifications to be performed
1266 +---+----+-----+------+---------------------------------------------------+
1267 | HS | PB | F | AFHI | ACTION
1268 +---+----+-----+------+---------------------------------------------------+
1269 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1270 | | update path, keep afsi
1271 +---+----+-----+------+---------------------------------------------------+
1272 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1273 | | otherwise: remove PB, HS: update path, keep afhi,
1275 +---+----+-----+------+---------------------------------------------------+
1276 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1278 +---+----+-----+------+---------------------------------------------------+
1279 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1280 +---+----+-----+------+---------------------------------------------------+
1281 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1282 | | (force has no effect)
1283 +---+----+-----+------+---------------------------------------------------+
1284 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1285 +---+----+-----+------+---------------------------------------------------+
1286 | N | N | Y | Y | (new file) create new entry (force has no effect)
1287 +---+----+-----+------+---------------------------------------------------+
1288 | N | N | N | Y | (new file) create new entry
1289 +---+----+-----+------+---------------------------------------------------+
1291 afhi <=> force or no HS
1296 #define ADD_FLAG_LAZY 1
1297 #define ADD_FLAG_FORCE 2
1298 #define ADD_FLAG_VERBOSE 4
1300 /* TODO: change log messages so that they get written to the result buffer */
1302 static int com_add_callback(const struct osl_object *query,
1303 __a_unused struct osl_object *result)
1305 char *buf = query->data, *path;
1306 struct osl_row *pb, *aft_row;
1307 const struct osl_row *hs;
1308 struct osl_object objs[NUM_AFT_COLUMNS];
1310 char asc[2 * HASH_SIZE + 1];
1312 char afsi_buf[AFSI_SIZE];
1313 uint32_t flags = read_u32(buf + AFTROW_FLAGS_OFFSET);
1314 struct afs_info default_afsi = {.last_played = 0};
1316 hash = (HASH_TYPE *)buf + AFTROW_HASH_OFFSET;
1317 hash_to_asc(hash, asc);;
1318 objs[AFTCOL_HASH].data = buf + AFTROW_HASH_OFFSET;
1319 objs[AFTCOL_HASH].size = HASH_SIZE;
1321 path = buf + AFTROW_PATH_OFFSET;
1322 objs[AFTCOL_PATH].data = path;
1323 objs[AFTCOL_PATH].size = strlen(path) + 1;
1325 PARA_INFO_LOG("request to add %s\n", path);
1326 hs = find_hash_sister(hash);
1327 ret = aft_get_row_of_path(path, &pb);
1328 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1330 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1331 if (flags & ADD_FLAG_VERBOSE)
1332 PARA_NOTICE_LOG("ignoring duplicate %p\n", path);
1335 if (hs && hs != pb) {
1336 struct osl_object obj;
1337 if (pb) { /* hs trumps pb, remove pb */
1338 if (flags & ADD_FLAG_VERBOSE)
1339 PARA_NOTICE_LOG("removing path brother\n");
1340 ret = osl_del_row(audio_file_table, pb);
1345 /* file rename, update hs' path */
1346 ret = osl_get_object(audio_file_table, hs, AFTCOL_PATH, &obj);
1347 if (flags & ADD_FLAG_VERBOSE)
1348 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj.data, path);
1349 ret = osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1350 &objs[AFTCOL_PATH]);
1353 if (!(flags & ADD_FLAG_FORCE))
1356 /* no hs or force mode, child must have sent afhi */
1357 uint16_t afhi_offset = read_u16(buf + AFTROW_AFHI_OFFSET_POS);
1358 uint16_t chunks_offset = read_u16(buf + AFTROW_CHUNKS_OFFSET_POS);
1360 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1361 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1362 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1364 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1365 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1366 if (pb && !hs) { /* update pb's hash */
1367 char old_asc[2 * HASH_SIZE + 1];
1368 HASH_TYPE *old_hash;
1369 ret = get_hash_of_row(pb, &old_hash);
1372 hash_to_asc(old_hash, old_asc);
1373 if (flags & ADD_FLAG_VERBOSE)
1374 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path,
1376 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1377 &objs[AFTCOL_HASH]);
1381 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1382 const struct osl_row *row = pb? pb : hs;
1383 /* update afhi and chunk_table */
1384 if (flags & ADD_FLAG_VERBOSE)
1385 PARA_DEBUG_LOG("updating audio format handler info (%zd bytes)\n",
1386 objs[AFTCOL_AFHI].size);
1387 ret = osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1388 &objs[AFTCOL_AFHI]);
1391 if (flags & ADD_FLAG_VERBOSE)
1392 PARA_DEBUG_LOG("updating chunk table\n");
1393 ret = osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1394 &objs[AFTCOL_CHUNKS]);
1397 return mood_update_audio_file(row, NULL);
1399 /* new entry, use default afsi */
1400 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1401 default_afsi.audio_format_id = read_u8(buf + AFTROW_AUDIO_FORMAT_OFFSET);
1403 objs[AFTCOL_AFSI].data = &afsi_buf;
1404 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1405 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1406 ret = osl_add_and_get_row(audio_file_table, objs, &aft_row);
1409 return mood_update_audio_file(aft_row, NULL);
1412 struct private_add_data {
1417 static int path_brother_callback(const struct osl_object *query,
1418 struct osl_object *result)
1420 char *path = query->data;
1421 struct osl_row *path_brother;
1422 int ret = aft_get_row_of_path(path, &path_brother);
1425 result->data = para_malloc(sizeof(path_brother));
1426 result->size = sizeof(path_brother);
1427 *(struct osl_row **)(result->data) = path_brother;
1431 static int hash_sister_callback(const struct osl_object *query,
1432 struct osl_object *result)
1434 HASH_TYPE *hash = query->data;
1435 struct osl_row *hash_sister;
1437 hash_sister = find_hash_sister(hash);
1439 return -E_RB_KEY_NOT_FOUND;
1440 result->data = para_malloc(sizeof(hash_sister));
1441 result->size = sizeof(hash_sister);
1442 *(struct osl_row **)(result->data) = hash_sister;
1446 static int add_one_audio_file(const char *arg, const void *private_data)
1449 uint8_t format_num = -1;
1450 const struct private_add_data *pad = private_data;
1451 struct audio_format_info afhi, *afhi_ptr = NULL;
1452 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1453 struct osl_object map, obj = {.data = NULL}, query, result;
1455 HASH_TYPE hash[HASH_SIZE];
1457 afhi.header_offset = 0;
1458 afhi.header_len = 0;
1459 ret = verify_path(arg, &path);
1463 query.size = strlen(path) + 1;
1464 ret = send_callback_request(path_brother_callback, &query, &result);
1465 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1468 pb = *(struct osl_row **)result.data;
1472 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1473 if (pad->flags & ADD_FLAG_VERBOSE)
1474 ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1477 /* We still want to add this file. Compute its hash. */
1478 ret = mmap_full_file(path, O_RDONLY, &map);
1481 hash_function(map.data, map.size, hash);
1483 /* Check whether database contains file with the same hash. */
1485 query.size = HASH_SIZE;
1486 ret = send_callback_request(hash_sister_callback, &query, &result);
1487 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1490 hs = *(struct osl_row **)result.data;
1493 /* Return success if we already know this file. */
1495 if (pb && hs && hs == pb && (!(pad->flags & ADD_FLAG_FORCE))) {
1496 if (pad->flags & ADD_FLAG_VERBOSE)
1497 ret = send_va_buffer(pad->fd,
1498 "not forcing update: %s\n", path);
1502 * we won't recalculate the audio format info and the chunk table if
1503 * there is a hash sister unless in FORCE mode.
1505 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1506 ret = compute_afhi(path, map.data, map.size, &afhi);
1512 if (pad->flags & ADD_FLAG_VERBOSE)
1513 send_va_buffer(pad->fd, "adding %s\n", path);
1514 munmap(map.data, map.size);
1515 save_audio_file_info(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1516 /* Ask afs to consider this entry for adding. */
1517 ret = send_callback_request(com_add_callback, &obj, NULL);
1521 munmap(map.data, map.size);
1524 send_va_buffer(pad->fd, "failed to add %s (%s)\n", path?
1525 path : arg, PARA_STRERROR(-ret));
1529 free(afhi_ptr->chunk_table);
1530 return 1; /* it's not an error if not all files could be added */
1533 int com_add(int fd, int argc, char * const * const argv)
1536 struct private_add_data pad = {.fd = fd, .flags = 0};
1537 struct stat statbuf;
1539 for (i = 1; i < argc; i++) {
1540 const char *arg = argv[i];
1543 if (!strcmp(arg, "--")) {
1547 if (!strcmp(arg, "-l")) {
1548 pad.flags |= ADD_FLAG_LAZY;
1551 if (!strcmp(arg, "-f")) {
1552 pad.flags |= ADD_FLAG_FORCE;
1555 if (!strcmp(arg, "-v")) {
1556 pad.flags |= ADD_FLAG_VERBOSE;
1561 return -E_AFT_SYNTAX;
1562 for (; i < argc; i++) {
1563 char *path = para_strdup(argv[i]);
1564 size_t len = strlen(path);
1565 while (len > 1 && path[--len] == '/')
1567 ret = stat(path, &statbuf);
1569 PARA_NOTICE_LOG("failed to stat %s (%s)", path,
1572 if (S_ISDIR(statbuf.st_mode))
1573 for_each_file_in_dir(path, add_one_audio_file,
1576 add_one_audio_file(path, &pad);
1584 struct com_touch_options {
1591 static int com_touch_callback(const struct osl_object *query,
1592 __a_unused struct osl_object *result)
1594 struct com_touch_options *cto = query->data;
1595 char *p = (char *)query->data + sizeof(*cto);
1597 int ret, no_options = cto->num_played < 0 && cto->last_played < 0 &&
1598 cto->lyrics_id < 0 && cto->image_id < 0;
1600 for (;p < (char *)query->data + query->size; p += len + 1) {
1601 struct afs_info old_afsi, new_afsi;
1602 struct osl_object obj;
1603 struct osl_row *row;
1606 ret = aft_get_row_of_path(p, &row);
1609 ret = get_afsi_object_of_row(row, &obj);
1612 ret = load_afsi(&old_afsi, &obj);
1615 new_afsi = old_afsi;
1617 new_afsi.num_played++;
1618 new_afsi.last_played = time(NULL);
1620 if (cto->lyrics_id >= 0)
1621 new_afsi.lyrics_id = cto->lyrics_id;
1622 if (cto->image_id >= 0)
1623 new_afsi.image_id = cto->image_id;
1624 if (cto->num_played >= 0)
1625 new_afsi.num_played = cto->num_played;
1626 if (cto->last_played >= 0)
1627 new_afsi.last_played = cto->last_played;
1629 save_afsi(&new_afsi, &obj); /* in-place update */
1630 ret = mood_update_audio_file(row, &old_afsi);
1637 int com_touch(__a_unused int fd, int argc, char * const * const argv)
1639 struct com_touch_options cto = {
1645 struct osl_object options = {.data = &cto, .size = sizeof(cto)};
1649 for (i = 1; i < argc; i++) {
1650 const char *arg = argv[i];
1653 if (!strcmp(arg, "--")) {
1657 if (!strncmp(arg, "-n", 2)) {
1658 ret = para_atol(arg + 2, &cto.num_played);
1663 if (!strncmp(arg, "-l", 2)) {
1664 ret = para_atol(arg + 2, &cto.last_played);
1669 if (!strncmp(arg, "-y", 2)) {
1670 ret = para_atol(arg + 2, &cto.lyrics_id);
1675 if (!strncmp(arg, "-i", 2)) {
1676 ret = para_atol(arg + 2, &cto.image_id);
1682 ret = -E_AFT_SYNTAX;
1685 return send_option_arg_callback_request(&options, argc - i,
1686 argv + i, com_touch_callback, NULL);
1691 struct com_rm_options {
1695 static int com_rm_callback(const struct osl_object *query,
1696 __a_unused struct osl_object *result)
1698 struct com_rm_options *cro = query->data;
1699 char *p = (char *)query->data + sizeof(*cro);
1703 for (;p < (char *)query->data + query->size; p += len + 1) {
1704 struct osl_row *row;
1707 ret = aft_get_row_of_path(p, &row);
1710 ret = mood_delete_audio_file(row);
1713 ret = osl_del_row(audio_file_table, row);
1721 * TODO options: -v verbose, -f dont stop if file not found
1722 * -h remove by hash, use fnmatch
1726 int com_afs_rm(__a_unused int fd, int argc, char * const * const argv)
1728 struct com_rm_options cro = {.flags = 0};
1729 struct osl_object options = {.data = &cro, .size = sizeof(cro)};
1732 for (i = 1; i < argc; i++) {
1733 const char *arg = argv[i];
1736 if (!strcmp(arg, "--")) {
1741 ret = -E_AFT_SYNTAX;
1744 return send_option_arg_callback_request(&options, argc - i,
1745 argv + i, com_rm_callback, NULL);
1750 /* TODO: optionally fix problems by removing offending rows */
1751 static int check_audio_file(struct osl_row *row, void *data)
1754 struct para_buffer *pb = data;
1755 struct stat statbuf;
1756 int ret = get_audio_file_path_of_row(row, &path);
1757 struct afs_info afsi;
1761 para_printf(pb, "%s\n", PARA_STRERROR(-ret));
1764 if (stat(path, &statbuf) < 0)
1765 para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
1767 if (!S_ISREG(statbuf.st_mode))
1768 para_printf(pb, "%s: not a regular file\n", path);
1770 ret = get_afsi_of_row(row, &afsi);
1772 para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
1775 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
1777 para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
1778 PARA_STRERROR(-ret));
1779 ret = img_get_name_by_id(afsi.image_id, &blob_name);
1781 para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
1782 PARA_STRERROR(-ret));
1786 int aft_check_callback(__a_unused const struct osl_object *query, struct osl_object *result)
1788 struct para_buffer pb = {.buf = NULL};
1790 para_printf(&pb, "checking audio file table...\n");
1791 audio_file_loop(&pb, check_audio_file);
1792 result->data = pb.buf;
1793 result->size = pb.size;
1799 * Close the audio file table.
1801 * \param flags Ususal flags that are passed to osl_close_table().
1803 * \sa osl_close_table().
1805 void aft_shutdown(enum osl_close_flags flags)
1807 osl_close_table(audio_file_table, flags);
1808 audio_file_table = NULL;
1812 * Open the audio file table.
1814 * \param ti Gets initialized by this function.
1815 * \param db The database directory.
1817 * \return Positive on success, negative on errors.
1819 * \sa osl_open_table().
1821 int aft_init(struct table_info *ti, const char *db)
1825 audio_file_table_desc.dir = db;
1826 ti->desc = &audio_file_table_desc;
1827 ret = osl_open_table(ti->desc, &ti->table);
1830 audio_file_table = ti->table;
1831 osl_get_num_rows(audio_file_table, &num);
1832 PARA_INFO_LOG("audio file table contains %d files\n", num);
1835 PARA_INFO_LOG("failed to open audio file table\n");
1836 audio_file_table = NULL;
1837 return ret == -E_NOENT? 1 : ret;