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;
24 * Describes the layout of the mmapped-afs info struct.
26 * \sa struct afs_info.
29 /** Where .last_played is stored. */
30 AFSI_LAST_PLAYED_OFFSET = 0,
31 /** Storage position of the attributes bitmap. */
32 AFSI_ATTRIBUTES_OFFSET = 8,
33 /** Storage position of the .num_played field. */
34 AFSI_NUM_PLAYED_OFFSET = 16,
35 /** Storage position of the .image_id field. */
36 AFSI_IMAGE_ID_OFFSET = 20,
37 /** Storage position of the .lyrics_id field. */
38 AFSI_LYRICS_ID_OFFSET = 24,
39 /** Storage position of the .audio_format_id field. */
40 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
41 /** On-disk storage space needed. */
46 * Convert a struct afs_info to an osl object.
48 * \param afsi Pointer to the audio file info to be converted.
49 * \param obj Result pointer.
53 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
55 char *buf = obj->data;
57 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
58 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
59 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
60 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
61 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
62 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
63 afsi->audio_format_id);
67 * Get the audio file selector info struct stored in an osl object.
69 * \param afsi Points to the audio_file info structure to be filled in.
70 * \param obj The osl object holding the data.
72 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
76 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
78 char *buf = obj->data;
79 if (obj->size < AFSI_SIZE)
81 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
82 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
83 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
84 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
85 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
86 afsi->audio_format_id = read_u8(buf +
87 AFSI_AUDIO_FORMAT_ID_OFFSET);
91 /** The columns of the audio file table. */
92 enum audio_file_table_columns {
93 /** The hash on the content of the audio file. */
95 /** The full path in the filesystem. */
97 /** The audio file selector info. */
99 /** The audio format handler info. */
101 /** The chunk table info and the chunk table of the audio file. */
103 /** The number of columns of this table. */
107 static struct osl_column_description aft_cols[] = {
109 .storage_type = OSL_MAPPED_STORAGE,
110 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
112 .compare_function = osl_hash_compare,
113 .data_size = HASH_SIZE
116 .storage_type = OSL_MAPPED_STORAGE,
117 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
119 .compare_function = string_compare,
122 .storage_type = OSL_MAPPED_STORAGE,
123 .storage_flags = OSL_FIXED_SIZE,
125 .data_size = AFSI_SIZE
128 .storage_type = OSL_MAPPED_STORAGE,
132 .storage_type = OSL_DISK_STORAGE,
137 static struct osl_table_description audio_file_table_desc = {
138 .name = "audio_files",
139 .num_columns = NUM_AFT_COLUMNS,
140 .flags = OSL_LARGE_TABLE,
141 .column_descriptions = aft_cols
144 static char *prefix_path(const char *prefix, int len, const char *path)
174 /* Remove last component of the prefix */
179 } while (len && prefix[len-1] != '/');
183 return para_strdup(path);
184 speclen = strlen(path);
185 n = para_malloc(speclen + len + 1);
186 memcpy(n, prefix, len);
187 memcpy(n + len, path, speclen+1);
192 * We fundamentally don't like some paths: we don't want
193 * dot or dot-dot anywhere.
195 * Also, we don't want double slashes or slashes at the
196 * end that can make pathnames ambiguous.
198 static int verify_dotfile(const char *rest)
201 * The first character was '.', but that has already been discarded, we
205 /* "." is not allowed */
210 if (rest[1] == '\0' || rest[1] == '/')
216 static int verify_path(const char *orig_path, char **resolved_path)
219 const char prefix[] = AFS_AUDIO_FILE_DIR "/";
220 const char *path = orig_path;
221 const size_t prefix_len = strlen(prefix);
232 case '/': /* double slash */
235 if (verify_dotfile(path) < 0)
241 if (*orig_path != '/')
242 *resolved_path = prefix_path(prefix, prefix_len, orig_path);
244 *resolved_path = para_strdup(orig_path);
251 AFHI_SECONDS_TOTAL_OFFSET = 0,
252 AFHI_BITRATE_OFFSET = 4,
253 AFHI_FREQUENCY_OFFSET = 8,
254 AFHI_CHANNELS_OFFSET = 12,
255 AFHI_INFO_STRING_OFFSET = 13,
259 static unsigned sizeof_afhi_buf(const struct audio_format_info *afhi)
263 return strlen(afhi->info_string) + MIN_AFHI_SIZE;
266 static void save_afhi(struct audio_format_info *afhi, char *buf)
270 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET,
271 afhi->seconds_total);
272 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
273 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
274 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
275 strcpy(buf + AFHI_INFO_STRING_OFFSET, afhi->info_string); /* OK */
276 PARA_DEBUG_LOG("last byte written: %p\n", buf + AFHI_INFO_STRING_OFFSET + strlen(afhi->info_string));
279 static void load_afhi(const char *buf, struct audio_format_info *afhi)
281 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
282 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
283 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
284 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
285 strcpy(afhi->info_string, buf + AFHI_INFO_STRING_OFFSET);
288 static unsigned sizeof_chunk_info_buf(struct audio_format_info *afhi)
292 return 4 * afhi->chunks_total + 20;
296 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
297 enum chunk_info_offsets{
298 /** The total number of chunks (4 bytes). */
299 CHUNKS_TOTAL_OFFSET = 0,
300 /** The length of the audio file header (4 bytes). */
301 HEADER_LEN_OFFSET = 4,
302 /** The start of the audio file header (4 bytes). */
303 HEADER_OFFSET_OFFSET = 8,
304 /** The seconds part of the chunk time (4 bytes). */
305 CHUNK_TV_TV_SEC_OFFSET = 12,
306 /** The microseconds part of the chunk time (4 bytes). */
307 CHUNK_TV_TV_USEC = 16,
308 /** Chunk table entries start here. */
309 CHUNK_TABLE_OFFSET = 20,
312 /* TODO: audio format handlers could just produce this */
313 static void save_chunk_info(struct audio_format_info *afhi, char *buf)
319 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
320 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
321 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
322 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
323 write_u32(buf + CHUNK_TV_TV_USEC, afhi->chunk_tv.tv_usec);
324 for (i = 0; i < afhi->chunks_total; i++)
325 write_u32(buf + CHUNK_TABLE_OFFSET + 4 * i, afhi->chunk_table[i]);
328 static int load_chunk_info(struct osl_object *obj, struct audio_format_info *afhi)
330 char *buf = obj->data;
333 if (obj->size < CHUNK_TABLE_OFFSET)
334 return -E_BAD_DATA_SIZE;
336 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
337 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
338 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
339 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
340 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC);
342 if (afhi->chunks_total * 4 + CHUNK_TABLE_OFFSET > obj->size)
343 return -E_BAD_DATA_SIZE;
344 afhi->chunk_table = para_malloc(afhi->chunks_total * sizeof(size_t));
345 for (i = 0; i < afhi->chunks_total; i++)
346 afhi->chunk_table[i] = read_u32(buf + CHUNK_TABLE_OFFSET + 4 * i);
351 * Get the row of the audio file table corresponding to the given path.
353 * \param path The full path of the audio file.
354 * \param row Result pointer.
356 * \return The return value of the underlying call to osl_get_row().
358 int aft_get_row_of_path(char *path, struct osl_row **row)
360 struct osl_object obj = {.data = path, .size = strlen(path) + 1};
362 return osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row);
366 * Get the row of the audio file table corresponding to the given hash value.
368 * \param hash The hash value of the desired audio file.
369 * \param row resul pointer.
371 * \return The return value of the underlying call to osl_get_row().
373 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
375 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
376 return osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row);
380 * Get the osl object holding the audio file selector info of a row.
382 * \param row Pointer to a row in the audio file table.
383 * \param obj Result pointer.
385 * \return The return value of the underlying call to osl_get_object().
387 int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
389 return osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj);
393 * Get the osl object holding the audio file selector info, given a path.
396 * \param path The full path of the audio file.
397 * \param obj Result pointer.
399 * \return Positive on success, negative on errors.
401 int get_afsi_object_of_path(char *path, struct osl_object *obj)
404 int ret = aft_get_row_of_path(path, &row);
407 return get_afsi_object_of_row(row, obj);
411 * Get the audio file selector info, given a row of the audio file table.
413 * \param row Pointer to a row in the audio file table.
414 * \param afsi Result pointer.
416 * \return Positive on success, negative on errors.
418 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
420 struct osl_object obj;
421 int ret = get_afsi_object_of_row(row, &obj);
424 return load_afsi(afsi, &obj);
428 * Get the path of an audio file, given a row of the audio file table.
430 * \param row Pointer to a row in the audio file table.
431 * \param path Result pointer.
433 * \return Positive on success, negative on errors.
435 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
437 struct osl_object path_obj;
438 int ret = osl_get_object(audio_file_table, row, AFTCOL_PATH,
442 *path = path_obj.data;
447 * Get the object containing the hash value of an audio file, given a row.
449 * \param row Pointer to a row of the audio file table.
450 * \param obj Result pointer.
452 * \return The return value of the underlying call to osl_get_object().
454 * \sa get_hash_of_row().
456 static int get_hash_object_of_aft_row(const struct osl_row *row, struct osl_object *obj)
458 return osl_get_object(audio_file_table, row, AFTCOL_HASH, obj);
462 * Get the hash value of an audio file, given a row of the audio file table.
464 * \param row Pointer to a row of the audio file table.
465 * \param hash Result pointer.
467 * \a hash points to mapped data and must not be freed by the caller.
469 * \return The return value of the underlying call to
470 * get_hash_object_of_aft_row().
472 static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash)
474 struct osl_object obj;
475 int ret = get_hash_object_of_aft_row(row, &obj);
484 * Get the audio format handler info, given a row of the audio file table.
486 * \param row Pointer to a row of the audio file table.
487 * \param afhi Result pointer.
489 * \return The return value of the underlying call to osl_get_object().
491 * \sa get_chunk_table_of_row().
493 static int get_afhi_of_row(const struct osl_row *row, struct audio_format_info *afhi)
495 struct osl_object obj;
496 int ret = osl_get_object(audio_file_table, row, AFTCOL_AFHI,
500 load_afhi(obj.data, afhi);
505 * Get the chunk table of an audio file, given a row of the audio file table.
507 * \param row Pointer to a row of the audio file table.
508 * \param afhi Result pointer.
510 * \return The return value of the underlying call to osl_open_disk_object().
512 * \sa get_afhi_of_row().
514 static int get_chunk_table_of_row(const struct osl_row *row, struct audio_format_info *afhi)
516 struct osl_object obj;
517 int ret = osl_open_disk_object(audio_file_table, row, AFTCOL_CHUNKS,
521 ret = load_chunk_info(&obj, afhi);
522 osl_close_disk_object(&obj);
527 * Mmap the given audio file and update statistics.
529 * \param aft_row Determines the audio file to be opened and updated.
530 * \param afd Result pointer.
532 * On success, the numplayed field of the audio file selector info is increased
533 * and the lastplayed time is set to the current time. Finally, the score of
534 * the audio file is updated.
536 * \return Positive on success, negative on errors.
538 int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd)
540 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
541 struct osl_object afsi_obj;
542 struct afs_info new_afsi;
543 int ret = get_hash_of_row(aft_row, &aft_hash);
547 ret = get_audio_file_path_of_row(aft_row, &afd->path);
550 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
553 ret = load_afsi(&afd->afsi, &afsi_obj);
556 ret = get_afhi_of_row(aft_row, &afd->afhi);
559 ret = get_chunk_table_of_row(aft_row, &afd->afhi);
562 ret = mmap_full_file(afd->path, O_RDONLY, &afd->map);
565 hash_function(afd->map.data, afd->map.size, file_hash);
566 ret = -E_HASH_MISMATCH;
567 if (hash_compare(file_hash, aft_hash))
569 new_afsi = afd->afsi;
570 new_afsi.num_played++;
571 new_afsi.last_played = time(NULL);
572 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
573 if (afd->current_play_mode == PLAY_MODE_PLAYLIST)
574 ret = playlist_update_audio_file(aft_row);
576 ret = mood_update_audio_file(aft_row, &afd->afsi);
579 free(afd->afhi.chunk_table);
583 static int get_local_time(uint64_t *seconds, char *buf, size_t size,
588 if (!localtime_r((time_t *)seconds, &t))
590 if (*seconds + 6 * 30 * 24 * 3600 > current_time) {
591 if (!strftime(buf, size, "%b %e %k:%M", &t))
595 if (!strftime(buf, size, "%b %e %Y", &t))
600 #define GET_NUM_DIGITS(x, num) { \
601 typeof((x)) _tmp = PARA_ABS(x); \
604 while ((_tmp) > 9) { \
610 static short unsigned get_duration_width(int seconds)
612 short unsigned width;
613 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
615 if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
616 return 4 + (mins > 9);
617 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
618 GET_NUM_DIGITS(hours, &width);
622 static void get_duration_buf(int seconds, char *buf, short unsigned max_width)
624 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
626 if (!hours) /* m:ss or mm:ss */
627 sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
628 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
629 sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
633 static char *make_attribute_line(const char *att_bitmap, struct afs_info *afsi)
635 char *att_text, *att_line;
637 get_attribute_text(&afsi->attributes, " ", &att_text);
639 return para_strdup(att_bitmap);
640 att_line = make_message("%s (%s)", att_bitmap, att_text);
645 static char *make_lyrics_line(struct afs_info *afsi)
648 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
650 return make_message("%u", afsi->lyrics_id);
651 return make_message("%u (%s)", afsi->lyrics_id, lyrics_name);
654 static char *make_image_line(struct afs_info *afsi)
657 img_get_name_by_id(afsi->image_id, &image_name);
659 return make_message("%u", afsi->image_id);
660 return make_message("%u (%s)", afsi->image_id, image_name);
663 static int print_list_item(struct ls_data *d, struct ls_options *opts,
664 struct para_buffer *b, time_t current_time)
668 char last_played_time[30];
669 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
670 char score_buf[30] = "";
671 struct afs_info *afsi = &d->afsi;
672 struct audio_format_info *afhi = &d->afhi;
673 struct ls_widths *w = &opts->widths;
674 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
676 if (opts->mode == LS_MODE_SHORT) {
677 para_printf(b, "%s\n", d->path);
680 get_attribute_bitmap(&afsi->attributes, att_buf);
681 ret = get_local_time(&afsi->last_played, last_played_time,
682 sizeof(last_played_time), current_time);
685 get_duration_buf(afhi->seconds_total, duration_buf, w->duration_width);
687 if (opts->mode == LS_MODE_LONG)
688 sprintf(score_buf, "%*li ", w->score_width, d->score);
690 sprintf(score_buf, "%li ", d->score);
693 PARA_NOTICE_LOG("id: %s, %d\n", d->path, afsi->audio_format_id);
694 if (opts->mode == LS_MODE_LONG) {
697 "%s " /* attributes */
698 "%*d " /* image_id */
699 "%*d " /* lyrics_id */
701 "%s " /* audio format */
702 "%*d " /* frequency */
705 "%*d " /* num_played */
706 "%s " /* last_played */
710 w->image_id_width, afsi->image_id,
711 w->lyrics_id_width, afsi->lyrics_id,
712 w->bitrate_width, afhi->bitrate,
713 audio_format_name(afsi->audio_format_id),
714 w->frequency_width, afhi->frequency,
717 w->num_played_width, afsi->num_played,
723 if (opts->mode == LS_MODE_VERBOSE) {
724 char asc_hash[2 * HASH_SIZE + 1];
725 char *att_line, *lyrics_line, *image_line;
727 hash_to_asc(d->hash, asc_hash);
728 att_line = make_attribute_line(att_buf, afsi);
729 lyrics_line = make_lyrics_line(afsi);
730 image_line = make_image_line(afsi);
732 "%s: %s\n" /* path */
738 "bitrate: %dkbit/s\n"
744 "last_played: %s\n\n",
745 (opts->flags & LS_FLAG_FULL_PATH)?
746 "path" : "file", d->path,
747 have_score? "score: " : "", score_buf,
748 have_score? "\n" : "",
754 audio_format_name(afsi->audio_format_id),
769 static int ls_audio_format_compare(const void *a, const void *b)
771 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
772 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
775 static int ls_duration_compare(const void *a, const void *b)
777 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
778 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
781 static int ls_bitrate_compare(const void *a, const void *b)
783 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
784 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
787 static int ls_lyrics_id_compare(const void *a, const void *b)
789 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
790 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
793 static int ls_image_id_compare(const void *a, const void *b)
795 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
796 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
799 static int ls_channels_compare(const void *a, const void *b)
801 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
802 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
805 static int ls_frequency_compare(const void *a, const void *b)
807 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
808 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
811 static int ls_num_played_compare(const void *a, const void *b)
813 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
814 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
817 static int ls_last_played_compare(const void *a, const void *b)
819 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
820 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
823 static int ls_score_compare(const void *a, const void *b)
825 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
826 return NUM_COMPARE(d1->score, d2->score);
829 static int ls_path_compare(const void *a, const void *b)
831 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
832 return strcmp(d1->path, d2->path);
835 static int sort_matching_paths(struct ls_options *options)
837 size_t nmemb = options->num_matching_paths;
838 size_t size = sizeof(*options->data_ptr);
839 int (*compar)(const void *, const void *);
842 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
843 for (i = 0; i < nmemb; i++)
844 options->data_ptr[i] = options->data + i;
846 /* In these cases the array is already sorted */
847 if (options->sorting == LS_SORT_BY_PATH
848 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
849 && (options->flags & LS_FLAG_FULL_PATH))
851 if (options->sorting == LS_SORT_BY_SCORE &&
852 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
855 switch (options->sorting) {
856 case LS_SORT_BY_PATH:
857 compar = ls_path_compare; break;
858 case LS_SORT_BY_SCORE:
859 compar = ls_score_compare; break;
860 case LS_SORT_BY_LAST_PLAYED:
861 compar = ls_last_played_compare; break;
862 case LS_SORT_BY_NUM_PLAYED:
863 compar = ls_num_played_compare; break;
864 case LS_SORT_BY_FREQUENCY:
865 compar = ls_frequency_compare; break;
866 case LS_SORT_BY_CHANNELS:
867 compar = ls_channels_compare; break;
868 case LS_SORT_BY_IMAGE_ID:
869 compar = ls_image_id_compare; break;
870 case LS_SORT_BY_LYRICS_ID:
871 compar = ls_lyrics_id_compare; break;
872 case LS_SORT_BY_BITRATE:
873 compar = ls_bitrate_compare; break;
874 case LS_SORT_BY_DURATION:
875 compar = ls_duration_compare; break;
876 case LS_SORT_BY_AUDIO_FORMAT:
877 compar = ls_audio_format_compare; break;
881 qsort(options->data_ptr, nmemb, size, compar);
885 /* row is either an aft_row or a row of the score table */
886 /* TODO: Only compute widths if we need them */
887 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
890 struct ls_options *options = ls_opts;
893 unsigned short num_digits;
895 struct osl_row *aft_row;
899 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
900 ret = get_score_and_aft_row(row, &score, &aft_row);
905 ret = get_audio_file_path_of_row(aft_row, &path);
908 if (!(options->flags & LS_FLAG_FULL_PATH)) {
909 char *p = strrchr(path, '/');
913 if (options->num_patterns) {
914 for (i = 0; i < options->num_patterns; i++) {
915 ret = fnmatch(options->patterns[i], path, FNM_PATHNAME);
918 if (ret == FNM_NOMATCH)
922 if (i >= options->num_patterns) /* no match */
925 tmp = options->num_matching_paths++;
926 if (options->num_matching_paths > options->array_size) {
927 options->array_size++;
928 options->array_size *= 2;
929 options->data = para_realloc(options->data, options->array_size
930 * sizeof(*options->data));
932 d = options->data + tmp;
933 ret = get_afsi_of_row(aft_row, &d->afsi);
936 ret = get_afhi_of_row(aft_row, &d->afhi);
940 ret = get_hash_of_row(aft_row, &d->hash);
943 w = &options->widths;
944 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
945 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
946 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
947 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
948 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
949 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
950 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
951 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
952 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
953 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
954 /* get the number of chars to print this amount of time */
955 tmp = get_duration_width(d->afhi.seconds_total);
956 w->duration_width = PARA_MAX(w->duration_width, tmp);
957 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
958 GET_NUM_DIGITS(score, &num_digits);
959 num_digits++; /* add one for the sign (space or "-") */
960 w->score_width = PARA_MAX(w->score_width, num_digits);
966 static int com_ls_callback(const struct osl_object *query,
967 struct osl_object *ls_output)
969 struct ls_options *opts = query->data;
970 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
971 struct para_buffer b = {.buf = NULL, .size = 0};
976 PARA_NOTICE_LOG("%d patterns\n", opts->num_patterns);
977 if (opts->num_patterns) {
978 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
979 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
980 opts->patterns[i] = p;
982 PARA_NOTICE_LOG("pattern %d: %s\n", i, opts->patterns[i]);
985 opts->patterns = NULL;
986 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
987 ret = admissible_file_loop(opts, prepare_ls_row);
989 ret = osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
993 ret = opts->num_patterns? -E_NO_MATCH : 0;
994 if (!opts->num_matching_paths) {
995 PARA_NOTICE_LOG("no match, ret: %d\n", ret);
998 ret = sort_matching_paths(opts);
1001 time(¤t_time);
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, current_time);
1009 for (i = 0; i < opts->num_matching_paths; i++) {
1010 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1016 ls_output->data = b.buf;
1017 PARA_NOTICE_LOG("ls_outoute.data: %p\n", ls_output->data);
1018 ls_output->size = b.size;
1020 free(opts->data_ptr);
1021 free(opts->patterns);
1026 * TODO: flags -h (sort by hash) -lm (list in mbox format)
1028 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1029 * full list: list everything, including afsi, afhi, atts as clear text
1032 int com_afs_ls(int fd, int argc, char * const * const argv)
1036 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1037 enum ls_listing_mode mode = LS_MODE_SHORT;
1038 struct ls_options opts = {.patterns = NULL};
1039 struct osl_object query = {.data = &opts, .size = sizeof(opts)},
1042 for (i = 1; i < argc; i++) {
1043 const char *arg = argv[i];
1046 if (!strcmp(arg, "--")) {
1050 if (!strncmp(arg, "-l", 2)) {
1052 mode = LS_MODE_LONG;
1056 return -E_AFT_SYNTAX;
1057 switch(*(arg + 2)) {
1059 mode = LS_MODE_SHORT;
1062 mode = LS_MODE_LONG;
1065 mode = LS_MODE_VERBOSE;
1068 mode = LS_MODE_MBOX;
1071 return -E_AFT_SYNTAX;
1074 if (!strcmp(arg, "-p")) {
1075 flags |= LS_FLAG_FULL_PATH;
1078 if (!strcmp(arg, "-a")) {
1079 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1082 if (!strcmp(arg, "-r")) {
1083 flags |= LS_FLAG_REVERSE;
1086 if (!strncmp(arg, "-s", 2)) {
1087 if (!*(arg + 2) || *(arg + 3))
1088 return -E_AFT_SYNTAX;
1089 switch(*(arg + 2)) {
1091 sort = LS_SORT_BY_PATH;
1093 case 's': /* -ss implies -a */
1094 sort = LS_SORT_BY_SCORE;
1095 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1098 sort = LS_SORT_BY_LAST_PLAYED;
1101 sort = LS_SORT_BY_NUM_PLAYED;
1104 sort = LS_SORT_BY_FREQUENCY;
1107 sort = LS_SORT_BY_CHANNELS;
1110 sort = LS_SORT_BY_IMAGE_ID;
1113 sort = LS_SORT_BY_LYRICS_ID;
1116 sort = LS_SORT_BY_BITRATE;
1119 sort = LS_SORT_BY_DURATION;
1122 sort = LS_SORT_BY_AUDIO_FORMAT;
1125 return -E_AFT_SYNTAX;
1128 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);
1137 ret = send_buffer(fd, (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 struct osl_row *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 enum aft_row_offsets {
1167 AFTROW_AFHI_OFFSET_POS = 0,
1168 AFTROW_CHUNKS_OFFSET_POS = 2,
1169 AFTROW_AUDIO_FORMAT_OFFSET = 4,
1170 AFTROW_FLAGS_OFFSET = 5,
1171 AFTROW_HASH_OFFSET = 9,
1172 AFTROW_PATH_OFFSET = (AFTROW_HASH_OFFSET + HASH_SIZE),
1175 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1176 * In this case, afhi won't be stored in the buffer */
1177 static void save_audio_file_info(HASH_TYPE *hash, const char *path,
1178 struct audio_format_info *afhi, uint32_t flags,
1179 uint8_t audio_format_num, struct osl_object *obj)
1181 size_t path_len = strlen(path) + 1;
1182 size_t afhi_size = sizeof_afhi_buf(afhi);
1183 size_t size = AFTROW_PATH_OFFSET + path_len + afhi_size
1184 + sizeof_chunk_info_buf(afhi);
1185 char *buf = para_malloc(size);
1188 write_u8(buf + AFTROW_AUDIO_FORMAT_OFFSET, audio_format_num);
1189 write_u32(buf + AFTROW_FLAGS_OFFSET, flags);
1191 memcpy(buf + AFTROW_HASH_OFFSET, hash, HASH_SIZE);
1192 strcpy(buf + AFTROW_PATH_OFFSET, path);
1194 pos = AFTROW_PATH_OFFSET + path_len;
1195 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1196 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1197 pos + afhi_size - 1);
1198 write_u16(buf + AFTROW_AFHI_OFFSET_POS, pos);
1199 save_afhi(afhi, buf + pos);
1202 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1203 write_u16(buf + AFTROW_CHUNKS_OFFSET_POS, pos);
1204 save_chunk_info(afhi, buf + pos);
1205 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1219 AFHI: whether afhi and chunk table are computed and sent
1220 ACTION: table modifications to be performed
1222 +---+----+-----+------+---------------------------------------------------+
1223 | HS | PB | F | AFHI | ACTION
1224 +---+----+-----+------+---------------------------------------------------+
1225 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1226 | | update path, keep afsi
1227 +---+----+-----+------+---------------------------------------------------+
1228 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1229 | | otherwise: remove PB, HS: update path, keep afhi,
1231 +---+----+-----+------+---------------------------------------------------+
1232 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1234 +---+----+-----+------+---------------------------------------------------+
1235 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1236 +---+----+-----+------+---------------------------------------------------+
1237 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1238 | | (force has no effect)
1239 +---+----+-----+------+---------------------------------------------------+
1240 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1241 +---+----+-----+------+---------------------------------------------------+
1242 | N | N | Y | Y | (new file) create new entry (force has no effect)
1243 +---+----+-----+------+---------------------------------------------------+
1244 | N | N | N | Y | (new file) create new entry
1245 +---+----+-----+------+---------------------------------------------------+
1247 afhi <=> force or no HS
1252 #define ADD_FLAG_LAZY 1
1253 #define ADD_FLAG_FORCE 2
1254 #define ADD_FLAG_VERBOSE 4
1256 /* TODO: change log messages so that they get written to the result buffer */
1258 static int com_add_callback(const struct osl_object *query,
1259 __a_unused struct osl_object *result)
1261 char *buf = query->data, *path;
1262 struct osl_row *pb, *aft_row;
1263 const struct osl_row *hs;
1264 struct osl_object objs[NUM_AFT_COLUMNS];
1266 char asc[2 * HASH_SIZE + 1];
1268 char afsi_buf[AFSI_SIZE];
1269 uint32_t flags = read_u32(buf + AFTROW_FLAGS_OFFSET);
1270 struct afs_info default_afsi = {.last_played = 0};
1272 hash = (HASH_TYPE *)buf + AFTROW_HASH_OFFSET;
1273 hash_to_asc(hash, asc);;
1274 objs[AFTCOL_HASH].data = buf + AFTROW_HASH_OFFSET;
1275 objs[AFTCOL_HASH].size = HASH_SIZE;
1277 path = buf + AFTROW_PATH_OFFSET;
1278 objs[AFTCOL_PATH].data = path;
1279 objs[AFTCOL_PATH].size = strlen(path) + 1;
1281 PARA_INFO_LOG("request to add %s\n", path);
1282 hs = find_hash_sister(hash);
1283 ret = aft_get_row_of_path(path, &pb);
1284 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1286 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1287 if (flags & ADD_FLAG_VERBOSE)
1288 PARA_NOTICE_LOG("ignoring duplicate %p\n", path);
1291 if (hs && hs != pb) {
1292 struct osl_object obj;
1293 if (pb) { /* hs trumps pb, remove pb */
1294 if (flags & ADD_FLAG_VERBOSE)
1295 PARA_NOTICE_LOG("removing path brother\n");
1296 ret = osl_del_row(audio_file_table, pb);
1301 /* file rename, update hs' path */
1302 ret = osl_get_object(audio_file_table, hs, AFTCOL_PATH, &obj);
1303 if (flags & ADD_FLAG_VERBOSE)
1304 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj.data, path);
1305 ret = osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1306 &objs[AFTCOL_PATH]);
1309 if (!(flags & ADD_FLAG_FORCE))
1312 /* no hs or force mode, child must have sent afhi */
1313 uint16_t afhi_offset = read_u16(buf + AFTROW_AFHI_OFFSET_POS);
1314 uint16_t chunks_offset = read_u16(buf + AFTROW_CHUNKS_OFFSET_POS);
1316 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1317 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1318 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1320 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1321 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1322 if (pb && !hs) { /* update pb's hash */
1323 char old_asc[2 * HASH_SIZE + 1];
1324 HASH_TYPE *old_hash;
1325 ret = get_hash_of_row(pb, &old_hash);
1328 hash_to_asc(old_hash, old_asc);
1329 if (flags & ADD_FLAG_VERBOSE)
1330 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path,
1332 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1333 &objs[AFTCOL_HASH]);
1337 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1338 const struct osl_row *row = pb? pb : hs;
1339 /* update afhi and chunk_table */
1340 if (flags & ADD_FLAG_VERBOSE)
1341 PARA_DEBUG_LOG("updating audio format handler info (%zd bytes)\n",
1342 objs[AFTCOL_AFHI].size);
1343 ret = osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1344 &objs[AFTCOL_AFHI]);
1347 if (flags & ADD_FLAG_VERBOSE)
1348 PARA_DEBUG_LOG("updating chunk table\n");
1349 ret = osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1350 &objs[AFTCOL_CHUNKS]);
1353 return mood_update_audio_file(row, NULL);
1355 /* new entry, use default afsi */
1356 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1357 default_afsi.audio_format_id = read_u8(buf + AFTROW_AUDIO_FORMAT_OFFSET);
1359 objs[AFTCOL_AFSI].data = &afsi_buf;
1360 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1361 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1362 ret = osl_add_and_get_row(audio_file_table, objs, &aft_row);
1365 return mood_update_audio_file(aft_row, NULL);
1368 struct private_add_data {
1373 static int path_brother_callback(const struct osl_object *query,
1374 struct osl_object *result)
1376 char *path = query->data;
1377 struct osl_row *path_brother;
1378 int ret = aft_get_row_of_path(path, &path_brother);
1381 result->data = para_malloc(sizeof(path_brother));
1382 result->size = sizeof(path_brother);
1383 *(struct osl_row **)(result->data) = path_brother;
1387 static int hash_sister_callback(const struct osl_object *query,
1388 struct osl_object *result)
1390 HASH_TYPE *hash = query->data;
1391 struct osl_row *hash_sister;
1393 hash_sister = find_hash_sister(hash);
1395 return -E_RB_KEY_NOT_FOUND;
1396 result->data = para_malloc(sizeof(hash_sister));
1397 result->size = sizeof(hash_sister);
1398 *(struct osl_row **)(result->data) = hash_sister;
1402 static int add_one_audio_file(const char *arg, const void *private_data)
1405 uint8_t format_num = -1;
1406 const struct private_add_data *pad = private_data;
1407 struct audio_format_info afhi, *afhi_ptr = NULL;
1408 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1409 struct osl_object map, obj = {.data = NULL}, query, result;
1411 HASH_TYPE hash[HASH_SIZE];
1413 afhi.header_offset = 0;
1414 afhi.header_len = 0;
1415 ret = verify_path(arg, &path);
1419 query.size = strlen(path) + 1;
1420 ret = send_callback_request(path_brother_callback, &query, &result);
1421 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1424 pb = *(struct osl_row **)result.data;
1428 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1429 if (pad->flags & ADD_FLAG_VERBOSE)
1430 ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1433 /* We still want to add this file. Compute its hash. */
1434 ret = mmap_full_file(path, O_RDONLY, &map);
1437 hash_function(map.data, map.size, hash);
1439 /* Check whether database contains file with the same hash. */
1441 query.size = HASH_SIZE;
1442 ret = send_callback_request(hash_sister_callback, &query, &result);
1443 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1446 hs = *(struct osl_row **)result.data;
1449 /* Return success if we already know this file. */
1451 if (pb && hs && hs == pb && (!(pad->flags & ADD_FLAG_FORCE))) {
1452 if (pad->flags & ADD_FLAG_VERBOSE)
1453 ret = send_va_buffer(pad->fd,
1454 "not forcing update: %s\n", path);
1458 * we won't recalculate the audio format info and the chunk table if
1459 * there is a hash sister unless in FORCE mode.
1461 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1462 ret = compute_afhi(path, map.data, map.size, &afhi);
1468 if (pad->flags & ADD_FLAG_VERBOSE)
1469 send_va_buffer(pad->fd, "adding %s\n", path);
1470 munmap(map.data, map.size);
1471 save_audio_file_info(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1472 /* Ask afs to consider this entry for adding. */
1473 ret = send_callback_request(com_add_callback, &obj, NULL);
1477 munmap(map.data, map.size);
1480 send_va_buffer(pad->fd, "failed to add %s (%s)\n", path?
1481 path : arg, PARA_STRERROR(-ret));
1485 free(afhi_ptr->chunk_table);
1486 return 1; /* it's not an error if not all files could be added */
1489 int com_add(int fd, int argc, char * const * const argv)
1492 struct private_add_data pad = {.fd = fd, .flags = 0};
1493 struct stat statbuf;
1495 for (i = 1; i < argc; i++) {
1496 const char *arg = argv[i];
1499 if (!strcmp(arg, "--")) {
1503 if (!strcmp(arg, "-l")) {
1504 pad.flags |= ADD_FLAG_LAZY;
1507 if (!strcmp(arg, "-f")) {
1508 pad.flags |= ADD_FLAG_FORCE;
1511 if (!strcmp(arg, "-v")) {
1512 pad.flags |= ADD_FLAG_VERBOSE;
1517 return -E_AFT_SYNTAX;
1518 for (; i < argc; i++) {
1519 char *path = para_strdup(argv[i]);
1520 size_t len = strlen(path);
1521 while (len > 1 && path[--len] == '/')
1523 ret = stat(path, &statbuf);
1525 PARA_NOTICE_LOG("failed to stat %s (%s)", path,
1528 if (S_ISDIR(statbuf.st_mode))
1529 for_each_file_in_dir(path, add_one_audio_file,
1532 add_one_audio_file(path, &pad);
1540 struct com_touch_options {
1547 static int com_touch_callback(const struct osl_object *query,
1548 __a_unused struct osl_object *result)
1550 struct com_touch_options *cto = query->data;
1551 char *p = (char *)query->data + sizeof(*cto);
1553 int ret, no_options = cto->num_played < 0 && cto->last_played < 0 &&
1554 cto->lyrics_id < 0 && cto->image_id < 0;
1556 for (;p < (char *)query->data + query->size; p += len + 1) {
1557 struct afs_info old_afsi, new_afsi;
1558 struct osl_object obj;
1559 struct osl_row *row;
1562 ret = aft_get_row_of_path(p, &row);
1565 ret = get_afsi_object_of_row(row, &obj);
1568 ret = load_afsi(&old_afsi, &obj);
1571 new_afsi = old_afsi;
1573 new_afsi.num_played++;
1574 new_afsi.last_played = time(NULL);
1576 if (cto->lyrics_id >= 0)
1577 new_afsi.lyrics_id = cto->lyrics_id;
1578 if (cto->image_id >= 0)
1579 new_afsi.image_id = cto->image_id;
1580 if (cto->num_played >= 0)
1581 new_afsi.num_played = cto->num_played;
1582 if (cto->last_played >= 0)
1583 new_afsi.last_played = cto->last_played;
1585 save_afsi(&new_afsi, &obj); /* in-place update */
1586 ret = mood_update_audio_file(row, &old_afsi);
1593 int com_touch(__a_unused int fd, int argc, char * const * const argv)
1595 struct com_touch_options cto = {
1601 struct osl_object options = {.data = &cto, .size = sizeof(cto)};
1605 for (i = 1; i < argc; i++) {
1606 const char *arg = argv[i];
1609 if (!strcmp(arg, "--")) {
1613 if (!strncmp(arg, "-n", 2)) {
1614 ret = para_atol(arg + 2, &cto.num_played);
1619 if (!strncmp(arg, "-l", 2)) {
1620 ret = para_atol(arg + 2, &cto.last_played);
1625 if (!strncmp(arg, "-y", 2)) {
1626 ret = para_atol(arg + 2, &cto.lyrics_id);
1631 if (!strncmp(arg, "-i", 2)) {
1632 ret = para_atol(arg + 2, &cto.image_id);
1638 ret = -E_AFT_SYNTAX;
1641 return send_option_arg_callback_request(&options, argc - i,
1642 argv + i, com_touch_callback, NULL);
1647 struct com_rm_options {
1651 static int com_rm_callback(const struct osl_object *query,
1652 __a_unused struct osl_object *result)
1654 struct com_rm_options *cro = query->data;
1655 char *p = (char *)query->data + sizeof(*cro);
1659 for (;p < (char *)query->data + query->size; p += len + 1) {
1660 struct osl_row *row;
1663 ret = aft_get_row_of_path(p, &row);
1666 ret = mood_delete_audio_file(row);
1669 ret = osl_del_row(audio_file_table, row);
1677 * TODO options: -v verbose, -f dont stop if file not found
1678 * -h remove by hash, use fnmatch
1682 int com_afs_rm(__a_unused int fd, int argc, char * const * const argv)
1684 struct com_rm_options cro = {.flags = 0};
1685 struct osl_object options = {.data = &cro, .size = sizeof(cro)};
1688 for (i = 1; i < argc; i++) {
1689 const char *arg = argv[i];
1692 if (!strcmp(arg, "--")) {
1697 ret = -E_AFT_SYNTAX;
1700 return send_option_arg_callback_request(&options, argc - i,
1701 argv + i, com_rm_callback, NULL);
1706 /* TODO: optionally fix problems by removing offending rows */
1707 static int check_audio_file(struct osl_row *row, void *data)
1710 struct para_buffer *pb = data;
1711 struct stat statbuf;
1712 int ret = get_audio_file_path_of_row(row, &path);
1713 struct afs_info afsi;
1717 para_printf(pb, "%s\n", PARA_STRERROR(-ret));
1720 if (stat(path, &statbuf) < 0)
1721 para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
1723 if (!S_ISREG(statbuf.st_mode))
1724 para_printf(pb, "%s: not a regular file\n", path);
1726 ret = get_afsi_of_row(row, &afsi);
1728 para_printf(pb, "%s: %s\n", path, PARA_STRERROR(-ret));
1731 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
1733 para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
1734 PARA_STRERROR(-ret));
1735 ret = img_get_name_by_id(afsi.image_id, &blob_name);
1737 para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
1738 PARA_STRERROR(-ret));
1742 int aft_check_callback(__a_unused const struct osl_object *query, struct osl_object *result)
1744 struct para_buffer pb = {.buf = NULL};
1746 para_printf(&pb, "checking audio file table...\n");
1747 audio_file_loop(&pb, check_audio_file);
1748 result->data = pb.buf;
1749 result->size = pb.size;
1755 * Close the audio file table.
1757 * \param flags Ususal flags that are passed to osl_close_table().
1759 * \sa osl_close_table().
1761 void aft_shutdown(enum osl_close_flags flags)
1763 osl_close_table(audio_file_table, flags);
1764 audio_file_table = NULL;
1768 * Open the audio file table.
1770 * \param ti Gets initialized by this function.
1771 * \param db The database directory.
1773 * \return Positive on success, negative on errors.
1775 * \sa osl_open_table().
1777 int aft_init(struct table_info *ti, const char *db)
1781 audio_file_table_desc.dir = db;
1782 ti->desc = &audio_file_table_desc;
1783 ret = osl_open_table(ti->desc, &ti->table);
1786 audio_file_table = ti->table;
1787 osl_get_num_rows(audio_file_table, &num);
1788 PARA_INFO_LOG("audio file table contains %d files\n", num);
1791 PARA_INFO_LOG("failed to open audio file table\n");
1792 audio_file_table = NULL;
1793 return ret == -E_NOENT? 1 : ret;