2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
9 #include <dirent.h> /* readdir() */
23 #include "portable_io.h"
25 static struct osl_table
*audio_file_table
;
27 /** The different sorting methods of the ls command. */
28 enum ls_sorting_method
{
34 LS_SORT_BY_LAST_PLAYED
,
36 LS_SORT_BY_NUM_PLAYED
,
50 LS_SORT_BY_AUDIO_FORMAT
,
55 /** The different listing modes of the ls command. */
56 enum ls_listing_mode
{
57 /** Default listing mode. */
69 /** The flags accepted by the ls command. */
72 LS_FLAG_FULL_PATH
= 1,
74 LS_FLAG_ADMISSIBLE_ONLY
= 2,
80 * The size of the individual output fields of the ls command.
82 * These depend on the actual content being listed. If, for instance only files
83 * with duration less than an hour are being listed, then the duration with is
84 * made smaller because then the duration is listed as mm:ss rather than
88 /** size of the score field. */
89 unsigned short score_width
;
90 /** size of the image id field. */
91 unsigned short image_id_width
;
92 /** size of the lyrics id field. */
93 unsigned short lyrics_id_width
;
94 /** size of the bitrate field. */
95 unsigned short bitrate_width
;
96 /** size of the frequency field. */
97 unsigned short frequency_width
;
98 /** size of the duration field. */
99 unsigned short duration_width
;
100 /** size of the num played field. */
101 unsigned short num_played_width
;
102 /** size of the amp field. */
103 unsigned short amp_width
;
106 /** Data passed from the ls command handler to its callback function. */
108 /** The given command line flags. */
110 /** The sorting method given at the command line. */
111 enum ls_sorting_method sorting
;
112 /** The given listing mode (short, long, verbose, mbox). */
113 enum ls_listing_mode mode
;
114 /** The arguments passed to the ls command. */
116 /** Number of non-option arguments. */
118 /** Used for long listing mode to align the output fields. */
119 struct ls_widths widths
;
120 /** Size of the \a data array. */
122 /** Number of used entries in the data array. */
123 uint32_t num_matching_paths
;
124 /** Array of matching entries. */
125 struct ls_data
*data
;
126 /** Used to sort the array. */
127 struct ls_data
**data_ptr
;
131 * Describes the layout of the mmapped-afs info struct.
133 * \sa struct afs_info.
136 /** Where .last_played is stored. */
137 AFSI_LAST_PLAYED_OFFSET
= 0,
138 /** Storage position of the attributes bitmap. */
139 AFSI_ATTRIBUTES_OFFSET
= 8,
140 /** Storage position of the .num_played field. */
141 AFSI_NUM_PLAYED_OFFSET
= 16,
142 /** Storage position of the .image_id field. */
143 AFSI_IMAGE_ID_OFFSET
= 20,
144 /** Storage position of the .lyrics_id field. */
145 AFSI_LYRICS_ID_OFFSET
= 24,
146 /** Storage position of the .audio_format_id field. */
147 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
148 /** Storage position of the amplification field. */
149 AFSI_AMP_OFFSET
= 29,
150 /** 2 bytes reserved space for future usage. */
151 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 30,
152 /** On-disk storage space needed. */
157 * Convert a struct afs_info to an osl object.
159 * \param afsi Pointer to the audio file info to be converted.
160 * \param obj Result pointer.
164 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
166 char *buf
= obj
->data
;
168 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
169 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
170 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
171 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
172 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
173 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
174 afsi
->audio_format_id
);
175 write_u8(buf
+ AFSI_AMP_OFFSET
, afsi
->amp
);
176 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 2);
180 * Get the audio file selector info struct stored in an osl object.
182 * \param afsi Points to the audio_file info structure to be filled in.
183 * \param obj The osl object holding the data.
185 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
189 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
191 char *buf
= obj
->data
;
192 if (obj
->size
< AFSI_SIZE
)
194 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
195 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
196 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
197 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
198 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
199 afsi
->audio_format_id
= read_u8(buf
+
200 AFSI_AUDIO_FORMAT_ID_OFFSET
);
201 afsi
->amp
= read_u8(buf
+ AFSI_AMP_OFFSET
);
205 /** The columns of the audio file table. */
206 enum audio_file_table_columns
{
207 /** The hash on the content of the audio file. */
209 /** The full path in the filesystem. */
211 /** The audio file selector info. */
213 /** The audio format handler info. */
215 /** The chunk table info and the chunk table of the audio file. */
217 /** The number of columns of this table. */
222 * Compare two osl objects pointing to hash values.
224 * \param obj1 Pointer to the first hash object.
225 * \param obj2 Pointer to the second hash object.
227 * \return The values required for an osl compare function.
229 * \sa osl_compare_func, uint32_compare().
231 int aft_hash_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
233 return hash_compare((HASH_TYPE
*)obj1
->data
, (HASH_TYPE
*)obj2
->data
);
236 static struct osl_column_description aft_cols
[] = {
238 .storage_type
= OSL_MAPPED_STORAGE
,
239 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
241 .compare_function
= aft_hash_compare
,
242 .data_size
= HASH_SIZE
245 .storage_type
= OSL_MAPPED_STORAGE
,
246 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
248 .compare_function
= string_compare
,
251 .storage_type
= OSL_MAPPED_STORAGE
,
252 .storage_flags
= OSL_FIXED_SIZE
,
254 .data_size
= AFSI_SIZE
257 .storage_type
= OSL_MAPPED_STORAGE
,
261 .storage_type
= OSL_DISK_STORAGE
,
266 static struct osl_table_description audio_file_table_desc
= {
267 .name
= "audio_files",
268 .num_columns
= NUM_AFT_COLUMNS
,
269 .flags
= OSL_LARGE_TABLE
,
270 .column_descriptions
= aft_cols
273 /* We don't want * dot or dot-dot anywhere. */
274 static int verify_dotfile(const char *rest
)
277 * The first character was '.', but that has already been discarded, we
281 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
283 case '.': /* path start with /foo/.. */
284 if (rest
[1] == '\0' || rest
[1] == '/')
285 return -1; /* /foo/.. or /foo/../bar are not ok */
286 /* /foo/..bar is ok */
292 * We fundamentally don't like some paths: We don't want double slashes or
293 * slashes at the end that can make pathnames ambiguous.
295 static int verify_path(const char *orig_path
, char **resolved_path
)
301 if (*orig_path
!= '/') /* we only accept absolute paths */
303 len
= strlen(orig_path
);
304 *resolved_path
= para_strdup(orig_path
);
305 path
= *resolved_path
;
306 while (len
> 1 && path
[--len
] == '/')
307 path
[len
] = '\0'; /* remove slash at the end */
313 case '/': /* double slash */
316 if (verify_dotfile(path
) < 0)
326 free(*resolved_path
);
330 /** The on-disk layout of a afhi struct. */
332 /** Where the number of seconds is stored. */
333 AFHI_SECONDS_TOTAL_OFFSET
= 0,
334 /** Position of the bitrate. */
335 AFHI_BITRATE_OFFSET
= 4,
336 /** Position of the frequency. */
337 AFHI_FREQUENCY_OFFSET
= 8,
338 /** Location of the audio file header. */
339 AFHI_HEADER_OFFSET_OFFSET
= 12,
340 /* Length of the audio file header. Zero means: No header. */
341 AFHI_HEADER_LEN_OFFSET
= 16,
342 /** The total number of chunks (4 bytes). */
343 CHUNKS_TOTAL_OFFSET
= 20,
344 /** The length of the audio file header (4 bytes). */
345 HEADER_LEN_OFFSET
= 24,
346 /** The start of the audio file header (4 bytes). */
347 HEADER_OFFSET_OFFSET
= 28,
348 /** The seconds part of the chunk time (4 bytes). */
349 CHUNK_TV_TV_SEC_OFFSET
= 32,
350 /** The microseconds part of the chunk time (4 bytes). */
351 CHUNK_TV_TV_USEC_OFFSET
= 36,
352 /** Number of channels is stored here. (1 byte) */
353 AFHI_CHANNELS_OFFSET
= 40,
354 /** EOF timeout in ms. (2 byte) */
355 AFHI_EOF_OFFSET
= 41,
356 /** The tag info position. */
357 AFHI_INFO_STRING_OFFSET
= 43,
358 /** Minimal on-disk size of a valid afhi struct. */
362 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
366 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
369 static void save_afhi(struct afh_info
*afhi
, char *buf
)
373 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
, afhi
->seconds_total
);
374 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
375 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
376 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
377 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
378 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
379 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
380 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
381 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
382 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
383 write_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
, afhi
->chunk_tv
.tv_usec
);
384 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
385 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
388 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
390 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
391 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
392 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
393 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
394 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
395 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
396 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
397 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
398 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
399 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
400 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
);
401 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
402 afhi
->info_string
= para_strdup(buf
+ AFHI_INFO_STRING_OFFSET
);
405 static unsigned sizeof_chunk_table(struct afh_info
*afhi
)
409 return 4 * (afhi
->chunks_total
+ 1);
412 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
416 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
417 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
420 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
424 afhi
->chunk_table
= para_malloc(sizeof_chunk_table(afhi
));
425 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
426 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
430 * Get the row of the audio file table corresponding to the given path.
432 * \param path The full path of the audio file.
433 * \param row Result pointer.
435 * \return The return value of the underlying call to osl_get_row().
437 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
439 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
441 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
445 * Get the row of the audio file table corresponding to the given hash value.
447 * \param hash The hash value of the desired audio file.
448 * \param row resul pointer.
450 * \return The return value of the underlying call to osl_get_row().
452 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
454 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
455 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
459 * Get the osl object holding the audio file selector info of a row.
461 * \param row Pointer to a row in the audio file table.
462 * \param obj Result pointer.
464 * \return The return value of the underlying call to osl_get_object().
466 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
468 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
472 * Get the osl object holding the audio file selector info, given a path.
475 * \param path The full path of the audio file.
476 * \param obj Result pointer.
478 * \return Positive on success, negative on errors.
480 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
483 int ret
= aft_get_row_of_path(path
, &row
);
486 return get_afsi_object_of_row(row
, obj
);
490 * Get the audio file selector info, given a row of the audio file table.
492 * \param row Pointer to a row in the audio file table.
493 * \param afsi Result pointer.
495 * \return Positive on success, negative on errors.
497 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
499 struct osl_object obj
;
500 int ret
= get_afsi_object_of_row(row
, &obj
);
503 return load_afsi(afsi
, &obj
);
507 * Get the audio file selector info, given the path of an audio table.
509 * \param path The full path of the audio file.
510 * \param afsi Result pointer.
512 * \return Positive on success, negative on errors.
514 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
516 struct osl_object obj
;
517 int ret
= get_afsi_object_of_path(path
, &obj
);
520 return load_afsi(afsi
, &obj
);
524 * Get the path of an audio file, given a row of the audio file table.
526 * \param row Pointer to a row in the audio file table.
527 * \param path Result pointer.
529 * The result is a pointer to mmapped data. The caller must not attempt
534 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
536 struct osl_object path_obj
;
537 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
541 *path
= path_obj
.data
;
546 * Get the object containing the hash value of an audio file, given a row.
548 * \param row Pointer to a row of the audio file table.
549 * \param obj Result pointer.
551 * \return The return value of the underlying call to osl_get_object().
553 * \sa get_hash_of_row().
555 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
557 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
561 * Get the hash value of an audio file, given a row of the audio file table.
563 * \param row Pointer to a row of the audio file table.
564 * \param hash Result pointer.
566 * \a hash points to mapped data and must not be freed by the caller.
568 * \return The return value of the underlying call to
569 * get_hash_object_of_aft_row().
571 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
573 struct osl_object obj
;
574 int ret
= get_hash_object_of_aft_row(row
, &obj
);
583 * Get the audio format handler info, given a row of the audio file table.
585 * \param row Pointer to a row of the audio file table.
586 * \param afhi Result pointer.
588 * \return The return value of the underlying call to osl_get_object().
590 * \sa get_chunk_table_of_row().
592 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
594 struct osl_object obj
;
595 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
599 load_afhi(obj
.data
, afhi
);
603 /* returns shmid on success */
604 static int save_afd(struct audio_file_data
*afd
)
606 size_t size
= sizeof(*afd
) + sizeof_chunk_table(&afd
->afhi
);
607 int shmid
, ret
= shm_new(size
);
614 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
617 *(struct audio_file_data
*)shm_afd
= *afd
;
620 save_chunk_table(&afd
->afhi
, buf
);
628 int load_afd(int shmid
, struct audio_file_data
*afd
)
634 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
637 *afd
= *(struct audio_file_data
*)shm_afd
;
640 load_chunk_table(&afd
->afhi
, buf
);
645 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
646 time_t current_time
, enum ls_listing_mode lm
)
650 if (!localtime_r((time_t *)seconds
, &t
))
652 if (lm
== LS_MODE_MBOX
) {
653 if (!strftime(buf
, size
, "%c", &t
))
657 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
658 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
662 if (!strftime(buf
, size
, "%b %e %Y", &t
))
667 /** Compute the number of (decimal) digits of a number. */
668 #define GET_NUM_DIGITS(x, num) { \
669 typeof((x)) _tmp = PARA_ABS(x); \
672 while ((_tmp) > 9) { \
678 static short unsigned get_duration_width(int seconds
)
680 short unsigned width
;
681 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
683 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
684 return 4 + (mins
> 9);
685 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
686 GET_NUM_DIGITS(hours
, &width
);
690 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
692 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
693 short unsigned max_width
;
695 if (!hours
) { /* m:ss or mm:ss */
696 max_width
= opts
->mode
== LS_MODE_LONG
?
697 opts
->widths
.duration_width
: 4;
698 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
699 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
700 max_width
= opts
->mode
== LS_MODE_LONG
?
701 opts
->widths
.duration_width
: 7;
702 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
707 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
709 char *att_text
, *att_lines
;
711 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
713 return para_strdup(att_bitmap
);
714 att_lines
= make_message("%s: %s\n%s: %s",
715 status_item_list
[SI_ATTRIBUTES_BITMAP
], att_bitmap
,
716 status_item_list
[SI_ATTRIBUTES_TXT
], att_text
);
721 static char *make_lyrics_lines(struct afs_info
*afsi
)
725 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
726 return make_message("%s: %u\n%s: %s\n",
727 status_item_list
[SI_LYRICS_ID
], afsi
->lyrics_id
,
728 status_item_list
[SI_LYRICS_NAME
], lyrics_name
?
729 lyrics_name
: "(none)");
732 static char *make_image_lines(struct afs_info
*afsi
)
735 img_get_name_by_id(afsi
->image_id
, &image_name
);
736 return make_message("%s: %u\n%s: %s\n",
737 status_item_list
[SI_IMAGE_ID
], afsi
->image_id
,
738 status_item_list
[SI_IMAGE_NAME
], image_name
?
739 image_name
: "(none)");
742 static char *make_filename_lines(const char *path
, unsigned flags
)
745 const char *basename
;
747 if (!(flags
& LS_FLAG_FULL_PATH
))
748 return make_message("%s: %s\n",
749 status_item_list
[SI_BASENAME
], path
);
750 basename
= para_basename(path
),
751 dirname
= para_dirname(path
);
752 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n",
753 status_item_list
[SI_PATH
], path
,
754 status_item_list
[SI_DIRECTORY
], dirname
? dirname
: "?",
755 status_item_list
[SI_BASENAME
], basename
? basename
: "?");
760 static int print_chunk_table(struct ls_data
*d
, struct para_buffer
*b
)
762 struct osl_object chunk_table_obj
;
763 struct osl_row
*aft_row
;
767 ret
= aft_get_row_of_hash(d
->hash
, &aft_row
);
770 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
771 AFTCOL_CHUNKS
, &chunk_table_obj
);
774 ret
= para_printf(b
, "%s\n"
775 "chunk_time: %lu:%lu\nchunk_offsets: ",
777 (long unsigned) d
->afhi
.chunk_tv
.tv_sec
,
778 (long unsigned) d
->afhi
.chunk_tv
.tv_usec
782 buf
= chunk_table_obj
.data
;
783 for (i
= 0; i
<= d
->afhi
.chunks_total
; i
++) {
784 ret
= para_printf(b
, "%u ", (unsigned) read_u32(buf
+ 4 * i
));
788 ret
= para_printf(b
, "\n");
790 osl_close_disk_object(&chunk_table_obj
);
794 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
795 struct para_buffer
*b
, time_t current_time
)
799 char last_played_time
[30];
800 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
801 char score_buf
[30] = "";
802 struct afs_info
*afsi
= &d
->afsi
;
803 struct afh_info
*afhi
= &d
->afhi
;
804 struct ls_widths
*w
= &opts
->widths
;
805 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
806 char asc_hash
[2 * HASH_SIZE
+ 1];
807 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
809 if (opts
->mode
== LS_MODE_SHORT
) {
810 ret
= para_printf(b
, "%s\n", d
->path
);
813 if (opts
->mode
== LS_MODE_CHUNKS
) {
814 ret
= print_chunk_table(d
, b
);
817 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
818 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
819 sizeof(last_played_time
), current_time
, opts
->mode
);
822 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
824 if (opts
->mode
== LS_MODE_LONG
)
825 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
827 sprintf(score_buf
, "%li ", d
->score
);
830 if (opts
->mode
== LS_MODE_LONG
) {
833 "%s " /* attributes */
835 "%*d " /* image_id */
836 "%*d " /* lyrics_id */
838 "%s " /* audio format */
839 "%*d " /* frequency */
842 "%*d " /* num_played */
843 "%s " /* last_played */
847 w
->amp_width
, afsi
->amp
,
848 w
->image_id_width
, afsi
->image_id
,
849 w
->lyrics_id_width
, afsi
->lyrics_id
,
850 w
->bitrate_width
, afhi
->bitrate
,
851 audio_format_name(afsi
->audio_format_id
),
852 w
->frequency_width
, afhi
->frequency
,
855 w
->num_played_width
, afsi
->num_played
,
861 hash_to_asc(d
->hash
, asc_hash
);
862 att_lines
= make_attribute_lines(att_buf
, afsi
);
863 lyrics_lines
= make_lyrics_lines(afsi
);
864 image_lines
= make_image_lines(afsi
);
865 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
866 if (opts
->mode
== LS_MODE_MBOX
) {
867 const char *bn
= para_basename(d
->path
);
869 "From foo@localhost %s\n"
870 "Received: from\nTo: bar\nFrom: a\n"
878 "%s" /* filename stuff */
879 "%s%s%s%s" /* score */
880 "%s\n" /* attributes */
881 "%s: %s\n" /* hash */
882 "%s" /* image id, image name */
884 "%s: %dkbit/s\n" /* bitrate */
885 "%s: %s\n" /* format */
886 "%s: %dHz\n" /* frequency */
887 "%s: %d\n" /* channels */
888 "%s: %s\n" /* duration */
889 "%s: %lu\n" /* seconds total */
890 "%s: %s\n" /* last played time */
891 "%s: %d\n" /* num_played */
892 "%s: %u\n" /* ampplification */
894 "%s: %lu\n" /* chunk time */
895 "%s: %lu\n", /* num chunks */
897 have_score
? status_item_list
[SI_SCORE
] : "",
898 have_score
? ": " : "",
900 have_score
? "\n" : "",
902 status_item_list
[SI_HASH
], asc_hash
,
905 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
906 status_item_list
[SI_FORMAT
], audio_format_name(afsi
->audio_format_id
),
907 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
908 status_item_list
[SI_CHANNELS
], afhi
->channels
,
909 status_item_list
[SI_DURATION
], duration_buf
,
910 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
911 status_item_list
[SI_LAST_PLAYED
], last_played_time
,
912 status_item_list
[SI_NUM_PLAYED
], afsi
->num_played
,
913 status_item_list
[SI_AMPLIFICATION
], afsi
->amp
,
915 status_item_list
[SI_CHUNK_TIME
], tv2ms(&afhi
->chunk_tv
),
916 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
920 if (opts
->mode
== LS_MODE_MBOX
) {
921 struct osl_object lyrics_def
;
922 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
923 if (lyrics_def
.data
) {
924 ret
= para_printf(b
, "Lyrics:\n~~~~~~~\n%s",
925 (char *)lyrics_def
.data
);
926 osl_close_disk_object(&lyrics_def
);
932 free(filename_lines
);
934 free(afhi
->info_string
);
939 * Write a list of audio-file related status items with empty values.
941 * \param buf Result pointer.
943 * This is used by vss when currently no audio file is open.
945 void make_empty_status_items(char *buf
)
949 "%s: \n" /* dirname */
950 "%s: \n" /* basename */
952 "%s: \n" /* attributes bitmap */
953 "%s: \n" /* attributes txt */
955 "%s: \n" /* image id */
956 "%s: \n" /* image name */
957 "%s: \n" /* lyrics id */
958 "%s: \n" /* lyrics name */
959 "%s: \n" /* bitrate */
960 "%s: \n" /* format */
961 "%s: \n" /* frequency */
962 "%s: \n" /* channels */
963 "%s: \n" /* duration */
964 "%s: \n" /* seconds total */
965 "%s: \n" /* num played */
966 "%s: \n" /* last played */
967 "%s: \n" /* audio file info */
968 "%s: \n" /* taginfo1 */
969 "%s: \n" /* taginfo2 */
970 "%s: \n" /* amplification */
972 status_item_list
[SI_PATH
],
973 status_item_list
[SI_DIRECTORY
],
974 status_item_list
[SI_BASENAME
],
975 status_item_list
[SI_SCORE
],
976 status_item_list
[SI_ATTRIBUTES_BITMAP
],
977 status_item_list
[SI_ATTRIBUTES_TXT
],
978 status_item_list
[SI_HASH
],
979 status_item_list
[SI_IMAGE_ID
],
980 status_item_list
[SI_IMAGE_NAME
],
981 status_item_list
[SI_LYRICS_ID
],
982 status_item_list
[SI_LYRICS_NAME
],
983 status_item_list
[SI_BITRATE
],
984 status_item_list
[SI_FORMAT
],
985 status_item_list
[SI_FREQUENCY
],
986 status_item_list
[SI_CHANNELS
],
987 status_item_list
[SI_DURATION
],
988 status_item_list
[SI_SECONDS_TOTAL
],
989 status_item_list
[SI_NUM_PLAYED
],
990 status_item_list
[SI_LAST_PLAYED
],
991 status_item_list
[SI_AUDIO_FILE_INFO
],
992 status_item_list
[SI_TAGINFO1
],
993 status_item_list
[SI_TAGINFO2
],
994 status_item_list
[SI_AMPLIFICATION
]
998 static void fixup_taginfo(char *begin
, char *end
)
1003 p
= strchr(p
, '\n');
1013 /* crap, remove this ASAP. */
1014 static int fixup_info_string(char *info_string
)
1016 char *t1
, *t2
, *end
;
1018 if (strncmp(info_string
, "audio_file_info:", 16))
1019 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1020 t1
= strstr(info_string
, "\ntaginfo1:");
1022 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1023 t2
= strstr(info_string
, "\ntaginfo2: ");
1025 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1027 end
= t2
+ strlen(t2
) + 1;
1028 fixup_taginfo(info_string
+ 16, t1
);
1029 fixup_taginfo(t1
+ 10, t2
);
1030 fixup_taginfo(t2
+ 10, end
);
1032 if (t1
- info_string
< 80 && t2
- t1
< 80 && end
- t2
< 80)
1034 if (t1
- info_string
>= 80) {
1035 memmove(info_string
+ 80, t1
, end
- t1
);
1036 t1
= info_string
+ 80;
1037 t2
-= t1
- info_string
- 80;
1038 end
-= t1
- info_string
- 80;
1040 if (t2
- t1
>= 80) {
1041 memmove(t1
+ 80, t2
, end
- t2
);
1042 end
-= t2
- t1
- 80;
1045 if (end
- t2
>= 80) {
1052 static int make_status_items(struct audio_file_data
*afd
,
1053 struct afs_info
*afsi
, char *path
, long score
,
1056 struct ls_data d
= {
1063 struct ls_options opts
= {
1064 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
1065 .mode
= LS_MODE_VERBOSE
,
1067 struct para_buffer pb
= {.max_size
= VERBOSE_LS_OUTPUT_SIZE
- 1};
1068 time_t current_time
;
1071 ret
= fixup_info_string(afd
->afhi
.info_string
);
1073 PARA_WARNING_LOG("ignoring invalid tag info\n");
1074 afd
->afhi
.info_string
[0] = '\0';
1076 PARA_NOTICE_LOG("truncated overlong tag info\n");
1077 time(¤t_time
);
1078 ret
= print_list_item(&d
, &opts
, &pb
, current_time
); /* frees info string */
1079 afd
->afhi
.info_string
= NULL
;
1082 strncpy(afd
->verbose_ls_output
, pb
.buf
, VERBOSE_LS_OUTPUT_SIZE
);
1083 afd
->verbose_ls_output
[VERBOSE_LS_OUTPUT_SIZE
- 1] = '\0';
1090 * Mmap the given audio file and update statistics.
1092 * \param aft_row Determines the audio file to be opened and updated.
1093 * \param score The score of the audio file.
1094 * \param afd Result pointer.
1096 * On success, the numplayed field of the audio file selector info is increased
1097 * and the lastplayed time is set to the current time. Finally, the score of
1098 * the audio file is updated.
1100 * \return Positive shmid on success, negative on errors.
1102 int open_and_update_audio_file(struct osl_row
*aft_row
, long score
,
1103 struct audio_file_data
*afd
)
1105 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
1106 struct osl_object afsi_obj
;
1107 struct afs_info old_afsi
, new_afsi
;
1108 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
1109 struct afsi_change_event_data aced
;
1110 struct osl_object map
, chunk_table_obj
;
1115 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1118 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
1121 ret
= load_afsi(&old_afsi
, &afsi_obj
);
1124 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
1127 afd
->afhi
.chunk_table
= NULL
;
1128 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
1129 AFTCOL_CHUNKS
, &chunk_table_obj
);
1132 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
1133 &map
.size
, &afd
->fd
);
1136 hash_function(map
.data
, map
.size
, file_hash
);
1137 ret
= hash_compare(file_hash
, aft_hash
);
1138 para_munmap(map
.data
, map
.size
);
1140 ret
= -E_HASH_MISMATCH
;
1143 new_afsi
= old_afsi
;
1144 new_afsi
.num_played
++;
1145 new_afsi
.last_played
= time(NULL
);
1146 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
1148 load_chunk_table(&afd
->afhi
, chunk_table_obj
.data
);
1149 ret
= make_status_items(afd
, &old_afsi
, path
, score
, file_hash
);
1152 aced
.aft_row
= aft_row
;
1153 aced
.old_afsi
= &old_afsi
;
1154 afs_event(AFSI_CHANGE
, NULL
, &aced
);
1155 ret
= save_afd(afd
);
1157 free(afd
->afhi
.chunk_table
);
1158 free(afd
->afhi
.info_string
);
1159 osl_close_disk_object(&chunk_table_obj
);
1163 static int ls_audio_format_compare(const void *a
, const void *b
)
1165 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1166 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1169 static int ls_duration_compare(const void *a
, const void *b
)
1171 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1172 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1175 static int ls_bitrate_compare(const void *a
, const void *b
)
1177 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1178 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1181 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1183 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1184 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1187 static int ls_image_id_compare(const void *a
, const void *b
)
1189 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1190 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1193 static int ls_channels_compare(const void *a
, const void *b
)
1195 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1196 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1199 static int ls_frequency_compare(const void *a
, const void *b
)
1201 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1202 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1205 static int ls_num_played_compare(const void *a
, const void *b
)
1207 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1208 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1211 static int ls_last_played_compare(const void *a
, const void *b
)
1213 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1214 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1217 static int ls_score_compare(const void *a
, const void *b
)
1219 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1220 return NUM_COMPARE(d1
->score
, d2
->score
);
1223 static int ls_path_compare(const void *a
, const void *b
)
1225 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1226 return strcmp(d1
->path
, d2
->path
);
1229 static int sort_matching_paths(struct ls_options
*options
)
1231 size_t nmemb
= options
->num_matching_paths
;
1232 size_t size
= sizeof(*options
->data_ptr
);
1233 int (*compar
)(const void *, const void *);
1236 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1237 for (i
= 0; i
< nmemb
; i
++)
1238 options
->data_ptr
[i
] = options
->data
+ i
;
1240 /* In these cases the array is already sorted */
1241 if (options
->sorting
== LS_SORT_BY_PATH
1242 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1243 && (options
->flags
& LS_FLAG_FULL_PATH
))
1245 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1246 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1249 switch (options
->sorting
) {
1250 case LS_SORT_BY_PATH
:
1251 compar
= ls_path_compare
; break;
1252 case LS_SORT_BY_SCORE
:
1253 compar
= ls_score_compare
; break;
1254 case LS_SORT_BY_LAST_PLAYED
:
1255 compar
= ls_last_played_compare
; break;
1256 case LS_SORT_BY_NUM_PLAYED
:
1257 compar
= ls_num_played_compare
; break;
1258 case LS_SORT_BY_FREQUENCY
:
1259 compar
= ls_frequency_compare
; break;
1260 case LS_SORT_BY_CHANNELS
:
1261 compar
= ls_channels_compare
; break;
1262 case LS_SORT_BY_IMAGE_ID
:
1263 compar
= ls_image_id_compare
; break;
1264 case LS_SORT_BY_LYRICS_ID
:
1265 compar
= ls_lyrics_id_compare
; break;
1266 case LS_SORT_BY_BITRATE
:
1267 compar
= ls_bitrate_compare
; break;
1268 case LS_SORT_BY_DURATION
:
1269 compar
= ls_duration_compare
; break;
1270 case LS_SORT_BY_AUDIO_FORMAT
:
1271 compar
= ls_audio_format_compare
; break;
1275 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1279 /* row is either an aft_row or a row of the score table */
1280 /* TODO: Only compute widths if we need them */
1281 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1284 struct ls_options
*options
= ls_opts
;
1286 struct ls_widths
*w
;
1287 unsigned short num_digits
;
1289 struct osl_row
*aft_row
;
1293 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1294 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1299 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1302 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1303 char *p
= strrchr(path
, '/');
1307 if (options
->num_patterns
) {
1308 for (i
= 0; i
< options
->num_patterns
; i
++) {
1309 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1312 if (ret
== FNM_NOMATCH
)
1316 if (i
>= options
->num_patterns
) /* no match */
1319 tmp
= options
->num_matching_paths
++;
1320 if (options
->num_matching_paths
> options
->array_size
) {
1321 options
->array_size
++;
1322 options
->array_size
*= 2;
1323 options
->data
= para_realloc(options
->data
, options
->array_size
1324 * sizeof(*options
->data
));
1326 d
= options
->data
+ tmp
;
1327 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1330 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1334 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1337 w
= &options
->widths
;
1338 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1339 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1340 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1341 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1342 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1343 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1344 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1345 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1346 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1347 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1348 /* get the number of chars to print this amount of time */
1349 num_digits
= get_duration_width(d
->afhi
.seconds_total
);
1350 w
->duration_width
= PARA_MAX(w
->duration_width
, num_digits
);
1351 GET_NUM_DIGITS(d
->afsi
.amp
, &num_digits
);
1352 w
->amp_width
= PARA_MAX(w
->amp_width
, num_digits
);
1353 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1354 GET_NUM_DIGITS(score
, &num_digits
);
1355 num_digits
++; /* add one for the sign (space or "-") */
1356 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1361 free(d
->afhi
.info_string
);
1365 static void com_ls_callback(int fd
, const struct osl_object
*query
)
1367 struct ls_options
*opts
= query
->data
;
1368 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1369 struct para_buffer b
= {.max_size
= SHMMAX
,
1370 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1372 time_t current_time
;
1375 if (opts
->num_patterns
) {
1376 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1377 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1378 opts
->patterns
[i
] = p
;
1382 opts
->patterns
= NULL
;
1383 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1384 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1386 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1390 if (!opts
->num_matching_paths
)
1392 ret
= sort_matching_paths(opts
);
1395 time(¤t_time
);
1396 if (opts
->flags
& LS_FLAG_REVERSE
)
1397 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1398 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1403 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1404 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1410 pass_buffer_as_shm(b
.buf
, b
.offset
, &fd
);
1413 free(opts
->data_ptr
);
1414 free(opts
->patterns
);
1418 * TODO: flags -h (sort by hash)
1420 int com_ls(int fd
, int argc
, char * const * const argv
)
1424 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1425 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1426 struct ls_options opts
= {.patterns
= NULL
};
1427 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)};
1429 for (i
= 1; i
< argc
; i
++) {
1430 const char *arg
= argv
[i
];
1433 if (!strcmp(arg
, "--")) {
1437 if (!strncmp(arg
, "-l", 2)) {
1439 mode
= LS_MODE_LONG
;
1443 return -E_AFT_SYNTAX
;
1444 switch(*(arg
+ 2)) {
1446 mode
= LS_MODE_SHORT
;
1449 mode
= LS_MODE_LONG
;
1452 mode
= LS_MODE_VERBOSE
;
1455 mode
= LS_MODE_MBOX
;
1458 mode
= LS_MODE_CHUNKS
;
1461 return -E_AFT_SYNTAX
;
1464 if (!strcmp(arg
, "-p")) {
1465 flags
|= LS_FLAG_FULL_PATH
;
1468 if (!strcmp(arg
, "-a")) {
1469 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1472 if (!strcmp(arg
, "-r")) {
1473 flags
|= LS_FLAG_REVERSE
;
1476 if (!strncmp(arg
, "-s", 2)) {
1477 if (!*(arg
+ 2) || *(arg
+ 3))
1478 return -E_AFT_SYNTAX
;
1479 switch(*(arg
+ 2)) {
1481 sort
= LS_SORT_BY_PATH
;
1483 case 's': /* -ss implies -a */
1484 sort
= LS_SORT_BY_SCORE
;
1485 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1488 sort
= LS_SORT_BY_LAST_PLAYED
;
1491 sort
= LS_SORT_BY_NUM_PLAYED
;
1494 sort
= LS_SORT_BY_FREQUENCY
;
1497 sort
= LS_SORT_BY_CHANNELS
;
1500 sort
= LS_SORT_BY_IMAGE_ID
;
1503 sort
= LS_SORT_BY_LYRICS_ID
;
1506 sort
= LS_SORT_BY_BITRATE
;
1509 sort
= LS_SORT_BY_DURATION
;
1512 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1515 return -E_AFT_SYNTAX
;
1518 return -E_AFT_SYNTAX
;
1521 opts
.sorting
= sort
;
1523 opts
.num_patterns
= argc
- i
;
1524 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1525 argv
+ i
, com_ls_callback
, send_result
, &fd
);
1530 * Call the given function for each file in the audio file table.
1532 * \param private_data An arbitrary data pointer, passed to \a func.
1533 * \param func The custom function to be called.
1535 * \return The return value of the underlying call to osl_rbtree_loop().
1537 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1539 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1543 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1545 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1546 struct osl_row
*row
;
1548 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1552 /** The format of the data stored by save_audio_file_data(). */
1553 enum com_add_buffer_offsets
{
1554 /** afhi (if present) starts here. */
1555 CAB_AFHI_OFFSET_POS
= 0,
1556 /** Start of the chunk table (if present). */
1557 CAB_CHUNKS_OFFSET_POS
= 2,
1558 /** Audio format id. */
1559 CAB_AUDIO_FORMAT_OFFSET
= 4,
1560 /** Flags given to the add command. */
1561 CAB_FLAGS_OFFSET
= 5,
1562 /** The hash of the audio file being added. */
1563 CAB_HASH_OFFSET
= 9,
1564 /** Start of the path of the audio file. */
1565 CAB_PATH_OFFSET
= (CAB_HASH_OFFSET
+ HASH_SIZE
),
1569 * Store the given data to a single buffer. Doesn't need the audio file selector
1570 * info struct as the server knows it as well.
1572 * It's OK to call this with afhi == NULL. In this case, the audio format
1573 * handler info won't be stored in the buffer.
1575 static void save_add_callback_buffer(HASH_TYPE
*hash
, const char *path
,
1576 struct afh_info
*afhi
, uint32_t flags
,
1577 uint8_t audio_format_num
, struct osl_object
*obj
)
1579 size_t path_len
= strlen(path
) + 1;
1580 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1581 size_t size
= CAB_PATH_OFFSET
+ path_len
+ afhi_size
1582 + sizeof_chunk_table(afhi
);
1583 char *buf
= para_malloc(size
);
1586 write_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1587 write_u32(buf
+ CAB_FLAGS_OFFSET
, flags
);
1589 memcpy(buf
+ CAB_HASH_OFFSET
, hash
, HASH_SIZE
);
1590 strcpy(buf
+ CAB_PATH_OFFSET
, path
);
1592 pos
= CAB_PATH_OFFSET
+ path_len
;
1593 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1594 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1595 pos
+ afhi_size
- 1);
1596 write_u16(buf
+ CAB_AFHI_OFFSET_POS
, pos
);
1597 save_afhi(afhi
, buf
+ pos
);
1600 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1601 write_u16(buf
+ CAB_CHUNKS_OFFSET_POS
, pos
);
1603 save_chunk_table(afhi
, buf
+ pos
);
1604 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1611 Overview of the add command.
1613 Input: What was passed to the callback by the command handler.
1615 HS: Hash sister. Whether an audio file with identical hash
1616 already exists in the osl database.
1618 PB: Path brother. Whether a file with the given path exists
1621 F: Force flag given. Whether add was called with -f.
1623 output: Action performed by the callback.
1625 AFHI: Whether afhi and chunk table are computed and sent.
1626 ACTION: Table modifications to be done by the callback.
1628 +----+----+---+------+---------------------------------------------------+
1629 | HS | PB | F | AFHI | ACTION
1630 +----+----+---+------+---------------------------------------------------+
1631 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1632 | | update path, keep afsi
1633 +----+----+---+------+---------------------------------------------------+
1634 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1635 | | otherwise: remove PB, HS: update path, keep afhi,
1637 +----+----+---+------+---------------------------------------------------+
1638 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1640 +----+----+---+------+---------------------------------------------------+
1641 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1642 +----+----+---+------+---------------------------------------------------+
1643 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1644 | | (force has no effect)
1645 +----+----+---+------+---------------------------------------------------+
1646 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1647 +----+----+---+------+---------------------------------------------------+
1648 | N | N | Y | Y | (new file) create new entry (force has no effect)
1649 +----+----+---+------+---------------------------------------------------+
1650 | N | N | N | Y | (new file) create new entry
1651 +----+----+---+------+---------------------------------------------------+
1655 afhi <=> force or no HS
1660 /** Flags passed to the add command. */
1661 enum com_add_flags
{
1662 /** Skip paths that exist already. */
1664 /** Force adding. */
1666 /** Print what is being done. */
1667 ADD_FLAG_VERBOSE
= 4,
1668 /** Try to add files with unknown suffixes. */
1672 static void com_add_callback(int fd
, const struct osl_object
*query
)
1674 char *buf
= query
->data
, *path
;
1675 struct osl_row
*pb
, *aft_row
;
1677 struct osl_object objs
[NUM_AFT_COLUMNS
];
1679 char asc
[2 * HASH_SIZE
+ 1];
1681 char afsi_buf
[AFSI_SIZE
];
1682 uint32_t flags
= read_u32(buf
+ CAB_FLAGS_OFFSET
);
1683 struct afs_info default_afsi
= {.last_played
= 0};
1684 struct para_buffer msg
= {.max_size
= SHMMAX
,
1685 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1686 uint16_t afhi_offset
, chunks_offset
;
1688 hash
= (HASH_TYPE
*)buf
+ CAB_HASH_OFFSET
;
1689 hash_to_asc(hash
, asc
);;
1690 objs
[AFTCOL_HASH
].data
= buf
+ CAB_HASH_OFFSET
;
1691 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1693 path
= buf
+ CAB_PATH_OFFSET
;
1694 objs
[AFTCOL_PATH
].data
= path
;
1695 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1697 PARA_INFO_LOG("request to add %s\n", path
);
1698 hs
= find_hash_sister(hash
);
1699 ret
= aft_get_row_of_path(path
, &pb
);
1700 if (ret
< 0 && ret
!= -E_OSL_RB_KEY_NOT_FOUND
)
1702 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1703 if (flags
& ADD_FLAG_VERBOSE
)
1704 ret
= para_printf(&msg
, "ignoring duplicate\n");
1709 if (hs
&& hs
!= pb
) {
1710 struct osl_object obj
;
1711 if (pb
) { /* hs trumps pb, remove pb */
1712 if (flags
& ADD_FLAG_VERBOSE
) {
1713 ret
= para_printf(&msg
, "removing path brother\n");
1717 ret
= osl_del_row(audio_file_table
, pb
);
1722 /* file rename, update hs' path */
1723 if (flags
& ADD_FLAG_VERBOSE
) {
1724 ret
= osl_get_object(audio_file_table
, hs
,
1728 ret
= para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1732 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1733 &objs
[AFTCOL_PATH
]);
1736 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1737 if (!(flags
& ADD_FLAG_FORCE
))
1740 /* no hs or force mode, child must have sent afhi */
1741 afhi_offset
= read_u16(buf
+ CAB_AFHI_OFFSET_POS
);
1742 chunks_offset
= read_u16(buf
+ CAB_CHUNKS_OFFSET_POS
);
1744 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1745 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1747 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1749 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1750 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1751 if (pb
&& !hs
) { /* update pb's hash */
1752 char old_asc
[2 * HASH_SIZE
+ 1];
1753 HASH_TYPE
*old_hash
;
1754 ret
= get_hash_of_row(pb
, &old_hash
);
1757 hash_to_asc(old_hash
, old_asc
);
1758 if (flags
& ADD_FLAG_VERBOSE
) {
1759 ret
= para_printf(&msg
, "file change: %s -> %s\n",
1764 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1765 &objs
[AFTCOL_HASH
]);
1769 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1770 struct osl_row
*row
= pb
? pb
: hs
;
1771 /* update afhi and chunk_table */
1772 if (flags
& ADD_FLAG_VERBOSE
) {
1773 ret
= para_printf(&msg
, "updating afhi and chunk table\n");
1777 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1778 &objs
[AFTCOL_AFHI
]);
1781 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1782 &objs
[AFTCOL_CHUNKS
]);
1785 afs_event(AFHI_CHANGE
, &msg
, row
);
1788 /* new entry, use default afsi */
1789 if (flags
& ADD_FLAG_VERBOSE
) {
1790 ret
= para_printf(&msg
, "new file\n");
1794 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1795 default_afsi
.audio_format_id
= read_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
);
1797 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1798 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1799 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1800 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1801 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1804 para_printf(&msg
, "%s\n", para_strerror(-ret
));
1806 pass_buffer_as_shm(msg
.buf
, msg
.offset
, &fd
);
1810 /** Used by com_add(). */
1811 struct private_add_data
{
1812 /** The socket file descriptor. */
1814 /** The given add flags. */
1818 static void path_brother_callback(int fd
, const struct osl_object
*query
)
1820 char *path
= query
->data
;
1821 struct osl_row
*path_brother
;
1822 int ret
= aft_get_row_of_path(path
, &path_brother
);
1825 pass_buffer_as_shm((char *)&path_brother
, sizeof(path_brother
), &fd
);
1828 static void hash_sister_callback(int fd
, const struct osl_object
*query
)
1830 HASH_TYPE
*hash
= query
->data
;
1831 struct osl_row
*hash_sister
;
1833 hash_sister
= find_hash_sister(hash
);
1836 pass_buffer_as_shm((char *)&hash_sister
, sizeof(hash_sister
), &fd
);
1839 static int get_row_pointer_from_result(struct osl_object
*result
, void *private)
1841 struct osl_row
**row
= private;
1842 *row
= result
->data
;
1846 static int add_one_audio_file(const char *path
, void *private_data
)
1848 int ret
, send_ret
= 1, fd
;
1849 uint8_t format_num
= -1;
1850 struct private_add_data
*pad
= private_data
;
1851 struct afh_info afhi
, *afhi_ptr
= NULL
;
1852 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1853 struct osl_object map
, obj
= {.data
= NULL
}, query
;
1854 HASH_TYPE hash
[HASH_SIZE
];
1856 ret
= guess_audio_format(path
);
1857 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1859 query
.data
= (char *)path
;
1860 query
.size
= strlen(path
) + 1;
1861 ret
= send_callback_request(path_brother_callback
, &query
,
1862 get_row_pointer_from_result
, &pb
);
1863 if (ret
< 0 && ret
!= -E_OSL_RB_KEY_NOT_FOUND
)
1866 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1867 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1868 send_ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1871 /* We still want to add this file. Compute its hash. */
1872 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, &fd
);
1875 hash_function(map
.data
, map
.size
, hash
);
1877 /* Check whether the database contains a file with the same hash. */
1879 query
.size
= HASH_SIZE
;
1880 ret
= send_callback_request(hash_sister_callback
, &query
,
1881 get_row_pointer_from_result
, &hs
);
1884 /* Return success if we already know this file. */
1886 if (pb
&& hs
&& hs
== pb
&& !(pad
->flags
& ADD_FLAG_FORCE
)) {
1887 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1888 send_ret
= send_va_buffer(pad
->fd
,
1889 "%s exists, not forcing update\n", path
);
1893 * We won't recalculate the audio format info and the chunk table if
1894 * there is a hash sister and FORCE was not given.
1896 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1897 ret
= compute_afhi(path
, map
.data
, map
.size
, fd
, &afhi
);
1903 munmap(map
.data
, map
.size
);
1905 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1906 send_ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1910 save_add_callback_buffer(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1911 /* Ask afs to consider this entry for adding. */
1912 ret
= send_callback_request(com_add_callback
, &obj
, send_result
, &pad
->fd
);
1917 munmap(map
.data
, map
.size
);
1919 if (ret
< 0 && send_ret
>= 0)
1920 send_ret
= send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1921 para_strerror(-ret
));
1924 free(afhi_ptr
->chunk_table
);
1925 free(afhi_ptr
->info_string
);
1927 /* Stop adding files only on send errors. */
1931 int com_add(int fd
, int argc
, char * const * const argv
)
1934 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1935 struct stat statbuf
;
1937 for (i
= 1; i
< argc
; i
++) {
1938 const char *arg
= argv
[i
];
1941 if (!strcmp(arg
, "--")) {
1945 if (!strcmp(arg
, "-a")) {
1946 pad
.flags
|= ADD_FLAG_ALL
;
1949 if (!strcmp(arg
, "-l")) {
1950 pad
.flags
|= ADD_FLAG_LAZY
;
1953 if (!strcmp(arg
, "-f")) {
1954 pad
.flags
|= ADD_FLAG_FORCE
;
1957 if (!strcmp(arg
, "-v")) {
1958 pad
.flags
|= ADD_FLAG_VERBOSE
;
1963 return -E_AFT_SYNTAX
;
1964 for (; i
< argc
; i
++) {
1966 ret
= verify_path(argv
[i
], &path
);
1968 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
],
1969 para_strerror(-ret
));
1974 ret
= stat(path
, &statbuf
);
1976 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1983 if (S_ISDIR(statbuf
.st_mode
))
1984 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1987 ret
= add_one_audio_file(path
, &pad
);
1989 send_va_buffer(fd
, "%s: %s\n", path
, para_strerror(-ret
));
2000 * Flags used by the touch command.
2005 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
2006 TOUCH_FLAG_FNM_PATHNAME
= 1,
2007 /** Activates verbose mode. */
2008 TOUCH_FLAG_VERBOSE
= 2
2011 /** Options used by com_touch(). */
2012 struct com_touch_options
{
2013 /** New num_played value. */
2015 /** New last played count. */
2016 int64_t last_played
;
2017 /** New lyrics id. */
2019 /** New image id. */
2021 /** New amplification value. */
2023 /** Command line flags (see \ref touch_flags). */
2027 /** Data passed to the action handler of com_touch(). */
2028 struct touch_action_data
{
2029 /** Command line options (see \ref com_touch_options). */
2030 struct com_touch_options
*cto
;
2031 /** Message buffer. */
2032 struct para_buffer pb
;
2033 /** How many audio files matched the given pattern. */
2034 unsigned num_matches
;
2037 static int touch_audio_file(__a_unused
struct osl_table
*table
,
2038 struct osl_row
*row
, const char *name
, void *data
)
2040 struct touch_action_data
*tad
= data
;
2041 struct osl_object obj
;
2042 struct afs_info old_afsi
, new_afsi
;
2043 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
2044 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0 && tad
->cto
->amp
< 0;
2045 struct afsi_change_event_data aced
;
2047 ret
= get_afsi_object_of_row(row
, &obj
);
2049 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2050 ret
= load_afsi(&old_afsi
, &obj
);
2052 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2053 new_afsi
= old_afsi
;
2055 new_afsi
.num_played
++;
2056 new_afsi
.last_played
= time(NULL
);
2057 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
2058 ret
= para_printf(&tad
->pb
, "%s: num_played = %u, "
2059 "last_played = now()\n", name
,
2060 new_afsi
.num_played
);
2065 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
2066 ret
= para_printf(&tad
->pb
, "touching %s\n", name
);
2070 if (tad
->cto
->lyrics_id
>= 0)
2071 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
2072 if (tad
->cto
->image_id
>= 0)
2073 new_afsi
.image_id
= tad
->cto
->image_id
;
2074 if (tad
->cto
->num_played
>= 0)
2075 new_afsi
.num_played
= tad
->cto
->num_played
;
2076 if (tad
->cto
->last_played
>= 0)
2077 new_afsi
.last_played
= tad
->cto
->last_played
;
2078 if (tad
->cto
->amp
>= 0)
2079 new_afsi
.amp
= tad
->cto
->amp
;
2082 save_afsi(&new_afsi
, &obj
); /* in-place update */
2084 aced
.old_afsi
= &old_afsi
;
2085 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
2089 static void com_touch_callback(int fd
, const struct osl_object
*query
)
2091 struct touch_action_data tad
= {.cto
= query
->data
,
2094 .private_data
= &fd
,
2095 .max_size_handler
= pass_buffer_as_shm
2099 struct pattern_match_data pmd
= {
2100 .table
= audio_file_table
,
2101 .loop_col_num
= AFTCOL_HASH
,
2102 .match_col_num
= AFTCOL_PATH
,
2103 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
2104 .size
= query
->size
- sizeof(*tad
.cto
)},
2106 .action
= touch_audio_file
2108 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
2109 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2110 ret
= for_each_matching_row(&pmd
);
2112 ret2
= para_printf(&tad
.pb
, "%s\n", para_strerror(-ret
));
2114 if (!tad
.num_matches
)
2115 ret2
= para_printf(&tad
.pb
, "no matches\n");
2116 if (ret2
>= 0 && tad
.pb
.offset
)
2117 pass_buffer_as_shm(tad
.pb
.buf
, tad
.pb
.offset
, &fd
);
2121 int com_touch(int fd
, int argc
, char * const * const argv
)
2123 struct com_touch_options cto
= {
2130 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)};
2134 for (i
= 1; i
< argc
; i
++) {
2135 const char *arg
= argv
[i
];
2138 if (!strcmp(arg
, "--")) {
2142 if (!strncmp(arg
, "-n", 2)) {
2143 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
2148 if (!strncmp(arg
, "-l", 2)) {
2149 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
2154 if (!strncmp(arg
, "-y", 2)) {
2155 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
2160 if (!strncmp(arg
, "-i", 2)) {
2161 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
2166 if (!strncmp(arg
, "-a", 2)) {
2168 ret
= para_atoi32(arg
+ 2, &val
);
2171 if (val
< 0 || val
> 255)
2172 return -ERRNO_TO_PARA_ERROR(EINVAL
);
2176 if (!strcmp(arg
, "-p")) {
2177 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
2180 if (!strcmp(arg
, "-v")) {
2181 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
2184 break; /* non-option starting with dash */
2187 return -E_AFT_SYNTAX
;
2188 ret
= send_option_arg_callback_request(&query
, argc
- i
,
2189 argv
+ i
, com_touch_callback
, send_result
, &fd
);
2191 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2195 /** Flags for com_rm(). */
2198 RM_FLAG_VERBOSE
= 1,
2202 RM_FLAG_FNM_PATHNAME
= 4
2205 /** Data passed to the action handler of com_rm(). */
2206 struct com_rm_action_data
{
2207 /** Command line flags ((see \ref rm_flags). */
2209 /** Message buffer. */
2210 struct para_buffer pb
;
2211 /** Number of audio files removed. */
2212 unsigned num_removed
;
2215 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2216 struct osl_row
*row
, const char *name
, void *data
)
2218 struct com_rm_action_data
*crd
= data
;
2221 if (crd
->flags
& RM_FLAG_VERBOSE
) {
2222 ret
= para_printf(&crd
->pb
, "removing %s\n", name
);
2226 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2227 ret
= osl_del_row(audio_file_table
, row
);
2229 para_printf(&crd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2235 static void com_rm_callback(int fd
, const struct osl_object
*query
)
2237 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
,
2240 .private_data
= &fd
,
2241 .max_size_handler
= pass_buffer_as_shm
2245 struct pattern_match_data pmd
= {
2246 .table
= audio_file_table
,
2247 .loop_col_num
= AFTCOL_HASH
,
2248 .match_col_num
= AFTCOL_PATH
,
2249 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2250 .size
= query
->size
- sizeof(uint32_t)},
2252 .action
= remove_audio_file
2254 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2255 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2256 ret
= for_each_matching_row(&pmd
);
2258 para_printf(&crd
.pb
, "%s\n", para_strerror(-ret
));
2261 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2262 ret
= para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2264 if (crd
.flags
& RM_FLAG_VERBOSE
)
2265 ret
= para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2267 if (ret
>= 0 && crd
.pb
.offset
)
2268 pass_buffer_as_shm(crd
.pb
.buf
, crd
.pb
.offset
, &fd
);
2272 /* TODO options: -r (recursive) */
2273 int com_rm(int fd
, int argc
, char * const * const argv
)
2276 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)};
2279 for (i
= 1; i
< argc
; i
++) {
2280 const char *arg
= argv
[i
];
2283 if (!strcmp(arg
, "--")) {
2287 if (!strcmp(arg
, "-f")) {
2288 flags
|= RM_FLAG_FORCE
;
2291 if (!strcmp(arg
, "-p")) {
2292 flags
|= RM_FLAG_FNM_PATHNAME
;
2295 if (!strcmp(arg
, "-v")) {
2296 flags
|= RM_FLAG_VERBOSE
;
2302 return -E_AFT_SYNTAX
;
2303 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2304 com_rm_callback
, send_result
, &fd
);
2306 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2311 * Flags used by the cpsi command.
2316 /** Whether the lyrics id should be copied. */
2317 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2318 /** Whether the image id should be copied. */
2319 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2320 /** Whether the lastplayed time should be copied. */
2321 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2322 /** Whether the numplayed count should be copied. */
2323 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2324 /** Whether the attributes should be copied. */
2325 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2326 /** Activates verbose mode. */
2327 CPSI_FLAG_VERBOSE
= 32,
2330 /** Data passed to the action handler of com_cpsi(). */
2331 struct cpsi_action_data
{
2332 /** command line flags (see \ref cpsi_flags). */
2334 /** Number of audio files changed. */
2335 unsigned num_copied
;
2336 /** Message buffer. */
2337 struct para_buffer pb
;
2338 /** Values are copied from here. */
2339 struct afs_info source_afsi
;
2342 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2343 struct osl_row
*row
, const char *name
, void *data
)
2345 struct cpsi_action_data
*cad
= data
;
2346 struct osl_object target_afsi_obj
;
2348 struct afs_info old_afsi
, target_afsi
;
2349 struct afsi_change_event_data aced
;
2351 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2354 load_afsi(&target_afsi
, &target_afsi_obj
);
2355 old_afsi
= target_afsi
;
2356 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2357 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2358 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2359 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2360 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2361 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2362 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2363 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2364 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2365 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2366 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2368 if (cad
->flags
& CPSI_FLAG_VERBOSE
) {
2369 ret
= para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2374 aced
.old_afsi
= &old_afsi
;
2375 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2379 static void com_cpsi_callback(int fd
, const struct osl_object
*query
)
2381 struct cpsi_action_data cad
= {
2382 .flags
= *(unsigned *)query
->data
,
2385 .private_data
= &fd
,
2386 .max_size_handler
= pass_buffer_as_shm
2390 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2391 struct pattern_match_data pmd
= {
2392 .table
= audio_file_table
,
2393 .loop_col_num
= AFTCOL_HASH
,
2394 .match_col_num
= AFTCOL_PATH
,
2395 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2396 .size
= query
->size
- sizeof(cad
.flags
)
2397 - strlen(source_path
) - 1},
2399 .action
= copy_selector_info
2402 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2405 ret
= for_each_matching_row(&pmd
);
2408 para_printf(&cad
.pb
, "%s\n", para_strerror(-ret
));
2409 else if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2411 para_printf(&cad
.pb
, "copied requested afsi from %s "
2412 "to %u files\n", source_path
, cad
.num_copied
);
2414 para_printf(&cad
.pb
, "nothing copied\n");
2417 pass_buffer_as_shm(cad
.pb
.buf
, cad
.pb
.offset
, &fd
);
2421 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2425 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
2427 for (i
= 1; i
< argc
; i
++) {
2428 const char *arg
= argv
[i
];
2431 if (!strcmp(arg
, "--")) {
2435 if (!strcmp(arg
, "-y")) {
2436 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2439 if (!strcmp(arg
, "-i")) {
2440 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2443 if (!strcmp(arg
, "-l")) {
2444 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2447 if (!strcmp(arg
, "-n")) {
2448 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2451 if (!strcmp(arg
, "-a")) {
2452 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2455 if (!strcmp(arg
, "-v")) {
2456 flags
|= CPSI_FLAG_VERBOSE
;
2461 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2462 return -E_AFT_SYNTAX
;
2463 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2464 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2465 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2466 com_cpsi_callback
, send_result
, &fd
);
2468 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2472 /* TODO: optionally fix problems by removing offending rows */
2473 static int check_audio_file(struct osl_row
*row
, void *data
)
2476 struct para_buffer
*pb
= data
;
2477 struct stat statbuf
;
2478 int ret
= get_audio_file_path_of_row(row
, &path
);
2479 struct afs_info afsi
;
2483 return para_printf(pb
, "%s\n", para_strerror(-ret
));
2484 if (stat(path
, &statbuf
) < 0) {
2485 ret
= para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2489 if (!S_ISREG(statbuf
.st_mode
)) {
2490 ret
= para_printf(pb
, "%s: not a regular file\n", path
);
2495 ret
= get_afsi_of_row(row
, &afsi
);
2497 return para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
2498 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2500 ret
= para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2501 para_strerror(-ret
));
2505 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2507 ret
= para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2508 para_strerror(-ret
));
2513 * Check the audio file table for inconsistencies.
2515 * \param fd The afs socket.
2516 * \param query Unused.
2518 * This function always succeeds.
2522 void aft_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
2524 struct para_buffer pb
= {
2526 .private_data
= &fd
,
2527 .max_size_handler
= pass_buffer_as_shm
2529 int ret
= para_printf(&pb
, "checking audio file table...\n");
2533 audio_file_loop(&pb
, check_audio_file
);
2535 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
2540 * Close the audio file table.
2542 * \param flags Usual flags that are passed to osl_close_table().
2544 * \sa osl_close_table().
2546 static void aft_close(void)
2548 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2549 audio_file_table
= NULL
;
2553 * Open the audio file table.
2555 * \param dir The database directory.
2559 * \sa osl_open_table().
2561 static int aft_open(const char *dir
)
2565 audio_file_table_desc
.dir
= dir
;
2566 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2569 osl_get_num_rows(audio_file_table
, &num
);
2570 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2573 PARA_INFO_LOG("failed to open audio file table\n");
2574 audio_file_table
= NULL
;
2575 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2580 static int aft_create(const char *dir
)
2582 audio_file_table_desc
.dir
= dir
;
2583 return osl_create_table(&audio_file_table_desc
);
2586 static int clear_attribute(struct osl_row
*row
, void *data
)
2588 struct rmatt_event_data
*red
= data
;
2589 struct afs_info afsi
;
2590 struct osl_object obj
;
2591 int ret
= get_afsi_object_of_row(row
, &obj
);
2592 uint64_t mask
= ~(1ULL << red
->bitnum
);
2596 ret
= load_afsi(&afsi
, &obj
);
2599 afsi
.attributes
&= mask
;
2600 save_afsi(&afsi
, &obj
);
2604 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2610 case ATTRIBUTE_REMOVE
: {
2611 const struct rmatt_event_data
*red
= data
;
2612 ret
= para_printf(pb
, "clearing attribute %s (bit %u) from all "
2613 "entries in the audio file table\n", red
->name
,
2617 return audio_file_loop(data
, clear_attribute
);
2624 void aft_init(struct afs_table
*t
)
2626 t
->name
= audio_file_table_desc
.name
;
2628 t
->close
= aft_close
;
2629 t
->create
= aft_create
;
2630 t
->event_handler
= aft_event_handler
;