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. */
221 static struct osl_column_description aft_cols
[] = {
223 .storage_type
= OSL_MAPPED_STORAGE
,
224 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
226 .compare_function
= osl_hash_compare
,
227 .data_size
= HASH_SIZE
230 .storage_type
= OSL_MAPPED_STORAGE
,
231 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
233 .compare_function
= string_compare
,
236 .storage_type
= OSL_MAPPED_STORAGE
,
237 .storage_flags
= OSL_FIXED_SIZE
,
239 .data_size
= AFSI_SIZE
242 .storage_type
= OSL_MAPPED_STORAGE
,
246 .storage_type
= OSL_DISK_STORAGE
,
251 static struct osl_table_description audio_file_table_desc
= {
252 .name
= "audio_files",
253 .num_columns
= NUM_AFT_COLUMNS
,
254 .flags
= OSL_LARGE_TABLE
,
255 .column_descriptions
= aft_cols
258 /* We don't want * dot or dot-dot anywhere. */
259 static int verify_dotfile(const char *rest
)
262 * The first character was '.', but that has already been discarded, we
266 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
268 case '.': /* path start with /foo/.. */
269 if (rest
[1] == '\0' || rest
[1] == '/')
270 return -1; /* /foo/.. or /foo/../bar are not ok */
271 /* /foo/..bar is ok */
277 * We fundamentally don't like some paths: We don't want double slashes or
278 * slashes at the end that can make pathnames ambiguous.
280 static int verify_path(const char *orig_path
, char **resolved_path
)
286 if (*orig_path
!= '/') /* we only accept absolute paths */
288 len
= strlen(orig_path
);
289 *resolved_path
= para_strdup(orig_path
);
290 path
= *resolved_path
;
291 while (len
> 1 && path
[--len
] == '/')
292 path
[len
] = '\0'; /* remove slash at the end */
298 case '/': /* double slash */
301 if (verify_dotfile(path
) < 0)
311 free(*resolved_path
);
315 /** The on-disk layout of a afhi struct. */
317 /** Where the number of seconds is stored. */
318 AFHI_SECONDS_TOTAL_OFFSET
= 0,
319 /** Position of the bitrate. */
320 AFHI_BITRATE_OFFSET
= 4,
321 /** Position of the frequency. */
322 AFHI_FREQUENCY_OFFSET
= 8,
323 /** Location of the audio file header. */
324 AFHI_HEADER_OFFSET_OFFSET
= 12,
325 /* Length of the audio file header. Zero means: No header. */
326 AFHI_HEADER_LEN_OFFSET
= 16,
327 /** The total number of chunks (4 bytes). */
328 CHUNKS_TOTAL_OFFSET
= 20,
329 /** The length of the audio file header (4 bytes). */
330 HEADER_LEN_OFFSET
= 24,
331 /** The start of the audio file header (4 bytes). */
332 HEADER_OFFSET_OFFSET
= 28,
333 /** The seconds part of the chunk time (4 bytes). */
334 CHUNK_TV_TV_SEC_OFFSET
= 32,
335 /** The microseconds part of the chunk time (4 bytes). */
336 CHUNK_TV_TV_USEC_OFFSET
= 36,
337 /** Number of channels is stored here. (1 byte) */
338 AFHI_CHANNELS_OFFSET
= 40,
339 /** EOF timeout in ms. (2 byte) */
340 AFHI_EOF_OFFSET
= 41,
341 /** The tag info position. */
342 AFHI_INFO_STRING_OFFSET
= 43,
343 /** Minimal on-disk size of a valid afhi struct. */
347 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
351 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
354 static void save_afhi(struct afh_info
*afhi
, char *buf
)
358 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
, afhi
->seconds_total
);
359 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
360 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
361 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
362 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
363 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
364 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
365 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
366 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
367 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
368 write_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
, afhi
->chunk_tv
.tv_usec
);
369 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
370 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
373 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
375 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
376 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
377 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
378 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
379 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
380 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
381 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
382 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
383 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
384 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
385 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
);
386 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
387 afhi
->info_string
= para_strdup(buf
+ AFHI_INFO_STRING_OFFSET
);
390 static unsigned sizeof_chunk_table(struct afh_info
*afhi
)
394 return 4 * (afhi
->chunks_total
+ 1);
397 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
401 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
402 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
405 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
409 afhi
->chunk_table
= para_malloc(sizeof_chunk_table(afhi
));
410 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
411 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
415 * Get the row of the audio file table corresponding to the given path.
417 * \param path The full path of the audio file.
418 * \param row Result pointer.
420 * \return The return value of the underlying call to osl_get_row().
422 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
424 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
426 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
430 * Get the row of the audio file table corresponding to the given hash value.
432 * \param hash The hash value of the desired audio file.
433 * \param row resul pointer.
435 * \return The return value of the underlying call to osl_get_row().
437 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
439 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
440 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
444 * Get the osl object holding the audio file selector info of a row.
446 * \param row Pointer to a row in the audio file table.
447 * \param obj Result pointer.
449 * \return The return value of the underlying call to osl_get_object().
451 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
453 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
457 * Get the osl object holding the audio file selector info, given a path.
460 * \param path The full path of the audio file.
461 * \param obj Result pointer.
463 * \return Positive on success, negative on errors.
465 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
468 int ret
= aft_get_row_of_path(path
, &row
);
471 return get_afsi_object_of_row(row
, obj
);
475 * Get the audio file selector info, given a row of the audio file table.
477 * \param row Pointer to a row in the audio file table.
478 * \param afsi Result pointer.
480 * \return Positive on success, negative on errors.
482 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
484 struct osl_object obj
;
485 int ret
= get_afsi_object_of_row(row
, &obj
);
488 return load_afsi(afsi
, &obj
);
492 * Get the audio file selector info, given the path of an audio table.
494 * \param path The full path of the audio file.
495 * \param afsi Result pointer.
497 * \return Positive on success, negative on errors.
499 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
501 struct osl_object obj
;
502 int ret
= get_afsi_object_of_path(path
, &obj
);
505 return load_afsi(afsi
, &obj
);
509 * Get the path of an audio file, given a row of the audio file table.
511 * \param row Pointer to a row in the audio file table.
512 * \param path Result pointer.
514 * The result is a pointer to mmapped data. The caller must not attempt
519 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
521 struct osl_object path_obj
;
522 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
526 *path
= path_obj
.data
;
531 * Get the object containing the hash value of an audio file, given a row.
533 * \param row Pointer to a row of the audio file table.
534 * \param obj Result pointer.
536 * \return The return value of the underlying call to osl_get_object().
538 * \sa get_hash_of_row().
540 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
542 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
546 * Get the hash value of an audio file, given a row of the audio file table.
548 * \param row Pointer to a row of the audio file table.
549 * \param hash Result pointer.
551 * \a hash points to mapped data and must not be freed by the caller.
553 * \return The return value of the underlying call to
554 * get_hash_object_of_aft_row().
556 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
558 struct osl_object obj
;
559 int ret
= get_hash_object_of_aft_row(row
, &obj
);
568 * Get the audio format handler info, given a row of the audio file table.
570 * \param row Pointer to a row of the audio file table.
571 * \param afhi Result pointer.
573 * \return The return value of the underlying call to osl_get_object().
575 * \sa get_chunk_table_of_row().
577 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
579 struct osl_object obj
;
580 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
584 load_afhi(obj
.data
, afhi
);
588 /* returns shmid on success */
589 static int save_afd(struct audio_file_data
*afd
)
591 size_t size
= sizeof(*afd
) + sizeof_chunk_table(&afd
->afhi
);
592 int shmid
, ret
= shm_new(size
);
599 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
602 *(struct audio_file_data
*)shm_afd
= *afd
;
605 save_chunk_table(&afd
->afhi
, buf
);
613 int load_afd(int shmid
, struct audio_file_data
*afd
)
619 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
622 *afd
= *(struct audio_file_data
*)shm_afd
;
625 load_chunk_table(&afd
->afhi
, buf
);
630 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
631 time_t current_time
, enum ls_listing_mode lm
)
635 if (!localtime_r((time_t *)seconds
, &t
))
637 if (lm
== LS_MODE_MBOX
) {
638 if (!strftime(buf
, size
, "%c", &t
))
642 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
643 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
647 if (!strftime(buf
, size
, "%b %e %Y", &t
))
652 /** Compute the number of (decimal) digits of a number. */
653 #define GET_NUM_DIGITS(x, num) { \
654 typeof((x)) _tmp = PARA_ABS(x); \
657 while ((_tmp) > 9) { \
663 static short unsigned get_duration_width(int seconds
)
665 short unsigned width
;
666 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
668 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
669 return 4 + (mins
> 9);
670 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
671 GET_NUM_DIGITS(hours
, &width
);
675 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
677 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
678 short unsigned max_width
;
680 if (!hours
) { /* m:ss or mm:ss */
681 max_width
= opts
->mode
== LS_MODE_LONG
?
682 opts
->widths
.duration_width
: 4;
683 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
684 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
685 max_width
= opts
->mode
== LS_MODE_LONG
?
686 opts
->widths
.duration_width
: 7;
687 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
692 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
694 char *att_text
, *att_lines
;
696 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
698 return para_strdup(att_bitmap
);
699 att_lines
= make_message("%s: %s\n%s: %s",
700 status_item_list
[SI_ATTRIBUTES_BITMAP
], att_bitmap
,
701 status_item_list
[SI_ATTRIBUTES_TXT
], att_text
);
706 static char *make_lyrics_lines(struct afs_info
*afsi
)
710 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
711 return make_message("%s: %u\n%s: %s\n",
712 status_item_list
[SI_LYRICS_ID
], afsi
->lyrics_id
,
713 status_item_list
[SI_LYRICS_NAME
], lyrics_name
?
714 lyrics_name
: "(none)");
717 static char *make_image_lines(struct afs_info
*afsi
)
720 img_get_name_by_id(afsi
->image_id
, &image_name
);
721 return make_message("%s: %u\n%s: %s\n",
722 status_item_list
[SI_IMAGE_ID
], afsi
->image_id
,
723 status_item_list
[SI_IMAGE_NAME
], image_name
?
724 image_name
: "(none)");
727 static char *make_filename_lines(const char *path
, unsigned flags
)
730 const char *basename
;
732 if (!(flags
& LS_FLAG_FULL_PATH
))
733 return make_message("%s: %s\n",
734 status_item_list
[SI_BASENAME
], path
);
735 basename
= para_basename(path
),
736 dirname
= para_dirname(path
);
737 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n",
738 status_item_list
[SI_PATH
], path
,
739 status_item_list
[SI_DIRECTORY
], dirname
? dirname
: "?",
740 status_item_list
[SI_BASENAME
], basename
? basename
: "?");
745 static int print_chunk_table(struct ls_data
*d
, struct para_buffer
*b
)
747 struct osl_object chunk_table_obj
;
748 struct osl_row
*aft_row
;
752 ret
= aft_get_row_of_hash(d
->hash
, &aft_row
);
755 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
756 AFTCOL_CHUNKS
, &chunk_table_obj
);
759 ret
= para_printf(b
, "%s\n"
760 "chunk_time: %lu:%lu\nchunk_offsets: ",
762 (long unsigned) d
->afhi
.chunk_tv
.tv_sec
,
763 (long unsigned) d
->afhi
.chunk_tv
.tv_usec
767 buf
= chunk_table_obj
.data
;
768 for (i
= 0; i
<= d
->afhi
.chunks_total
; i
++) {
769 ret
= para_printf(b
, "%u ", (unsigned) read_u32(buf
+ 4 * i
));
773 ret
= para_printf(b
, "\n");
775 osl_close_disk_object(&chunk_table_obj
);
779 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
780 struct para_buffer
*b
, time_t current_time
)
784 char last_played_time
[30];
785 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
786 char score_buf
[30] = "";
787 struct afs_info
*afsi
= &d
->afsi
;
788 struct afh_info
*afhi
= &d
->afhi
;
789 struct ls_widths
*w
= &opts
->widths
;
790 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
791 char asc_hash
[2 * HASH_SIZE
+ 1];
792 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
794 if (opts
->mode
== LS_MODE_SHORT
) {
795 ret
= para_printf(b
, "%s\n", d
->path
);
798 if (opts
->mode
== LS_MODE_CHUNKS
) {
799 ret
= print_chunk_table(d
, b
);
802 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
803 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
804 sizeof(last_played_time
), current_time
, opts
->mode
);
807 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
809 if (opts
->mode
== LS_MODE_LONG
)
810 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
812 sprintf(score_buf
, "%li ", d
->score
);
815 if (opts
->mode
== LS_MODE_LONG
) {
818 "%s " /* attributes */
820 "%*d " /* image_id */
821 "%*d " /* lyrics_id */
823 "%s " /* audio format */
824 "%*d " /* frequency */
827 "%*d " /* num_played */
828 "%s " /* last_played */
832 w
->amp_width
, afsi
->amp
,
833 w
->image_id_width
, afsi
->image_id
,
834 w
->lyrics_id_width
, afsi
->lyrics_id
,
835 w
->bitrate_width
, afhi
->bitrate
,
836 audio_format_name(afsi
->audio_format_id
),
837 w
->frequency_width
, afhi
->frequency
,
840 w
->num_played_width
, afsi
->num_played
,
846 hash_to_asc(d
->hash
, asc_hash
);
847 att_lines
= make_attribute_lines(att_buf
, afsi
);
848 lyrics_lines
= make_lyrics_lines(afsi
);
849 image_lines
= make_image_lines(afsi
);
850 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
851 if (opts
->mode
== LS_MODE_MBOX
) {
852 const char *bn
= para_basename(d
->path
);
854 "From foo@localhost %s\n"
855 "Received: from\nTo: bar\nFrom: a\n"
863 "%s" /* filename stuff */
864 "%s%s%s%s" /* score */
865 "%s\n" /* attributes */
866 "%s: %s\n" /* hash */
867 "%s" /* image id, image name */
869 "%s: %dkbit/s\n" /* bitrate */
870 "%s: %s\n" /* format */
871 "%s: %dHz\n" /* frequency */
872 "%s: %d\n" /* channels */
873 "%s: %s\n" /* duration */
874 "%s: %lu\n" /* seconds total */
875 "%s: %s\n" /* last played time */
876 "%s: %d\n" /* num_played */
877 "%s: %u\n" /* ampplification */
879 "%s: %lu\n" /* chunk time */
880 "%s: %lu\n", /* num chunks */
882 have_score
? status_item_list
[SI_SCORE
] : "",
883 have_score
? ": " : "",
885 have_score
? "\n" : "",
887 status_item_list
[SI_HASH
], asc_hash
,
890 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
891 status_item_list
[SI_FORMAT
], audio_format_name(afsi
->audio_format_id
),
892 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
893 status_item_list
[SI_CHANNELS
], afhi
->channels
,
894 status_item_list
[SI_DURATION
], duration_buf
,
895 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
896 status_item_list
[SI_LAST_PLAYED
], last_played_time
,
897 status_item_list
[SI_NUM_PLAYED
], afsi
->num_played
,
898 status_item_list
[SI_AMPLIFICATION
], afsi
->amp
,
900 status_item_list
[SI_CHUNK_TIME
], tv2ms(&afhi
->chunk_tv
),
901 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
905 if (opts
->mode
== LS_MODE_MBOX
) {
906 struct osl_object lyrics_def
;
907 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
908 if (lyrics_def
.data
) {
909 ret
= para_printf(b
, "Lyrics:\n~~~~~~~\n%s",
910 (char *)lyrics_def
.data
);
911 osl_close_disk_object(&lyrics_def
);
917 free(filename_lines
);
919 free(afhi
->info_string
);
924 * Write a list of audio-file related status items with empty values.
926 * \param buf Result pointer.
928 * This is used by vss when currently no audio file is open.
930 void make_empty_status_items(char *buf
)
934 "%s: \n" /* dirname */
935 "%s: \n" /* basename */
937 "%s: \n" /* attributes bitmap */
938 "%s: \n" /* attributes txt */
940 "%s: \n" /* image id */
941 "%s: \n" /* image name */
942 "%s: \n" /* lyrics id */
943 "%s: \n" /* lyrics name */
944 "%s: \n" /* bitrate */
945 "%s: \n" /* format */
946 "%s: \n" /* frequency */
947 "%s: \n" /* channels */
948 "%s: \n" /* duration */
949 "%s: \n" /* seconds total */
950 "%s: \n" /* num played */
951 "%s: \n" /* last played */
952 "%s: \n" /* audio file info */
953 "%s: \n" /* taginfo1 */
954 "%s: \n" /* taginfo2 */
955 "%s: \n" /* amplification */
957 status_item_list
[SI_PATH
],
958 status_item_list
[SI_DIRECTORY
],
959 status_item_list
[SI_BASENAME
],
960 status_item_list
[SI_SCORE
],
961 status_item_list
[SI_ATTRIBUTES_BITMAP
],
962 status_item_list
[SI_ATTRIBUTES_TXT
],
963 status_item_list
[SI_HASH
],
964 status_item_list
[SI_IMAGE_ID
],
965 status_item_list
[SI_IMAGE_NAME
],
966 status_item_list
[SI_LYRICS_ID
],
967 status_item_list
[SI_LYRICS_NAME
],
968 status_item_list
[SI_BITRATE
],
969 status_item_list
[SI_FORMAT
],
970 status_item_list
[SI_FREQUENCY
],
971 status_item_list
[SI_CHANNELS
],
972 status_item_list
[SI_DURATION
],
973 status_item_list
[SI_SECONDS_TOTAL
],
974 status_item_list
[SI_NUM_PLAYED
],
975 status_item_list
[SI_LAST_PLAYED
],
976 status_item_list
[SI_AUDIO_FILE_INFO
],
977 status_item_list
[SI_TAGINFO1
],
978 status_item_list
[SI_TAGINFO2
],
979 status_item_list
[SI_AMPLIFICATION
]
983 static void fixup_taginfo(char *begin
, char *end
)
998 /* crap, remove this ASAP. */
999 static int fixup_info_string(char *info_string
)
1001 char *t1
, *t2
, *end
;
1003 if (strncmp(info_string
, "audio_file_info:", 16))
1004 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1005 t1
= strstr(info_string
, "\ntaginfo1:");
1007 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1008 t2
= strstr(info_string
, "\ntaginfo2: ");
1010 return -ERRNO_TO_PARA_ERROR(EINVAL
);
1012 end
= t2
+ strlen(t2
) + 1;
1013 fixup_taginfo(info_string
+ 16, t1
);
1014 fixup_taginfo(t1
+ 10, t2
);
1015 fixup_taginfo(t2
+ 10, end
);
1017 if (t1
- info_string
< 80 && t2
- t1
< 80 && end
- t2
< 80)
1019 if (t1
- info_string
>= 80) {
1020 memmove(info_string
+ 80, t1
, end
- t1
);
1021 t1
= info_string
+ 80;
1022 t2
-= t1
- info_string
- 80;
1023 end
-= t1
- info_string
- 80;
1025 if (t2
- t1
>= 80) {
1026 memmove(t1
+ 80, t2
, end
- t2
);
1027 end
-= t2
- t1
- 80;
1030 if (end
- t2
>= 80) {
1037 static int make_status_items(struct audio_file_data
*afd
,
1038 struct afs_info
*afsi
, char *path
, long score
,
1041 struct ls_data d
= {
1048 struct ls_options opts
= {
1049 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
1050 .mode
= LS_MODE_VERBOSE
,
1052 struct para_buffer pb
= {.max_size
= VERBOSE_LS_OUTPUT_SIZE
- 1};
1053 time_t current_time
;
1056 ret
= fixup_info_string(afd
->afhi
.info_string
);
1058 PARA_WARNING_LOG("ignoring invalid tag info\n");
1059 afd
->afhi
.info_string
[0] = '\0';
1061 PARA_NOTICE_LOG("truncated overlong tag info\n");
1062 time(¤t_time
);
1063 ret
= print_list_item(&d
, &opts
, &pb
, current_time
); /* frees info string */
1064 afd
->afhi
.info_string
= NULL
;
1067 strncpy(afd
->verbose_ls_output
, pb
.buf
, VERBOSE_LS_OUTPUT_SIZE
);
1068 afd
->verbose_ls_output
[VERBOSE_LS_OUTPUT_SIZE
- 1] = '\0';
1075 * Mmap the given audio file and update statistics.
1077 * \param aft_row Determines the audio file to be opened and updated.
1078 * \param score The score of the audio file.
1079 * \param afd Result pointer.
1081 * On success, the numplayed field of the audio file selector info is increased
1082 * and the lastplayed time is set to the current time. Finally, the score of
1083 * the audio file is updated.
1085 * \return Positive shmid on success, negative on errors.
1087 int open_and_update_audio_file(struct osl_row
*aft_row
, long score
,
1088 struct audio_file_data
*afd
)
1090 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
1091 struct osl_object afsi_obj
;
1092 struct afs_info old_afsi
, new_afsi
;
1093 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
1094 struct afsi_change_event_data aced
;
1095 struct osl_object map
, chunk_table_obj
;
1100 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1103 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
1106 ret
= load_afsi(&old_afsi
, &afsi_obj
);
1109 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
1112 afd
->afhi
.chunk_table
= NULL
;
1113 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
1114 AFTCOL_CHUNKS
, &chunk_table_obj
);
1117 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
1118 &map
.size
, &afd
->fd
);
1121 hash_function(map
.data
, map
.size
, file_hash
);
1122 ret
= hash_compare(file_hash
, aft_hash
);
1123 para_munmap(map
.data
, map
.size
);
1125 ret
= -E_HASH_MISMATCH
;
1128 new_afsi
= old_afsi
;
1129 new_afsi
.num_played
++;
1130 new_afsi
.last_played
= time(NULL
);
1131 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
1133 load_chunk_table(&afd
->afhi
, chunk_table_obj
.data
);
1134 ret
= make_status_items(afd
, &old_afsi
, path
, score
, file_hash
);
1137 aced
.aft_row
= aft_row
;
1138 aced
.old_afsi
= &old_afsi
;
1139 afs_event(AFSI_CHANGE
, NULL
, &aced
);
1140 ret
= save_afd(afd
);
1142 free(afd
->afhi
.chunk_table
);
1143 free(afd
->afhi
.info_string
);
1144 osl_close_disk_object(&chunk_table_obj
);
1148 static int ls_audio_format_compare(const void *a
, const void *b
)
1150 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1151 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1154 static int ls_duration_compare(const void *a
, const void *b
)
1156 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1157 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1160 static int ls_bitrate_compare(const void *a
, const void *b
)
1162 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1163 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1166 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1168 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1169 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1172 static int ls_image_id_compare(const void *a
, const void *b
)
1174 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1175 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1178 static int ls_channels_compare(const void *a
, const void *b
)
1180 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1181 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1184 static int ls_frequency_compare(const void *a
, const void *b
)
1186 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1187 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1190 static int ls_num_played_compare(const void *a
, const void *b
)
1192 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1193 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1196 static int ls_last_played_compare(const void *a
, const void *b
)
1198 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1199 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1202 static int ls_score_compare(const void *a
, const void *b
)
1204 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1205 return NUM_COMPARE(d1
->score
, d2
->score
);
1208 static int ls_path_compare(const void *a
, const void *b
)
1210 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1211 return strcmp(d1
->path
, d2
->path
);
1214 static int sort_matching_paths(struct ls_options
*options
)
1216 size_t nmemb
= options
->num_matching_paths
;
1217 size_t size
= sizeof(*options
->data_ptr
);
1218 int (*compar
)(const void *, const void *);
1221 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1222 for (i
= 0; i
< nmemb
; i
++)
1223 options
->data_ptr
[i
] = options
->data
+ i
;
1225 /* In these cases the array is already sorted */
1226 if (options
->sorting
== LS_SORT_BY_PATH
1227 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1228 && (options
->flags
& LS_FLAG_FULL_PATH
))
1230 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1231 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1234 switch (options
->sorting
) {
1235 case LS_SORT_BY_PATH
:
1236 compar
= ls_path_compare
; break;
1237 case LS_SORT_BY_SCORE
:
1238 compar
= ls_score_compare
; break;
1239 case LS_SORT_BY_LAST_PLAYED
:
1240 compar
= ls_last_played_compare
; break;
1241 case LS_SORT_BY_NUM_PLAYED
:
1242 compar
= ls_num_played_compare
; break;
1243 case LS_SORT_BY_FREQUENCY
:
1244 compar
= ls_frequency_compare
; break;
1245 case LS_SORT_BY_CHANNELS
:
1246 compar
= ls_channels_compare
; break;
1247 case LS_SORT_BY_IMAGE_ID
:
1248 compar
= ls_image_id_compare
; break;
1249 case LS_SORT_BY_LYRICS_ID
:
1250 compar
= ls_lyrics_id_compare
; break;
1251 case LS_SORT_BY_BITRATE
:
1252 compar
= ls_bitrate_compare
; break;
1253 case LS_SORT_BY_DURATION
:
1254 compar
= ls_duration_compare
; break;
1255 case LS_SORT_BY_AUDIO_FORMAT
:
1256 compar
= ls_audio_format_compare
; break;
1260 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1264 /* row is either an aft_row or a row of the score table */
1265 /* TODO: Only compute widths if we need them */
1266 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1269 struct ls_options
*options
= ls_opts
;
1271 struct ls_widths
*w
;
1272 unsigned short num_digits
;
1274 struct osl_row
*aft_row
;
1278 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1279 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1284 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1287 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1288 char *p
= strrchr(path
, '/');
1292 if (options
->num_patterns
) {
1293 for (i
= 0; i
< options
->num_patterns
; i
++) {
1294 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1297 if (ret
== FNM_NOMATCH
)
1301 if (i
>= options
->num_patterns
) /* no match */
1304 tmp
= options
->num_matching_paths
++;
1305 if (options
->num_matching_paths
> options
->array_size
) {
1306 options
->array_size
++;
1307 options
->array_size
*= 2;
1308 options
->data
= para_realloc(options
->data
, options
->array_size
1309 * sizeof(*options
->data
));
1311 d
= options
->data
+ tmp
;
1312 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1315 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1319 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1322 w
= &options
->widths
;
1323 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1324 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1325 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1326 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1327 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1328 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1329 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1330 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1331 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1332 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1333 /* get the number of chars to print this amount of time */
1334 num_digits
= get_duration_width(d
->afhi
.seconds_total
);
1335 w
->duration_width
= PARA_MAX(w
->duration_width
, num_digits
);
1336 GET_NUM_DIGITS(d
->afsi
.amp
, &num_digits
);
1337 w
->amp_width
= PARA_MAX(w
->amp_width
, num_digits
);
1338 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1339 GET_NUM_DIGITS(score
, &num_digits
);
1340 num_digits
++; /* add one for the sign (space or "-") */
1341 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1346 free(d
->afhi
.info_string
);
1350 static void com_ls_callback(int fd
, const struct osl_object
*query
)
1352 struct ls_options
*opts
= query
->data
;
1353 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1354 struct para_buffer b
= {.max_size
= SHMMAX
,
1355 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1357 time_t current_time
;
1360 if (opts
->num_patterns
) {
1361 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1362 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1363 opts
->patterns
[i
] = p
;
1367 opts
->patterns
= NULL
;
1368 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1369 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1371 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1375 if (!opts
->num_matching_paths
)
1377 ret
= sort_matching_paths(opts
);
1380 time(¤t_time
);
1381 if (opts
->flags
& LS_FLAG_REVERSE
)
1382 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1383 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1388 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1389 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1395 pass_buffer_as_shm(b
.buf
, b
.offset
, &fd
);
1398 free(opts
->data_ptr
);
1399 free(opts
->patterns
);
1403 * TODO: flags -h (sort by hash)
1405 int com_ls(int fd
, int argc
, char * const * const argv
)
1409 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1410 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1411 struct ls_options opts
= {.patterns
= NULL
};
1412 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)};
1414 for (i
= 1; i
< argc
; i
++) {
1415 const char *arg
= argv
[i
];
1418 if (!strcmp(arg
, "--")) {
1422 if (!strncmp(arg
, "-l", 2)) {
1424 mode
= LS_MODE_LONG
;
1428 return -E_AFT_SYNTAX
;
1429 switch(*(arg
+ 2)) {
1431 mode
= LS_MODE_SHORT
;
1434 mode
= LS_MODE_LONG
;
1437 mode
= LS_MODE_VERBOSE
;
1440 mode
= LS_MODE_MBOX
;
1443 mode
= LS_MODE_CHUNKS
;
1446 return -E_AFT_SYNTAX
;
1449 if (!strcmp(arg
, "-p")) {
1450 flags
|= LS_FLAG_FULL_PATH
;
1453 if (!strcmp(arg
, "-a")) {
1454 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1457 if (!strcmp(arg
, "-r")) {
1458 flags
|= LS_FLAG_REVERSE
;
1461 if (!strncmp(arg
, "-s", 2)) {
1462 if (!*(arg
+ 2) || *(arg
+ 3))
1463 return -E_AFT_SYNTAX
;
1464 switch(*(arg
+ 2)) {
1466 sort
= LS_SORT_BY_PATH
;
1468 case 's': /* -ss implies -a */
1469 sort
= LS_SORT_BY_SCORE
;
1470 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1473 sort
= LS_SORT_BY_LAST_PLAYED
;
1476 sort
= LS_SORT_BY_NUM_PLAYED
;
1479 sort
= LS_SORT_BY_FREQUENCY
;
1482 sort
= LS_SORT_BY_CHANNELS
;
1485 sort
= LS_SORT_BY_IMAGE_ID
;
1488 sort
= LS_SORT_BY_LYRICS_ID
;
1491 sort
= LS_SORT_BY_BITRATE
;
1494 sort
= LS_SORT_BY_DURATION
;
1497 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1500 return -E_AFT_SYNTAX
;
1503 return -E_AFT_SYNTAX
;
1506 opts
.sorting
= sort
;
1508 opts
.num_patterns
= argc
- i
;
1509 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1510 argv
+ i
, com_ls_callback
, send_result
, &fd
);
1515 * Call the given function for each file in the audio file table.
1517 * \param private_data An arbitrary data pointer, passed to \a func.
1518 * \param func The custom function to be called.
1520 * \return The return value of the underlying call to osl_rbtree_loop().
1522 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1524 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1528 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1530 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1531 struct osl_row
*row
;
1533 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1537 /** The format of the data stored by save_audio_file_data(). */
1538 enum com_add_buffer_offsets
{
1539 /** afhi (if present) starts here. */
1540 CAB_AFHI_OFFSET_POS
= 0,
1541 /** Start of the chunk table (if present). */
1542 CAB_CHUNKS_OFFSET_POS
= 2,
1543 /** Audio format id. */
1544 CAB_AUDIO_FORMAT_OFFSET
= 4,
1545 /** Flags given to the add command. */
1546 CAB_FLAGS_OFFSET
= 5,
1547 /** The hash of the audio file being added. */
1548 CAB_HASH_OFFSET
= 9,
1549 /** Start of the path of the audio file. */
1550 CAB_PATH_OFFSET
= (CAB_HASH_OFFSET
+ HASH_SIZE
),
1554 * Store the given data to a single buffer. Doesn't need the audio file selector
1555 * info struct as the server knows it as well.
1557 * It's OK to call this with afhi == NULL. In this case, the audio format
1558 * handler info won't be stored in the buffer.
1560 static void save_add_callback_buffer(HASH_TYPE
*hash
, const char *path
,
1561 struct afh_info
*afhi
, uint32_t flags
,
1562 uint8_t audio_format_num
, struct osl_object
*obj
)
1564 size_t path_len
= strlen(path
) + 1;
1565 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1566 size_t size
= CAB_PATH_OFFSET
+ path_len
+ afhi_size
1567 + sizeof_chunk_table(afhi
);
1568 char *buf
= para_malloc(size
);
1571 write_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1572 write_u32(buf
+ CAB_FLAGS_OFFSET
, flags
);
1574 memcpy(buf
+ CAB_HASH_OFFSET
, hash
, HASH_SIZE
);
1575 strcpy(buf
+ CAB_PATH_OFFSET
, path
);
1577 pos
= CAB_PATH_OFFSET
+ path_len
;
1578 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1579 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1580 pos
+ afhi_size
- 1);
1581 write_u16(buf
+ CAB_AFHI_OFFSET_POS
, pos
);
1582 save_afhi(afhi
, buf
+ pos
);
1585 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1586 write_u16(buf
+ CAB_CHUNKS_OFFSET_POS
, pos
);
1588 save_chunk_table(afhi
, buf
+ pos
);
1589 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1596 Overview of the add command.
1598 Input: What was passed to the callback by the command handler.
1600 HS: Hash sister. Whether an audio file with identical hash
1601 already exists in the osl database.
1603 PB: Path brother. Whether a file with the given path exists
1606 F: Force flag given. Whether add was called with -f.
1608 output: Action performed by the callback.
1610 AFHI: Whether afhi and chunk table are computed and sent.
1611 ACTION: Table modifications to be done by the callback.
1613 +----+----+---+------+---------------------------------------------------+
1614 | HS | PB | F | AFHI | ACTION
1615 +----+----+---+------+---------------------------------------------------+
1616 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1617 | | update path, keep afsi
1618 +----+----+---+------+---------------------------------------------------+
1619 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1620 | | otherwise: remove PB, HS: update path, keep afhi,
1622 +----+----+---+------+---------------------------------------------------+
1623 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1625 +----+----+---+------+---------------------------------------------------+
1626 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1627 +----+----+---+------+---------------------------------------------------+
1628 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1629 | | (force has no effect)
1630 +----+----+---+------+---------------------------------------------------+
1631 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1632 +----+----+---+------+---------------------------------------------------+
1633 | N | N | Y | Y | (new file) create new entry (force has no effect)
1634 +----+----+---+------+---------------------------------------------------+
1635 | N | N | N | Y | (new file) create new entry
1636 +----+----+---+------+---------------------------------------------------+
1640 afhi <=> force or no HS
1645 /** Flags passed to the add command. */
1646 enum com_add_flags
{
1647 /** Skip paths that exist already. */
1649 /** Force adding. */
1651 /** Print what is being done. */
1652 ADD_FLAG_VERBOSE
= 4,
1653 /** Try to add files with unknown suffixes. */
1657 static void com_add_callback(int fd
, const struct osl_object
*query
)
1659 char *buf
= query
->data
, *path
;
1660 struct osl_row
*pb
, *aft_row
;
1662 struct osl_object objs
[NUM_AFT_COLUMNS
];
1664 char asc
[2 * HASH_SIZE
+ 1];
1666 char afsi_buf
[AFSI_SIZE
];
1667 uint32_t flags
= read_u32(buf
+ CAB_FLAGS_OFFSET
);
1668 struct afs_info default_afsi
= {.last_played
= 0};
1669 struct para_buffer msg
= {.max_size
= SHMMAX
,
1670 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1671 uint16_t afhi_offset
, chunks_offset
;
1673 hash
= (HASH_TYPE
*)buf
+ CAB_HASH_OFFSET
;
1674 hash_to_asc(hash
, asc
);;
1675 objs
[AFTCOL_HASH
].data
= buf
+ CAB_HASH_OFFSET
;
1676 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1678 path
= buf
+ CAB_PATH_OFFSET
;
1679 objs
[AFTCOL_PATH
].data
= path
;
1680 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1682 PARA_INFO_LOG("request to add %s\n", path
);
1683 hs
= find_hash_sister(hash
);
1684 ret
= aft_get_row_of_path(path
, &pb
);
1685 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1687 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1688 if (flags
& ADD_FLAG_VERBOSE
)
1689 ret
= para_printf(&msg
, "ignoring duplicate\n");
1694 if (hs
&& hs
!= pb
) {
1695 struct osl_object obj
;
1696 if (pb
) { /* hs trumps pb, remove pb */
1697 if (flags
& ADD_FLAG_VERBOSE
) {
1698 ret
= para_printf(&msg
, "removing path brother\n");
1702 ret
= osl_del_row(audio_file_table
, pb
);
1707 /* file rename, update hs' path */
1708 if (flags
& ADD_FLAG_VERBOSE
) {
1709 ret
= osl_get_object(audio_file_table
, hs
,
1713 ret
= para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1717 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1718 &objs
[AFTCOL_PATH
]);
1721 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1722 if (!(flags
& ADD_FLAG_FORCE
))
1725 /* no hs or force mode, child must have sent afhi */
1726 afhi_offset
= read_u16(buf
+ CAB_AFHI_OFFSET_POS
);
1727 chunks_offset
= read_u16(buf
+ CAB_CHUNKS_OFFSET_POS
);
1729 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1730 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1732 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1734 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1735 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1736 if (pb
&& !hs
) { /* update pb's hash */
1737 char old_asc
[2 * HASH_SIZE
+ 1];
1738 HASH_TYPE
*old_hash
;
1739 ret
= get_hash_of_row(pb
, &old_hash
);
1742 hash_to_asc(old_hash
, old_asc
);
1743 if (flags
& ADD_FLAG_VERBOSE
) {
1744 ret
= para_printf(&msg
, "file change: %s -> %s\n",
1749 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1750 &objs
[AFTCOL_HASH
]);
1754 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1755 struct osl_row
*row
= pb
? pb
: hs
;
1756 /* update afhi and chunk_table */
1757 if (flags
& ADD_FLAG_VERBOSE
) {
1758 ret
= para_printf(&msg
, "updating afhi and chunk table\n");
1762 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1763 &objs
[AFTCOL_AFHI
]);
1766 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1767 &objs
[AFTCOL_CHUNKS
]);
1770 afs_event(AFHI_CHANGE
, &msg
, row
);
1773 /* new entry, use default afsi */
1774 if (flags
& ADD_FLAG_VERBOSE
) {
1775 ret
= para_printf(&msg
, "new file\n");
1779 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1780 default_afsi
.audio_format_id
= read_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
);
1782 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1783 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1784 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1785 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1786 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1789 para_printf(&msg
, "%s\n", para_strerror(-ret
));
1791 pass_buffer_as_shm(msg
.buf
, msg
.offset
, &fd
);
1795 /** Used by com_add(). */
1796 struct private_add_data
{
1797 /** The socket file descriptor. */
1799 /** The given add flags. */
1803 static void path_brother_callback(int fd
, const struct osl_object
*query
)
1805 char *path
= query
->data
;
1806 struct osl_row
*path_brother
;
1807 int ret
= aft_get_row_of_path(path
, &path_brother
);
1810 pass_buffer_as_shm((char *)&path_brother
, sizeof(path_brother
), &fd
);
1813 static void hash_sister_callback(int fd
, const struct osl_object
*query
)
1815 HASH_TYPE
*hash
= query
->data
;
1816 struct osl_row
*hash_sister
;
1818 hash_sister
= find_hash_sister(hash
);
1821 pass_buffer_as_shm((char *)&hash_sister
, sizeof(hash_sister
), &fd
);
1824 static int get_row_pointer_from_result(struct osl_object
*result
, void *private)
1826 struct osl_row
**row
= private;
1827 *row
= result
->data
;
1831 static int add_one_audio_file(const char *path
, void *private_data
)
1833 int ret
, send_ret
= 1, fd
;
1834 uint8_t format_num
= -1;
1835 struct private_add_data
*pad
= private_data
;
1836 struct afh_info afhi
, *afhi_ptr
= NULL
;
1837 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1838 struct osl_object map
, obj
= {.data
= NULL
}, query
;
1839 HASH_TYPE hash
[HASH_SIZE
];
1841 ret
= guess_audio_format(path
);
1842 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1844 query
.data
= (char *)path
;
1845 query
.size
= strlen(path
) + 1;
1846 ret
= send_callback_request(path_brother_callback
, &query
,
1847 get_row_pointer_from_result
, &pb
);
1848 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1851 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1852 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1853 send_ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1856 /* We still want to add this file. Compute its hash. */
1857 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, &fd
);
1860 hash_function(map
.data
, map
.size
, hash
);
1862 /* Check whether the database contains a file with the same hash. */
1864 query
.size
= HASH_SIZE
;
1865 ret
= send_callback_request(hash_sister_callback
, &query
,
1866 get_row_pointer_from_result
, &hs
);
1869 /* Return success if we already know this file. */
1871 if (pb
&& hs
&& hs
== pb
&& !(pad
->flags
& ADD_FLAG_FORCE
)) {
1872 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1873 send_ret
= send_va_buffer(pad
->fd
,
1874 "%s exists, not forcing update\n", path
);
1878 * We won't recalculate the audio format info and the chunk table if
1879 * there is a hash sister and FORCE was not given.
1881 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1882 ret
= compute_afhi(path
, map
.data
, map
.size
, fd
, &afhi
);
1888 munmap(map
.data
, map
.size
);
1890 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1891 send_ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1895 save_add_callback_buffer(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1896 /* Ask afs to consider this entry for adding. */
1897 ret
= send_callback_request(com_add_callback
, &obj
, send_result
, &pad
->fd
);
1902 munmap(map
.data
, map
.size
);
1904 if (ret
< 0 && send_ret
>= 0)
1905 send_ret
= send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1906 para_strerror(-ret
));
1909 free(afhi_ptr
->chunk_table
);
1910 free(afhi_ptr
->info_string
);
1912 /* Stop adding files only on send errors. */
1916 int com_add(int fd
, int argc
, char * const * const argv
)
1919 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1920 struct stat statbuf
;
1922 for (i
= 1; i
< argc
; i
++) {
1923 const char *arg
= argv
[i
];
1926 if (!strcmp(arg
, "--")) {
1930 if (!strcmp(arg
, "-a")) {
1931 pad
.flags
|= ADD_FLAG_ALL
;
1934 if (!strcmp(arg
, "-l")) {
1935 pad
.flags
|= ADD_FLAG_LAZY
;
1938 if (!strcmp(arg
, "-f")) {
1939 pad
.flags
|= ADD_FLAG_FORCE
;
1942 if (!strcmp(arg
, "-v")) {
1943 pad
.flags
|= ADD_FLAG_VERBOSE
;
1948 return -E_AFT_SYNTAX
;
1949 for (; i
< argc
; i
++) {
1951 ret
= verify_path(argv
[i
], &path
);
1953 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
],
1954 para_strerror(-ret
));
1959 ret
= stat(path
, &statbuf
);
1961 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1968 if (S_ISDIR(statbuf
.st_mode
))
1969 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1972 ret
= add_one_audio_file(path
, &pad
);
1974 send_va_buffer(fd
, "%s: %s\n", path
, para_strerror(-ret
));
1985 * Flags used by the touch command.
1990 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1991 TOUCH_FLAG_FNM_PATHNAME
= 1,
1992 /** Activates verbose mode. */
1993 TOUCH_FLAG_VERBOSE
= 2
1996 /** Options used by com_touch(). */
1997 struct com_touch_options
{
1998 /** New num_played value. */
2000 /** New last played count. */
2001 int64_t last_played
;
2002 /** New lyrics id. */
2004 /** New image id. */
2006 /** New amplification value. */
2008 /** Command line flags (see \ref touch_flags). */
2012 /** Data passed to the action handler of com_touch(). */
2013 struct touch_action_data
{
2014 /** Command line options (see \ref com_touch_options). */
2015 struct com_touch_options
*cto
;
2016 /** Message buffer. */
2017 struct para_buffer pb
;
2018 /** How many audio files matched the given pattern. */
2019 unsigned num_matches
;
2022 static int touch_audio_file(__a_unused
struct osl_table
*table
,
2023 struct osl_row
*row
, const char *name
, void *data
)
2025 struct touch_action_data
*tad
= data
;
2026 struct osl_object obj
;
2027 struct afs_info old_afsi
, new_afsi
;
2028 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
2029 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0 && tad
->cto
->amp
< 0;
2030 struct afsi_change_event_data aced
;
2032 ret
= get_afsi_object_of_row(row
, &obj
);
2034 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2035 ret
= load_afsi(&old_afsi
, &obj
);
2037 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2038 new_afsi
= old_afsi
;
2040 new_afsi
.num_played
++;
2041 new_afsi
.last_played
= time(NULL
);
2042 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
2043 ret
= para_printf(&tad
->pb
, "%s: num_played = %u, "
2044 "last_played = now()\n", name
,
2045 new_afsi
.num_played
);
2050 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
2051 ret
= para_printf(&tad
->pb
, "touching %s\n", name
);
2055 if (tad
->cto
->lyrics_id
>= 0)
2056 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
2057 if (tad
->cto
->image_id
>= 0)
2058 new_afsi
.image_id
= tad
->cto
->image_id
;
2059 if (tad
->cto
->num_played
>= 0)
2060 new_afsi
.num_played
= tad
->cto
->num_played
;
2061 if (tad
->cto
->last_played
>= 0)
2062 new_afsi
.last_played
= tad
->cto
->last_played
;
2063 if (tad
->cto
->amp
>= 0)
2064 new_afsi
.amp
= tad
->cto
->amp
;
2067 save_afsi(&new_afsi
, &obj
); /* in-place update */
2069 aced
.old_afsi
= &old_afsi
;
2070 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
2074 static void com_touch_callback(int fd
, const struct osl_object
*query
)
2076 struct touch_action_data tad
= {.cto
= query
->data
,
2079 .private_data
= &fd
,
2080 .max_size_handler
= pass_buffer_as_shm
2084 struct pattern_match_data pmd
= {
2085 .table
= audio_file_table
,
2086 .loop_col_num
= AFTCOL_HASH
,
2087 .match_col_num
= AFTCOL_PATH
,
2088 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
2089 .size
= query
->size
- sizeof(*tad
.cto
)},
2091 .action
= touch_audio_file
2093 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
2094 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2095 ret
= for_each_matching_row(&pmd
);
2097 ret2
= para_printf(&tad
.pb
, "%s\n", para_strerror(-ret
));
2099 if (!tad
.num_matches
)
2100 ret2
= para_printf(&tad
.pb
, "no matches\n");
2101 if (ret2
>= 0 && tad
.pb
.offset
)
2102 pass_buffer_as_shm(tad
.pb
.buf
, tad
.pb
.offset
, &fd
);
2106 int com_touch(int fd
, int argc
, char * const * const argv
)
2108 struct com_touch_options cto
= {
2115 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)};
2119 for (i
= 1; i
< argc
; i
++) {
2120 const char *arg
= argv
[i
];
2123 if (!strcmp(arg
, "--")) {
2127 if (!strncmp(arg
, "-n", 2)) {
2128 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
2133 if (!strncmp(arg
, "-l", 2)) {
2134 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
2139 if (!strncmp(arg
, "-y", 2)) {
2140 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
2145 if (!strncmp(arg
, "-i", 2)) {
2146 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
2151 if (!strncmp(arg
, "-a", 2)) {
2153 ret
= para_atoi32(arg
+ 2, &val
);
2156 if (val
< 0 || val
> 255)
2157 return -ERRNO_TO_PARA_ERROR(EINVAL
);
2161 if (!strcmp(arg
, "-p")) {
2162 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
2165 if (!strcmp(arg
, "-v")) {
2166 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
2169 break; /* non-option starting with dash */
2172 return -E_AFT_SYNTAX
;
2173 ret
= send_option_arg_callback_request(&query
, argc
- i
,
2174 argv
+ i
, com_touch_callback
, send_result
, &fd
);
2176 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2180 /** Flags for com_rm(). */
2183 RM_FLAG_VERBOSE
= 1,
2187 RM_FLAG_FNM_PATHNAME
= 4
2190 /** Data passed to the action handler of com_rm(). */
2191 struct com_rm_action_data
{
2192 /** Command line flags ((see \ref rm_flags). */
2194 /** Message buffer. */
2195 struct para_buffer pb
;
2196 /** Number of audio files removed. */
2197 unsigned num_removed
;
2200 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2201 struct osl_row
*row
, const char *name
, void *data
)
2203 struct com_rm_action_data
*crd
= data
;
2206 if (crd
->flags
& RM_FLAG_VERBOSE
) {
2207 ret
= para_printf(&crd
->pb
, "removing %s\n", name
);
2211 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2212 ret
= osl_del_row(audio_file_table
, row
);
2214 para_printf(&crd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2220 static void com_rm_callback(int fd
, const struct osl_object
*query
)
2222 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
,
2225 .private_data
= &fd
,
2226 .max_size_handler
= pass_buffer_as_shm
2230 struct pattern_match_data pmd
= {
2231 .table
= audio_file_table
,
2232 .loop_col_num
= AFTCOL_HASH
,
2233 .match_col_num
= AFTCOL_PATH
,
2234 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2235 .size
= query
->size
- sizeof(uint32_t)},
2237 .action
= remove_audio_file
2239 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2240 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2241 ret
= for_each_matching_row(&pmd
);
2243 para_printf(&crd
.pb
, "%s\n", para_strerror(-ret
));
2246 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2247 ret
= para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2249 if (crd
.flags
& RM_FLAG_VERBOSE
)
2250 ret
= para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2252 if (ret
>= 0 && crd
.pb
.offset
)
2253 pass_buffer_as_shm(crd
.pb
.buf
, crd
.pb
.offset
, &fd
);
2257 /* TODO options: -r (recursive) */
2258 int com_rm(int fd
, int argc
, char * const * const argv
)
2261 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)};
2264 for (i
= 1; i
< argc
; i
++) {
2265 const char *arg
= argv
[i
];
2268 if (!strcmp(arg
, "--")) {
2272 if (!strcmp(arg
, "-f")) {
2273 flags
|= RM_FLAG_FORCE
;
2276 if (!strcmp(arg
, "-p")) {
2277 flags
|= RM_FLAG_FNM_PATHNAME
;
2280 if (!strcmp(arg
, "-v")) {
2281 flags
|= RM_FLAG_VERBOSE
;
2287 return -E_AFT_SYNTAX
;
2288 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2289 com_rm_callback
, send_result
, &fd
);
2291 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2296 * Flags used by the cpsi command.
2301 /** Whether the lyrics id should be copied. */
2302 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2303 /** Whether the image id should be copied. */
2304 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2305 /** Whether the lastplayed time should be copied. */
2306 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2307 /** Whether the numplayed count should be copied. */
2308 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2309 /** Whether the attributes should be copied. */
2310 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2311 /** Activates verbose mode. */
2312 CPSI_FLAG_VERBOSE
= 32,
2315 /** Data passed to the action handler of com_cpsi(). */
2316 struct cpsi_action_data
{
2317 /** command line flags (see \ref cpsi_flags). */
2319 /** Number of audio files changed. */
2320 unsigned num_copied
;
2321 /** Message buffer. */
2322 struct para_buffer pb
;
2323 /** Values are copied from here. */
2324 struct afs_info source_afsi
;
2327 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2328 struct osl_row
*row
, const char *name
, void *data
)
2330 struct cpsi_action_data
*cad
= data
;
2331 struct osl_object target_afsi_obj
;
2333 struct afs_info old_afsi
, target_afsi
;
2334 struct afsi_change_event_data aced
;
2336 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2339 load_afsi(&target_afsi
, &target_afsi_obj
);
2340 old_afsi
= target_afsi
;
2341 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2342 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2343 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2344 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2345 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2346 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2347 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2348 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2349 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2350 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2351 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2353 if (cad
->flags
& CPSI_FLAG_VERBOSE
) {
2354 ret
= para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2359 aced
.old_afsi
= &old_afsi
;
2360 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2364 static void com_cpsi_callback(int fd
, const struct osl_object
*query
)
2366 struct cpsi_action_data cad
= {
2367 .flags
= *(unsigned *)query
->data
,
2370 .private_data
= &fd
,
2371 .max_size_handler
= pass_buffer_as_shm
2375 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2376 struct pattern_match_data pmd
= {
2377 .table
= audio_file_table
,
2378 .loop_col_num
= AFTCOL_HASH
,
2379 .match_col_num
= AFTCOL_PATH
,
2380 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2381 .size
= query
->size
- sizeof(cad
.flags
)
2382 - strlen(source_path
) - 1},
2384 .action
= copy_selector_info
2387 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2390 ret
= for_each_matching_row(&pmd
);
2393 para_printf(&cad
.pb
, "%s\n", para_strerror(-ret
));
2394 else if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2396 para_printf(&cad
.pb
, "copied requested afsi from %s "
2397 "to %u files\n", source_path
, cad
.num_copied
);
2399 para_printf(&cad
.pb
, "nothing copied\n");
2402 pass_buffer_as_shm(cad
.pb
.buf
, cad
.pb
.offset
, &fd
);
2406 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2410 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
2412 for (i
= 1; i
< argc
; i
++) {
2413 const char *arg
= argv
[i
];
2416 if (!strcmp(arg
, "--")) {
2420 if (!strcmp(arg
, "-y")) {
2421 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2424 if (!strcmp(arg
, "-i")) {
2425 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2428 if (!strcmp(arg
, "-l")) {
2429 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2432 if (!strcmp(arg
, "-n")) {
2433 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2436 if (!strcmp(arg
, "-a")) {
2437 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2440 if (!strcmp(arg
, "-v")) {
2441 flags
|= CPSI_FLAG_VERBOSE
;
2446 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2447 return -E_AFT_SYNTAX
;
2448 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2449 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2450 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2451 com_cpsi_callback
, send_result
, &fd
);
2453 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2457 /* TODO: optionally fix problems by removing offending rows */
2458 static int check_audio_file(struct osl_row
*row
, void *data
)
2461 struct para_buffer
*pb
= data
;
2462 struct stat statbuf
;
2463 int ret
= get_audio_file_path_of_row(row
, &path
);
2464 struct afs_info afsi
;
2468 return para_printf(pb
, "%s\n", para_strerror(-ret
));
2469 if (stat(path
, &statbuf
) < 0) {
2470 ret
= para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2474 if (!S_ISREG(statbuf
.st_mode
)) {
2475 ret
= para_printf(pb
, "%s: not a regular file\n", path
);
2480 ret
= get_afsi_of_row(row
, &afsi
);
2482 return para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
2483 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2485 ret
= para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2486 para_strerror(-ret
));
2490 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2492 ret
= para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2493 para_strerror(-ret
));
2498 * Check the audio file table for inconsistencies.
2500 * \param fd The afs socket.
2501 * \param query Unused.
2503 * This function always succeeds.
2507 void aft_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
2509 struct para_buffer pb
= {
2511 .private_data
= &fd
,
2512 .max_size_handler
= pass_buffer_as_shm
2514 int ret
= para_printf(&pb
, "checking audio file table...\n");
2518 audio_file_loop(&pb
, check_audio_file
);
2520 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
2525 * Close the audio file table.
2527 * \param flags Usual flags that are passed to osl_close_table().
2529 * \sa osl_close_table().
2531 static void aft_close(void)
2533 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2534 audio_file_table
= NULL
;
2538 * Open the audio file table.
2540 * \param dir The database directory.
2544 * \sa osl_open_table().
2546 static int aft_open(const char *dir
)
2550 audio_file_table_desc
.dir
= dir
;
2551 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2554 osl_get_num_rows(audio_file_table
, &num
);
2555 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2558 PARA_INFO_LOG("failed to open audio file table\n");
2559 audio_file_table
= NULL
;
2560 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2565 static int aft_create(const char *dir
)
2567 audio_file_table_desc
.dir
= dir
;
2568 return osl_create_table(&audio_file_table_desc
);
2571 static int clear_attribute(struct osl_row
*row
, void *data
)
2573 struct rmatt_event_data
*red
= data
;
2574 struct afs_info afsi
;
2575 struct osl_object obj
;
2576 int ret
= get_afsi_object_of_row(row
, &obj
);
2577 uint64_t mask
= ~(1ULL << red
->bitnum
);
2581 ret
= load_afsi(&afsi
, &obj
);
2584 afsi
.attributes
&= mask
;
2585 save_afsi(&afsi
, &obj
);
2589 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2595 case ATTRIBUTE_REMOVE
: {
2596 const struct rmatt_event_data
*red
= data
;
2597 ret
= para_printf(pb
, "clearing attribute %s (bit %u) from all "
2598 "entries in the audio file table\n", red
->name
,
2602 return audio_file_loop(data
, clear_attribute
);
2609 void aft_init(struct afs_table
*t
)
2611 t
->name
= audio_file_table_desc
.name
;
2613 t
->close
= aft_close
;
2614 t
->create
= aft_create
;
2615 t
->event_handler
= aft_event_handler
;