2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
9 #include <dirent.h> /* readdir() */
21 static struct osl_table
*audio_file_table
;
23 /** The different sorting methods of the ls command. */
24 enum ls_sorting_method
{
30 LS_SORT_BY_LAST_PLAYED
,
32 LS_SORT_BY_NUM_PLAYED
,
46 LS_SORT_BY_AUDIO_FORMAT
,
51 /** The different listing modes of the ls command. */
52 enum ls_listing_mode
{
53 /** Default listing mode. */
63 /** The flags accepted by the ls command. */
66 LS_FLAG_FULL_PATH
= 1,
68 LS_FLAG_ADMISSIBLE_ONLY
= 2,
74 * The size of the individual output fields of the ls command.
76 * These depend on the actual content being listed. If, for instance only files
77 * with duration less than an hour are being listed, then the duration with is
78 * made smaller because then the duration is listed as mm:ss rather than
82 /** size of the score field. */
83 unsigned short score_width
;
84 /** size of the image id field. */
85 unsigned short image_id_width
;
86 /** size of the lyrics id field. */
87 unsigned short lyrics_id_width
;
88 /** size of the bitrate field. */
89 unsigned short bitrate_width
;
90 /** size of the frequency field. */
91 unsigned short frequency_width
;
92 /** size of the duration field. */
93 unsigned short duration_width
;
94 /** size of the num played field. */
95 unsigned short num_played_width
;
98 /** Data passed to the different compare functions (called by qsort()). */
100 /** Usual audio format handler information. */
101 struct audio_format_info afhi
;
102 /** Audio file selector information. */
103 struct afs_info afsi
;
104 /** The full path of the audio file. */
106 /** The score value (if -a was given). */
108 /** The sha1 hash of audio file. */
114 enum ls_sorting_method sorting
;
115 enum ls_listing_mode mode
;
118 struct ls_widths widths
;
120 uint32_t num_matching_paths
;
121 struct ls_data
*data
;
122 struct ls_data
**data_ptr
;
126 * Describes the layout of the mmapped-afs info struct.
128 * \sa struct afs_info.
131 /** Where .last_played is stored. */
132 AFSI_LAST_PLAYED_OFFSET
= 0,
133 /** Storage position of the attributes bitmap. */
134 AFSI_ATTRIBUTES_OFFSET
= 8,
135 /** Storage position of the .num_played field. */
136 AFSI_NUM_PLAYED_OFFSET
= 16,
137 /** Storage position of the .image_id field. */
138 AFSI_IMAGE_ID_OFFSET
= 20,
139 /** Storage position of the .lyrics_id field. */
140 AFSI_LYRICS_ID_OFFSET
= 24,
141 /** Storage position of the .audio_format_id field. */
142 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
143 /** 3 bytes reserved space for future usage. */
144 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 29,
145 /** On-disk storage space needed. */
150 * Convert a struct afs_info to an osl object.
152 * \param afsi Pointer to the audio file info to be converted.
153 * \param obj Result pointer.
157 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
159 char *buf
= obj
->data
;
161 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
162 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
163 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
164 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
165 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
166 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
167 afsi
->audio_format_id
);
168 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 3);
172 * Get the audio file selector info struct stored in an osl object.
174 * \param afsi Points to the audio_file info structure to be filled in.
175 * \param obj The osl object holding the data.
177 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
181 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
183 char *buf
= obj
->data
;
184 if (obj
->size
< AFSI_SIZE
)
186 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
187 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
188 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
189 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
190 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
191 afsi
->audio_format_id
= read_u8(buf
+
192 AFSI_AUDIO_FORMAT_ID_OFFSET
);
196 /** The columns of the audio file table. */
197 enum audio_file_table_columns
{
198 /** The hash on the content of the audio file. */
200 /** The full path in the filesystem. */
202 /** The audio file selector info. */
204 /** The audio format handler info. */
206 /** The chunk table info and the chunk table of the audio file. */
208 /** The number of columns of this table. */
212 static struct osl_column_description aft_cols
[] = {
214 .storage_type
= OSL_MAPPED_STORAGE
,
215 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
217 .compare_function
= osl_hash_compare
,
218 .data_size
= HASH_SIZE
221 .storage_type
= OSL_MAPPED_STORAGE
,
222 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
224 .compare_function
= string_compare
,
227 .storage_type
= OSL_MAPPED_STORAGE
,
228 .storage_flags
= OSL_FIXED_SIZE
,
230 .data_size
= AFSI_SIZE
233 .storage_type
= OSL_MAPPED_STORAGE
,
237 .storage_type
= OSL_DISK_STORAGE
,
242 static struct osl_table_description audio_file_table_desc
= {
243 .name
= "audio_files",
244 .num_columns
= NUM_AFT_COLUMNS
,
245 .flags
= OSL_LARGE_TABLE
,
246 .column_descriptions
= aft_cols
249 /* We don't want * dot or dot-dot anywhere. */
250 static int verify_dotfile(const char *rest
)
253 * The first character was '.', but that has already been discarded, we
257 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
259 case '.': /* path start with /foo/.. */
260 if (rest
[1] == '\0' || rest
[1] == '/')
261 return -1; /* /foo/.. or /foo/../bar are not ok */
262 /* /foo/..bar is ok */
268 * We fundamentally don't like some paths: We don't want double slashes or
269 * slashes at the end that can make pathnames ambiguous.
271 static int verify_path(const char *orig_path
, char **resolved_path
)
277 if (*orig_path
!= '/') /* we only accept absolute paths */
279 len
= strlen(orig_path
);
280 *resolved_path
= para_strdup(orig_path
);
281 path
= *resolved_path
;
282 while (len
> 1 && path
[--len
] == '/')
283 path
[len
] = '\0'; /* remove slash at the end */
289 case '/': /* double slash */
292 if (verify_dotfile(path
) < 0)
302 free(*resolved_path
);
306 /** The on-disk layout of a afhi struct. */
308 /** Where the number of seconds is stored. */
309 AFHI_SECONDS_TOTAL_OFFSET
= 0,
310 /** Position of the bitrate. */
311 AFHI_BITRATE_OFFSET
= 4,
312 /** Position of the frequency. */
313 AFHI_FREQUENCY_OFFSET
= 8,
314 /** Number of channels is stored here. */
315 AFHI_CHANNELS_OFFSET
= 12,
316 /** The tag info position. */
317 AFHI_INFO_STRING_OFFSET
= 13,
318 /** Minimal on-disk size of a valid afhi struct. */
322 static unsigned sizeof_afhi_buf(const struct audio_format_info
*afhi
)
326 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
329 static void save_afhi(struct audio_format_info
*afhi
, char *buf
)
333 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
334 afhi
->seconds_total
);
335 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
336 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
337 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
338 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
339 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
342 static void load_afhi(const char *buf
, struct audio_format_info
*afhi
)
344 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
345 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
346 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
347 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
348 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
351 static unsigned sizeof_chunk_info_buf(struct audio_format_info
*afhi
)
355 return 4 * afhi
->chunks_total
+ 20;
359 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
360 enum chunk_info_offsets
{
361 /** The total number of chunks (4 bytes). */
362 CHUNKS_TOTAL_OFFSET
= 0,
363 /** The length of the audio file header (4 bytes). */
364 HEADER_LEN_OFFSET
= 4,
365 /** The start of the audio file header (4 bytes). */
366 HEADER_OFFSET_OFFSET
= 8,
367 /** The seconds part of the chunk time (4 bytes). */
368 CHUNK_TV_TV_SEC_OFFSET
= 12,
369 /** The microseconds part of the chunk time (4 bytes). */
370 CHUNK_TV_TV_USEC
= 16,
371 /** Chunk table entries start here. */
372 CHUNK_TABLE_OFFSET
= 20,
375 /* TODO: audio format handlers could just produce this */
376 static void save_chunk_info(struct audio_format_info
*afhi
, char *buf
)
382 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
383 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
384 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
385 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
386 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
387 for (i
= 0; i
< afhi
->chunks_total
; i
++)
388 write_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
, afhi
->chunk_table
[i
]);
391 static int load_chunk_info(struct osl_object
*obj
, struct audio_format_info
*afhi
)
393 char *buf
= obj
->data
;
396 if (obj
->size
< CHUNK_TABLE_OFFSET
)
397 return -E_BAD_DATA_SIZE
;
399 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
400 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
401 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
402 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
403 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
405 if (afhi
->chunks_total
* 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
406 return -E_BAD_DATA_SIZE
;
407 afhi
->chunk_table
= para_malloc(afhi
->chunks_total
* sizeof(size_t));
408 for (i
= 0; i
< afhi
->chunks_total
; i
++)
409 afhi
->chunk_table
[i
] = read_u32(buf
+ CHUNK_TABLE_OFFSET
+ 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 * \return Positive on success, negative on errors.
515 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
517 struct osl_object path_obj
;
518 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
522 *path
= path_obj
.data
;
527 * Get the object containing the hash value of an audio file, given a row.
529 * \param row Pointer to a row of the audio file table.
530 * \param obj Result pointer.
532 * \return The return value of the underlying call to osl_get_object().
534 * \sa get_hash_of_row().
536 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
538 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
542 * Get the hash value of an audio file, given a row of the audio file table.
544 * \param row Pointer to a row of the audio file table.
545 * \param hash Result pointer.
547 * \a hash points to mapped data and must not be freed by the caller.
549 * \return The return value of the underlying call to
550 * get_hash_object_of_aft_row().
552 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
554 struct osl_object obj
;
555 int ret
= get_hash_object_of_aft_row(row
, &obj
);
564 * Get the audio format handler info, given a row of the audio file table.
566 * \param row Pointer to a row of the audio file table.
567 * \param afhi Result pointer.
569 * \return The return value of the underlying call to osl_get_object().
571 * \sa get_chunk_table_of_row().
573 int get_afhi_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
575 struct osl_object obj
;
576 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
580 load_afhi(obj
.data
, afhi
);
585 * Get the chunk table of an audio file, given a row of the audio file table.
587 * \param row Pointer to a row of the audio file table.
588 * \param afhi Result pointer.
590 * \return The return value of the underlying call to osl_open_disk_object().
592 * \sa get_afhi_of_row().
594 static int get_chunk_table_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
596 struct osl_object obj
;
597 int ret
= osl_open_disk_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
601 ret
= load_chunk_info(&obj
, afhi
);
602 osl_close_disk_object(&obj
);
607 * Mmap the given audio file and update statistics.
609 * \param aft_row Determines the audio file to be opened and updated.
610 * \param afd Result pointer.
612 * On success, the numplayed field of the audio file selector info is increased
613 * and the lastplayed time is set to the current time. Finally, the score of
614 * the audio file is updated.
616 * \return Positive on success, negative on errors.
618 int open_and_update_audio_file(struct osl_row
*aft_row
, struct audio_file_data
*afd
)
620 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
621 struct osl_object afsi_obj
;
622 struct afs_info new_afsi
;
623 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
624 struct afsi_change_event_data aced
;
628 ret
= get_audio_file_path_of_row(aft_row
, &afd
->path
);
631 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
634 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
637 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
640 ret
= get_chunk_table_of_row(aft_row
, &afd
->afhi
);
643 ret
= mmap_full_file(afd
->path
, O_RDONLY
, &afd
->map
.data
,
644 &afd
->map
.size
, NULL
);
647 hash_function(afd
->map
.data
, afd
->map
.size
, file_hash
);
648 ret
= -E_HASH_MISMATCH
;
649 if (hash_compare(file_hash
, aft_hash
))
651 new_afsi
= afd
->afsi
;
652 new_afsi
.num_played
++;
653 new_afsi
.last_played
= time(NULL
);
654 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
656 aced
.aft_row
= aft_row
;
657 aced
.old_afsi
= &afd
->afsi
;
658 afs_event(AFSI_CHANGE
, NULL
, &aced
);
662 free(afd
->afhi
.chunk_table
);
666 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
667 time_t current_time
, enum ls_listing_mode lm
)
671 if (!localtime_r((time_t *)seconds
, &t
))
673 if (lm
== LS_MODE_MBOX
) {
674 if (!strftime(buf
, size
, "%c", &t
))
678 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
679 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
683 if (!strftime(buf
, size
, "%b %e %Y", &t
))
688 /** Compute the number of (decimal) digits of a number. */
689 #define GET_NUM_DIGITS(x, num) { \
690 typeof((x)) _tmp = PARA_ABS(x); \
693 while ((_tmp) > 9) { \
699 static short unsigned get_duration_width(int seconds
)
701 short unsigned width
;
702 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
704 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
705 return 4 + (mins
> 9);
706 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
707 GET_NUM_DIGITS(hours
, &width
);
711 static void get_duration_buf(int seconds
, char *buf
, short unsigned max_width
)
713 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
715 if (!hours
) /* m:ss or mm:ss */
716 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
717 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
718 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
722 static char *make_attribute_line(const char *att_bitmap
, struct afs_info
*afsi
)
724 char *att_text
, *att_line
;
726 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
728 return para_strdup(att_bitmap
);
729 att_line
= make_message("%s (%s)", att_bitmap
, att_text
);
734 static char *make_lyrics_line(struct afs_info
*afsi
)
738 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
740 return make_message("%u", afsi
->lyrics_id
);
741 return make_message("%u (%s)", afsi
->lyrics_id
, lyrics_name
);
744 static char *make_image_line(struct afs_info
*afsi
)
747 img_get_name_by_id(afsi
->image_id
, &image_name
);
749 return make_message("%u", afsi
->image_id
);
750 return make_message("%u (%s)", afsi
->image_id
, image_name
);
753 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
754 struct para_buffer
*b
, time_t current_time
)
758 char last_played_time
[30];
759 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
760 char score_buf
[30] = "";
761 struct afs_info
*afsi
= &d
->afsi
;
762 struct audio_format_info
*afhi
= &d
->afhi
;
763 struct ls_widths
*w
= &opts
->widths
;
764 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
765 char asc_hash
[2 * HASH_SIZE
+ 1];
766 char *att_line
, *lyrics_line
, *image_line
;
768 if (opts
->mode
== LS_MODE_SHORT
) {
769 para_printf(b
, "%s\n", d
->path
);
772 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
773 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
774 sizeof(last_played_time
), current_time
, opts
->mode
);
777 get_duration_buf(afhi
->seconds_total
, duration_buf
, w
->duration_width
);
779 if (opts
->mode
== LS_MODE_LONG
)
780 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
782 sprintf(score_buf
, "%li ", d
->score
);
785 if (opts
->mode
== LS_MODE_LONG
) {
788 "%s " /* attributes */
789 "%*d " /* image_id */
790 "%*d " /* lyrics_id */
792 "%s " /* audio format */
793 "%*d " /* frequency */
796 "%*d " /* num_played */
797 "%s " /* last_played */
801 w
->image_id_width
, afsi
->image_id
,
802 w
->lyrics_id_width
, afsi
->lyrics_id
,
803 w
->bitrate_width
, afhi
->bitrate
,
804 audio_format_name(afsi
->audio_format_id
),
805 w
->frequency_width
, afhi
->frequency
,
808 w
->num_played_width
, afsi
->num_played
,
814 hash_to_asc(d
->hash
, asc_hash
);
815 att_line
= make_attribute_line(att_buf
, afsi
);
816 lyrics_line
= make_lyrics_line(afsi
);
817 image_line
= make_image_line(afsi
);
818 if (opts
->mode
== LS_MODE_VERBOSE
) {
821 "%s: %s\n" /* path */
827 "bitrate: %dkbit/s\n"
835 (opts
->flags
& LS_FLAG_FULL_PATH
)?
836 "path" : "file", d
->path
,
837 have_score
? "score: " : "", score_buf
,
838 have_score
? "\n" : "",
844 audio_format_name(afsi
->audio_format_id
),
852 } else { /* mbox mode */
853 struct osl_object lyrics_def
;
854 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
856 "From foo@localhost %s\n"
857 "Received: from\nTo: bar\nFrom: a\n"
858 "Subject: %s\n\n" /* path */
864 "bitrate: %dkbit/s\n"
874 have_score
? "score: " : "", score_buf
,
875 have_score
? "\n" : "",
881 audio_format_name(afsi
->audio_format_id
),
887 lyrics_def
.data
? "Lyrics:\n~~~~~~~\n" : "",
888 lyrics_def
.data
? (char *)lyrics_def
.data
: ""
891 osl_close_disk_object(lyrics_def
.data
);
899 static int ls_audio_format_compare(const void *a
, const void *b
)
901 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
902 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
905 static int ls_duration_compare(const void *a
, const void *b
)
907 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
908 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
911 static int ls_bitrate_compare(const void *a
, const void *b
)
913 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
914 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
917 static int ls_lyrics_id_compare(const void *a
, const void *b
)
919 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
920 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
923 static int ls_image_id_compare(const void *a
, const void *b
)
925 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
926 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
929 static int ls_channels_compare(const void *a
, const void *b
)
931 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
932 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
935 static int ls_frequency_compare(const void *a
, const void *b
)
937 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
938 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
941 static int ls_num_played_compare(const void *a
, const void *b
)
943 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
944 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
947 static int ls_last_played_compare(const void *a
, const void *b
)
949 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
950 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
953 static int ls_score_compare(const void *a
, const void *b
)
955 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
956 return NUM_COMPARE(d1
->score
, d2
->score
);
959 static int ls_path_compare(const void *a
, const void *b
)
961 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
962 return strcmp(d1
->path
, d2
->path
);
965 static int sort_matching_paths(struct ls_options
*options
)
967 size_t nmemb
= options
->num_matching_paths
;
968 size_t size
= sizeof(*options
->data_ptr
);
969 int (*compar
)(const void *, const void *);
972 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
973 for (i
= 0; i
< nmemb
; i
++)
974 options
->data_ptr
[i
] = options
->data
+ i
;
976 /* In these cases the array is already sorted */
977 if (options
->sorting
== LS_SORT_BY_PATH
978 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
979 && (options
->flags
& LS_FLAG_FULL_PATH
))
981 if (options
->sorting
== LS_SORT_BY_SCORE
&&
982 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
985 switch (options
->sorting
) {
986 case LS_SORT_BY_PATH
:
987 compar
= ls_path_compare
; break;
988 case LS_SORT_BY_SCORE
:
989 compar
= ls_score_compare
; break;
990 case LS_SORT_BY_LAST_PLAYED
:
991 compar
= ls_last_played_compare
; break;
992 case LS_SORT_BY_NUM_PLAYED
:
993 compar
= ls_num_played_compare
; break;
994 case LS_SORT_BY_FREQUENCY
:
995 compar
= ls_frequency_compare
; break;
996 case LS_SORT_BY_CHANNELS
:
997 compar
= ls_channels_compare
; break;
998 case LS_SORT_BY_IMAGE_ID
:
999 compar
= ls_image_id_compare
; break;
1000 case LS_SORT_BY_LYRICS_ID
:
1001 compar
= ls_lyrics_id_compare
; break;
1002 case LS_SORT_BY_BITRATE
:
1003 compar
= ls_bitrate_compare
; break;
1004 case LS_SORT_BY_DURATION
:
1005 compar
= ls_duration_compare
; break;
1006 case LS_SORT_BY_AUDIO_FORMAT
:
1007 compar
= ls_audio_format_compare
; break;
1011 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1015 /* row is either an aft_row or a row of the score table */
1016 /* TODO: Only compute widths if we need them */
1017 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1020 struct ls_options
*options
= ls_opts
;
1022 struct ls_widths
*w
;
1023 unsigned short num_digits
;
1025 struct osl_row
*aft_row
;
1029 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1030 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1035 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1038 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1039 char *p
= strrchr(path
, '/');
1043 if (options
->num_patterns
) {
1044 for (i
= 0; i
< options
->num_patterns
; i
++) {
1045 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1048 if (ret
== FNM_NOMATCH
)
1052 if (i
>= options
->num_patterns
) /* no match */
1055 tmp
= options
->num_matching_paths
++;
1056 if (options
->num_matching_paths
> options
->array_size
) {
1057 options
->array_size
++;
1058 options
->array_size
*= 2;
1059 options
->data
= para_realloc(options
->data
, options
->array_size
1060 * sizeof(*options
->data
));
1062 d
= options
->data
+ tmp
;
1063 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1066 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1070 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1073 w
= &options
->widths
;
1074 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1075 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1076 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1077 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1078 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1079 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1080 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1081 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1082 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1083 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1084 /* get the number of chars to print this amount of time */
1085 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1086 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1087 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1088 GET_NUM_DIGITS(score
, &num_digits
);
1089 num_digits
++; /* add one for the sign (space or "-") */
1090 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1096 static int com_ls_callback(const struct osl_object
*query
,
1097 struct osl_object
*ls_output
)
1099 struct ls_options
*opts
= query
->data
;
1100 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1101 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1103 time_t current_time
;
1106 if (opts
->num_patterns
) {
1107 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1108 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1109 opts
->patterns
[i
] = p
;
1113 opts
->patterns
= NULL
;
1114 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1115 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1117 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1121 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1122 if (!opts
->num_matching_paths
)
1124 ret
= sort_matching_paths(opts
);
1127 time(¤t_time
);
1128 if (opts
->flags
& LS_FLAG_REVERSE
)
1129 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1130 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1135 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1136 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1142 ls_output
->data
= b
.buf
;
1143 ls_output
->size
= b
.size
;
1145 free(opts
->data_ptr
);
1146 free(opts
->patterns
);
1151 * TODO: flags -h (sort by hash) -lm (list in mbox format)
1153 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1154 * full list: list everything, including afsi, afhi, atts as clear text
1157 int com_afs_ls(int fd
, int argc
, char * const * const argv
)
1161 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1162 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1163 struct ls_options opts
= {.patterns
= NULL
};
1164 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1167 for (i
= 1; i
< argc
; i
++) {
1168 const char *arg
= argv
[i
];
1171 if (!strcmp(arg
, "--")) {
1175 if (!strncmp(arg
, "-l", 2)) {
1177 mode
= LS_MODE_LONG
;
1181 return -E_AFT_SYNTAX
;
1182 switch(*(arg
+ 2)) {
1184 mode
= LS_MODE_SHORT
;
1187 mode
= LS_MODE_LONG
;
1190 mode
= LS_MODE_VERBOSE
;
1193 mode
= LS_MODE_MBOX
;
1196 return -E_AFT_SYNTAX
;
1199 if (!strcmp(arg
, "-p")) {
1200 flags
|= LS_FLAG_FULL_PATH
;
1203 if (!strcmp(arg
, "-a")) {
1204 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1207 if (!strcmp(arg
, "-r")) {
1208 flags
|= LS_FLAG_REVERSE
;
1211 if (!strncmp(arg
, "-s", 2)) {
1212 if (!*(arg
+ 2) || *(arg
+ 3))
1213 return -E_AFT_SYNTAX
;
1214 switch(*(arg
+ 2)) {
1216 sort
= LS_SORT_BY_PATH
;
1218 case 's': /* -ss implies -a */
1219 sort
= LS_SORT_BY_SCORE
;
1220 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1223 sort
= LS_SORT_BY_LAST_PLAYED
;
1226 sort
= LS_SORT_BY_NUM_PLAYED
;
1229 sort
= LS_SORT_BY_FREQUENCY
;
1232 sort
= LS_SORT_BY_CHANNELS
;
1235 sort
= LS_SORT_BY_IMAGE_ID
;
1238 sort
= LS_SORT_BY_LYRICS_ID
;
1241 sort
= LS_SORT_BY_BITRATE
;
1244 sort
= LS_SORT_BY_DURATION
;
1247 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1250 return -E_AFT_SYNTAX
;
1253 return -E_AFT_SYNTAX
;
1256 opts
.sorting
= sort
;
1258 opts
.num_patterns
= argc
- i
;
1259 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1260 argv
+ i
, com_ls_callback
, &ls_output
);
1262 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1263 free(ls_output
.data
);
1269 * Call the given function for each file in the audio file table.
1271 * \param private_data An arbitrary data pointer, passed to \a func.
1272 * \param func The custom function to be called.
1274 * \return The return value of the underlying call to osl_rbtree_loop().
1276 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1278 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1282 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1284 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1285 struct osl_row
*row
;
1287 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1291 enum aft_row_offsets
{
1292 AFTROW_AFHI_OFFSET_POS
= 0,
1293 AFTROW_CHUNKS_OFFSET_POS
= 2,
1294 AFTROW_AUDIO_FORMAT_OFFSET
= 4,
1295 AFTROW_FLAGS_OFFSET
= 5,
1296 AFTROW_HASH_OFFSET
= 9,
1297 AFTROW_PATH_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1300 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1301 * In this case, afhi won't be stored in the buffer */
1302 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1303 struct audio_format_info
*afhi
, uint32_t flags
,
1304 uint8_t audio_format_num
, struct osl_object
*obj
)
1306 size_t path_len
= strlen(path
) + 1;
1307 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1308 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1309 + sizeof_chunk_info_buf(afhi
);
1310 char *buf
= para_malloc(size
);
1313 write_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1314 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1316 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1317 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1319 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1320 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1321 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1322 pos
+ afhi_size
- 1);
1323 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1324 save_afhi(afhi
, buf
+ pos
);
1327 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1328 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1329 save_chunk_info(afhi
, buf
+ pos
);
1330 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1344 AFHI: whether afhi and chunk table are computed and sent
1345 ACTION: table modifications to be performed
1347 +---+----+-----+------+---------------------------------------------------+
1348 | HS | PB | F | AFHI | ACTION
1349 +---+----+-----+------+---------------------------------------------------+
1350 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1351 | | update path, keep afsi
1352 +---+----+-----+------+---------------------------------------------------+
1353 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1354 | | otherwise: remove PB, HS: update path, keep afhi,
1356 +---+----+-----+------+---------------------------------------------------+
1357 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1359 +---+----+-----+------+---------------------------------------------------+
1360 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1361 +---+----+-----+------+---------------------------------------------------+
1362 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1363 | | (force has no effect)
1364 +---+----+-----+------+---------------------------------------------------+
1365 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1366 +---+----+-----+------+---------------------------------------------------+
1367 | N | N | Y | Y | (new file) create new entry (force has no effect)
1368 +---+----+-----+------+---------------------------------------------------+
1369 | N | N | N | Y | (new file) create new entry
1370 +---+----+-----+------+---------------------------------------------------+
1372 afhi <=> force or no HS
1376 /** Flags passed to the add command. */
1377 enum com_add_flags
{
1378 /** Skip paths that exist already. */
1380 /** Force adding. */
1382 /** Print what is being done. */
1383 ADD_FLAG_VERBOSE
= 4,
1384 /** Try to add files with unknown suffixes. */
1388 static int com_add_callback(const struct osl_object
*query
,
1389 struct osl_object
*result
)
1391 char *buf
= query
->data
, *path
;
1392 struct osl_row
*pb
, *aft_row
;
1394 struct osl_object objs
[NUM_AFT_COLUMNS
];
1396 char asc
[2 * HASH_SIZE
+ 1];
1398 char afsi_buf
[AFSI_SIZE
];
1399 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1400 struct afs_info default_afsi
= {.last_played
= 0};
1401 struct para_buffer msg
= {.buf
= NULL
};
1403 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1404 hash_to_asc(hash
, asc
);;
1405 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1406 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1408 path
= buf
+ AFTROW_PATH_OFFSET
;
1409 objs
[AFTCOL_PATH
].data
= path
;
1410 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1412 PARA_INFO_LOG("request to add %s\n", path
);
1413 hs
= find_hash_sister(hash
);
1414 ret
= aft_get_row_of_path(path
, &pb
);
1415 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1417 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1418 if (flags
& ADD_FLAG_VERBOSE
)
1419 para_printf(&msg
, "ignoring duplicate\n");
1423 if (hs
&& hs
!= pb
) {
1424 struct osl_object obj
;
1425 if (pb
) { /* hs trumps pb, remove pb */
1426 if (flags
& ADD_FLAG_VERBOSE
)
1427 para_printf(&msg
, "removing path brother\n");
1428 ret
= osl_del_row(audio_file_table
, pb
);
1433 /* file rename, update hs' path */
1434 if (flags
& ADD_FLAG_VERBOSE
) {
1435 ret
= osl_get_object(audio_file_table
, hs
,
1439 para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1441 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1442 &objs
[AFTCOL_PATH
]);
1445 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1446 if (!(flags
& ADD_FLAG_FORCE
))
1449 /* no hs or force mode, child must have sent afhi */
1450 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1451 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1453 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1454 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1456 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1458 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1459 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1460 if (pb
&& !hs
) { /* update pb's hash */
1461 char old_asc
[2 * HASH_SIZE
+ 1];
1462 HASH_TYPE
*old_hash
;
1463 ret
= get_hash_of_row(pb
, &old_hash
);
1466 hash_to_asc(old_hash
, old_asc
);
1467 if (flags
& ADD_FLAG_VERBOSE
)
1468 para_printf(&msg
, "file change: %s -> %s\n",
1470 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1471 &objs
[AFTCOL_HASH
]);
1475 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1476 struct osl_row
*row
= pb
? pb
: hs
;
1477 /* update afhi and chunk_table */
1478 if (flags
& ADD_FLAG_VERBOSE
)
1479 para_printf(&msg
, "updating afhi and chunk table\n");
1480 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1481 &objs
[AFTCOL_AFHI
]);
1484 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1485 &objs
[AFTCOL_CHUNKS
]);
1488 afs_event(AFHI_CHANGE
, &msg
, row
);
1491 /* new entry, use default afsi */
1492 if (flags
& ADD_FLAG_VERBOSE
)
1493 para_printf(&msg
, "new file\n");
1494 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1495 default_afsi
.audio_format_id
= read_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
);
1497 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1498 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1499 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1500 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1503 para_printf(&msg
, "%s\n", PARA_STRERROR(-ret
));
1506 result
->data
= msg
.buf
;
1507 result
->size
= msg
.size
;
1508 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1512 struct private_add_data
{
1517 static int path_brother_callback(const struct osl_object
*query
,
1518 struct osl_object
*result
)
1520 char *path
= query
->data
;
1521 struct osl_row
*path_brother
;
1522 int ret
= aft_get_row_of_path(path
, &path_brother
);
1525 result
->data
= para_malloc(sizeof(path_brother
));
1526 result
->size
= sizeof(path_brother
);
1527 *(struct osl_row
**)(result
->data
) = path_brother
;
1531 static int hash_sister_callback(const struct osl_object
*query
,
1532 struct osl_object
*result
)
1534 HASH_TYPE
*hash
= query
->data
;
1535 struct osl_row
*hash_sister
;
1537 hash_sister
= find_hash_sister(hash
);
1539 return -E_RB_KEY_NOT_FOUND
;
1540 result
->data
= para_malloc(sizeof(hash_sister
));
1541 result
->size
= sizeof(hash_sister
);
1542 *(struct osl_row
**)(result
->data
) = hash_sister
;
1546 static int add_one_audio_file(const char *path
, const void *private_data
)
1549 uint8_t format_num
= -1;
1550 const struct private_add_data
*pad
= private_data
;
1551 struct audio_format_info afhi
, *afhi_ptr
= NULL
;
1552 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1553 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
;
1554 HASH_TYPE hash
[HASH_SIZE
];
1556 afhi
.header_offset
= 0;
1557 afhi
.header_len
= 0;
1558 ret
= guess_audio_format(path
);
1559 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1561 query
.data
= (char *)path
;
1562 query
.size
= strlen(path
) + 1;
1563 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1564 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1567 pb
= *(struct osl_row
**)result
.data
;
1571 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1572 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1573 ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1576 /* We still want to add this file. Compute its hash. */
1577 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, NULL
);
1580 hash_function(map
.data
, map
.size
, hash
);
1582 /* Check whether database contains file with the same hash. */
1584 query
.size
= HASH_SIZE
;
1585 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1586 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1589 hs
= *(struct osl_row
**)result
.data
;
1592 /* Return success if we already know this file. */
1594 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1595 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1596 ret
= send_va_buffer(pad
->fd
,
1597 "%s exists, not forcing update\n", path
);
1601 * we won't recalculate the audio format info and the chunk table if
1602 * there is a hash sister unless in FORCE mode.
1604 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1605 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1611 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1612 ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1616 munmap(map
.data
, map
.size
);
1617 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1618 /* Ask afs to consider this entry for adding. */
1619 ret
= send_callback_request(com_add_callback
, &obj
, &result
);
1620 if (result
.data
&& result
.size
) {
1621 ret2
= send_va_buffer(pad
->fd
, "%s", (char *)result
.data
);
1623 if (ret
>= 0 && ret2
< 0)
1629 munmap(map
.data
, map
.size
);
1631 if (ret
< 0 && ret
!= -E_SEND
)
1632 send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1633 PARA_STRERROR(-ret
));
1636 free(afhi_ptr
->chunk_table
);
1637 /* it's not an error if not all files could be added */
1638 return ret
== -E_SEND
? ret
: 1;
1641 int com_add(int fd
, int argc
, char * const * const argv
)
1644 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1645 struct stat statbuf
;
1647 for (i
= 1; i
< argc
; i
++) {
1648 const char *arg
= argv
[i
];
1651 if (!strcmp(arg
, "--")) {
1655 if (!strcmp(arg
, "-a")) {
1656 pad
.flags
|= ADD_FLAG_ALL
;
1659 if (!strcmp(arg
, "-l")) {
1660 pad
.flags
|= ADD_FLAG_LAZY
;
1663 if (!strcmp(arg
, "-f")) {
1664 pad
.flags
|= ADD_FLAG_FORCE
;
1667 if (!strcmp(arg
, "-v")) {
1668 pad
.flags
|= ADD_FLAG_VERBOSE
;
1673 return -E_AFT_SYNTAX
;
1674 for (; i
< argc
; i
++) {
1676 ret
= verify_path(argv
[i
], &path
);
1678 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
], PARA_STRERROR(-ret
));
1683 ret
= stat(path
, &statbuf
);
1685 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1692 if (S_ISDIR(statbuf
.st_mode
))
1693 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1696 ret
= add_one_audio_file(path
, &pad
);
1698 send_va_buffer(fd
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
1709 * Flags used by the touch command.
1714 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1715 TOUCH_FLAG_FNM_PATHNAME
= 1,
1716 /** Activates verbose mode. */
1717 TOUCH_FLAG_VERBOSE
= 2
1720 struct com_touch_options
{
1722 int64_t last_played
;
1728 struct touch_action_data
{
1729 struct com_touch_options
*cto
;
1730 struct para_buffer pb
;
1733 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1734 struct osl_row
*row
, const char *name
, void *data
)
1736 struct touch_action_data
*tad
= data
;
1737 struct osl_object obj
;
1738 struct afs_info old_afsi
, new_afsi
;
1739 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1740 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0;
1741 struct afsi_change_event_data aced
;
1743 ret
= get_afsi_object_of_row(row
, &obj
);
1745 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1748 ret
= load_afsi(&old_afsi
, &obj
);
1750 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1753 new_afsi
= old_afsi
;
1755 new_afsi
.num_played
++;
1756 new_afsi
.last_played
= time(NULL
);
1757 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1758 para_printf(&tad
->pb
, "%s: num_played = %u, "
1759 "last_played = now()\n", name
,
1760 new_afsi
.num_played
);
1762 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1763 para_printf(&tad
->pb
, "touching %s\n", name
);
1764 if (tad
->cto
->lyrics_id
>= 0)
1765 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1766 if (tad
->cto
->image_id
>= 0)
1767 new_afsi
.image_id
= tad
->cto
->image_id
;
1768 if (tad
->cto
->num_played
>= 0)
1769 new_afsi
.num_played
= tad
->cto
->num_played
;
1770 if (tad
->cto
->last_played
>= 0)
1771 new_afsi
.last_played
= tad
->cto
->last_played
;
1773 save_afsi(&new_afsi
, &obj
); /* in-place update */
1775 aced
.old_afsi
= &old_afsi
;
1776 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
1780 static int com_touch_callback(const struct osl_object
*query
,
1781 struct osl_object
*result
)
1783 struct touch_action_data tad
= {.cto
= query
->data
};
1785 struct pattern_match_data pmd
= {
1786 .table
= audio_file_table
,
1787 .loop_col_num
= AFTCOL_HASH
,
1788 .match_col_num
= AFTCOL_PATH
,
1789 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
1790 .size
= query
->size
- sizeof(*tad
.cto
)},
1792 .action
= touch_audio_file
1794 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
1795 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1796 ret
= for_each_matching_row(&pmd
);
1798 para_printf(&tad
.pb
, "%s\n", PARA_STRERROR(-ret
));
1800 result
->data
= tad
.pb
.buf
;
1801 result
->size
= tad
.pb
.size
;
1804 return ret
< 0? ret
: 0;
1807 int com_touch(int fd
, int argc
, char * const * const argv
)
1809 struct com_touch_options cto
= {
1815 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)},
1820 for (i
= 1; i
< argc
; i
++) {
1821 const char *arg
= argv
[i
];
1824 if (!strcmp(arg
, "--")) {
1828 if (!strncmp(arg
, "-n", 2)) {
1829 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
1834 if (!strncmp(arg
, "-l", 2)) {
1835 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
1840 if (!strncmp(arg
, "-y", 2)) {
1841 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
1846 if (!strncmp(arg
, "-i", 2)) {
1847 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
1852 if (!strcmp(arg
, "-p")) {
1853 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
1856 if (!strcmp(arg
, "-v")) {
1857 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
1860 break; /* non-option starting with dash */
1863 return -E_AFT_SYNTAX
;
1864 ret
= send_option_arg_callback_request(&query
, argc
- i
,
1865 argv
+ i
, com_touch_callback
, &result
);
1867 send_buffer(fd
, (char *)result
.data
);
1870 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
1875 RM_FLAG_VERBOSE
= 1,
1877 RM_FLAG_FNM_PATHNAME
= 4
1880 struct com_rm_data
{
1882 struct para_buffer pb
;
1883 unsigned num_removed
;
1886 static int remove_audio_file(__a_unused
struct osl_table
*table
,
1887 struct osl_row
*row
, const char *name
, void *data
)
1889 struct com_rm_data
*crd
= data
;
1892 if (crd
->flags
& RM_FLAG_VERBOSE
)
1893 para_printf(&crd
->pb
, "removing %s\n", name
);
1894 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
1895 ret
= osl_del_row(audio_file_table
, row
);
1897 para_printf(&crd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1903 static int com_rm_callback(const struct osl_object
*query
,
1904 __a_unused
struct osl_object
*result
)
1906 struct com_rm_data crd
= {.flags
= *(uint32_t *)query
->data
};
1908 struct pattern_match_data pmd
= {
1909 .table
= audio_file_table
,
1910 .loop_col_num
= AFTCOL_HASH
,
1911 .match_col_num
= AFTCOL_PATH
,
1912 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
1913 .size
= query
->size
- sizeof(uint32_t)},
1915 .action
= remove_audio_file
1917 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
1918 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1919 ret
= for_each_matching_row(&pmd
);
1921 para_printf(&crd
.pb
, "%s\n", PARA_STRERROR(-ret
));
1922 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
1923 para_printf(&crd
.pb
, "no matches -- nothing removed\n");
1925 if (crd
.flags
& RM_FLAG_VERBOSE
)
1926 para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
1929 result
->data
= crd
.pb
.buf
;
1930 result
->size
= crd
.pb
.size
;
1933 return ret
< 0? ret
: 0;
1936 /* TODO options: -r (recursive) */
1937 int com_afs_rm(int fd
, int argc
, char * const * const argv
)
1940 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)},
1944 for (i
= 1; i
< argc
; i
++) {
1945 const char *arg
= argv
[i
];
1948 if (!strcmp(arg
, "--")) {
1952 if (!strcmp(arg
, "-f")) {
1953 flags
|= RM_FLAG_FORCE
;
1956 if (!strcmp(arg
, "-p")) {
1957 flags
|= RM_FLAG_FNM_PATHNAME
;
1960 if (!strcmp(arg
, "-v")) {
1961 flags
|= RM_FLAG_VERBOSE
;
1967 return -E_AFT_SYNTAX
;
1968 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
1969 com_rm_callback
, &result
);
1971 send_buffer(fd
, (char *)result
.data
);
1974 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
1979 * Flags used by the cpsi command.
1984 /** Whether the lyrics id should be copied. */
1985 CPSI_FLAG_COPY_LYRICS_ID
= 1,
1986 /** Whether the image id should be copied. */
1987 CPSI_FLAG_COPY_IMAGE_ID
= 2,
1988 /** Whether the lastplayed time should be copied. */
1989 CPSI_FLAG_COPY_LASTPLAYED
= 4,
1990 /** Whether the numplayed count should be copied. */
1991 CPSI_FLAG_COPY_NUMPLAYED
= 8,
1992 /** Whether the attributes should be copied. */
1993 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
1994 /** Activates verbose mode. */
1995 CPSI_FLAG_VERBOSE
= 32,
1998 struct cpsi_action_data
{
2000 unsigned num_copied
;
2001 struct para_buffer pb
;
2002 struct afs_info source_afsi
;
2005 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2006 struct osl_row
*row
, const char *name
, void *data
)
2008 struct cpsi_action_data
*cad
= data
;
2009 struct osl_object target_afsi_obj
;
2011 struct afs_info old_afsi
, target_afsi
;
2012 struct afsi_change_event_data aced
;
2014 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2017 load_afsi(&target_afsi
, &target_afsi_obj
);
2018 old_afsi
= target_afsi
;
2019 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2020 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2021 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2022 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2023 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2024 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2025 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2026 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2027 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2028 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2029 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2031 if (cad
->flags
& CPSI_FLAG_VERBOSE
)
2032 para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2034 aced
.old_afsi
= &old_afsi
;
2035 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2039 static int com_cpsi_callback(const struct osl_object
*query
,
2040 struct osl_object
*result
)
2042 struct cpsi_action_data cad
= {.flags
= *(unsigned *)query
->data
};
2044 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2046 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2049 struct pattern_match_data pmd
= {
2050 .table
= audio_file_table
,
2051 .loop_col_num
= AFTCOL_HASH
,
2052 .match_col_num
= AFTCOL_PATH
,
2053 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2054 .size
= query
->size
- sizeof(cad
.flags
)
2055 - strlen(source_path
) - 1},
2057 .action
= copy_selector_info
2059 ret
= for_each_matching_row(&pmd
);
2062 para_printf(&cad
.pb
, "%s\n", PARA_STRERROR(-ret
));
2063 if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2065 para_printf(&cad
.pb
, "copied requested afsi from %s "
2067 source_path
, cad
.num_copied
);
2069 para_printf(&cad
.pb
, "nothing copied\n");
2072 result
->data
= cad
.pb
.buf
;
2073 result
->size
= cad
.pb
.size
;
2076 return ret
< 0? ret
: 0;
2079 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2083 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
2086 for (i
= 1; i
< argc
; i
++) {
2087 const char *arg
= argv
[i
];
2090 if (!strcmp(arg
, "--")) {
2094 if (!strcmp(arg
, "-y")) {
2095 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2098 if (!strcmp(arg
, "-i")) {
2099 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2102 if (!strcmp(arg
, "-l")) {
2103 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2106 if (!strcmp(arg
, "-n")) {
2107 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2110 if (!strcmp(arg
, "-a")) {
2111 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2114 if (!strcmp(arg
, "-v")) {
2115 flags
|= CPSI_FLAG_VERBOSE
;
2120 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2121 return -E_AFT_SYNTAX
;
2122 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2123 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2124 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2125 com_cpsi_callback
, &result
);
2127 send_buffer(fd
, (char *)result
.data
);
2130 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
2134 /* TODO: optionally fix problems by removing offending rows */
2135 static int check_audio_file(struct osl_row
*row
, void *data
)
2138 struct para_buffer
*pb
= data
;
2139 struct stat statbuf
;
2140 int ret
= get_audio_file_path_of_row(row
, &path
);
2141 struct afs_info afsi
;
2145 para_printf(pb
, "%s\n", PARA_STRERROR(-ret
));
2148 if (stat(path
, &statbuf
) < 0)
2149 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2151 if (!S_ISREG(statbuf
.st_mode
))
2152 para_printf(pb
, "%s: not a regular file\n", path
);
2154 ret
= get_afsi_of_row(row
, &afsi
);
2156 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
2159 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2161 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2162 PARA_STRERROR(-ret
));
2163 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2165 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2166 PARA_STRERROR(-ret
));
2171 * Check the audio file table for inconsistencies.
2173 * \param query Unused.
2174 * \param result Contains message string upon return.
2176 * This function always succeeds.
2180 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
2182 struct para_buffer pb
= {.buf
= NULL
};
2184 para_printf(&pb
, "checking audio file table...\n");
2185 audio_file_loop(&pb
, check_audio_file
);
2186 result
->data
= pb
.buf
;
2187 result
->size
= pb
.size
;
2193 * Close the audio file table.
2195 * \param flags Ususal flags that are passed to osl_close_table().
2197 * \sa osl_close_table().
2199 static void aft_close(void)
2201 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2202 audio_file_table
= NULL
;
2206 * Open the audio file table.
2208 * \param dir The database directory.
2212 * \sa osl_open_table().
2214 static int aft_open(const char *dir
)
2218 audio_file_table_desc
.dir
= dir
;
2219 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2222 osl_get_num_rows(audio_file_table
, &num
);
2223 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2226 PARA_INFO_LOG("failed to open audio file table\n");
2227 audio_file_table
= NULL
;
2228 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2233 static int aft_create(const char *dir
)
2235 audio_file_table_desc
.dir
= dir
;
2236 return osl_create_table(&audio_file_table_desc
);
2239 static int clear_attribute(struct osl_row
*row
, void *data
)
2241 struct rmatt_event_data
*red
= data
;
2242 struct afs_info afsi
;
2243 struct osl_object obj
;
2244 int ret
= get_afsi_object_of_row(row
, &obj
);
2245 uint64_t mask
= ~(1ULL << red
->bitnum
);
2249 ret
= load_afsi(&afsi
, &obj
);
2252 afsi
.attributes
&= mask
;
2253 save_afsi(&afsi
, &obj
);
2257 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2261 case ATTRIBUTE_REMOVE
: {
2262 const struct rmatt_event_data
*red
= data
;
2263 para_printf(pb
, "clearing attribute %s (bit %u) from all "
2264 "entries in the audio file table\n", red
->name
,
2266 return audio_file_loop(data
, clear_attribute
);
2273 void aft_init(struct afs_table
*t
)
2275 t
->name
= audio_file_table_desc
.name
;
2277 t
->close
= aft_close
;
2278 t
->create
= aft_create
;
2279 t
->event_handler
= aft_event_handler
;