2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
9 #include <dirent.h> /* readdir() */
24 #include "portable_io.h"
26 static struct osl_table *audio_file_table;
27 static char *current_status_items;
29 /** The different sorting methods of the ls command. */
30 enum ls_sorting_method {
36 LS_SORT_BY_LAST_PLAYED,
38 LS_SORT_BY_NUM_PLAYED,
52 LS_SORT_BY_AUDIO_FORMAT,
57 /** The different listing modes of the ls command. */
58 enum ls_listing_mode {
59 /** Default listing mode. */
71 /** The flags accepted by the ls command. */
74 LS_FLAG_FULL_PATH = 1,
76 LS_FLAG_ADMISSIBLE_ONLY = 2,
82 * The size of the individual output fields of the ls command.
84 * These depend on the actual content being listed. If, for instance only files
85 * with duration less than an hour are being listed, then the duration with is
86 * made smaller because then the duration is listed as mm:ss rather than
90 /** size of the score field. */
91 unsigned short score_width;
92 /** size of the image id field. */
93 unsigned short image_id_width;
94 /** size of the lyrics id field. */
95 unsigned short lyrics_id_width;
96 /** size of the bitrate field. */
97 unsigned short bitrate_width;
98 /** size of the frequency field. */
99 unsigned short frequency_width;
100 /** size of the duration field. */
101 unsigned short duration_width;
102 /** size of the num played field. */
103 unsigned short num_played_width;
104 /** size of the amp field. */
105 unsigned short amp_width;
108 /** Data passed from the ls command handler to its callback function. */
110 /** The given command line flags. */
112 /** The sorting method given at the command line. */
113 enum ls_sorting_method sorting;
114 /** The given listing mode (short, long, verbose, mbox). */
115 enum ls_listing_mode mode;
116 /** The arguments passed to the ls command. */
118 /** Number of non-option arguments. */
120 /** Used for long listing mode to align the output fields. */
121 struct ls_widths widths;
122 /** Size of the \a data array. */
124 /** Number of used entries in the data array. */
125 uint32_t num_matching_paths;
126 /** Array of matching entries. */
127 struct ls_data *data;
128 /** Used to sort the array. */
129 struct ls_data **data_ptr;
133 * Describes the layout of the mmapped-afs info struct.
135 * \sa struct afs_info.
138 /** Where .last_played is stored. */
139 AFSI_LAST_PLAYED_OFFSET = 0,
140 /** Storage position of the attributes bitmap. */
141 AFSI_ATTRIBUTES_OFFSET = 8,
142 /** Storage position of the .num_played field. */
143 AFSI_NUM_PLAYED_OFFSET = 16,
144 /** Storage position of the .image_id field. */
145 AFSI_IMAGE_ID_OFFSET = 20,
146 /** Storage position of the .lyrics_id field. */
147 AFSI_LYRICS_ID_OFFSET = 24,
148 /** Storage position of the .audio_format_id field. */
149 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
150 /** Storage position of the amplification field. */
151 AFSI_AMP_OFFSET = 29,
152 /** 2 bytes reserved space for future usage. */
153 AFSI_AUDIO_FORMAT_UNUSED_OFFSET = 30,
154 /** On-disk storage space needed. */
159 * Convert a struct afs_info to an osl object.
161 * \param afsi Pointer to the audio file info to be converted.
162 * \param obj Result pointer.
166 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
168 char *buf = obj->data;
170 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
171 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
172 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
173 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
174 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
175 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
176 afsi->audio_format_id);
177 write_u8(buf + AFSI_AMP_OFFSET, afsi->amp);
178 memset(buf + AFSI_AUDIO_FORMAT_UNUSED_OFFSET, 0, 2);
182 * Get the audio file selector info struct stored in an osl object.
184 * \param afsi Points to the audio_file info structure to be filled in.
185 * \param obj The osl object holding the data.
187 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
191 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
193 char *buf = obj->data;
194 if (obj->size < AFSI_SIZE)
196 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
197 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
198 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
199 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
200 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
201 afsi->audio_format_id = read_u8(buf +
202 AFSI_AUDIO_FORMAT_ID_OFFSET);
203 afsi->amp = read_u8(buf + AFSI_AMP_OFFSET);
207 /** The columns of the audio file table. */
208 enum audio_file_table_columns {
209 /** The hash on the content of the audio file. */
211 /** The full path in the filesystem. */
213 /** The audio file selector info. */
215 /** The audio format handler info. */
217 /** The chunk table info and the chunk table of the audio file. */
219 /** The number of columns of this table. */
224 * Compare two osl objects pointing to hash values.
226 * \param obj1 Pointer to the first hash object.
227 * \param obj2 Pointer to the second hash object.
229 * \return The values required for an osl compare function.
231 * \sa osl_compare_func, uint32_compare().
233 int aft_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
235 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
238 static struct osl_column_description aft_cols[] = {
240 .storage_type = OSL_MAPPED_STORAGE,
241 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
243 .compare_function = aft_hash_compare,
244 .data_size = HASH_SIZE
247 .storage_type = OSL_MAPPED_STORAGE,
248 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
250 .compare_function = string_compare,
253 .storage_type = OSL_MAPPED_STORAGE,
254 .storage_flags = OSL_FIXED_SIZE,
256 .data_size = AFSI_SIZE
259 .storage_type = OSL_MAPPED_STORAGE,
263 .storage_type = OSL_DISK_STORAGE,
268 static struct osl_table_description audio_file_table_desc = {
269 .name = "audio_files",
270 .num_columns = NUM_AFT_COLUMNS,
271 .flags = OSL_LARGE_TABLE,
272 .column_descriptions = aft_cols
275 /* We don't want * dot or dot-dot anywhere. */
276 static int verify_dotfile(const char *rest)
279 * The first character was '.', but that has already been discarded, we
283 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
285 case '.': /* path start with /foo/.. */
286 if (rest[1] == '\0' || rest[1] == '/')
287 return -1; /* /foo/.. or /foo/../bar are not ok */
288 /* /foo/..bar is ok */
294 * We fundamentally don't like some paths: We don't want double slashes or
295 * slashes at the end that can make pathnames ambiguous.
297 static int verify_path(const char *orig_path, char **resolved_path)
303 if (*orig_path != '/') /* we only accept absolute paths */
305 len = strlen(orig_path);
306 *resolved_path = para_strdup(orig_path);
307 path = *resolved_path;
308 while (len > 1 && path[--len] == '/')
309 path[len] = '\0'; /* remove slash at the end */
315 case '/': /* double slash */
318 if (verify_dotfile(path) < 0)
328 free(*resolved_path);
332 /** The on-disk layout of a afhi struct. */
334 /** Where the number of seconds is stored. */
335 AFHI_SECONDS_TOTAL_OFFSET = 0,
336 /** Position of the bitrate. */
337 AFHI_BITRATE_OFFSET = 4,
338 /** Position of the frequency. */
339 AFHI_FREQUENCY_OFFSET = 8,
340 /** Location of the audio file header. */
341 AFHI_HEADER_OFFSET_OFFSET = 12,
342 /* Length of the audio file header. Zero means: No header. */
343 AFHI_HEADER_LEN_OFFSET = 16,
344 /** The total number of chunks (4 bytes). */
345 CHUNKS_TOTAL_OFFSET = 20,
346 /** The length of the audio file header (4 bytes). */
347 HEADER_LEN_OFFSET = 24,
348 /** The start of the audio file header (4 bytes). */
349 HEADER_OFFSET_OFFSET = 28,
350 /** The seconds part of the chunk time (4 bytes). */
351 CHUNK_TV_TV_SEC_OFFSET = 32,
352 /** The microseconds part of the chunk time (4 bytes). */
353 CHUNK_TV_TV_USEC_OFFSET = 36,
354 /** Number of channels is stored here. (1 byte) */
355 AFHI_CHANNELS_OFFSET = 40,
356 /** The tag info position. */
357 AFHI_INFO_STRING_OFFSET = 41,
358 /** Minimal on-disk size of a valid afhi struct. */
359 MIN_AFHI_SIZE = 47, /* at least 6 null bytes for techinfo/tags */
362 static unsigned sizeof_afhi_buf(const struct afh_info *afhi)
367 + strlen(afhi->techinfo)
368 + strlen(afhi->tags.artist)
369 + strlen(afhi->tags.title)
370 + strlen(afhi->tags.year)
371 + strlen(afhi->tags.album)
372 + strlen(afhi->tags.comment);
375 static void save_afhi(struct afh_info *afhi, char *buf)
381 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET, afhi->seconds_total);
382 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
383 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
384 write_u32(buf + AFHI_HEADER_OFFSET_OFFSET, afhi->header_offset);
385 write_u32(buf + AFHI_HEADER_LEN_OFFSET, afhi->header_len);
386 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
387 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
388 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
389 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
390 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
391 write_u32(buf + CHUNK_TV_TV_USEC_OFFSET, afhi->chunk_tv.tv_usec);
392 p = buf + AFHI_INFO_STRING_OFFSET;
393 /* The sprintf's below are OK as our caller made sure that buf is large enough */
394 p += sprintf(p, "%s", afhi->techinfo) + 1;
395 p += sprintf(p, "%s", afhi->tags.artist) + 1;
396 p += sprintf(p, "%s", afhi->tags.title) + 1;
397 p += sprintf(p, "%s", afhi->tags.year) + 1;
398 p += sprintf(p, "%s", afhi->tags.album) + 1;
399 sprintf(p, "%s", afhi->tags.comment);
402 static void load_afhi(const char *buf, struct afh_info *afhi)
404 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
405 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
406 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
407 afhi->header_offset = read_u32(buf + AFHI_HEADER_OFFSET_OFFSET);
408 afhi->header_len = read_u32(buf + AFHI_HEADER_LEN_OFFSET);
409 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
410 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
411 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
412 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
413 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
414 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC_OFFSET);
415 afhi->techinfo = buf + AFHI_INFO_STRING_OFFSET;
416 afhi->tags.artist = afhi->techinfo + strlen(afhi->techinfo) + 1;
417 afhi->tags.title = afhi->tags.artist + strlen(afhi->tags.artist) + 1;
418 afhi->tags.year = afhi->tags.title + strlen(afhi->tags.title) + 1;
419 afhi->tags.album = afhi->tags.year + strlen(afhi->tags.year) + 1;
420 afhi->tags.comment = afhi->tags.album + strlen(afhi->tags.album) + 1;
423 static unsigned sizeof_chunk_table(struct afh_info *afhi)
427 return 4 * (afhi->chunks_total + 1);
430 static void save_chunk_table(struct afh_info *afhi, char *buf)
434 for (i = 0; i <= afhi->chunks_total; i++)
435 write_u32(buf + 4 * i, afhi->chunk_table[i]);
438 static void load_chunk_table(struct afh_info *afhi, char *buf)
442 afhi->chunk_table = para_malloc(sizeof_chunk_table(afhi));
443 for (i = 0; i <= afhi->chunks_total; i++)
444 afhi->chunk_table[i] = read_u32(buf + 4 * i);
448 * Get the row of the audio file table corresponding to the given path.
450 * \param path The full path of the audio file.
451 * \param row Result pointer.
455 int aft_get_row_of_path(const char *path, struct osl_row **row)
457 struct osl_object obj = {.data = (char *)path, .size = strlen(path) + 1};
459 return osl(osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row));
463 * Get the row of the audio file table corresponding to the given hash value.
465 * \param hash The hash value of the desired audio file.
466 * \param row Result pointer.
470 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
472 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
473 return osl(osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row));
477 * Get the osl object holding the audio file selector info of a row.
479 * \param row Pointer to a row in the audio file table.
480 * \param obj Result pointer.
484 int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
486 return osl(osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj));
490 * Get the osl object holding the audio file selector info, given a path.
493 * \param path The full path of the audio file.
494 * \param obj Result pointer.
496 * \return Positive on success, negative on errors.
498 int get_afsi_object_of_path(const char *path, struct osl_object *obj)
501 int ret = aft_get_row_of_path(path, &row);
504 return get_afsi_object_of_row(row, obj);
508 * Get the audio file selector info, given a row of the audio file table.
510 * \param row Pointer to a row in the audio file table.
511 * \param afsi Result pointer.
513 * \return Positive on success, negative on errors.
515 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
517 struct osl_object obj;
518 int ret = get_afsi_object_of_row(row, &obj);
521 return load_afsi(afsi, &obj);
525 * Get the audio file selector info, given the path of an audio table.
527 * \param path The full path of the audio file.
528 * \param afsi Result pointer.
530 * \return Positive on success, negative on errors.
532 int get_afsi_of_path(const char *path, struct afs_info *afsi)
534 struct osl_object obj;
535 int ret = get_afsi_object_of_path(path, &obj);
538 return load_afsi(afsi, &obj);
542 * Get the path of an audio file, given a row of the audio file table.
544 * \param row Pointer to a row in the audio file table.
545 * \param path Result pointer.
547 * The result is a pointer to mmapped data. The caller must not attempt
552 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
554 struct osl_object path_obj;
555 int ret = osl(osl_get_object(audio_file_table, row, AFTCOL_PATH,
559 *path = path_obj.data;
564 * Get the object containing the hash value of an audio file, given a row.
566 * \param row Pointer to a row of the audio file table.
567 * \param obj Result pointer.
569 * \return The return value of the underlying call to osl_get_object().
571 * \sa get_hash_of_row().
573 static int get_hash_object_of_aft_row(const struct osl_row *row,
574 struct osl_object *obj)
576 return osl(osl_get_object(audio_file_table, row, AFTCOL_HASH, obj));
580 * Get the hash value of an audio file, given a row of the audio file table.
582 * \param row Pointer to a row of the audio file table.
583 * \param hash Result pointer.
585 * \a hash points to mapped data and must not be freed by the caller.
587 * \return The return value of the underlying call to
588 * get_hash_object_of_aft_row().
590 static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash)
592 struct osl_object obj;
593 int ret = get_hash_object_of_aft_row(row, &obj);
602 * Get the audio format handler info, given a row of the audio file table.
604 * \param row Pointer to a row of the audio file table.
605 * \param afhi Result pointer.
607 * \return The return value of the underlying call to osl_get_object().
609 * \sa get_chunk_table_of_row().
611 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi)
613 struct osl_object obj;
614 int ret = osl(osl_get_object(audio_file_table, row, AFTCOL_AFHI,
618 load_afhi(obj.data, afhi);
622 /* returns shmid on success */
623 static int save_afd(struct audio_file_data *afd)
625 size_t size = sizeof(*afd) + sizeof_chunk_table(&afd->afhi);
626 int shmid, ret = shm_new(size);
633 ret = shm_attach(shmid, ATTACH_RW, &shm_afd);
636 *(struct audio_file_data *)shm_afd = *afd;
639 save_chunk_table(&afd->afhi, buf);
647 int load_afd(int shmid, struct audio_file_data *afd)
653 ret = shm_attach(shmid, ATTACH_RO, &shm_afd);
656 *afd = *(struct audio_file_data *)shm_afd;
659 load_chunk_table(&afd->afhi, buf);
664 static int get_local_time(uint64_t *seconds, char *buf, size_t size,
665 time_t current_time, enum ls_listing_mode lm)
669 if (!localtime_r((time_t *)seconds, &t))
671 if (lm == LS_MODE_MBOX) {
672 if (!strftime(buf, size, "%c", &t))
676 if (*seconds + 6 * 30 * 24 * 3600 > current_time) {
677 if (!strftime(buf, size, "%b %e %k:%M", &t))
681 if (!strftime(buf, size, "%b %e %Y", &t))
686 /** Compute the number of (decimal) digits of a number. */
687 #define GET_NUM_DIGITS(x, num) { \
688 typeof((x)) _tmp = PARA_ABS(x); \
691 while ((_tmp) > 9) { \
697 static short unsigned get_duration_width(int seconds)
699 short unsigned width;
700 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
702 if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
703 return 4 + (mins > 9);
704 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
705 GET_NUM_DIGITS(hours, &width);
709 static void get_duration_buf(int seconds, char *buf, struct ls_options *opts)
711 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
712 short unsigned max_width;
714 if (!hours) { /* m:ss or mm:ss */
715 max_width = opts->mode == LS_MODE_LONG?
716 opts->widths.duration_width : 4;
717 sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
718 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
719 max_width = opts->mode == LS_MODE_LONG?
720 opts->widths.duration_width : 7;
721 sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
726 static char *make_attribute_lines(const char *att_bitmap, struct afs_info *afsi)
728 char *att_text, *att_lines;
730 get_attribute_text(&afsi->attributes, " ", &att_text);
732 return para_strdup(att_bitmap);
733 att_lines = make_message("%s: %s\n%s: %s",
734 status_item_list[SI_ATTRIBUTES_BITMAP], att_bitmap,
735 status_item_list[SI_ATTRIBUTES_TXT], att_text);
740 static char *make_lyrics_lines(struct afs_info *afsi)
744 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
745 return make_message("%s: %u\n%s: %s\n",
746 status_item_list[SI_LYRICS_ID], afsi->lyrics_id,
747 status_item_list[SI_LYRICS_NAME], lyrics_name?
748 lyrics_name : "(none)");
751 static char *make_image_lines(struct afs_info *afsi)
754 img_get_name_by_id(afsi->image_id, &image_name);
755 return make_message("%s: %u\n%s: %s\n",
756 status_item_list[SI_IMAGE_ID], afsi->image_id,
757 status_item_list[SI_IMAGE_NAME], image_name?
758 image_name : "(none)");
761 static char *make_filename_lines(const char *path, unsigned flags)
764 const char *basename;
766 if (!(flags & LS_FLAG_FULL_PATH))
767 return make_message("%s: %s\n",
768 status_item_list[SI_BASENAME], path);
769 basename = para_basename(path),
770 dirname = para_dirname(path);
771 ret = make_message("%s: %s\n%s: %s\n%s: %s\n",
772 status_item_list[SI_PATH], path,
773 status_item_list[SI_DIRECTORY], dirname? dirname : "?",
774 status_item_list[SI_BASENAME], basename? basename : "?");
779 static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
781 struct osl_object chunk_table_obj;
782 struct osl_row *aft_row;
786 ret = aft_get_row_of_hash(d->hash, &aft_row);
789 ret = osl_open_disk_object(audio_file_table, aft_row,
790 AFTCOL_CHUNKS, &chunk_table_obj);
793 ret = para_printf(b, "%s\n"
794 "chunk_time: %lu:%lu\nchunk_offsets: ",
796 (long unsigned) d->afhi.chunk_tv.tv_sec,
797 (long unsigned) d->afhi.chunk_tv.tv_usec
801 buf = chunk_table_obj.data;
802 for (i = 0; i <= d->afhi.chunks_total; i++) {
803 ret = para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
807 ret = para_printf(b, "\n");
809 osl_close_disk_object(&chunk_table_obj);
813 static int print_list_item(struct ls_data *d, struct ls_options *opts,
814 struct para_buffer *b, time_t current_time)
818 char last_played_time[30];
819 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
820 char score_buf[30] = "";
821 struct afs_info *afsi = &d->afsi;
822 struct afh_info *afhi = &d->afhi;
823 struct ls_widths *w = &opts->widths;
824 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
825 char asc_hash[2 * HASH_SIZE + 1];
826 char *att_lines, *lyrics_lines, *image_lines, *filename_lines;
828 if (opts->mode == LS_MODE_SHORT) {
829 ret = para_printf(b, "%s\n", d->path);
832 if (opts->mode == LS_MODE_CHUNKS) {
833 ret = print_chunk_table(d, b);
836 get_attribute_bitmap(&afsi->attributes, att_buf);
837 ret = get_local_time(&afsi->last_played, last_played_time,
838 sizeof(last_played_time), current_time, opts->mode);
841 get_duration_buf(afhi->seconds_total, duration_buf, opts);
843 if (opts->mode == LS_MODE_LONG)
844 sprintf(score_buf, "%*li ", w->score_width, d->score);
846 sprintf(score_buf, "%li ", d->score);
849 if (opts->mode == LS_MODE_LONG) {
852 "%s " /* attributes */
854 "%*d " /* image_id */
855 "%*d " /* lyrics_id */
857 "%s " /* audio format */
858 "%*d " /* frequency */
861 "%*d " /* num_played */
862 "%s " /* last_played */
866 w->amp_width, afsi->amp,
867 w->image_id_width, afsi->image_id,
868 w->lyrics_id_width, afsi->lyrics_id,
869 w->bitrate_width, afhi->bitrate,
870 audio_format_name(afsi->audio_format_id),
871 w->frequency_width, afhi->frequency,
874 w->num_played_width, afsi->num_played,
880 hash_to_asc(d->hash, asc_hash);
881 att_lines = make_attribute_lines(att_buf, afsi);
882 lyrics_lines = make_lyrics_lines(afsi);
883 image_lines = make_image_lines(afsi);
884 filename_lines = make_filename_lines(d->path, opts->flags);
885 if (opts->mode == LS_MODE_MBOX) {
886 const char *bn = para_basename(d->path);
888 "From foo@localhost %s\n"
889 "Received: from\nTo: bar\nFrom: a\n"
897 "%s" /* filename stuff */
898 "%s%s%s%s" /* score */
899 "%s\n" /* attributes */
900 "%s: %s\n" /* hash */
901 "%s" /* image id, image name */
903 "%s: %dkbit/s\n" /* bitrate */
904 "%s: %s\n" /* format */
905 "%s: %dHz\n" /* frequency */
906 "%s: %d\n" /* channels */
907 "%s: %s\n" /* duration */
908 "%s: %lu\n" /* seconds total */
909 "%s: %s\n" /* last played time */
910 "%s: %d\n" /* num_played */
911 "%s: %u\n" /* amplification */
912 "%s: %lu\n" /* chunk time */
913 "%s: %lu\n" /* num chunks */
914 "%s: %s\n" /* techinfo */
915 "%s: %s\n" /* artist */
916 "%s: %s\n" /* title */
917 "%s: %s\n" /* year */
918 "%s: %s\n" /* album */
919 "%s: %s\n", /* comment */
921 have_score? status_item_list[SI_SCORE] : "",
922 have_score? ": " : "",
924 have_score? "\n" : "",
926 status_item_list[SI_HASH], asc_hash,
929 status_item_list[SI_BITRATE], afhi->bitrate,
930 status_item_list[SI_FORMAT], audio_format_name(afsi->audio_format_id),
931 status_item_list[SI_FREQUENCY], afhi->frequency,
932 status_item_list[SI_CHANNELS], afhi->channels,
933 status_item_list[SI_DURATION], duration_buf,
934 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
935 status_item_list[SI_LAST_PLAYED], last_played_time,
936 status_item_list[SI_NUM_PLAYED], afsi->num_played,
937 status_item_list[SI_AMPLIFICATION], afsi->amp,
938 status_item_list[SI_CHUNK_TIME], tv2ms(&afhi->chunk_tv),
939 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
940 status_item_list[SI_TECHINFO], afhi->techinfo,
941 status_item_list[SI_ARTIST], afhi->tags.artist,
942 status_item_list[SI_TITLE], afhi->tags.title,
943 status_item_list[SI_YEAR], afhi->tags.year,
944 status_item_list[SI_ALBUM], afhi->tags.album,
945 status_item_list[SI_COMMENT], afhi->tags.comment
949 if (opts->mode == LS_MODE_MBOX) {
950 struct osl_object lyrics_def;
951 lyr_get_def_by_id(afsi->lyrics_id, &lyrics_def);
952 if (lyrics_def.data) {
953 ret = para_printf(b, "Lyrics:\n~~~~~~~\n%s",
954 (char *)lyrics_def.data);
955 osl_close_disk_object(&lyrics_def);
961 free(filename_lines);
966 static int make_status_items(struct audio_file_data *afd,
967 struct afs_info *afsi, char *path, long score,
977 struct ls_options opts = {
978 .flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
979 .mode = LS_MODE_VERBOSE,
981 struct para_buffer pb = {.max_size = SHMMAX - 1};
986 ret = print_list_item(&d, &opts, &pb, current_time);
989 free(current_status_items);
990 current_status_items = pb.buf;
995 * Mmap the given audio file and update statistics.
997 * \param aft_row Determines the audio file to be opened and updated.
998 * \param score The score of the audio file.
999 * \param afd Result pointer.
1001 * On success, the numplayed field of the audio file selector info is increased
1002 * and the lastplayed time is set to the current time. Finally, the score of
1003 * the audio file is updated.
1005 * \return Positive shmid on success, negative on errors.
1007 int open_and_update_audio_file(struct osl_row *aft_row, long score,
1008 struct audio_file_data *afd)
1010 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
1011 struct osl_object afsi_obj;
1012 struct afs_info old_afsi, new_afsi;
1013 int ret = get_hash_of_row(aft_row, &aft_hash);
1014 struct afsi_change_event_data aced;
1015 struct osl_object map, chunk_table_obj;
1020 ret = get_audio_file_path_of_row(aft_row, &path);
1023 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
1026 ret = load_afsi(&old_afsi, &afsi_obj);
1029 ret = get_afhi_of_row(aft_row, &afd->afhi);
1032 afd->afhi.chunk_table = NULL;
1033 ret = osl_open_disk_object(audio_file_table, aft_row,
1034 AFTCOL_CHUNKS, &chunk_table_obj);
1037 ret = mmap_full_file(path, O_RDONLY, &map.data,
1038 &map.size, &afd->fd);
1041 hash_function(map.data, map.size, file_hash);
1042 ret = hash_compare(file_hash, aft_hash);
1043 para_munmap(map.data, map.size);
1045 ret = -E_HASH_MISMATCH;
1048 new_afsi = old_afsi;
1049 new_afsi.num_played++;
1050 new_afsi.last_played = time(NULL);
1051 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
1053 load_chunk_table(&afd->afhi, chunk_table_obj.data);
1054 ret = make_status_items(afd, &old_afsi, path, score, file_hash);
1057 aced.aft_row = aft_row;
1058 aced.old_afsi = &old_afsi;
1059 afs_event(AFSI_CHANGE, NULL, &aced);
1060 ret = save_afd(afd);
1062 free(afd->afhi.chunk_table);
1063 osl_close_disk_object(&chunk_table_obj);
1067 static int ls_audio_format_compare(const void *a, const void *b)
1069 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1070 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
1073 static int ls_duration_compare(const void *a, const void *b)
1075 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1076 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
1079 static int ls_bitrate_compare(const void *a, const void *b)
1081 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1082 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
1085 static int ls_lyrics_id_compare(const void *a, const void *b)
1087 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1088 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
1091 static int ls_image_id_compare(const void *a, const void *b)
1093 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1094 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
1097 static int ls_channels_compare(const void *a, const void *b)
1099 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1100 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
1103 static int ls_frequency_compare(const void *a, const void *b)
1105 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1106 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
1109 static int ls_num_played_compare(const void *a, const void *b)
1111 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1112 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
1115 static int ls_last_played_compare(const void *a, const void *b)
1117 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1118 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
1121 static int ls_score_compare(const void *a, const void *b)
1123 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1124 return NUM_COMPARE(d1->score, d2->score);
1127 static int ls_path_compare(const void *a, const void *b)
1129 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1130 return strcmp(d1->path, d2->path);
1133 static int sort_matching_paths(struct ls_options *options)
1135 size_t nmemb = options->num_matching_paths;
1136 size_t size = sizeof(*options->data_ptr);
1137 int (*compar)(const void *, const void *);
1140 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
1141 for (i = 0; i < nmemb; i++)
1142 options->data_ptr[i] = options->data + i;
1144 /* In these cases the array is already sorted */
1145 if (options->sorting == LS_SORT_BY_PATH
1146 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1147 && (options->flags & LS_FLAG_FULL_PATH))
1149 if (options->sorting == LS_SORT_BY_SCORE &&
1150 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1153 switch (options->sorting) {
1154 case LS_SORT_BY_PATH:
1155 compar = ls_path_compare; break;
1156 case LS_SORT_BY_SCORE:
1157 compar = ls_score_compare; break;
1158 case LS_SORT_BY_LAST_PLAYED:
1159 compar = ls_last_played_compare; break;
1160 case LS_SORT_BY_NUM_PLAYED:
1161 compar = ls_num_played_compare; break;
1162 case LS_SORT_BY_FREQUENCY:
1163 compar = ls_frequency_compare; break;
1164 case LS_SORT_BY_CHANNELS:
1165 compar = ls_channels_compare; break;
1166 case LS_SORT_BY_IMAGE_ID:
1167 compar = ls_image_id_compare; break;
1168 case LS_SORT_BY_LYRICS_ID:
1169 compar = ls_lyrics_id_compare; break;
1170 case LS_SORT_BY_BITRATE:
1171 compar = ls_bitrate_compare; break;
1172 case LS_SORT_BY_DURATION:
1173 compar = ls_duration_compare; break;
1174 case LS_SORT_BY_AUDIO_FORMAT:
1175 compar = ls_audio_format_compare; break;
1179 qsort(options->data_ptr, nmemb, size, compar);
1183 /* row is either an aft_row or a row of the score table */
1184 /* TODO: Only compute widths if we need them */
1185 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
1188 struct ls_options *options = ls_opts;
1190 struct ls_widths *w;
1191 unsigned short num_digits;
1193 struct osl_row *aft_row;
1197 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1198 ret = get_score_and_aft_row(row, &score, &aft_row);
1203 ret = get_audio_file_path_of_row(aft_row, &path);
1206 if (!(options->flags & LS_FLAG_FULL_PATH)) {
1207 char *p = strrchr(path, '/');
1211 if (options->num_patterns) {
1212 for (i = 0; i < options->num_patterns; i++) {
1213 ret = fnmatch(options->patterns[i], path, 0);
1216 if (ret == FNM_NOMATCH)
1220 if (i >= options->num_patterns) /* no match */
1223 tmp = options->num_matching_paths++;
1224 if (options->num_matching_paths > options->array_size) {
1225 options->array_size++;
1226 options->array_size *= 2;
1227 options->data = para_realloc(options->data, options->array_size
1228 * sizeof(*options->data));
1230 d = options->data + tmp;
1231 ret = get_afsi_of_row(aft_row, &d->afsi);
1234 ret = get_afhi_of_row(aft_row, &d->afhi);
1238 ret = get_hash_of_row(aft_row, &d->hash);
1241 w = &options->widths;
1242 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
1243 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
1244 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
1245 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
1246 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
1247 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
1248 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
1249 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
1250 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
1251 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
1252 /* get the number of chars to print this amount of time */
1253 num_digits = get_duration_width(d->afhi.seconds_total);
1254 w->duration_width = PARA_MAX(w->duration_width, num_digits);
1255 GET_NUM_DIGITS(d->afsi.amp, &num_digits);
1256 w->amp_width = PARA_MAX(w->amp_width, num_digits);
1257 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1258 GET_NUM_DIGITS(score, &num_digits);
1259 num_digits++; /* add one for the sign (space or "-") */
1260 w->score_width = PARA_MAX(w->score_width, num_digits);
1268 static void com_ls_callback(int fd, const struct osl_object *query)
1270 struct ls_options *opts = query->data;
1271 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
1272 struct para_buffer b = {.max_size = SHMMAX,
1273 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1275 time_t current_time;
1278 if (opts->num_patterns) {
1279 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
1280 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
1281 opts->patterns[i] = p;
1285 opts->patterns = NULL;
1286 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
1287 ret = admissible_file_loop(opts, prepare_ls_row);
1289 ret = osl(osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
1293 if (!opts->num_matching_paths)
1295 ret = sort_matching_paths(opts);
1298 time(¤t_time);
1299 if (opts->flags & LS_FLAG_REVERSE)
1300 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1301 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1306 for (i = 0; i < opts->num_matching_paths; i++) {
1307 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1313 pass_buffer_as_shm(b.buf, b.offset, &fd);
1316 free(opts->data_ptr);
1317 free(opts->patterns);
1321 * TODO: flags -h (sort by hash)
1323 int com_ls(int fd, int argc, char * const * const argv)
1327 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1328 enum ls_listing_mode mode = LS_MODE_SHORT;
1329 struct ls_options opts = {.patterns = NULL};
1330 struct osl_object query = {.data = &opts, .size = sizeof(opts)};
1332 for (i = 1; i < argc; i++) {
1333 const char *arg = argv[i];
1336 if (!strcmp(arg, "--")) {
1340 if (!strncmp(arg, "-l", 2)) {
1342 mode = LS_MODE_LONG;
1346 return -E_AFT_SYNTAX;
1347 switch(*(arg + 2)) {
1349 mode = LS_MODE_SHORT;
1352 mode = LS_MODE_LONG;
1355 mode = LS_MODE_VERBOSE;
1358 mode = LS_MODE_MBOX;
1361 mode = LS_MODE_CHUNKS;
1364 return -E_AFT_SYNTAX;
1367 if (!strcmp(arg, "-p")) {
1368 flags |= LS_FLAG_FULL_PATH;
1371 if (!strcmp(arg, "-a")) {
1372 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1375 if (!strcmp(arg, "-r")) {
1376 flags |= LS_FLAG_REVERSE;
1379 if (!strncmp(arg, "-s", 2)) {
1380 if (!*(arg + 2) || *(arg + 3))
1381 return -E_AFT_SYNTAX;
1382 switch(*(arg + 2)) {
1384 sort = LS_SORT_BY_PATH;
1386 case 's': /* -ss implies -a */
1387 sort = LS_SORT_BY_SCORE;
1388 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1391 sort = LS_SORT_BY_LAST_PLAYED;
1394 sort = LS_SORT_BY_NUM_PLAYED;
1397 sort = LS_SORT_BY_FREQUENCY;
1400 sort = LS_SORT_BY_CHANNELS;
1403 sort = LS_SORT_BY_IMAGE_ID;
1406 sort = LS_SORT_BY_LYRICS_ID;
1409 sort = LS_SORT_BY_BITRATE;
1412 sort = LS_SORT_BY_DURATION;
1415 sort = LS_SORT_BY_AUDIO_FORMAT;
1418 return -E_AFT_SYNTAX;
1421 return -E_AFT_SYNTAX;
1424 opts.sorting = sort;
1426 opts.num_patterns = argc - i;
1427 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1428 argv + i, com_ls_callback, send_result, &fd);
1433 * Call the given function for each file in the audio file table.
1435 * \param private_data An arbitrary data pointer, passed to \a func.
1436 * \param func The custom function to be called.
1440 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1442 return osl(osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1446 static struct osl_row *find_hash_sister(HASH_TYPE *hash)
1448 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1449 struct osl_row *row;
1451 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1455 /** The format of the data stored by save_audio_file_data(). */
1456 enum com_add_buffer_offsets {
1457 /** afhi (if present) starts here. */
1458 CAB_AFHI_OFFSET_POS = 0,
1459 /** Start of the chunk table (if present). */
1460 CAB_CHUNKS_OFFSET_POS = 2,
1461 /** Audio format id. */
1462 CAB_AUDIO_FORMAT_OFFSET = 4,
1463 /** Flags given to the add command. */
1464 CAB_FLAGS_OFFSET = 5,
1465 /** The hash of the audio file being added. */
1466 CAB_HASH_OFFSET = 9,
1467 /** Start of the path of the audio file. */
1468 CAB_PATH_OFFSET = (CAB_HASH_OFFSET + HASH_SIZE),
1472 * Store the given data to a single buffer. Doesn't need the audio file selector
1473 * info struct as the server knows it as well.
1475 * It's OK to call this with afhi == NULL. In this case, the audio format
1476 * handler info won't be stored in the buffer.
1478 static void save_add_callback_buffer(HASH_TYPE *hash, const char *path,
1479 struct afh_info *afhi, uint32_t flags,
1480 uint8_t audio_format_num, struct osl_object *obj)
1482 size_t path_len = strlen(path) + 1;
1483 size_t afhi_size = sizeof_afhi_buf(afhi);
1484 size_t size = CAB_PATH_OFFSET + path_len + afhi_size
1485 + sizeof_chunk_table(afhi);
1486 char *buf = para_malloc(size);
1489 write_u8(buf + CAB_AUDIO_FORMAT_OFFSET, audio_format_num);
1490 write_u32(buf + CAB_FLAGS_OFFSET, flags);
1492 memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE);
1493 strcpy(buf + CAB_PATH_OFFSET, path);
1495 pos = CAB_PATH_OFFSET + path_len;
1496 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1497 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1498 pos + afhi_size - 1);
1499 write_u16(buf + CAB_AFHI_OFFSET_POS, pos);
1500 save_afhi(afhi, buf + pos);
1503 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1504 write_u16(buf + CAB_CHUNKS_OFFSET_POS, pos);
1506 save_chunk_table(afhi, buf + pos);
1507 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1514 Overview of the add command.
1516 Input: What was passed to the callback by the command handler.
1518 HS: Hash sister. Whether an audio file with identical hash
1519 already exists in the osl database.
1521 PB: Path brother. Whether a file with the given path exists
1524 F: Force flag given. Whether add was called with -f.
1526 output: Action performed by the callback.
1528 AFHI: Whether afhi and chunk table are computed and sent.
1529 ACTION: Table modifications to be done by the callback.
1531 +----+----+---+------+---------------------------------------------------+
1532 | HS | PB | F | AFHI | ACTION
1533 +----+----+---+------+---------------------------------------------------+
1534 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1535 | | update path, keep afsi
1536 +----+----+---+------+---------------------------------------------------+
1537 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1538 | | otherwise: remove PB, HS: update path, keep afhi,
1540 +----+----+---+------+---------------------------------------------------+
1541 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1543 +----+----+---+------+---------------------------------------------------+
1544 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1545 +----+----+---+------+---------------------------------------------------+
1546 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1547 | | (force has no effect)
1548 +----+----+---+------+---------------------------------------------------+
1549 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1550 +----+----+---+------+---------------------------------------------------+
1551 | N | N | Y | Y | (new file) create new entry (force has no effect)
1552 +----+----+---+------+---------------------------------------------------+
1553 | N | N | N | Y | (new file) create new entry
1554 +----+----+---+------+---------------------------------------------------+
1558 afhi <=> force or no HS
1563 /** Flags passed to the add command. */
1564 enum com_add_flags {
1565 /** Skip paths that exist already. */
1567 /** Force adding. */
1569 /** Print what is being done. */
1570 ADD_FLAG_VERBOSE = 4,
1571 /** Try to add files with unknown suffixes. */
1575 static void com_add_callback(int fd, const struct osl_object *query)
1577 char *buf = query->data, *path;
1578 struct osl_row *pb, *aft_row;
1580 struct osl_object objs[NUM_AFT_COLUMNS];
1582 char asc[2 * HASH_SIZE + 1];
1584 char afsi_buf[AFSI_SIZE];
1585 uint32_t flags = read_u32(buf + CAB_FLAGS_OFFSET);
1586 struct afs_info default_afsi = {.last_played = 0};
1587 struct para_buffer msg = {.max_size = SHMMAX,
1588 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1589 uint16_t afhi_offset, chunks_offset;
1591 hash = (HASH_TYPE *)buf + CAB_HASH_OFFSET;
1592 hash_to_asc(hash, asc);;
1593 objs[AFTCOL_HASH].data = buf + CAB_HASH_OFFSET;
1594 objs[AFTCOL_HASH].size = HASH_SIZE;
1596 path = buf + CAB_PATH_OFFSET;
1597 objs[AFTCOL_PATH].data = path;
1598 objs[AFTCOL_PATH].size = strlen(path) + 1;
1600 PARA_INFO_LOG("request to add %s\n", path);
1601 hs = find_hash_sister(hash);
1602 ret = aft_get_row_of_path(path, &pb);
1603 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1605 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1606 if (flags & ADD_FLAG_VERBOSE)
1607 ret = para_printf(&msg, "ignoring duplicate\n");
1612 if (hs && hs != pb) {
1613 struct osl_object obj;
1614 if (pb) { /* hs trumps pb, remove pb */
1615 if (flags & ADD_FLAG_VERBOSE) {
1616 ret = para_printf(&msg, "removing path brother\n");
1620 ret = osl(osl_del_row(audio_file_table, pb));
1625 /* file rename, update hs' path */
1626 if (flags & ADD_FLAG_VERBOSE) {
1627 ret = osl(osl_get_object(audio_file_table, hs,
1628 AFTCOL_PATH, &obj));
1631 ret = para_printf(&msg, "renamed from %s\n", (char *)obj.data);
1635 ret = osl(osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1636 &objs[AFTCOL_PATH]));
1639 afs_event(AUDIO_FILE_RENAME, &msg, hs);
1640 if (!(flags & ADD_FLAG_FORCE))
1643 /* no hs or force mode, child must have sent afhi */
1644 afhi_offset = read_u16(buf + CAB_AFHI_OFFSET_POS);
1645 chunks_offset = read_u16(buf + CAB_CHUNKS_OFFSET_POS);
1647 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1648 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1650 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1652 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1653 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1654 if (pb && !hs) { /* update pb's hash */
1655 char old_asc[2 * HASH_SIZE + 1];
1656 HASH_TYPE *old_hash;
1657 ret = get_hash_of_row(pb, &old_hash);
1660 hash_to_asc(old_hash, old_asc);
1661 if (flags & ADD_FLAG_VERBOSE) {
1662 ret = para_printf(&msg, "file change: %s -> %s\n",
1667 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1668 &objs[AFTCOL_HASH]);
1672 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1673 struct osl_row *row = pb? pb : hs;
1674 /* update afhi and chunk_table */
1675 if (flags & ADD_FLAG_VERBOSE) {
1676 ret = para_printf(&msg, "updating afhi and chunk table\n");
1680 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1681 &objs[AFTCOL_AFHI]));
1684 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1685 &objs[AFTCOL_CHUNKS]));
1688 afs_event(AFHI_CHANGE, &msg, row);
1691 /* new entry, use default afsi */
1692 if (flags & ADD_FLAG_VERBOSE) {
1693 ret = para_printf(&msg, "new file\n");
1697 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1698 default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_OFFSET);
1700 objs[AFTCOL_AFSI].data = &afsi_buf;
1701 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1702 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1703 ret = osl(osl_add_and_get_row(audio_file_table, objs, &aft_row));
1704 afs_event(AUDIO_FILE_ADD, &msg, aft_row);
1707 para_printf(&msg, "%s\n", para_strerror(-ret));
1709 pass_buffer_as_shm(msg.buf, msg.offset, &fd);
1713 /** Used by com_add(). */
1714 struct private_add_data {
1715 /** The socket file descriptor. */
1717 /** The given add flags. */
1721 static void path_brother_callback(int fd, const struct osl_object *query)
1723 char *path = query->data;
1724 struct osl_row *path_brother;
1725 int ret = aft_get_row_of_path(path, &path_brother);
1728 pass_buffer_as_shm((char *)&path_brother, sizeof(path_brother), &fd);
1731 static void hash_sister_callback(int fd, const struct osl_object *query)
1733 HASH_TYPE *hash = query->data;
1734 struct osl_row *hash_sister;
1736 hash_sister = find_hash_sister(hash);
1739 pass_buffer_as_shm((char *)&hash_sister, sizeof(hash_sister), &fd);
1742 static int get_row_pointer_from_result(struct osl_object *result, void *private)
1744 struct osl_row **row = private;
1745 *row = result->data;
1749 static int add_one_audio_file(const char *path, void *private_data)
1751 int ret, send_ret = 1, fd;
1752 uint8_t format_num = -1;
1753 struct private_add_data *pad = private_data;
1754 struct afh_info afhi, *afhi_ptr = NULL;
1755 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1756 struct osl_object map, obj = {.data = NULL}, query;
1757 HASH_TYPE hash[HASH_SIZE];
1759 ret = guess_audio_format(path);
1760 if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
1762 query.data = (char *)path;
1763 query.size = strlen(path) + 1;
1764 ret = send_callback_request(path_brother_callback, &query,
1765 get_row_pointer_from_result, &pb);
1766 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1769 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1770 if (pad->flags & ADD_FLAG_VERBOSE)
1771 send_ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1774 /* We still want to add this file. Compute its hash. */
1775 ret = mmap_full_file(path, O_RDONLY, &map.data, &map.size, &fd);
1778 hash_function(map.data, map.size, hash);
1780 /* Check whether the database contains a file with the same hash. */
1782 query.size = HASH_SIZE;
1783 ret = send_callback_request(hash_sister_callback, &query,
1784 get_row_pointer_from_result, &hs);
1787 /* Return success if we already know this file. */
1789 if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) {
1790 if (pad->flags & ADD_FLAG_VERBOSE)
1791 send_ret = send_va_buffer(pad->fd,
1792 "%s exists, not forcing update\n", path);
1796 * We won't recalculate the audio format info and the chunk table if
1797 * there is a hash sister and FORCE was not given.
1799 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1800 ret = compute_afhi(path, map.data, map.size, fd, &afhi);
1806 munmap(map.data, map.size);
1808 if (pad->flags & ADD_FLAG_VERBOSE) {
1809 send_ret = send_va_buffer(pad->fd, "adding %s\n", path);
1813 save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1814 /* Ask afs to consider this entry for adding. */
1815 ret = send_callback_request(com_add_callback, &obj, send_result, &pad->fd);
1820 munmap(map.data, map.size);
1822 if (ret < 0 && send_ret >= 0)
1823 send_ret = send_va_buffer(pad->fd, "failed to add %s (%s)\n", path,
1824 para_strerror(-ret));
1827 free(afhi_ptr->chunk_table);
1828 free(afhi_ptr->techinfo);
1829 free(afhi_ptr->tags.artist);
1830 free(afhi_ptr->tags.title);
1831 free(afhi_ptr->tags.year);
1832 free(afhi_ptr->tags.album);
1833 free(afhi_ptr->tags.comment);
1835 /* Stop adding files only on send errors. */
1839 int com_add(int fd, int argc, char * const * const argv)
1842 struct private_add_data pad = {.fd = fd, .flags = 0};
1843 struct stat statbuf;
1845 for (i = 1; i < argc; i++) {
1846 const char *arg = argv[i];
1849 if (!strcmp(arg, "--")) {
1853 if (!strcmp(arg, "-a")) {
1854 pad.flags |= ADD_FLAG_ALL;
1857 if (!strcmp(arg, "-l")) {
1858 pad.flags |= ADD_FLAG_LAZY;
1861 if (!strcmp(arg, "-f")) {
1862 pad.flags |= ADD_FLAG_FORCE;
1865 if (!strcmp(arg, "-v")) {
1866 pad.flags |= ADD_FLAG_VERBOSE;
1871 return -E_AFT_SYNTAX;
1872 for (; i < argc; i++) {
1874 ret = verify_path(argv[i], &path);
1876 ret = send_va_buffer(fd, "%s: %s\n", argv[i],
1877 para_strerror(-ret));
1882 ret = stat(path, &statbuf);
1884 ret = send_va_buffer(fd, "failed to stat %s (%s)\n", path,
1891 if (S_ISDIR(statbuf.st_mode))
1892 ret = for_each_file_in_dir(path, add_one_audio_file,
1895 ret = add_one_audio_file(path, &pad);
1897 send_va_buffer(fd, "%s: %s\n", path, para_strerror(-ret));
1908 * Flags used by the touch command.
1913 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1914 TOUCH_FLAG_FNM_PATHNAME = 1,
1915 /** Activates verbose mode. */
1916 TOUCH_FLAG_VERBOSE = 2
1919 /** Options used by com_touch(). */
1920 struct com_touch_options {
1921 /** New num_played value. */
1923 /** New last played count. */
1924 int64_t last_played;
1925 /** New lyrics id. */
1927 /** New image id. */
1929 /** New amplification value. */
1931 /** Command line flags (see \ref touch_flags). */
1935 /** Data passed to the action handler of com_touch(). */
1936 struct touch_action_data {
1937 /** Command line options (see \ref com_touch_options). */
1938 struct com_touch_options *cto;
1939 /** Message buffer. */
1940 struct para_buffer pb;
1941 /** How many audio files matched the given pattern. */
1942 unsigned num_matches;
1945 static int touch_audio_file(__a_unused struct osl_table *table,
1946 struct osl_row *row, const char *name, void *data)
1948 struct touch_action_data *tad = data;
1949 struct osl_object obj;
1950 struct afs_info old_afsi, new_afsi;
1951 int ret, no_options = tad->cto->num_played < 0 && tad->cto->last_played < 0 &&
1952 tad->cto->lyrics_id < 0 && tad->cto->image_id < 0 && tad->cto->amp < 0;
1953 struct afsi_change_event_data aced;
1955 ret = get_afsi_object_of_row(row, &obj);
1957 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1958 ret = load_afsi(&old_afsi, &obj);
1960 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1961 new_afsi = old_afsi;
1963 new_afsi.num_played++;
1964 new_afsi.last_played = time(NULL);
1965 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1966 ret = para_printf(&tad->pb, "%s: num_played = %u, "
1967 "last_played = now()\n", name,
1968 new_afsi.num_played);
1973 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1974 ret = para_printf(&tad->pb, "touching %s\n", name);
1978 if (tad->cto->lyrics_id >= 0)
1979 new_afsi.lyrics_id = tad->cto->lyrics_id;
1980 if (tad->cto->image_id >= 0)
1981 new_afsi.image_id = tad->cto->image_id;
1982 if (tad->cto->num_played >= 0)
1983 new_afsi.num_played = tad->cto->num_played;
1984 if (tad->cto->last_played >= 0)
1985 new_afsi.last_played = tad->cto->last_played;
1986 if (tad->cto->amp >= 0)
1987 new_afsi.amp = tad->cto->amp;
1990 save_afsi(&new_afsi, &obj); /* in-place update */
1992 aced.old_afsi = &old_afsi;
1993 afs_event(AFSI_CHANGE, &tad->pb, &aced);
1997 static void com_touch_callback(int fd, const struct osl_object *query)
1999 struct touch_action_data tad = {.cto = query->data,
2002 .private_data = &fd,
2003 .max_size_handler = pass_buffer_as_shm
2007 struct pattern_match_data pmd = {
2008 .table = audio_file_table,
2009 .loop_col_num = AFTCOL_HASH,
2010 .match_col_num = AFTCOL_PATH,
2011 .patterns = {.data = (char *)query->data + sizeof(*tad.cto),
2012 .size = query->size - sizeof(*tad.cto)},
2014 .action = touch_audio_file
2016 if (tad.cto->flags & TOUCH_FLAG_FNM_PATHNAME)
2017 pmd.fnmatch_flags |= FNM_PATHNAME;
2018 ret = for_each_matching_row(&pmd);
2020 ret2 = para_printf(&tad.pb, "%s\n", para_strerror(-ret));
2022 if (!tad.num_matches)
2023 ret2 = para_printf(&tad.pb, "no matches\n");
2024 if (ret2 >= 0 && tad.pb.offset)
2025 pass_buffer_as_shm(tad.pb.buf, tad.pb.offset, &fd);
2029 int com_touch(int fd, int argc, char * const * const argv)
2031 struct com_touch_options cto = {
2038 struct osl_object query = {.data = &cto, .size = sizeof(cto)};
2042 for (i = 1; i < argc; i++) {
2043 const char *arg = argv[i];
2046 if (!strcmp(arg, "--")) {
2050 if (!strncmp(arg, "-n", 2)) {
2051 ret = para_atoi32(arg + 2, &cto.num_played);
2056 if (!strncmp(arg, "-l", 2)) {
2057 ret = para_atoi64(arg + 2, &cto.last_played);
2062 if (!strncmp(arg, "-y", 2)) {
2063 ret = para_atoi32(arg + 2, &cto.lyrics_id);
2068 if (!strncmp(arg, "-i", 2)) {
2069 ret = para_atoi32(arg + 2, &cto.image_id);
2074 if (!strncmp(arg, "-a", 2)) {
2076 ret = para_atoi32(arg + 2, &val);
2079 if (val < 0 || val > 255)
2080 return -ERRNO_TO_PARA_ERROR(EINVAL);
2084 if (!strcmp(arg, "-p")) {
2085 cto.flags |= TOUCH_FLAG_FNM_PATHNAME;
2088 if (!strcmp(arg, "-v")) {
2089 cto.flags |= TOUCH_FLAG_VERBOSE;
2092 break; /* non-option starting with dash */
2095 return -E_AFT_SYNTAX;
2096 ret = send_option_arg_callback_request(&query, argc - i,
2097 argv + i, com_touch_callback, send_result, &fd);
2099 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2103 /** Flags for com_rm(). */
2106 RM_FLAG_VERBOSE = 1,
2110 RM_FLAG_FNM_PATHNAME = 4
2113 /** Data passed to the action handler of com_rm(). */
2114 struct com_rm_action_data {
2115 /** Command line flags ((see \ref rm_flags). */
2117 /** Message buffer. */
2118 struct para_buffer pb;
2119 /** Number of audio files removed. */
2120 unsigned num_removed;
2123 static int remove_audio_file(__a_unused struct osl_table *table,
2124 struct osl_row *row, const char *name, void *data)
2126 struct com_rm_action_data *crd = data;
2129 if (crd->flags & RM_FLAG_VERBOSE) {
2130 ret = para_printf(&crd->pb, "removing %s\n", name);
2134 afs_event(AUDIO_FILE_REMOVE, &crd->pb, row);
2135 ret = osl(osl_del_row(audio_file_table, row));
2137 para_printf(&crd->pb, "%s: %s\n", name, para_strerror(-ret));
2143 static void com_rm_callback(int fd, const struct osl_object *query)
2145 struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
2148 .private_data = &fd,
2149 .max_size_handler = pass_buffer_as_shm
2153 struct pattern_match_data pmd = {
2154 .table = audio_file_table,
2155 .loop_col_num = AFTCOL_HASH,
2156 .match_col_num = AFTCOL_PATH,
2157 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
2158 .size = query->size - sizeof(uint32_t)},
2160 .action = remove_audio_file
2162 if (crd.flags & RM_FLAG_FNM_PATHNAME)
2163 pmd.fnmatch_flags |= FNM_PATHNAME;
2164 ret = for_each_matching_row(&pmd);
2166 para_printf(&crd.pb, "%s\n", para_strerror(-ret));
2169 if (!crd.num_removed && !(crd.flags & RM_FLAG_FORCE))
2170 ret = para_printf(&crd.pb, "no matches -- nothing removed\n");
2172 if (crd.flags & RM_FLAG_VERBOSE)
2173 ret = para_printf(&crd.pb, "removed %u files\n", crd.num_removed);
2175 if (ret >= 0 && crd.pb.offset)
2176 pass_buffer_as_shm(crd.pb.buf, crd.pb.offset, &fd);
2180 /* TODO options: -r (recursive) */
2181 int com_rm(int fd, int argc, char * const * const argv)
2184 struct osl_object query = {.data = &flags, .size = sizeof(flags)};
2187 for (i = 1; i < argc; i++) {
2188 const char *arg = argv[i];
2191 if (!strcmp(arg, "--")) {
2195 if (!strcmp(arg, "-f")) {
2196 flags |= RM_FLAG_FORCE;
2199 if (!strcmp(arg, "-p")) {
2200 flags |= RM_FLAG_FNM_PATHNAME;
2203 if (!strcmp(arg, "-v")) {
2204 flags |= RM_FLAG_VERBOSE;
2210 return -E_AFT_SYNTAX;
2211 ret = send_option_arg_callback_request(&query, argc - i, argv + i,
2212 com_rm_callback, send_result, &fd);
2214 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2219 * Flags used by the cpsi command.
2224 /** Whether the lyrics id should be copied. */
2225 CPSI_FLAG_COPY_LYRICS_ID = 1,
2226 /** Whether the image id should be copied. */
2227 CPSI_FLAG_COPY_IMAGE_ID = 2,
2228 /** Whether the lastplayed time should be copied. */
2229 CPSI_FLAG_COPY_LASTPLAYED = 4,
2230 /** Whether the numplayed count should be copied. */
2231 CPSI_FLAG_COPY_NUMPLAYED = 8,
2232 /** Whether the attributes should be copied. */
2233 CPSI_FLAG_COPY_ATTRIBUTES = 16,
2234 /** Activates verbose mode. */
2235 CPSI_FLAG_VERBOSE = 32,
2238 /** Data passed to the action handler of com_cpsi(). */
2239 struct cpsi_action_data {
2240 /** command line flags (see \ref cpsi_flags). */
2242 /** Number of audio files changed. */
2243 unsigned num_copied;
2244 /** Message buffer. */
2245 struct para_buffer pb;
2246 /** Values are copied from here. */
2247 struct afs_info source_afsi;
2250 static int copy_selector_info(__a_unused struct osl_table *table,
2251 struct osl_row *row, const char *name, void *data)
2253 struct cpsi_action_data *cad = data;
2254 struct osl_object target_afsi_obj;
2256 struct afs_info old_afsi, target_afsi;
2257 struct afsi_change_event_data aced;
2259 ret = get_afsi_object_of_row(row, &target_afsi_obj);
2262 load_afsi(&target_afsi, &target_afsi_obj);
2263 old_afsi = target_afsi;
2264 if (cad->flags & CPSI_FLAG_COPY_LYRICS_ID)
2265 target_afsi.lyrics_id = cad->source_afsi.lyrics_id;
2266 if (cad->flags & CPSI_FLAG_COPY_IMAGE_ID)
2267 target_afsi.image_id = cad->source_afsi.image_id;
2268 if (cad->flags & CPSI_FLAG_COPY_LASTPLAYED)
2269 target_afsi.last_played = cad->source_afsi.last_played;
2270 if (cad->flags & CPSI_FLAG_COPY_NUMPLAYED)
2271 target_afsi.num_played = cad->source_afsi.num_played;
2272 if (cad->flags & CPSI_FLAG_COPY_ATTRIBUTES)
2273 target_afsi.attributes = cad->source_afsi.attributes;
2274 save_afsi(&target_afsi, &target_afsi_obj); /* in-place update */
2276 if (cad->flags & CPSI_FLAG_VERBOSE) {
2277 ret = para_printf(&cad->pb, "copied afsi to %s\n", name);
2282 aced.old_afsi = &old_afsi;
2283 afs_event(AFSI_CHANGE, &cad->pb, &aced);
2287 static void com_cpsi_callback(int fd, const struct osl_object *query)
2289 struct cpsi_action_data cad = {
2290 .flags = *(unsigned *)query->data,
2293 .private_data = &fd,
2294 .max_size_handler = pass_buffer_as_shm
2298 char *source_path = (char *)query->data + sizeof(cad.flags);
2299 struct pattern_match_data pmd = {
2300 .table = audio_file_table,
2301 .loop_col_num = AFTCOL_HASH,
2302 .match_col_num = AFTCOL_PATH,
2303 .patterns = {.data = source_path + strlen(source_path) + 1,
2304 .size = query->size - sizeof(cad.flags)
2305 - strlen(source_path) - 1},
2307 .action = copy_selector_info
2310 ret = get_afsi_of_path(source_path, &cad.source_afsi);
2313 ret = for_each_matching_row(&pmd);
2316 para_printf(&cad.pb, "%s\n", para_strerror(-ret));
2317 else if (cad.flags & CPSI_FLAG_VERBOSE) {
2319 para_printf(&cad.pb, "copied requested afsi from %s "
2320 "to %u files\n", source_path, cad.num_copied);
2322 para_printf(&cad.pb, "nothing copied\n");
2325 pass_buffer_as_shm(cad.pb.buf, cad.pb.offset, &fd);
2329 int com_cpsi(int fd, int argc, char * const * const argv)
2333 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
2335 for (i = 1; i < argc; i++) {
2336 const char *arg = argv[i];
2339 if (!strcmp(arg, "--")) {
2343 if (!strcmp(arg, "-y")) {
2344 flags |= CPSI_FLAG_COPY_LYRICS_ID;
2347 if (!strcmp(arg, "-i")) {
2348 flags |= CPSI_FLAG_COPY_IMAGE_ID;
2351 if (!strcmp(arg, "-l")) {
2352 flags |= CPSI_FLAG_COPY_LASTPLAYED;
2355 if (!strcmp(arg, "-n")) {
2356 flags |= CPSI_FLAG_COPY_NUMPLAYED;
2359 if (!strcmp(arg, "-a")) {
2360 flags |= CPSI_FLAG_COPY_ATTRIBUTES;
2363 if (!strcmp(arg, "-v")) {
2364 flags |= CPSI_FLAG_VERBOSE;
2369 if (i + 1 >= argc) /* need at least souce file and pattern */
2370 return -E_AFT_SYNTAX;
2371 if (!(flags & ~CPSI_FLAG_VERBOSE)) /* no copy flags given */
2372 flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags;
2373 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
2374 com_cpsi_callback, send_result, &fd);
2376 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2380 void afs_stat_callback(int fd, __a_unused const struct osl_object *query)
2382 if (current_status_items)
2383 pass_buffer_as_shm(current_status_items,
2384 strlen(current_status_items), &fd);
2387 int send_afs_status(int fd)
2389 return send_callback_request(afs_stat_callback, NULL, send_result, &fd);
2392 /* TODO: optionally fix problems by removing offending rows */
2393 static int check_audio_file(struct osl_row *row, void *data)
2396 struct para_buffer *pb = data;
2397 struct stat statbuf;
2398 int ret = get_audio_file_path_of_row(row, &path);
2399 struct afs_info afsi;
2403 return para_printf(pb, "%s\n", para_strerror(-ret));
2404 if (stat(path, &statbuf) < 0) {
2405 ret = para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
2409 if (!S_ISREG(statbuf.st_mode)) {
2410 ret = para_printf(pb, "%s: not a regular file\n", path);
2415 ret = get_afsi_of_row(row, &afsi);
2417 return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
2418 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
2420 ret = para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
2421 para_strerror(-ret));
2425 ret = img_get_name_by_id(afsi.image_id, &blob_name);
2427 ret = para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
2428 para_strerror(-ret));
2433 * Check the audio file table for inconsistencies.
2435 * \param fd The afs socket.
2436 * \param query Unused.
2438 * This function always succeeds.
2442 void aft_check_callback(int fd, __a_unused const struct osl_object *query)
2444 struct para_buffer pb = {
2446 .private_data = &fd,
2447 .max_size_handler = pass_buffer_as_shm
2449 int ret = para_printf(&pb, "checking audio file table...\n");
2453 audio_file_loop(&pb, check_audio_file);
2455 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
2460 * Close the audio file table.
2462 * \param flags Usual flags that are passed to osl_close_table().
2464 * \sa osl_close_table().
2466 static void aft_close(void)
2468 osl_close_table(audio_file_table, OSL_MARK_CLEAN);
2469 audio_file_table = NULL;
2470 free(current_status_items);
2471 current_status_items = NULL;
2475 * Open the audio file table.
2477 * \param dir The database directory.
2481 * \sa osl_open_table().
2483 static int aft_open(const char *dir)
2487 audio_file_table_desc.dir = dir;
2488 ret = osl(osl_open_table(&audio_file_table_desc, &audio_file_table));
2491 osl_get_num_rows(audio_file_table, &num);
2492 PARA_INFO_LOG("audio file table contains %d files\n", num);
2495 PARA_INFO_LOG("failed to open audio file table\n");
2496 audio_file_table = NULL;
2497 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
2502 static int aft_create(const char *dir)
2504 audio_file_table_desc.dir = dir;
2505 return osl(osl_create_table(&audio_file_table_desc));
2508 static int clear_attribute(struct osl_row *row, void *data)
2510 struct rmatt_event_data *red = data;
2511 struct afs_info afsi;
2512 struct osl_object obj;
2513 int ret = get_afsi_object_of_row(row, &obj);
2514 uint64_t mask = ~(1ULL << red->bitnum);
2518 ret = load_afsi(&afsi, &obj);
2521 afsi.attributes &= mask;
2522 save_afsi(&afsi, &obj);
2526 static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
2532 case ATTRIBUTE_REMOVE: {
2533 const struct rmatt_event_data *red = data;
2534 ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
2535 "entries in the audio file table\n", red->name,
2539 return audio_file_loop(data, clear_attribute);
2546 void aft_init(struct afs_table *t)
2548 t->name = audio_file_table_desc.name;
2550 t->close = aft_close;
2551 t->create = aft_create;
2552 t->event_handler = aft_event_handler;