2 * Copyright (C) 2007-2008 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 static struct osl_table *audio_file_table;
26 /** The different sorting methods of the ls command. */
27 enum ls_sorting_method {
33 LS_SORT_BY_LAST_PLAYED,
35 LS_SORT_BY_NUM_PLAYED,
49 LS_SORT_BY_AUDIO_FORMAT,
54 /** The different listing modes of the ls command. */
55 enum ls_listing_mode {
56 /** Default listing mode. */
68 /** The flags accepted by the ls command. */
71 LS_FLAG_FULL_PATH = 1,
73 LS_FLAG_ADMISSIBLE_ONLY = 2,
79 * The size of the individual output fields of the ls command.
81 * These depend on the actual content being listed. If, for instance only files
82 * with duration less than an hour are being listed, then the duration with is
83 * made smaller because then the duration is listed as mm:ss rather than
87 /** size of the score field. */
88 unsigned short score_width;
89 /** size of the image id field. */
90 unsigned short image_id_width;
91 /** size of the lyrics id field. */
92 unsigned short lyrics_id_width;
93 /** size of the bitrate field. */
94 unsigned short bitrate_width;
95 /** size of the frequency field. */
96 unsigned short frequency_width;
97 /** size of the duration field. */
98 unsigned short duration_width;
99 /** size of the num played field. */
100 unsigned short num_played_width;
101 /** size of the amp field. */
102 unsigned short amp_width;
105 /** Data passed from the ls command handler to its callback function. */
107 /** The given command line flags. */
109 /** The sorting method given at the command line. */
110 enum ls_sorting_method sorting;
111 /** The given listing mode (short, long, verbose, mbox). */
112 enum ls_listing_mode mode;
113 /** The arguments passed to the ls command. */
115 /** Number of non-option arguments. */
117 /** Used for long listing mode to align the output fields. */
118 struct ls_widths widths;
119 /** Size of the \a data array. */
121 /** Number of used entries in the data array. */
122 uint32_t num_matching_paths;
123 /** Array of matching entries. */
124 struct ls_data *data;
125 /** Used to sort the array. */
126 struct ls_data **data_ptr;
130 * Describes the layout of the mmapped-afs info struct.
132 * \sa struct afs_info.
135 /** Where .last_played is stored. */
136 AFSI_LAST_PLAYED_OFFSET = 0,
137 /** Storage position of the attributes bitmap. */
138 AFSI_ATTRIBUTES_OFFSET = 8,
139 /** Storage position of the .num_played field. */
140 AFSI_NUM_PLAYED_OFFSET = 16,
141 /** Storage position of the .image_id field. */
142 AFSI_IMAGE_ID_OFFSET = 20,
143 /** Storage position of the .lyrics_id field. */
144 AFSI_LYRICS_ID_OFFSET = 24,
145 /** Storage position of the .audio_format_id field. */
146 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
147 /** Storage position of the amplification field. */
148 AFSI_AMP_OFFSET = 29,
149 /** 2 bytes reserved space for future usage. */
150 AFSI_AUDIO_FORMAT_UNUSED_OFFSET = 30,
151 /** On-disk storage space needed. */
156 * Convert a struct afs_info to an osl object.
158 * \param afsi Pointer to the audio file info to be converted.
159 * \param obj Result pointer.
163 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
165 char *buf = obj->data;
167 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
168 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
169 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
170 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
171 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
172 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
173 afsi->audio_format_id);
174 write_u8(buf + AFSI_AMP_OFFSET, afsi->amp);
175 memset(buf + AFSI_AUDIO_FORMAT_UNUSED_OFFSET, 0, 2);
179 * Get the audio file selector info struct stored in an osl object.
181 * \param afsi Points to the audio_file info structure to be filled in.
182 * \param obj The osl object holding the data.
184 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
188 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
190 char *buf = obj->data;
191 if (obj->size < AFSI_SIZE)
193 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
194 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
195 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
196 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
197 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
198 afsi->audio_format_id = read_u8(buf +
199 AFSI_AUDIO_FORMAT_ID_OFFSET);
200 afsi->amp = read_u8(buf + AFSI_AMP_OFFSET);
204 /** The columns of the audio file table. */
205 enum audio_file_table_columns {
206 /** The hash on the content of the audio file. */
208 /** The full path in the filesystem. */
210 /** The audio file selector info. */
212 /** The audio format handler info. */
214 /** The chunk table info and the chunk table of the audio file. */
216 /** The number of columns of this table. */
220 static struct osl_column_description aft_cols[] = {
222 .storage_type = OSL_MAPPED_STORAGE,
223 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
225 .compare_function = osl_hash_compare,
226 .data_size = HASH_SIZE
229 .storage_type = OSL_MAPPED_STORAGE,
230 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
232 .compare_function = string_compare,
235 .storage_type = OSL_MAPPED_STORAGE,
236 .storage_flags = OSL_FIXED_SIZE,
238 .data_size = AFSI_SIZE
241 .storage_type = OSL_MAPPED_STORAGE,
245 .storage_type = OSL_DISK_STORAGE,
250 static struct osl_table_description audio_file_table_desc = {
251 .name = "audio_files",
252 .num_columns = NUM_AFT_COLUMNS,
253 .flags = OSL_LARGE_TABLE,
254 .column_descriptions = aft_cols
257 /* We don't want * dot or dot-dot anywhere. */
258 static int verify_dotfile(const char *rest)
261 * The first character was '.', but that has already been discarded, we
265 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
267 case '.': /* path start with /foo/.. */
268 if (rest[1] == '\0' || rest[1] == '/')
269 return -1; /* /foo/.. or /foo/../bar are not ok */
270 /* /foo/..bar is ok */
276 * We fundamentally don't like some paths: We don't want double slashes or
277 * slashes at the end that can make pathnames ambiguous.
279 static int verify_path(const char *orig_path, char **resolved_path)
285 if (*orig_path != '/') /* we only accept absolute paths */
287 len = strlen(orig_path);
288 *resolved_path = para_strdup(orig_path);
289 path = *resolved_path;
290 while (len > 1 && path[--len] == '/')
291 path[len] = '\0'; /* remove slash at the end */
297 case '/': /* double slash */
300 if (verify_dotfile(path) < 0)
310 free(*resolved_path);
314 /** The on-disk layout of a afhi struct. */
316 /** Where the number of seconds is stored. */
317 AFHI_SECONDS_TOTAL_OFFSET = 0,
318 /** Position of the bitrate. */
319 AFHI_BITRATE_OFFSET = 4,
320 /** Position of the frequency. */
321 AFHI_FREQUENCY_OFFSET = 8,
322 /** Location of the audio file header. */
323 AFHI_HEADER_OFFSET_OFFSET = 12,
324 /* Length of the audio file header. Zero means: No header. */
325 AFHI_HEADER_LEN_OFFSET = 16,
326 /** The total number of chunks (4 bytes). */
327 CHUNKS_TOTAL_OFFSET = 20,
328 /** The length of the audio file header (4 bytes). */
329 HEADER_LEN_OFFSET = 24,
330 /** The start of the audio file header (4 bytes). */
331 HEADER_OFFSET_OFFSET = 28,
332 /** The seconds part of the chunk time (4 bytes). */
333 CHUNK_TV_TV_SEC_OFFSET = 32,
334 /** The microseconds part of the chunk time (4 bytes). */
335 CHUNK_TV_TV_USEC_OFFSET = 36,
336 /** Number of channels is stored here. (1 byte) */
337 AFHI_CHANNELS_OFFSET = 40,
338 /** EOF timeout in ms. (2 byte) */
339 AFHI_EOF_OFFSET = 41,
340 /** The tag info position. */
341 AFHI_INFO_STRING_OFFSET = 43,
342 /** Minimal on-disk size of a valid afhi struct. */
346 static unsigned sizeof_afhi_buf(const struct afh_info *afhi)
350 return strlen(afhi->info_string) + MIN_AFHI_SIZE;
353 static void save_afhi(struct afh_info *afhi, char *buf)
357 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET, afhi->seconds_total);
358 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
359 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
360 write_u32(buf + AFHI_HEADER_OFFSET_OFFSET, afhi->header_offset);
361 write_u32(buf + AFHI_HEADER_LEN_OFFSET, afhi->header_len);
362 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
363 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
364 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
365 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
366 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
367 write_u32(buf + CHUNK_TV_TV_USEC_OFFSET, afhi->chunk_tv.tv_usec);
368 write_u16(buf + AFHI_EOF_OFFSET, tv2ms(&afhi->eof_tv));
369 strcpy(buf + AFHI_INFO_STRING_OFFSET, afhi->info_string); /* OK */
372 static void load_afhi(const char *buf, struct afh_info *afhi)
374 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
375 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
376 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
377 afhi->header_offset = read_u32(buf + AFHI_HEADER_OFFSET_OFFSET);
378 afhi->header_len = read_u32(buf + AFHI_HEADER_LEN_OFFSET);
379 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
380 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
381 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
382 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
383 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
384 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC_OFFSET);
385 ms2tv(read_u16(buf + AFHI_EOF_OFFSET), &afhi->eof_tv);
386 afhi->info_string = para_strdup(buf + AFHI_INFO_STRING_OFFSET);
389 static unsigned sizeof_chunk_table(struct afh_info *afhi)
393 return 4 * (afhi->chunks_total + 1);
396 static void save_chunk_table(struct afh_info *afhi, char *buf)
400 for (i = 0; i <= afhi->chunks_total; i++)
401 write_u32(buf + 4 * i, afhi->chunk_table[i]);
404 static void load_chunk_table(struct afh_info *afhi, char *buf)
408 afhi->chunk_table = para_malloc(sizeof_chunk_table(afhi));
409 for (i = 0; i <= afhi->chunks_total; i++)
410 afhi->chunk_table[i] = read_u32(buf + 4 * i);
414 * Get the row of the audio file table corresponding to the given path.
416 * \param path The full path of the audio file.
417 * \param row Result pointer.
419 * \return The return value of the underlying call to osl_get_row().
421 int aft_get_row_of_path(const char *path, struct osl_row **row)
423 struct osl_object obj = {.data = (char *)path, .size = strlen(path) + 1};
425 return osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row);
429 * Get the row of the audio file table corresponding to the given hash value.
431 * \param hash The hash value of the desired audio file.
432 * \param row resul pointer.
434 * \return The return value of the underlying call to osl_get_row().
436 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
438 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
439 return osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row);
443 * Get the osl object holding the audio file selector info of a row.
445 * \param row Pointer to a row in the audio file table.
446 * \param obj Result pointer.
448 * \return The return value of the underlying call to osl_get_object().
450 int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
452 return osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj);
456 * Get the osl object holding the audio file selector info, given a path.
459 * \param path The full path of the audio file.
460 * \param obj Result pointer.
462 * \return Positive on success, negative on errors.
464 int get_afsi_object_of_path(const char *path, struct osl_object *obj)
467 int ret = aft_get_row_of_path(path, &row);
470 return get_afsi_object_of_row(row, obj);
474 * Get the audio file selector info, given a row of the audio file table.
476 * \param row Pointer to a row in the audio file table.
477 * \param afsi Result pointer.
479 * \return Positive on success, negative on errors.
481 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
483 struct osl_object obj;
484 int ret = get_afsi_object_of_row(row, &obj);
487 return load_afsi(afsi, &obj);
491 * Get the audio file selector info, given the path of an audio table.
493 * \param path The full path of the audio file.
494 * \param afsi Result pointer.
496 * \return Positive on success, negative on errors.
498 int get_afsi_of_path(const char *path, struct afs_info *afsi)
500 struct osl_object obj;
501 int ret = get_afsi_object_of_path(path, &obj);
504 return load_afsi(afsi, &obj);
508 * Get the path of an audio file, given a row of the audio file table.
510 * \param row Pointer to a row in the audio file table.
511 * \param path Result pointer.
513 * The result is a pointer to mmapped data. The caller must not attempt
518 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
520 struct osl_object path_obj;
521 int ret = osl_get_object(audio_file_table, row, AFTCOL_PATH,
525 *path = path_obj.data;
530 * Get the object containing the hash value of an audio file, given a row.
532 * \param row Pointer to a row of the audio file table.
533 * \param obj Result pointer.
535 * \return The return value of the underlying call to osl_get_object().
537 * \sa get_hash_of_row().
539 static int get_hash_object_of_aft_row(const struct osl_row *row, struct osl_object *obj)
541 return osl_get_object(audio_file_table, row, AFTCOL_HASH, obj);
545 * Get the hash value of an audio file, given a row of the audio file table.
547 * \param row Pointer to a row of the audio file table.
548 * \param hash Result pointer.
550 * \a hash points to mapped data and must not be freed by the caller.
552 * \return The return value of the underlying call to
553 * get_hash_object_of_aft_row().
555 static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash)
557 struct osl_object obj;
558 int ret = get_hash_object_of_aft_row(row, &obj);
567 * Get the audio format handler info, given a row of the audio file table.
569 * \param row Pointer to a row of the audio file table.
570 * \param afhi Result pointer.
572 * \return The return value of the underlying call to osl_get_object().
574 * \sa get_chunk_table_of_row().
576 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi)
578 struct osl_object obj;
579 int ret = osl_get_object(audio_file_table, row, AFTCOL_AFHI,
583 load_afhi(obj.data, afhi);
587 /* returns shmid on success */
588 static int save_afd(struct audio_file_data *afd)
590 size_t size = sizeof(*afd) + sizeof_chunk_table(&afd->afhi);
592 PARA_DEBUG_LOG("size: %zu\n", size);
593 int shmid, ret = shm_new(size);
600 ret = shm_attach(shmid, ATTACH_RW, &shm_afd);
603 *(struct audio_file_data *)shm_afd = *afd;
606 save_chunk_table(&afd->afhi, buf);
614 int load_afd(int shmid, struct audio_file_data *afd)
620 ret = shm_attach(shmid, ATTACH_RO, &shm_afd);
623 *afd = *(struct audio_file_data *)shm_afd;
626 load_chunk_table(&afd->afhi, buf);
632 * Mmap the given audio file and update statistics.
634 * \param aft_row Determines the audio file to be opened and updated.
635 * \param score The score of the audio file.
636 * \param afd Result pointer.
638 * On success, the numplayed field of the audio file selector info is increased
639 * and the lastplayed time is set to the current time. Finally, the score of
640 * the audio file is updated.
642 * \return Positive shmid on success, negative on errors.
644 int open_and_update_audio_file(struct osl_row *aft_row, long score,
645 struct audio_file_data *afd)
647 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
648 struct osl_object afsi_obj;
649 struct afs_info old_afsi, new_afsi;
650 int ret = get_hash_of_row(aft_row, &aft_hash);
651 struct afsi_change_event_data aced;
652 struct osl_object map, chunk_table_obj;
657 ret = get_audio_file_path_of_row(aft_row, &path);
660 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
663 ret = load_afsi(&old_afsi, &afsi_obj);
666 ret = get_afhi_of_row(aft_row, &afd->afhi);
669 afd->afhi.chunk_table = NULL;
670 ret = osl_open_disk_object(audio_file_table, aft_row,
671 AFTCOL_CHUNKS, &chunk_table_obj);
674 ret = mmap_full_file(path, O_RDONLY, &map.data,
675 &map.size, &afd->fd);
678 hash_function(map.data, map.size, file_hash);
679 ret = hash_compare(file_hash, aft_hash);
680 para_munmap(map.data, map.size);
682 ret = -E_HASH_MISMATCH;
686 new_afsi.num_played++;
687 new_afsi.last_played = time(NULL);
688 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
690 load_chunk_table(&afd->afhi, chunk_table_obj.data);
693 .afhi = afd->afhi, /* struct copy */
699 struct para_buffer pb = {.max_size = VERBOSE_LS_OUTPUT_SIZE - 1};
700 ret = make_status_items(&d, &pb); /* frees info string */
701 afd->afhi.info_string = NULL;
704 strncpy(afd->verbose_ls_output, pb.buf, VERBOSE_LS_OUTPUT_SIZE);
705 afd->verbose_ls_output[VERBOSE_LS_OUTPUT_SIZE - 1] = '\0';
708 aced.aft_row = aft_row;
709 aced.old_afsi = &old_afsi;
710 afs_event(AFSI_CHANGE, NULL, &aced);
713 free(afd->afhi.chunk_table);
714 free(afd->afhi.info_string);
715 osl_close_disk_object(&chunk_table_obj);
719 static int get_local_time(uint64_t *seconds, char *buf, size_t size,
720 time_t current_time, enum ls_listing_mode lm)
724 if (!localtime_r((time_t *)seconds, &t))
726 if (lm == LS_MODE_MBOX) {
727 if (!strftime(buf, size, "%c", &t))
731 if (*seconds + 6 * 30 * 24 * 3600 > current_time) {
732 if (!strftime(buf, size, "%b %e %k:%M", &t))
736 if (!strftime(buf, size, "%b %e %Y", &t))
741 /** Compute the number of (decimal) digits of a number. */
742 #define GET_NUM_DIGITS(x, num) { \
743 typeof((x)) _tmp = PARA_ABS(x); \
746 while ((_tmp) > 9) { \
752 static short unsigned get_duration_width(int seconds)
754 short unsigned width;
755 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
757 if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
758 return 4 + (mins > 9);
759 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
760 GET_NUM_DIGITS(hours, &width);
764 static void get_duration_buf(int seconds, char *buf, struct ls_options *opts)
766 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
767 short unsigned max_width;
769 if (!hours) { /* m:ss or mm:ss */
770 max_width = opts->mode == LS_MODE_LONG?
771 opts->widths.duration_width : 4;
772 sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
773 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
774 max_width = opts->mode == LS_MODE_LONG?
775 opts->widths.duration_width : 7;
776 sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
781 static char *make_attribute_lines(const char *att_bitmap, struct afs_info *afsi)
783 char *att_text, *att_lines;
785 get_attribute_text(&afsi->attributes, " ", &att_text);
787 return para_strdup(att_bitmap);
788 att_lines = make_message("%s: %s\n%s: %s",
789 status_item_list[SI_ATTRIBUTES_BITMAP], att_bitmap,
790 status_item_list[SI_ATTRIBUTES_TXT], att_text);
795 static char *make_lyrics_lines(struct afs_info *afsi)
799 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
800 return make_message("%s: %u\n%s: %s\n",
801 status_item_list[SI_LYRICS_ID], afsi->lyrics_id,
802 status_item_list[SI_LYRICS_NAME], lyrics_name?
803 lyrics_name : "(none)");
806 static char *make_image_lines(struct afs_info *afsi)
809 img_get_name_by_id(afsi->image_id, &image_name);
810 return make_message("%s: %u\n%s: %s\n",
811 status_item_list[SI_IMAGE_ID], afsi->image_id,
812 status_item_list[SI_IMAGE_NAME], image_name?
813 image_name : "(none)");
816 static char *make_filename_lines(const char *path, unsigned flags)
819 const char *basename;
821 if (!(flags & LS_FLAG_FULL_PATH))
822 return make_message("%s: %s\n",
823 status_item_list[SI_BASENAME], path);
824 basename = para_basename(path),
825 dirname = para_dirname(path);
826 ret = make_message("%s: %s\n%s: %s\n%s: %s\n",
827 status_item_list[SI_PATH], path,
828 status_item_list[SI_DIRECTORY], dirname? dirname : "?",
829 status_item_list[SI_BASENAME], basename? basename : "?");
834 static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
836 struct osl_object chunk_table_obj;
837 struct osl_row *aft_row;
841 ret = aft_get_row_of_hash(d->hash, &aft_row);
844 ret = osl_open_disk_object(audio_file_table, aft_row,
845 AFTCOL_CHUNKS, &chunk_table_obj);
848 ret = para_printf(b, "%s\n"
849 "chunk_time: %lu:%lu\nchunk_offsets: ",
851 (long unsigned) d->afhi.chunk_tv.tv_sec,
852 (long unsigned) d->afhi.chunk_tv.tv_usec
856 buf = chunk_table_obj.data;
857 for (i = 0; i <= d->afhi.chunks_total; i++) {
858 ret = para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
862 ret = para_printf(b, "\n");
864 osl_close_disk_object(&chunk_table_obj);
868 static int print_list_item(struct ls_data *d, struct ls_options *opts,
869 struct para_buffer *b, time_t current_time)
873 char last_played_time[30];
874 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
875 char score_buf[30] = "";
876 struct afs_info *afsi = &d->afsi;
877 struct afh_info *afhi = &d->afhi;
878 struct ls_widths *w = &opts->widths;
879 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
880 char asc_hash[2 * HASH_SIZE + 1];
881 char *att_lines, *lyrics_lines, *image_lines, *filename_lines;
883 if (opts->mode == LS_MODE_SHORT) {
884 ret = para_printf(b, "%s\n", d->path);
887 if (opts->mode == LS_MODE_CHUNKS) {
888 ret = print_chunk_table(d, b);
891 get_attribute_bitmap(&afsi->attributes, att_buf);
892 ret = get_local_time(&afsi->last_played, last_played_time,
893 sizeof(last_played_time), current_time, opts->mode);
896 get_duration_buf(afhi->seconds_total, duration_buf, opts);
898 if (opts->mode == LS_MODE_LONG)
899 sprintf(score_buf, "%*li ", w->score_width, d->score);
901 sprintf(score_buf, "%li ", d->score);
904 if (opts->mode == LS_MODE_LONG) {
907 "%s " /* attributes */
909 "%*d " /* image_id */
910 "%*d " /* lyrics_id */
912 "%s " /* audio format */
913 "%*d " /* frequency */
916 "%*d " /* num_played */
917 "%s " /* last_played */
921 w->amp_width, afsi->amp,
922 w->image_id_width, afsi->image_id,
923 w->lyrics_id_width, afsi->lyrics_id,
924 w->bitrate_width, afhi->bitrate,
925 audio_format_name(afsi->audio_format_id),
926 w->frequency_width, afhi->frequency,
929 w->num_played_width, afsi->num_played,
935 hash_to_asc(d->hash, asc_hash);
936 att_lines = make_attribute_lines(att_buf, afsi);
937 lyrics_lines = make_lyrics_lines(afsi);
938 image_lines = make_image_lines(afsi);
939 filename_lines = make_filename_lines(d->path, opts->flags);
940 if (opts->mode == LS_MODE_MBOX) {
941 const char *bn = para_basename(d->path);
943 "From foo@localhost %s\n"
944 "Received: from\nTo: bar\nFrom: a\n"
952 "%s" /* filename stuff */
953 "%s%s%s%s" /* score */
954 "%s\n" /* attributes */
955 "%s: %s\n" /* hash */
956 "%s" /* image id, image name */
958 "%s: %dkbit/s\n" /* bitrate */
959 "%s: %s\n" /* format */
960 "%s: %dHz\n" /* frequency */
961 "%s: %d\n" /* channels */
962 "%s: %s\n" /* duration */
963 "%s: %lu\n" /* seconds total */
964 "%s: %s\n" /* last played time */
965 "%s: %d\n" /* num_played */
966 "%s: %u\n" /* ampplification */
968 "%s: %lu\n" /* chunk time */
969 "%s: %lu\n", /* num chunks */
971 have_score? status_item_list[SI_SCORE] : "",
972 have_score? ": " : "",
974 have_score? "\n" : "",
976 status_item_list[SI_HASH], asc_hash,
979 status_item_list[SI_BITRATE], afhi->bitrate,
980 status_item_list[SI_FORMAT], audio_format_name(afsi->audio_format_id),
981 status_item_list[SI_FREQUENCY], afhi->frequency,
982 status_item_list[SI_CHANNELS], afhi->channels,
983 status_item_list[SI_DURATION], duration_buf,
984 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
985 status_item_list[SI_LAST_PLAYED], last_played_time,
986 status_item_list[SI_NUM_PLAYED], afsi->num_played,
987 status_item_list[SI_AMPLIFICATION], afsi->amp,
989 status_item_list[SI_CHUNK_TIME], tv2ms(&afhi->chunk_tv),
990 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total
994 if (opts->mode == LS_MODE_MBOX) {
995 struct osl_object lyrics_def;
996 lyr_get_def_by_id(afsi->lyrics_id, &lyrics_def);
997 if (lyrics_def.data) {
998 ret = para_printf(b, "Lyrics:\n~~~~~~~\n%s",
999 (char *)lyrics_def.data);
1000 osl_close_disk_object(&lyrics_def);
1006 free(filename_lines);
1008 free(afhi->info_string);
1012 void make_empty_status_items(char *buf)
1016 "%s: \n" /* dirname */
1017 "%s: \n" /* basename */
1018 "%s: \n" /* score */
1019 "%s: \n" /* attributes bitmap */
1020 "%s: \n" /* attributes txt */
1022 "%s: \n" /* image id */
1023 "%s: \n" /* image name */
1024 "%s: \n" /* lyrics id */
1025 "%s: \n" /* lyrics name */
1026 "%s: \n" /* bitrate */
1027 "%s: \n" /* format */
1028 "%s: \n" /* frequency */
1029 "%s: \n" /* channels */
1030 "%s: \n" /* duration */
1031 "%s: \n" /* seconds total */
1032 "%s: \n" /* num played */
1033 "%s: \n" /* last played */
1035 status_item_list[SI_PATH],
1036 status_item_list[SI_DIRECTORY],
1037 status_item_list[SI_BASENAME],
1038 status_item_list[SI_SCORE],
1039 status_item_list[SI_ATTRIBUTES_BITMAP],
1040 status_item_list[SI_ATTRIBUTES_TXT],
1041 status_item_list[SI_HASH],
1042 status_item_list[SI_IMAGE_ID],
1043 status_item_list[SI_IMAGE_NAME],
1044 status_item_list[SI_LYRICS_ID],
1045 status_item_list[SI_LYRICS_NAME],
1046 status_item_list[SI_BITRATE],
1047 status_item_list[SI_FORMAT],
1048 status_item_list[SI_FREQUENCY],
1049 status_item_list[SI_CHANNELS],
1050 status_item_list[SI_DURATION],
1051 status_item_list[SI_SECONDS_TOTAL],
1052 status_item_list[SI_NUM_PLAYED],
1053 status_item_list[SI_LAST_PLAYED]
1057 int make_status_items(struct ls_data *d, struct para_buffer *pb)
1059 struct ls_options opts = {
1060 .flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
1061 .mode = LS_MODE_VERBOSE,
1063 time_t current_time;
1065 time(¤t_time);
1066 return print_list_item(d, &opts, pb, current_time);
1070 static int ls_audio_format_compare(const void *a, const void *b)
1072 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1073 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
1076 static int ls_duration_compare(const void *a, const void *b)
1078 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1079 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
1082 static int ls_bitrate_compare(const void *a, const void *b)
1084 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1085 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
1088 static int ls_lyrics_id_compare(const void *a, const void *b)
1090 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1091 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
1094 static int ls_image_id_compare(const void *a, const void *b)
1096 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1097 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
1100 static int ls_channels_compare(const void *a, const void *b)
1102 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1103 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
1106 static int ls_frequency_compare(const void *a, const void *b)
1108 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1109 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
1112 static int ls_num_played_compare(const void *a, const void *b)
1114 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1115 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
1118 static int ls_last_played_compare(const void *a, const void *b)
1120 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1121 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
1124 static int ls_score_compare(const void *a, const void *b)
1126 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1127 return NUM_COMPARE(d1->score, d2->score);
1130 static int ls_path_compare(const void *a, const void *b)
1132 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1133 return strcmp(d1->path, d2->path);
1136 static int sort_matching_paths(struct ls_options *options)
1138 size_t nmemb = options->num_matching_paths;
1139 size_t size = sizeof(*options->data_ptr);
1140 int (*compar)(const void *, const void *);
1143 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
1144 for (i = 0; i < nmemb; i++)
1145 options->data_ptr[i] = options->data + i;
1147 /* In these cases the array is already sorted */
1148 if (options->sorting == LS_SORT_BY_PATH
1149 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1150 && (options->flags & LS_FLAG_FULL_PATH))
1152 if (options->sorting == LS_SORT_BY_SCORE &&
1153 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1156 switch (options->sorting) {
1157 case LS_SORT_BY_PATH:
1158 compar = ls_path_compare; break;
1159 case LS_SORT_BY_SCORE:
1160 compar = ls_score_compare; break;
1161 case LS_SORT_BY_LAST_PLAYED:
1162 compar = ls_last_played_compare; break;
1163 case LS_SORT_BY_NUM_PLAYED:
1164 compar = ls_num_played_compare; break;
1165 case LS_SORT_BY_FREQUENCY:
1166 compar = ls_frequency_compare; break;
1167 case LS_SORT_BY_CHANNELS:
1168 compar = ls_channels_compare; break;
1169 case LS_SORT_BY_IMAGE_ID:
1170 compar = ls_image_id_compare; break;
1171 case LS_SORT_BY_LYRICS_ID:
1172 compar = ls_lyrics_id_compare; break;
1173 case LS_SORT_BY_BITRATE:
1174 compar = ls_bitrate_compare; break;
1175 case LS_SORT_BY_DURATION:
1176 compar = ls_duration_compare; break;
1177 case LS_SORT_BY_AUDIO_FORMAT:
1178 compar = ls_audio_format_compare; break;
1182 qsort(options->data_ptr, nmemb, size, compar);
1186 /* row is either an aft_row or a row of the score table */
1187 /* TODO: Only compute widths if we need them */
1188 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
1191 struct ls_options *options = ls_opts;
1193 struct ls_widths *w;
1194 unsigned short num_digits;
1196 struct osl_row *aft_row;
1200 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1201 ret = get_score_and_aft_row(row, &score, &aft_row);
1206 ret = get_audio_file_path_of_row(aft_row, &path);
1209 if (!(options->flags & LS_FLAG_FULL_PATH)) {
1210 char *p = strrchr(path, '/');
1214 if (options->num_patterns) {
1215 for (i = 0; i < options->num_patterns; i++) {
1216 ret = fnmatch(options->patterns[i], path, 0);
1219 if (ret == FNM_NOMATCH)
1223 if (i >= options->num_patterns) /* no match */
1226 tmp = options->num_matching_paths++;
1227 if (options->num_matching_paths > options->array_size) {
1228 options->array_size++;
1229 options->array_size *= 2;
1230 options->data = para_realloc(options->data, options->array_size
1231 * sizeof(*options->data));
1233 d = options->data + tmp;
1234 ret = get_afsi_of_row(aft_row, &d->afsi);
1237 ret = get_afhi_of_row(aft_row, &d->afhi);
1241 ret = get_hash_of_row(aft_row, &d->hash);
1244 w = &options->widths;
1245 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
1246 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
1247 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
1248 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
1249 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
1250 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
1251 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
1252 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
1253 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
1254 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
1255 /* get the number of chars to print this amount of time */
1256 tmp = get_duration_width(d->afhi.seconds_total);
1257 w->duration_width = PARA_MAX(w->duration_width, tmp);
1258 GET_NUM_DIGITS(d->afsi.amp, &num_digits);
1259 w->amp_width = PARA_MAX(w->amp_width, num_digits);
1260 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1261 GET_NUM_DIGITS(score, &num_digits);
1262 num_digits++; /* add one for the sign (space or "-") */
1263 w->score_width = PARA_MAX(w->score_width, num_digits);
1268 free(d->afhi.info_string);
1272 static void com_ls_callback(int fd, const struct osl_object *query)
1274 struct ls_options *opts = query->data;
1275 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
1276 struct para_buffer b = {.max_size = SHMMAX,
1277 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1279 time_t current_time;
1282 if (opts->num_patterns) {
1283 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
1284 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
1285 opts->patterns[i] = p;
1289 opts->patterns = NULL;
1290 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
1291 ret = admissible_file_loop(opts, prepare_ls_row);
1293 ret = osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
1297 ret = opts->num_patterns? -E_NO_MATCH : 0;
1298 if (!opts->num_matching_paths)
1300 ret = sort_matching_paths(opts);
1303 time(¤t_time);
1304 if (opts->flags & LS_FLAG_REVERSE)
1305 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1306 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1311 for (i = 0; i < opts->num_matching_paths; i++) {
1312 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1318 pass_buffer_as_shm(b.buf, b.offset, &fd);
1321 free(opts->data_ptr);
1322 free(opts->patterns);
1326 * TODO: flags -h (sort by hash)
1328 int com_ls(int fd, int argc, char * const * const argv)
1332 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1333 enum ls_listing_mode mode = LS_MODE_SHORT;
1334 struct ls_options opts = {.patterns = NULL};
1335 struct osl_object query = {.data = &opts, .size = sizeof(opts)};
1337 for (i = 1; i < argc; i++) {
1338 const char *arg = argv[i];
1341 if (!strcmp(arg, "--")) {
1345 if (!strncmp(arg, "-l", 2)) {
1347 mode = LS_MODE_LONG;
1351 return -E_AFT_SYNTAX;
1352 switch(*(arg + 2)) {
1354 mode = LS_MODE_SHORT;
1357 mode = LS_MODE_LONG;
1360 mode = LS_MODE_VERBOSE;
1363 mode = LS_MODE_MBOX;
1366 mode = LS_MODE_CHUNKS;
1369 return -E_AFT_SYNTAX;
1372 if (!strcmp(arg, "-p")) {
1373 flags |= LS_FLAG_FULL_PATH;
1376 if (!strcmp(arg, "-a")) {
1377 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1380 if (!strcmp(arg, "-r")) {
1381 flags |= LS_FLAG_REVERSE;
1384 if (!strncmp(arg, "-s", 2)) {
1385 if (!*(arg + 2) || *(arg + 3))
1386 return -E_AFT_SYNTAX;
1387 switch(*(arg + 2)) {
1389 sort = LS_SORT_BY_PATH;
1391 case 's': /* -ss implies -a */
1392 sort = LS_SORT_BY_SCORE;
1393 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1396 sort = LS_SORT_BY_LAST_PLAYED;
1399 sort = LS_SORT_BY_NUM_PLAYED;
1402 sort = LS_SORT_BY_FREQUENCY;
1405 sort = LS_SORT_BY_CHANNELS;
1408 sort = LS_SORT_BY_IMAGE_ID;
1411 sort = LS_SORT_BY_LYRICS_ID;
1414 sort = LS_SORT_BY_BITRATE;
1417 sort = LS_SORT_BY_DURATION;
1420 sort = LS_SORT_BY_AUDIO_FORMAT;
1423 return -E_AFT_SYNTAX;
1426 return -E_AFT_SYNTAX;
1429 opts.sorting = sort;
1431 opts.num_patterns = argc - i;
1432 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1433 argv + i, com_ls_callback, send_result, &fd);
1438 * Call the given function for each file in the audio file table.
1440 * \param private_data An arbitrary data pointer, passed to \a func.
1441 * \param func The custom function to be called.
1443 * \return The return value of the underlying call to osl_rbtree_loop().
1445 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1447 return osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1451 static struct osl_row *find_hash_sister(HASH_TYPE *hash)
1453 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1454 struct osl_row *row;
1456 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1460 /** The format of the data stored by save_audio_file_data(). */
1461 enum com_add_buffer_offsets {
1462 /** afhi (if present) starts here. */
1463 CAB_AFHI_OFFSET_POS = 0,
1464 /** Start of the chunk table (if present). */
1465 CAB_CHUNKS_OFFSET_POS = 2,
1466 /** Audio format id. */
1467 CAB_AUDIO_FORMAT_OFFSET = 4,
1468 /** Flags given to the add command. */
1469 CAB_FLAGS_OFFSET = 5,
1470 /** The hash of the audio file being added. */
1471 CAB_HASH_OFFSET = 9,
1472 /** Start of the path of the audio file. */
1473 CAB_PATH_OFFSET = (CAB_HASH_OFFSET + HASH_SIZE),
1477 * Store the given data to a single buffer. Doesn't need the audio file selector
1478 * info struct as the server knows it as well.
1480 * It's OK to call this with afhi == NULL. In this case, the audio format
1481 * handler info won't be stored in the buffer.
1483 static void save_add_callback_buffer(HASH_TYPE *hash, const char *path,
1484 struct afh_info *afhi, uint32_t flags,
1485 uint8_t audio_format_num, struct osl_object *obj)
1487 size_t path_len = strlen(path) + 1;
1488 size_t afhi_size = sizeof_afhi_buf(afhi);
1489 size_t size = CAB_PATH_OFFSET + path_len + afhi_size
1490 + sizeof_chunk_table(afhi);
1491 char *buf = para_malloc(size);
1494 write_u8(buf + CAB_AUDIO_FORMAT_OFFSET, audio_format_num);
1495 write_u32(buf + CAB_FLAGS_OFFSET, flags);
1497 memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE);
1498 strcpy(buf + CAB_PATH_OFFSET, path);
1500 pos = CAB_PATH_OFFSET + path_len;
1501 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1502 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1503 pos + afhi_size - 1);
1504 write_u16(buf + CAB_AFHI_OFFSET_POS, pos);
1505 save_afhi(afhi, buf + pos);
1508 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1509 write_u16(buf + CAB_CHUNKS_OFFSET_POS, pos);
1511 save_chunk_table(afhi, buf + pos);
1512 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1519 Overview of the add command.
1521 Input: What was passed to the callback by the command handler.
1523 HS: Hash sister. Whether an audio file with identical hash
1524 already exists in the osl database.
1526 PB: Path brother. Whether a file with the given path exists
1529 F: Force flag given. Whether add was called with -f.
1531 output: Action performed by the callback.
1533 AFHI: Whether afhi and chunk table are computed and sent.
1534 ACTION: Table modifications to be done by the callback.
1536 +----+----+---+------+---------------------------------------------------+
1537 | HS | PB | F | AFHI | ACTION
1538 +----+----+---+------+---------------------------------------------------+
1539 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1540 | | update path, keep afsi
1541 +----+----+---+------+---------------------------------------------------+
1542 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1543 | | otherwise: remove PB, HS: update path, keep afhi,
1545 +----+----+---+------+---------------------------------------------------+
1546 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1548 +----+----+---+------+---------------------------------------------------+
1549 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1550 +----+----+---+------+---------------------------------------------------+
1551 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1552 | | (force has no effect)
1553 +----+----+---+------+---------------------------------------------------+
1554 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1555 +----+----+---+------+---------------------------------------------------+
1556 | N | N | Y | Y | (new file) create new entry (force has no effect)
1557 +----+----+---+------+---------------------------------------------------+
1558 | N | N | N | Y | (new file) create new entry
1559 +----+----+---+------+---------------------------------------------------+
1563 afhi <=> force or no HS
1568 /** Flags passed to the add command. */
1569 enum com_add_flags {
1570 /** Skip paths that exist already. */
1572 /** Force adding. */
1574 /** Print what is being done. */
1575 ADD_FLAG_VERBOSE = 4,
1576 /** Try to add files with unknown suffixes. */
1580 static void com_add_callback(int fd, const struct osl_object *query)
1582 char *buf = query->data, *path;
1583 struct osl_row *pb, *aft_row;
1585 struct osl_object objs[NUM_AFT_COLUMNS];
1587 char asc[2 * HASH_SIZE + 1];
1589 char afsi_buf[AFSI_SIZE];
1590 uint32_t flags = read_u32(buf + CAB_FLAGS_OFFSET);
1591 struct afs_info default_afsi = {.last_played = 0};
1592 struct para_buffer msg = {.max_size = SHMMAX,
1593 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1595 hash = (HASH_TYPE *)buf + CAB_HASH_OFFSET;
1596 hash_to_asc(hash, asc);;
1597 objs[AFTCOL_HASH].data = buf + CAB_HASH_OFFSET;
1598 objs[AFTCOL_HASH].size = HASH_SIZE;
1600 path = buf + CAB_PATH_OFFSET;
1601 objs[AFTCOL_PATH].data = path;
1602 objs[AFTCOL_PATH].size = strlen(path) + 1;
1604 PARA_INFO_LOG("request to add %s\n", path);
1605 hs = find_hash_sister(hash);
1606 ret = aft_get_row_of_path(path, &pb);
1607 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1609 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1610 if (flags & ADD_FLAG_VERBOSE)
1611 ret = para_printf(&msg, "ignoring duplicate\n");
1616 if (hs && hs != pb) {
1617 struct osl_object obj;
1618 if (pb) { /* hs trumps pb, remove pb */
1619 if (flags & ADD_FLAG_VERBOSE) {
1620 ret = para_printf(&msg, "removing path brother\n");
1624 ret = osl_del_row(audio_file_table, pb);
1629 /* file rename, update hs' path */
1630 if (flags & ADD_FLAG_VERBOSE) {
1631 ret = osl_get_object(audio_file_table, hs,
1635 ret = para_printf(&msg, "renamed from %s\n", (char *)obj.data);
1639 ret = osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1640 &objs[AFTCOL_PATH]);
1643 afs_event(AUDIO_FILE_RENAME, &msg, hs);
1644 if (!(flags & ADD_FLAG_FORCE))
1647 /* no hs or force mode, child must have sent afhi */
1648 uint16_t afhi_offset = read_u16(buf + CAB_AFHI_OFFSET_POS);
1649 uint16_t chunks_offset = read_u16(buf + CAB_CHUNKS_OFFSET_POS);
1651 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1652 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1654 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1656 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1657 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1658 if (pb && !hs) { /* update pb's hash */
1659 char old_asc[2 * HASH_SIZE + 1];
1660 HASH_TYPE *old_hash;
1661 ret = get_hash_of_row(pb, &old_hash);
1664 hash_to_asc(old_hash, old_asc);
1665 if (flags & ADD_FLAG_VERBOSE) {
1666 ret = para_printf(&msg, "file change: %s -> %s\n",
1671 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1672 &objs[AFTCOL_HASH]);
1676 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1677 struct osl_row *row = pb? pb : hs;
1678 /* update afhi and chunk_table */
1679 if (flags & ADD_FLAG_VERBOSE) {
1680 ret = para_printf(&msg, "updating afhi and chunk table\n");
1684 ret = osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1685 &objs[AFTCOL_AFHI]);
1688 ret = osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1689 &objs[AFTCOL_CHUNKS]);
1692 afs_event(AFHI_CHANGE, &msg, row);
1695 /* new entry, use default afsi */
1696 if (flags & ADD_FLAG_VERBOSE) {
1697 ret = para_printf(&msg, "new file\n");
1701 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1702 default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_OFFSET);
1704 objs[AFTCOL_AFSI].data = &afsi_buf;
1705 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1706 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1707 ret = osl_add_and_get_row(audio_file_table, objs, &aft_row);
1708 afs_event(AUDIO_FILE_ADD, &msg, aft_row);
1711 ret = para_printf(&msg, "%s\n", para_strerror(-ret));
1713 pass_buffer_as_shm(msg.buf, msg.offset, &fd);
1717 /** Used by com_add(). */
1718 struct private_add_data {
1719 /** The socket file descriptor. */
1721 /** The given add flags. */
1725 static void path_brother_callback(int fd, const struct osl_object *query)
1727 char *path = query->data;
1728 struct osl_row *path_brother;
1729 int ret = aft_get_row_of_path(path, &path_brother);
1732 pass_buffer_as_shm((char *)&path_brother, sizeof(path_brother), &fd);
1735 static void hash_sister_callback(int fd, const struct osl_object *query)
1737 HASH_TYPE *hash = query->data;
1738 struct osl_row *hash_sister;
1740 hash_sister = find_hash_sister(hash);
1743 pass_buffer_as_shm((char *)&hash_sister, sizeof(hash_sister), &fd);
1746 static int get_row_pointer_from_result(struct osl_object *result, void *private)
1748 struct osl_row **row = private;
1749 *row = result->data;
1753 static int add_one_audio_file(const char *path, void *private_data)
1755 int ret, send_ret = 1, fd;
1756 uint8_t format_num = -1;
1757 struct private_add_data *pad = private_data;
1758 struct afh_info afhi, *afhi_ptr = NULL;
1759 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1760 struct osl_object map, obj = {.data = NULL}, query;
1761 HASH_TYPE hash[HASH_SIZE];
1763 ret = guess_audio_format(path);
1764 if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
1766 query.data = (char *)path;
1767 query.size = strlen(path) + 1;
1768 ret = send_callback_request(path_brother_callback, &query,
1769 get_row_pointer_from_result, &pb);
1770 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
1773 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1774 if (pad->flags & ADD_FLAG_VERBOSE)
1775 send_ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1778 /* We still want to add this file. Compute its hash. */
1779 ret = mmap_full_file(path, O_RDONLY, &map.data, &map.size, &fd);
1782 hash_function(map.data, map.size, hash);
1784 /* Check whether the database contains a file with the same hash. */
1786 query.size = HASH_SIZE;
1787 ret = send_callback_request(hash_sister_callback, &query,
1788 get_row_pointer_from_result, &hs);
1791 /* Return success if we already know this file. */
1793 if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) {
1794 if (pad->flags & ADD_FLAG_VERBOSE)
1795 send_ret = send_va_buffer(pad->fd,
1796 "%s exists, not forcing update\n", path);
1800 * We won't recalculate the audio format info and the chunk table if
1801 * there is a hash sister and FORCE was not given.
1803 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1804 ret = compute_afhi(path, map.data, map.size, fd, &afhi);
1810 munmap(map.data, map.size);
1811 if (pad->flags & ADD_FLAG_VERBOSE) {
1812 send_ret = send_va_buffer(pad->fd, "adding %s\n", path);
1816 save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1817 /* Ask afs to consider this entry for adding. */
1818 ret = send_callback_request(com_add_callback, &obj, send_result, &pad->fd);
1823 munmap(map.data, map.size);
1825 if (ret < 0 && send_ret >= 0)
1826 send_ret = send_va_buffer(pad->fd, "failed to add %s (%s)\n", path,
1827 para_strerror(-ret));
1830 free(afhi_ptr->chunk_table);
1831 free(afhi_ptr->info_string);
1833 /* Stop adding files only on send errors. */
1837 int com_add(int fd, int argc, char * const * const argv)
1840 struct private_add_data pad = {.fd = fd, .flags = 0};
1841 struct stat statbuf;
1843 for (i = 1; i < argc; i++) {
1844 const char *arg = argv[i];
1847 if (!strcmp(arg, "--")) {
1851 if (!strcmp(arg, "-a")) {
1852 pad.flags |= ADD_FLAG_ALL;
1855 if (!strcmp(arg, "-l")) {
1856 pad.flags |= ADD_FLAG_LAZY;
1859 if (!strcmp(arg, "-f")) {
1860 pad.flags |= ADD_FLAG_FORCE;
1863 if (!strcmp(arg, "-v")) {
1864 pad.flags |= ADD_FLAG_VERBOSE;
1869 return -E_AFT_SYNTAX;
1870 for (; i < argc; i++) {
1872 ret = verify_path(argv[i], &path);
1874 ret = send_va_buffer(fd, "%s: %s\n", argv[i],
1875 para_strerror(-ret));
1880 ret = stat(path, &statbuf);
1882 ret = send_va_buffer(fd, "failed to stat %s (%s)\n", path,
1889 if (S_ISDIR(statbuf.st_mode))
1890 ret = for_each_file_in_dir(path, add_one_audio_file,
1893 ret = add_one_audio_file(path, &pad);
1895 send_va_buffer(fd, "%s: %s\n", path, para_strerror(-ret));
1906 * Flags used by the touch command.
1911 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1912 TOUCH_FLAG_FNM_PATHNAME = 1,
1913 /** Activates verbose mode. */
1914 TOUCH_FLAG_VERBOSE = 2
1917 /** Options used by com_touch(). */
1918 struct com_touch_options {
1919 /** New num_played value. */
1921 /** New last played count. */
1922 int64_t last_played;
1923 /** New lyrics id. */
1925 /** New image id. */
1927 /** New amplification value. */
1929 /** Command line flags (see \ref touch_flags). */
1933 /** Data passed to the action handler of com_touch(). */
1934 struct touch_action_data {
1935 /** Command line options (see \ref com_touch_options). */
1936 struct com_touch_options *cto;
1937 /** Message buffer. */
1938 struct para_buffer pb;
1939 /** How many audio files matched the given pattern. */
1940 unsigned num_matches;
1943 static int touch_audio_file(__a_unused struct osl_table *table,
1944 struct osl_row *row, const char *name, void *data)
1946 struct touch_action_data *tad = data;
1947 struct osl_object obj;
1948 struct afs_info old_afsi, new_afsi;
1949 int ret, no_options = tad->cto->num_played < 0 && tad->cto->last_played < 0 &&
1950 tad->cto->lyrics_id < 0 && tad->cto->image_id < 0;
1951 struct afsi_change_event_data aced;
1953 ret = get_afsi_object_of_row(row, &obj);
1955 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1956 ret = load_afsi(&old_afsi, &obj);
1958 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1959 new_afsi = old_afsi;
1961 new_afsi.num_played++;
1962 new_afsi.last_played = time(NULL);
1963 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1964 ret = para_printf(&tad->pb, "%s: num_played = %u, "
1965 "last_played = now()\n", name,
1966 new_afsi.num_played);
1971 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1972 ret = para_printf(&tad->pb, "touching %s\n", name);
1976 if (tad->cto->lyrics_id >= 0)
1977 new_afsi.lyrics_id = tad->cto->lyrics_id;
1978 if (tad->cto->image_id >= 0)
1979 new_afsi.image_id = tad->cto->image_id;
1980 if (tad->cto->num_played >= 0)
1981 new_afsi.num_played = tad->cto->num_played;
1982 if (tad->cto->last_played >= 0)
1983 new_afsi.last_played = tad->cto->last_played;
1984 new_afsi.amp = tad->cto->amp;
1987 save_afsi(&new_afsi, &obj); /* in-place update */
1989 aced.old_afsi = &old_afsi;
1990 afs_event(AFSI_CHANGE, &tad->pb, &aced);
1994 static void com_touch_callback(int fd, const struct osl_object *query)
1996 struct touch_action_data tad = {.cto = query->data,
1999 .private_data = &fd,
2000 .max_size_handler = pass_buffer_as_shm
2004 struct pattern_match_data pmd = {
2005 .table = audio_file_table,
2006 .loop_col_num = AFTCOL_HASH,
2007 .match_col_num = AFTCOL_PATH,
2008 .patterns = {.data = (char *)query->data + sizeof(*tad.cto),
2009 .size = query->size - sizeof(*tad.cto)},
2011 .action = touch_audio_file
2013 if (tad.cto->flags & TOUCH_FLAG_FNM_PATHNAME)
2014 pmd.fnmatch_flags |= FNM_PATHNAME;
2015 ret = for_each_matching_row(&pmd);
2017 ret2 = para_printf(&tad.pb, "%s\n", para_strerror(-ret));
2019 if (!tad.num_matches)
2020 ret2 = para_printf(&tad.pb, "no matches\n");
2021 if (ret2 >= 0 && tad.pb.offset)
2022 pass_buffer_as_shm(tad.pb.buf, tad.pb.offset, &fd);
2026 int com_touch(int fd, int argc, char * const * const argv)
2028 struct com_touch_options cto = {
2035 struct osl_object query = {.data = &cto, .size = sizeof(cto)};
2039 for (i = 1; i < argc; i++) {
2040 const char *arg = argv[i];
2043 if (!strcmp(arg, "--")) {
2047 if (!strncmp(arg, "-n", 2)) {
2048 ret = para_atoi32(arg + 2, &cto.num_played);
2053 if (!strncmp(arg, "-l", 2)) {
2054 ret = para_atoi64(arg + 2, &cto.last_played);
2059 if (!strncmp(arg, "-y", 2)) {
2060 ret = para_atoi32(arg + 2, &cto.lyrics_id);
2065 if (!strncmp(arg, "-i", 2)) {
2066 ret = para_atoi32(arg + 2, &cto.image_id);
2071 if (!strncmp(arg, "-a", 2)) {
2073 ret = para_atoi32(arg + 2, &val);
2076 if (val < 0 || val > 255)
2077 return -ERRNO_TO_PARA_ERROR(EINVAL);
2081 if (!strcmp(arg, "-p")) {
2082 cto.flags |= TOUCH_FLAG_FNM_PATHNAME;
2085 if (!strcmp(arg, "-v")) {
2086 cto.flags |= TOUCH_FLAG_VERBOSE;
2089 break; /* non-option starting with dash */
2092 return -E_AFT_SYNTAX;
2093 ret = send_option_arg_callback_request(&query, argc - i,
2094 argv + i, com_touch_callback, send_result, &fd);
2096 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2100 /** Flags for com_rm(). */
2103 RM_FLAG_VERBOSE = 1,
2107 RM_FLAG_FNM_PATHNAME = 4
2110 /** Data passed to the action handler of com_rm(). */
2111 struct com_rm_action_data {
2112 /** Command line flags ((see \ref rm_flags). */
2114 /** Message buffer. */
2115 struct para_buffer pb;
2116 /** Number of audio files removed. */
2117 unsigned num_removed;
2120 static int remove_audio_file(__a_unused struct osl_table *table,
2121 struct osl_row *row, const char *name, void *data)
2123 struct com_rm_action_data *crd = data;
2126 if (crd->flags & RM_FLAG_VERBOSE) {
2127 ret = para_printf(&crd->pb, "removing %s\n", name);
2131 afs_event(AUDIO_FILE_REMOVE, &crd->pb, row);
2132 ret = osl_del_row(audio_file_table, row);
2134 ret2 = para_printf(&crd->pb, "%s: %s\n", name,
2135 para_strerror(-ret));
2141 static void com_rm_callback(int fd, const struct osl_object *query)
2143 struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
2146 .private_data = &fd,
2147 .max_size_handler = pass_buffer_as_shm
2151 struct pattern_match_data pmd = {
2152 .table = audio_file_table,
2153 .loop_col_num = AFTCOL_HASH,
2154 .match_col_num = AFTCOL_PATH,
2155 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
2156 .size = query->size - sizeof(uint32_t)},
2158 .action = remove_audio_file
2160 if (crd.flags & RM_FLAG_FNM_PATHNAME)
2161 pmd.fnmatch_flags |= FNM_PATHNAME;
2162 ret = for_each_matching_row(&pmd);
2164 ret = para_printf(&crd.pb, "%s\n", para_strerror(-ret));
2167 if (!crd.num_removed && !(crd.flags & RM_FLAG_FORCE))
2168 ret = para_printf(&crd.pb, "no matches -- nothing removed\n");
2170 if (crd.flags & RM_FLAG_VERBOSE)
2171 ret = para_printf(&crd.pb, "removed %u files\n", crd.num_removed);
2173 if (ret >= 0 && crd.pb.offset)
2174 pass_buffer_as_shm(crd.pb.buf, crd.pb.offset, &fd);
2178 /* TODO options: -r (recursive) */
2179 int com_rm(int fd, int argc, char * const * const argv)
2182 struct osl_object query = {.data = &flags, .size = sizeof(flags)};
2185 for (i = 1; i < argc; i++) {
2186 const char *arg = argv[i];
2189 if (!strcmp(arg, "--")) {
2193 if (!strcmp(arg, "-f")) {
2194 flags |= RM_FLAG_FORCE;
2197 if (!strcmp(arg, "-p")) {
2198 flags |= RM_FLAG_FNM_PATHNAME;
2201 if (!strcmp(arg, "-v")) {
2202 flags |= RM_FLAG_VERBOSE;
2208 return -E_AFT_SYNTAX;
2209 ret = send_option_arg_callback_request(&query, argc - i, argv + i,
2210 com_rm_callback, send_result, &fd);
2212 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2217 * Flags used by the cpsi command.
2222 /** Whether the lyrics id should be copied. */
2223 CPSI_FLAG_COPY_LYRICS_ID = 1,
2224 /** Whether the image id should be copied. */
2225 CPSI_FLAG_COPY_IMAGE_ID = 2,
2226 /** Whether the lastplayed time should be copied. */
2227 CPSI_FLAG_COPY_LASTPLAYED = 4,
2228 /** Whether the numplayed count should be copied. */
2229 CPSI_FLAG_COPY_NUMPLAYED = 8,
2230 /** Whether the attributes should be copied. */
2231 CPSI_FLAG_COPY_ATTRIBUTES = 16,
2232 /** Activates verbose mode. */
2233 CPSI_FLAG_VERBOSE = 32,
2236 /** Data passed to the action handler of com_cpsi(). */
2237 struct cpsi_action_data {
2238 /** command line flags (see \ref cpsi_flags). */
2240 /** Number of audio files changed. */
2241 unsigned num_copied;
2242 /** Message buffer. */
2243 struct para_buffer pb;
2244 /** Values are copied from here. */
2245 struct afs_info source_afsi;
2248 static int copy_selector_info(__a_unused struct osl_table *table,
2249 struct osl_row *row, const char *name, void *data)
2251 struct cpsi_action_data *cad = data;
2252 struct osl_object target_afsi_obj;
2254 struct afs_info old_afsi, target_afsi;
2255 struct afsi_change_event_data aced;
2257 ret = get_afsi_object_of_row(row, &target_afsi_obj);
2260 load_afsi(&target_afsi, &target_afsi_obj);
2261 old_afsi = target_afsi;
2262 if (cad->flags & CPSI_FLAG_COPY_LYRICS_ID)
2263 target_afsi.lyrics_id = cad->source_afsi.lyrics_id;
2264 if (cad->flags & CPSI_FLAG_COPY_IMAGE_ID)
2265 target_afsi.image_id = cad->source_afsi.image_id;
2266 if (cad->flags & CPSI_FLAG_COPY_LASTPLAYED)
2267 target_afsi.last_played = cad->source_afsi.last_played;
2268 if (cad->flags & CPSI_FLAG_COPY_NUMPLAYED)
2269 target_afsi.num_played = cad->source_afsi.num_played;
2270 if (cad->flags & CPSI_FLAG_COPY_ATTRIBUTES)
2271 target_afsi.attributes = cad->source_afsi.attributes;
2272 save_afsi(&target_afsi, &target_afsi_obj); /* in-place update */
2274 if (cad->flags & CPSI_FLAG_VERBOSE) {
2275 ret = para_printf(&cad->pb, "copied afsi to %s\n", name);
2280 aced.old_afsi = &old_afsi;
2281 afs_event(AFSI_CHANGE, &cad->pb, &aced);
2285 static void com_cpsi_callback(int fd, const struct osl_object *query)
2287 struct cpsi_action_data cad = {
2288 .flags = *(unsigned *)query->data,
2291 .private_data = &fd,
2292 .max_size_handler = pass_buffer_as_shm
2296 char *source_path = (char *)query->data + sizeof(cad.flags);
2298 ret = get_afsi_of_path(source_path, &cad.source_afsi);
2301 struct pattern_match_data pmd = {
2302 .table = audio_file_table,
2303 .loop_col_num = AFTCOL_HASH,
2304 .match_col_num = AFTCOL_PATH,
2305 .patterns = {.data = source_path + strlen(source_path) + 1,
2306 .size = query->size - sizeof(cad.flags)
2307 - strlen(source_path) - 1},
2309 .action = copy_selector_info
2311 ret = for_each_matching_row(&pmd);
2314 ret = para_printf(&cad.pb, "%s\n", para_strerror(-ret));
2315 else if (cad.flags & CPSI_FLAG_VERBOSE) {
2317 ret = para_printf(&cad.pb, "copied requested afsi from %s "
2319 source_path, cad.num_copied);
2321 ret = para_printf(&cad.pb, "nothing copied\n");
2324 pass_buffer_as_shm(cad.pb.buf, cad.pb.offset, &fd);
2328 int com_cpsi(int fd, int argc, char * const * const argv)
2332 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
2334 for (i = 1; i < argc; i++) {
2335 const char *arg = argv[i];
2338 if (!strcmp(arg, "--")) {
2342 if (!strcmp(arg, "-y")) {
2343 flags |= CPSI_FLAG_COPY_LYRICS_ID;
2346 if (!strcmp(arg, "-i")) {
2347 flags |= CPSI_FLAG_COPY_IMAGE_ID;
2350 if (!strcmp(arg, "-l")) {
2351 flags |= CPSI_FLAG_COPY_LASTPLAYED;
2354 if (!strcmp(arg, "-n")) {
2355 flags |= CPSI_FLAG_COPY_NUMPLAYED;
2358 if (!strcmp(arg, "-a")) {
2359 flags |= CPSI_FLAG_COPY_ATTRIBUTES;
2362 if (!strcmp(arg, "-v")) {
2363 flags |= CPSI_FLAG_VERBOSE;
2368 if (i + 1 >= argc) /* need at least souce file and pattern */
2369 return -E_AFT_SYNTAX;
2370 if (!(flags & ~CPSI_FLAG_VERBOSE)) /* no copy flags given */
2371 flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags;
2372 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
2373 com_cpsi_callback, send_result, &fd);
2375 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2379 /* TODO: optionally fix problems by removing offending rows */
2380 static int check_audio_file(struct osl_row *row, void *data)
2383 struct para_buffer *pb = data;
2384 struct stat statbuf;
2385 int ret = get_audio_file_path_of_row(row, &path);
2386 struct afs_info afsi;
2390 return para_printf(pb, "%s\n", para_strerror(-ret));
2391 if (stat(path, &statbuf) < 0) {
2392 ret = para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
2396 if (!S_ISREG(statbuf.st_mode)) {
2397 ret = para_printf(pb, "%s: not a regular file\n", path);
2402 ret = get_afsi_of_row(row, &afsi);
2404 return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
2405 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
2407 ret = para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
2408 para_strerror(-ret));
2412 ret = img_get_name_by_id(afsi.image_id, &blob_name);
2414 ret = para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
2415 para_strerror(-ret));
2420 * Check the audio file table for inconsistencies.
2422 * \param fd The afs socket.
2423 * \param query Unused.
2425 * This function always succeeds.
2429 void aft_check_callback(int fd, __a_unused const struct osl_object *query)
2431 struct para_buffer pb = {
2433 .private_data = &fd,
2434 .max_size_handler = pass_buffer_as_shm
2436 int ret = para_printf(&pb, "checking audio file table...\n");
2440 audio_file_loop(&pb, check_audio_file);
2442 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
2447 * Close the audio file table.
2449 * \param flags Usual flags that are passed to osl_close_table().
2451 * \sa osl_close_table().
2453 static void aft_close(void)
2455 osl_close_table(audio_file_table, OSL_MARK_CLEAN);
2456 audio_file_table = NULL;
2460 * Open the audio file table.
2462 * \param dir The database directory.
2466 * \sa osl_open_table().
2468 static int aft_open(const char *dir)
2472 audio_file_table_desc.dir = dir;
2473 ret = osl_open_table(&audio_file_table_desc, &audio_file_table);
2476 osl_get_num_rows(audio_file_table, &num);
2477 PARA_INFO_LOG("audio file table contains %d files\n", num);
2480 PARA_INFO_LOG("failed to open audio file table\n");
2481 audio_file_table = NULL;
2482 if (ret >= 0 || is_errno(-ret, ENOENT))
2487 static int aft_create(const char *dir)
2489 audio_file_table_desc.dir = dir;
2490 return osl_create_table(&audio_file_table_desc);
2493 static int clear_attribute(struct osl_row *row, void *data)
2495 struct rmatt_event_data *red = data;
2496 struct afs_info afsi;
2497 struct osl_object obj;
2498 int ret = get_afsi_object_of_row(row, &obj);
2499 uint64_t mask = ~(1ULL << red->bitnum);
2503 ret = load_afsi(&afsi, &obj);
2506 afsi.attributes &= mask;
2507 save_afsi(&afsi, &obj);
2511 static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
2517 case ATTRIBUTE_REMOVE: {
2518 const struct rmatt_event_data *red = data;
2519 ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
2520 "entries in the audio file table\n", red->name,
2524 return audio_file_loop(data, clear_attribute);
2531 void aft_init(struct afs_table *t)
2533 t->name = audio_file_table_desc.name;
2535 t->close = aft_close;
2536 t->create = aft_create;
2537 t->event_handler = aft_event_handler;