2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
9 #include <dirent.h> /* readdir() */
22 static struct osl_table
*audio_file_table
;
24 /** The different sorting methods of the ls command. */
25 enum ls_sorting_method
{
31 LS_SORT_BY_LAST_PLAYED
,
33 LS_SORT_BY_NUM_PLAYED
,
47 LS_SORT_BY_AUDIO_FORMAT
,
52 /** The different listing modes of the ls command. */
53 enum ls_listing_mode
{
54 /** Default listing mode. */
64 /** The flags accepted by the ls command. */
67 LS_FLAG_FULL_PATH
= 1,
69 LS_FLAG_ADMISSIBLE_ONLY
= 2,
75 * The size of the individual output fields of the ls command.
77 * These depend on the actual content being listed. If, for instance only files
78 * with duration less than an hour are being listed, then the duration with is
79 * made smaller because then the duration is listed as mm:ss rather than
83 /** size of the score field. */
84 unsigned short score_width
;
85 /** size of the image id field. */
86 unsigned short image_id_width
;
87 /** size of the lyrics id field. */
88 unsigned short lyrics_id_width
;
89 /** size of the bitrate field. */
90 unsigned short bitrate_width
;
91 /** size of the frequency field. */
92 unsigned short frequency_width
;
93 /** size of the duration field. */
94 unsigned short duration_width
;
95 /** size of the num played field. */
96 unsigned short num_played_width
;
99 /** Data passed from the ls command handler to its callback function. */
101 /** The given command line flags. */
103 /** The sorting method given at the command line. */
104 enum ls_sorting_method sorting
;
105 /** The given listing mode (short, long, verbose, mbox). */
106 enum ls_listing_mode mode
;
107 /** The arguments passed to the ls command. */
109 /** Number of non-option arguments. */
111 /** Used for long listing mode to align the output fields. */
112 struct ls_widths widths
;
113 /** Size of the \a data array. */
115 /** Number of used entries in the data array. */
116 uint32_t num_matching_paths
;
117 /** Array of matching entries. */
118 struct ls_data
*data
;
119 /** Used to sort the array. */
120 struct ls_data
**data_ptr
;
124 * Describes the layout of the mmapped-afs info struct.
126 * \sa struct afs_info.
129 /** Where .last_played is stored. */
130 AFSI_LAST_PLAYED_OFFSET
= 0,
131 /** Storage position of the attributes bitmap. */
132 AFSI_ATTRIBUTES_OFFSET
= 8,
133 /** Storage position of the .num_played field. */
134 AFSI_NUM_PLAYED_OFFSET
= 16,
135 /** Storage position of the .image_id field. */
136 AFSI_IMAGE_ID_OFFSET
= 20,
137 /** Storage position of the .lyrics_id field. */
138 AFSI_LYRICS_ID_OFFSET
= 24,
139 /** Storage position of the .audio_format_id field. */
140 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
141 /** 3 bytes reserved space for future usage. */
142 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 29,
143 /** On-disk storage space needed. */
148 * Convert a struct afs_info to an osl object.
150 * \param afsi Pointer to the audio file info to be converted.
151 * \param obj Result pointer.
155 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
157 char *buf
= obj
->data
;
159 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
160 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
161 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
162 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
163 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
164 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
165 afsi
->audio_format_id
);
166 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 3);
170 * Get the audio file selector info struct stored in an osl object.
172 * \param afsi Points to the audio_file info structure to be filled in.
173 * \param obj The osl object holding the data.
175 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
179 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
181 char *buf
= obj
->data
;
182 if (obj
->size
< AFSI_SIZE
)
184 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
185 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
186 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
187 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
188 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
189 afsi
->audio_format_id
= read_u8(buf
+
190 AFSI_AUDIO_FORMAT_ID_OFFSET
);
194 /** The columns of the audio file table. */
195 enum audio_file_table_columns
{
196 /** The hash on the content of the audio file. */
198 /** The full path in the filesystem. */
200 /** The audio file selector info. */
202 /** The audio format handler info. */
204 /** The chunk table info and the chunk table of the audio file. */
206 /** The number of columns of this table. */
210 static struct osl_column_description aft_cols
[] = {
212 .storage_type
= OSL_MAPPED_STORAGE
,
213 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
215 .compare_function
= osl_hash_compare
,
216 .data_size
= HASH_SIZE
219 .storage_type
= OSL_MAPPED_STORAGE
,
220 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
222 .compare_function
= string_compare
,
225 .storage_type
= OSL_MAPPED_STORAGE
,
226 .storage_flags
= OSL_FIXED_SIZE
,
228 .data_size
= AFSI_SIZE
231 .storage_type
= OSL_MAPPED_STORAGE
,
235 .storage_type
= OSL_DISK_STORAGE
,
240 static struct osl_table_description audio_file_table_desc
= {
241 .name
= "audio_files",
242 .num_columns
= NUM_AFT_COLUMNS
,
243 .flags
= OSL_LARGE_TABLE
,
244 .column_descriptions
= aft_cols
247 /* We don't want * dot or dot-dot anywhere. */
248 static int verify_dotfile(const char *rest
)
251 * The first character was '.', but that has already been discarded, we
255 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
257 case '.': /* path start with /foo/.. */
258 if (rest
[1] == '\0' || rest
[1] == '/')
259 return -1; /* /foo/.. or /foo/../bar are not ok */
260 /* /foo/..bar is ok */
266 * We fundamentally don't like some paths: We don't want double slashes or
267 * slashes at the end that can make pathnames ambiguous.
269 static int verify_path(const char *orig_path
, char **resolved_path
)
275 if (*orig_path
!= '/') /* we only accept absolute paths */
277 len
= strlen(orig_path
);
278 *resolved_path
= para_strdup(orig_path
);
279 path
= *resolved_path
;
280 while (len
> 1 && path
[--len
] == '/')
281 path
[len
] = '\0'; /* remove slash at the end */
287 case '/': /* double slash */
290 if (verify_dotfile(path
) < 0)
300 free(*resolved_path
);
304 /** The on-disk layout of a afhi struct. */
306 /** Where the number of seconds is stored. */
307 AFHI_SECONDS_TOTAL_OFFSET
= 0,
308 /** Position of the bitrate. */
309 AFHI_BITRATE_OFFSET
= 4,
310 /** Position of the frequency. */
311 AFHI_FREQUENCY_OFFSET
= 8,
312 /** Location of the audio file header. */
313 AFHI_HEADER_OFFSET_OFFSET
= 12,
314 /* Length of the audio file header. Zero means: No header. */
315 AFHI_HEADER_LEN_OFFSET
= 16,
316 /** Number of channels is stored here. */
317 AFHI_CHANNELS_OFFSET
= 20,
318 /** EOF timeout in ms. */
319 AFHI_EOF_OFFSET
= 21,
320 /** The tag info position. */
321 AFHI_INFO_STRING_OFFSET
= 23,
322 /** Minimal on-disk size of a valid afhi struct. */
326 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
330 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
333 static void save_afhi(struct afh_info
*afhi
, char *buf
)
337 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
338 afhi
->seconds_total
);
339 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
340 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
341 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
342 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
343 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
344 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
345 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
346 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
349 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
351 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
352 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
353 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
354 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
355 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
356 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
357 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
358 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
361 //#define SIZEOF_CHUNK_TABLE(afhi) (((afhi)->chunks_total + 1) * sizeof(uint32_t))
363 static unsigned sizeof_chunk_info_buf(struct afh_info
*afhi
)
367 return 4 * (afhi
->chunks_total
+ 1) + 20;
371 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
372 enum chunk_info_offsets
{
373 /** The total number of chunks (4 bytes). */
374 CHUNKS_TOTAL_OFFSET
= 0,
375 /** The length of the audio file header (4 bytes). */
376 HEADER_LEN_OFFSET
= 4,
377 /** The start of the audio file header (4 bytes). */
378 HEADER_OFFSET_OFFSET
= 8,
379 /** The seconds part of the chunk time (4 bytes). */
380 CHUNK_TV_TV_SEC_OFFSET
= 12,
381 /** The microseconds part of the chunk time (4 bytes). */
382 CHUNK_TV_TV_USEC
= 16,
383 /** Chunk table entries start here. */
384 CHUNK_TABLE_OFFSET
= 20,
387 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
391 PARA_DEBUG_LOG("%lu chunks\n", afhi
->chunks_total
);
392 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
393 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
396 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
399 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
400 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
403 /* TODO: audio format handlers could just produce this */
404 static void save_chunk_info(struct afh_info
*afhi
, char *buf
)
408 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
409 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
410 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
411 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
412 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
413 save_chunk_table(afhi
, buf
+ CHUNK_TABLE_OFFSET
);
416 static int load_chunk_info(struct osl_object
*obj
, struct afh_info
*afhi
)
418 char *buf
= obj
->data
;
420 if (obj
->size
< CHUNK_TABLE_OFFSET
)
421 return -E_BAD_DATA_SIZE
;
423 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
424 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
425 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
426 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
427 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
429 if ((afhi
->chunks_total
+ 1) * 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
430 return -E_BAD_DATA_SIZE
;
431 afhi
->chunk_table
= para_malloc((afhi
->chunks_total
+ 1) * 4);
432 load_chunk_table(afhi
, buf
+ CHUNK_TABLE_OFFSET
);
437 * Get the row of the audio file table corresponding to the given path.
439 * \param path The full path of the audio file.
440 * \param row Result pointer.
442 * \return The return value of the underlying call to osl_get_row().
444 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
446 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
448 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
452 * Get the row of the audio file table corresponding to the given hash value.
454 * \param hash The hash value of the desired audio file.
455 * \param row resul pointer.
457 * \return The return value of the underlying call to osl_get_row().
459 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
461 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
462 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
466 * Get the osl object holding the audio file selector info of a row.
468 * \param row Pointer to a row in the audio file table.
469 * \param obj Result pointer.
471 * \return The return value of the underlying call to osl_get_object().
473 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
475 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
479 * Get the osl object holding the audio file selector info, given a path.
482 * \param path The full path of the audio file.
483 * \param obj Result pointer.
485 * \return Positive on success, negative on errors.
487 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
490 int ret
= aft_get_row_of_path(path
, &row
);
493 return get_afsi_object_of_row(row
, obj
);
497 * Get the audio file selector info, given a row of the audio file table.
499 * \param row Pointer to a row in the audio file table.
500 * \param afsi Result pointer.
502 * \return Positive on success, negative on errors.
504 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
506 struct osl_object obj
;
507 int ret
= get_afsi_object_of_row(row
, &obj
);
510 return load_afsi(afsi
, &obj
);
514 * Get the audio file selector info, given the path of an audio table.
516 * \param path The full path of the audio file.
517 * \param afsi Result pointer.
519 * \return Positive on success, negative on errors.
521 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
523 struct osl_object obj
;
524 int ret
= get_afsi_object_of_path(path
, &obj
);
527 return load_afsi(afsi
, &obj
);
531 * Get the path of an audio file, given a row of the audio file table.
533 * \param row Pointer to a row in the audio file table.
534 * \param path Result pointer.
536 * The result is a pointer to mmapped data. The caller must not attempt
541 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
543 struct osl_object path_obj
;
544 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
548 *path
= path_obj
.data
;
553 * Get the object containing the hash value of an audio file, given a row.
555 * \param row Pointer to a row of the audio file table.
556 * \param obj Result pointer.
558 * \return The return value of the underlying call to osl_get_object().
560 * \sa get_hash_of_row().
562 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
564 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
568 * Get the hash value of an audio file, given a row of the audio file table.
570 * \param row Pointer to a row of the audio file table.
571 * \param hash Result pointer.
573 * \a hash points to mapped data and must not be freed by the caller.
575 * \return The return value of the underlying call to
576 * get_hash_object_of_aft_row().
578 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
580 struct osl_object obj
;
581 int ret
= get_hash_object_of_aft_row(row
, &obj
);
590 * Get the audio format handler info, given a row of the audio file table.
592 * \param row Pointer to a row of the audio file table.
593 * \param afhi Result pointer.
595 * \return The return value of the underlying call to osl_get_object().
597 * \sa get_chunk_table_of_row().
599 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
601 struct osl_object obj
;
602 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
606 load_afhi(obj
.data
, afhi
);
610 /* returns shmid on success */
611 static int save_afd(struct audio_file_data
*afd
)
613 size_t size
= sizeof(*afd
)
614 + 4 * (afd
->afhi
.chunks_total
+ 1);
616 PARA_DEBUG_LOG("size: %zu\n", size
);
617 int shmid
, ret
= shm_new(size
);
624 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
627 *(struct audio_file_data
*)shm_afd
= *afd
;
630 save_chunk_table(&afd
->afhi
, buf
);
638 int load_afd(int shmid
, struct audio_file_data
*afd
)
644 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
647 *afd
= *(struct audio_file_data
*)shm_afd
;
650 afd
->afhi
.chunk_table
= para_malloc((afd
->afhi
.chunks_total
+ 1) * 4);
651 load_chunk_table(&afd
->afhi
, buf
);
657 * Mmap the given audio file and update statistics.
659 * \param aft_row Determines the audio file to be opened and updated.
660 * \param afd Result pointer.
662 * On success, the numplayed field of the audio file selector info is increased
663 * and the lastplayed time is set to the current time. Finally, the score of
664 * the audio file is updated.
666 * \return Positive on success, negative on errors.
668 int open_and_update_audio_file(struct osl_row
*aft_row
,
669 struct audio_file_data
*afd
, long score
)
671 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
672 struct osl_object afsi_obj
;
673 struct afs_info new_afsi
;
674 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
675 struct afsi_change_event_data aced
;
676 struct osl_object map
, chunk_table_obj
;
681 ret
= get_audio_file_path_of_row(aft_row
, &path
);
684 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
687 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
690 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
693 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
694 AFTCOL_CHUNKS
, &chunk_table_obj
);
697 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
698 &map
.size
, &afd
->fd
);
701 hash_function(map
.data
, map
.size
, file_hash
);
702 ret
= hash_compare(file_hash
, aft_hash
);
703 para_munmap(map
.data
, map
.size
);
705 ret
= -E_HASH_MISMATCH
;
708 new_afsi
= afd
->afsi
;
709 new_afsi
.num_played
++;
710 new_afsi
.last_played
= time(NULL
);
711 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
713 ret
= load_chunk_info(&chunk_table_obj
, &afd
->afhi
);
724 struct para_buffer pb
= {.buf
= NULL
};
725 ret
= make_status_items(&d
, &pb
);
728 strncpy(afd
->afs_status_info
, pb
.buf
, AFS_STATUS_INFO_SIZE
);
729 afd
->afs_status_info
[AFS_STATUS_INFO_SIZE
- 1] = '\0';
732 aced
.aft_row
= aft_row
;
733 aced
.old_afsi
= &afd
->afsi
;
734 afs_event(AFSI_CHANGE
, NULL
, &aced
);
738 free(afd
->afhi
.chunk_table
);
740 osl_close_disk_object(&chunk_table_obj
);
744 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
745 time_t current_time
, enum ls_listing_mode lm
)
749 if (!localtime_r((time_t *)seconds
, &t
))
751 if (lm
== LS_MODE_MBOX
) {
752 if (!strftime(buf
, size
, "%c", &t
))
756 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
757 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
761 if (!strftime(buf
, size
, "%b %e %Y", &t
))
766 /** Compute the number of (decimal) digits of a number. */
767 #define GET_NUM_DIGITS(x, num) { \
768 typeof((x)) _tmp = PARA_ABS(x); \
771 while ((_tmp) > 9) { \
777 static short unsigned get_duration_width(int seconds
)
779 short unsigned width
;
780 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
782 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
783 return 4 + (mins
> 9);
784 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
785 GET_NUM_DIGITS(hours
, &width
);
789 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
791 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
792 short unsigned max_width
;
794 if (!hours
) { /* m:ss or mm:ss */
795 max_width
= opts
->mode
== LS_MODE_LONG
?
796 opts
->widths
.duration_width
: 4;
797 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
798 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
799 max_width
= opts
->mode
== LS_MODE_LONG
?
800 opts
->widths
.duration_width
: 7;
801 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
806 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
808 char *att_text
, *att_lines
;
810 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
812 return para_strdup(att_bitmap
);
813 att_lines
= make_message("attributes: %s\nattributes_txt: %s",
814 att_bitmap
, att_text
);
819 static char *make_lyrics_lines(struct afs_info
*afsi
)
823 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
824 return make_message("lyrics_id: %u\nlyrics_name: %s\n",
825 afsi
->lyrics_id
, lyrics_name
? lyrics_name
: "(none)");
828 static char *make_image_lines(struct afs_info
*afsi
)
831 img_get_name_by_id(afsi
->image_id
, &image_name
);
832 return make_message("image_id: %u\nimage_name: %s\n",
833 afsi
->image_id
, image_name
? image_name
: "(none)");
836 static char *make_filename_lines(const char *path
, unsigned flags
)
838 char *basename
, *dirname
;
841 if (!(flags
& LS_FLAG_FULL_PATH
))
842 return make_message("%s: %s\n%s:\n", "basename", path
,
844 basename
= para_basename(path
),
845 dirname
= para_dirname(path
);
846 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n", "path", path
,
847 "dir", dirname
, "basename", basename
);
853 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
854 struct para_buffer
*b
, time_t current_time
)
858 char last_played_time
[30];
859 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
860 char score_buf
[30] = "";
861 struct afs_info
*afsi
= &d
->afsi
;
862 struct afh_info
*afhi
= &d
->afhi
;
863 struct ls_widths
*w
= &opts
->widths
;
864 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
865 char asc_hash
[2 * HASH_SIZE
+ 1];
866 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
868 if (opts
->mode
== LS_MODE_SHORT
) {
869 para_printf(b
, "%s\n", d
->path
);
872 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
873 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
874 sizeof(last_played_time
), current_time
, opts
->mode
);
877 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
879 if (opts
->mode
== LS_MODE_LONG
)
880 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
882 sprintf(score_buf
, "%li ", d
->score
);
885 if (opts
->mode
== LS_MODE_LONG
) {
888 "%s " /* attributes */
889 "%*d " /* image_id */
890 "%*d " /* lyrics_id */
892 "%s " /* audio format */
893 "%*d " /* frequency */
896 "%*d " /* num_played */
897 "%s " /* last_played */
901 w
->image_id_width
, afsi
->image_id
,
902 w
->lyrics_id_width
, afsi
->lyrics_id
,
903 w
->bitrate_width
, afhi
->bitrate
,
904 audio_format_name(afsi
->audio_format_id
),
905 w
->frequency_width
, afhi
->frequency
,
908 w
->num_played_width
, afsi
->num_played
,
914 hash_to_asc(d
->hash
, asc_hash
);
915 att_lines
= make_attribute_lines(att_buf
, afsi
);
916 lyrics_lines
= make_lyrics_lines(afsi
);
917 image_lines
= make_image_lines(afsi
);
918 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
919 if (opts
->mode
== LS_MODE_VERBOSE
) {
921 "%s" /* filename stuff */
923 "%s\n" /* attributes */
925 "%s" /* image id, image name */
927 "bitrate: %dkbit/s\n"
932 "seconds_total: %lu\n"
937 have_score
? "score: " : "", score_buf
,
938 have_score
? "\n" : "",
944 audio_format_name(afsi
->audio_format_id
),
953 } else { /* mbox mode */
954 struct osl_object lyrics_def
;
955 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
957 "From foo@localhost %s\n"
958 "Received: from\nTo: bar\nFrom: a\n"
959 "Subject: %s\n\n" /* path */
961 "%s\n" /* attributes */
963 "%s\n" /* image id, image name */
965 "bitrate: %dkbit/s\n"
975 have_score
? "score: " : "", score_buf
,
976 have_score
? "\n" : "",
982 audio_format_name(afsi
->audio_format_id
),
988 lyrics_def
.data
? "Lyrics:\n~~~~~~~\n" : "",
989 lyrics_def
.data
? (char *)lyrics_def
.data
: ""
992 osl_close_disk_object(lyrics_def
.data
);
997 free(filename_lines
);
1001 int make_status_items(struct ls_data
*d
, struct para_buffer
*pb
)
1003 struct ls_options opts
= {
1004 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
1005 .mode
= LS_MODE_VERBOSE
,
1007 time_t current_time
;
1009 time(¤t_time
);
1010 return print_list_item(d
, &opts
, pb
, current_time
);
1014 static int ls_audio_format_compare(const void *a
, const void *b
)
1016 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1017 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1020 static int ls_duration_compare(const void *a
, const void *b
)
1022 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1023 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1026 static int ls_bitrate_compare(const void *a
, const void *b
)
1028 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1029 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1032 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1034 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1035 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1038 static int ls_image_id_compare(const void *a
, const void *b
)
1040 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1041 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1044 static int ls_channels_compare(const void *a
, const void *b
)
1046 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1047 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1050 static int ls_frequency_compare(const void *a
, const void *b
)
1052 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1053 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1056 static int ls_num_played_compare(const void *a
, const void *b
)
1058 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1059 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1062 static int ls_last_played_compare(const void *a
, const void *b
)
1064 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1065 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1068 static int ls_score_compare(const void *a
, const void *b
)
1070 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1071 return NUM_COMPARE(d1
->score
, d2
->score
);
1074 static int ls_path_compare(const void *a
, const void *b
)
1076 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1077 return strcmp(d1
->path
, d2
->path
);
1080 static int sort_matching_paths(struct ls_options
*options
)
1082 size_t nmemb
= options
->num_matching_paths
;
1083 size_t size
= sizeof(*options
->data_ptr
);
1084 int (*compar
)(const void *, const void *);
1087 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1088 for (i
= 0; i
< nmemb
; i
++)
1089 options
->data_ptr
[i
] = options
->data
+ i
;
1091 /* In these cases the array is already sorted */
1092 if (options
->sorting
== LS_SORT_BY_PATH
1093 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1094 && (options
->flags
& LS_FLAG_FULL_PATH
))
1096 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1097 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1100 switch (options
->sorting
) {
1101 case LS_SORT_BY_PATH
:
1102 compar
= ls_path_compare
; break;
1103 case LS_SORT_BY_SCORE
:
1104 compar
= ls_score_compare
; break;
1105 case LS_SORT_BY_LAST_PLAYED
:
1106 compar
= ls_last_played_compare
; break;
1107 case LS_SORT_BY_NUM_PLAYED
:
1108 compar
= ls_num_played_compare
; break;
1109 case LS_SORT_BY_FREQUENCY
:
1110 compar
= ls_frequency_compare
; break;
1111 case LS_SORT_BY_CHANNELS
:
1112 compar
= ls_channels_compare
; break;
1113 case LS_SORT_BY_IMAGE_ID
:
1114 compar
= ls_image_id_compare
; break;
1115 case LS_SORT_BY_LYRICS_ID
:
1116 compar
= ls_lyrics_id_compare
; break;
1117 case LS_SORT_BY_BITRATE
:
1118 compar
= ls_bitrate_compare
; break;
1119 case LS_SORT_BY_DURATION
:
1120 compar
= ls_duration_compare
; break;
1121 case LS_SORT_BY_AUDIO_FORMAT
:
1122 compar
= ls_audio_format_compare
; break;
1126 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1130 /* row is either an aft_row or a row of the score table */
1131 /* TODO: Only compute widths if we need them */
1132 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1135 struct ls_options
*options
= ls_opts
;
1137 struct ls_widths
*w
;
1138 unsigned short num_digits
;
1140 struct osl_row
*aft_row
;
1144 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1145 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1150 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1153 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1154 char *p
= strrchr(path
, '/');
1158 if (options
->num_patterns
) {
1159 for (i
= 0; i
< options
->num_patterns
; i
++) {
1160 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1163 if (ret
== FNM_NOMATCH
)
1167 if (i
>= options
->num_patterns
) /* no match */
1170 tmp
= options
->num_matching_paths
++;
1171 if (options
->num_matching_paths
> options
->array_size
) {
1172 options
->array_size
++;
1173 options
->array_size
*= 2;
1174 options
->data
= para_realloc(options
->data
, options
->array_size
1175 * sizeof(*options
->data
));
1177 d
= options
->data
+ tmp
;
1178 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1181 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1185 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1188 w
= &options
->widths
;
1189 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1190 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1191 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1192 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1193 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1194 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1195 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1196 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1197 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1198 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1199 /* get the number of chars to print this amount of time */
1200 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1201 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1202 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1203 GET_NUM_DIGITS(score
, &num_digits
);
1204 num_digits
++; /* add one for the sign (space or "-") */
1205 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1211 static int com_ls_callback(const struct osl_object
*query
,
1212 struct osl_object
*ls_output
)
1214 struct ls_options
*opts
= query
->data
;
1215 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1216 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1218 time_t current_time
;
1221 if (opts
->num_patterns
) {
1222 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1223 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1224 opts
->patterns
[i
] = p
;
1228 opts
->patterns
= NULL
;
1229 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1230 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1232 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1236 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1237 if (!opts
->num_matching_paths
)
1239 ret
= sort_matching_paths(opts
);
1242 time(¤t_time
);
1243 if (opts
->flags
& LS_FLAG_REVERSE
)
1244 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1245 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1250 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1251 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1257 ls_output
->data
= b
.buf
;
1258 ls_output
->size
= b
.size
;
1260 free(opts
->data_ptr
);
1261 free(opts
->patterns
);
1266 * TODO: flags -h (sort by hash)
1268 int com_ls(int fd
, int argc
, char * const * const argv
)
1272 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1273 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1274 struct ls_options opts
= {.patterns
= NULL
};
1275 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1278 for (i
= 1; i
< argc
; i
++) {
1279 const char *arg
= argv
[i
];
1282 if (!strcmp(arg
, "--")) {
1286 if (!strncmp(arg
, "-l", 2)) {
1288 mode
= LS_MODE_LONG
;
1292 return -E_AFT_SYNTAX
;
1293 switch(*(arg
+ 2)) {
1295 mode
= LS_MODE_SHORT
;
1298 mode
= LS_MODE_LONG
;
1301 mode
= LS_MODE_VERBOSE
;
1304 mode
= LS_MODE_MBOX
;
1307 return -E_AFT_SYNTAX
;
1310 if (!strcmp(arg
, "-p")) {
1311 flags
|= LS_FLAG_FULL_PATH
;
1314 if (!strcmp(arg
, "-a")) {
1315 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1318 if (!strcmp(arg
, "-r")) {
1319 flags
|= LS_FLAG_REVERSE
;
1322 if (!strncmp(arg
, "-s", 2)) {
1323 if (!*(arg
+ 2) || *(arg
+ 3))
1324 return -E_AFT_SYNTAX
;
1325 switch(*(arg
+ 2)) {
1327 sort
= LS_SORT_BY_PATH
;
1329 case 's': /* -ss implies -a */
1330 sort
= LS_SORT_BY_SCORE
;
1331 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1334 sort
= LS_SORT_BY_LAST_PLAYED
;
1337 sort
= LS_SORT_BY_NUM_PLAYED
;
1340 sort
= LS_SORT_BY_FREQUENCY
;
1343 sort
= LS_SORT_BY_CHANNELS
;
1346 sort
= LS_SORT_BY_IMAGE_ID
;
1349 sort
= LS_SORT_BY_LYRICS_ID
;
1352 sort
= LS_SORT_BY_BITRATE
;
1355 sort
= LS_SORT_BY_DURATION
;
1358 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1361 return -E_AFT_SYNTAX
;
1364 return -E_AFT_SYNTAX
;
1367 opts
.sorting
= sort
;
1369 opts
.num_patterns
= argc
- i
;
1370 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1371 argv
+ i
, com_ls_callback
, &ls_output
);
1373 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1374 free(ls_output
.data
);
1380 * Call the given function for each file in the audio file table.
1382 * \param private_data An arbitrary data pointer, passed to \a func.
1383 * \param func The custom function to be called.
1385 * \return The return value of the underlying call to osl_rbtree_loop().
1387 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1389 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1393 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1395 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1396 struct osl_row
*row
;
1398 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1402 enum aft_row_offsets
{
1403 AFTROW_AFHI_OFFSET_POS
= 0,
1404 AFTROW_CHUNKS_OFFSET_POS
= 2,
1405 AFTROW_AUDIO_FORMAT_OFFSET
= 4,
1406 AFTROW_FLAGS_OFFSET
= 5,
1407 AFTROW_HASH_OFFSET
= 9,
1408 AFTROW_PATH_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1411 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1412 * In this case, afhi won't be stored in the buffer */
1413 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1414 struct afh_info
*afhi
, uint32_t flags
,
1415 uint8_t audio_format_num
, struct osl_object
*obj
)
1417 size_t path_len
= strlen(path
) + 1;
1418 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1419 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1420 + sizeof_chunk_info_buf(afhi
);
1421 char *buf
= para_malloc(size
);
1424 write_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1425 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1427 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1428 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1430 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1431 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1432 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1433 pos
+ afhi_size
- 1);
1434 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1435 save_afhi(afhi
, buf
+ pos
);
1438 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1439 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1440 save_chunk_info(afhi
, buf
+ pos
);
1441 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1455 AFHI: whether afhi and chunk table are computed and sent
1456 ACTION: table modifications to be performed
1458 +---+----+-----+------+---------------------------------------------------+
1459 | HS | PB | F | AFHI | ACTION
1460 +---+----+-----+------+---------------------------------------------------+
1461 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1462 | | update path, keep afsi
1463 +---+----+-----+------+---------------------------------------------------+
1464 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1465 | | otherwise: remove PB, HS: update path, keep afhi,
1467 +---+----+-----+------+---------------------------------------------------+
1468 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1470 +---+----+-----+------+---------------------------------------------------+
1471 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1472 +---+----+-----+------+---------------------------------------------------+
1473 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1474 | | (force has no effect)
1475 +---+----+-----+------+---------------------------------------------------+
1476 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1477 +---+----+-----+------+---------------------------------------------------+
1478 | N | N | Y | Y | (new file) create new entry (force has no effect)
1479 +---+----+-----+------+---------------------------------------------------+
1480 | N | N | N | Y | (new file) create new entry
1481 +---+----+-----+------+---------------------------------------------------+
1483 afhi <=> force or no HS
1487 /** Flags passed to the add command. */
1488 enum com_add_flags
{
1489 /** Skip paths that exist already. */
1491 /** Force adding. */
1493 /** Print what is being done. */
1494 ADD_FLAG_VERBOSE
= 4,
1495 /** Try to add files with unknown suffixes. */
1499 static int com_add_callback(const struct osl_object
*query
,
1500 struct osl_object
*result
)
1502 char *buf
= query
->data
, *path
;
1503 struct osl_row
*pb
, *aft_row
;
1505 struct osl_object objs
[NUM_AFT_COLUMNS
];
1507 char asc
[2 * HASH_SIZE
+ 1];
1509 char afsi_buf
[AFSI_SIZE
];
1510 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1511 struct afs_info default_afsi
= {.last_played
= 0};
1512 struct para_buffer msg
= {.buf
= NULL
};
1514 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1515 hash_to_asc(hash
, asc
);;
1516 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1517 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1519 path
= buf
+ AFTROW_PATH_OFFSET
;
1520 objs
[AFTCOL_PATH
].data
= path
;
1521 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1523 PARA_INFO_LOG("request to add %s\n", path
);
1524 hs
= find_hash_sister(hash
);
1525 ret
= aft_get_row_of_path(path
, &pb
);
1526 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1528 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1529 if (flags
& ADD_FLAG_VERBOSE
)
1530 para_printf(&msg
, "ignoring duplicate\n");
1534 if (hs
&& hs
!= pb
) {
1535 struct osl_object obj
;
1536 if (pb
) { /* hs trumps pb, remove pb */
1537 if (flags
& ADD_FLAG_VERBOSE
)
1538 para_printf(&msg
, "removing path brother\n");
1539 ret
= osl_del_row(audio_file_table
, pb
);
1544 /* file rename, update hs' path */
1545 if (flags
& ADD_FLAG_VERBOSE
) {
1546 ret
= osl_get_object(audio_file_table
, hs
,
1550 para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1552 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1553 &objs
[AFTCOL_PATH
]);
1556 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1557 if (!(flags
& ADD_FLAG_FORCE
))
1560 /* no hs or force mode, child must have sent afhi */
1561 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1562 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1564 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1565 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1567 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1569 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1570 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1571 if (pb
&& !hs
) { /* update pb's hash */
1572 char old_asc
[2 * HASH_SIZE
+ 1];
1573 HASH_TYPE
*old_hash
;
1574 ret
= get_hash_of_row(pb
, &old_hash
);
1577 hash_to_asc(old_hash
, old_asc
);
1578 if (flags
& ADD_FLAG_VERBOSE
)
1579 para_printf(&msg
, "file change: %s -> %s\n",
1581 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1582 &objs
[AFTCOL_HASH
]);
1586 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1587 struct osl_row
*row
= pb
? pb
: hs
;
1588 /* update afhi and chunk_table */
1589 if (flags
& ADD_FLAG_VERBOSE
)
1590 para_printf(&msg
, "updating afhi and chunk table\n");
1591 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1592 &objs
[AFTCOL_AFHI
]);
1595 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1596 &objs
[AFTCOL_CHUNKS
]);
1599 afs_event(AFHI_CHANGE
, &msg
, row
);
1602 /* new entry, use default afsi */
1603 if (flags
& ADD_FLAG_VERBOSE
)
1604 para_printf(&msg
, "new file\n");
1605 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1606 default_afsi
.audio_format_id
= read_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
);
1608 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1609 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1610 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1611 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1614 para_printf(&msg
, "%s\n", PARA_STRERROR(-ret
));
1617 result
->data
= msg
.buf
;
1618 result
->size
= msg
.size
;
1619 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1623 /** Used by com_add(). */
1624 struct private_add_data
{
1625 /** The socket file descriptor. */
1627 /** The given add flags. */
1631 static int path_brother_callback(const struct osl_object
*query
,
1632 struct osl_object
*result
)
1634 char *path
= query
->data
;
1635 struct osl_row
*path_brother
;
1636 int ret
= aft_get_row_of_path(path
, &path_brother
);
1639 result
->data
= para_malloc(sizeof(path_brother
));
1640 result
->size
= sizeof(path_brother
);
1641 *(struct osl_row
**)(result
->data
) = path_brother
;
1645 static int hash_sister_callback(const struct osl_object
*query
,
1646 struct osl_object
*result
)
1648 HASH_TYPE
*hash
= query
->data
;
1649 struct osl_row
*hash_sister
;
1651 hash_sister
= find_hash_sister(hash
);
1653 return -E_RB_KEY_NOT_FOUND
;
1654 result
->data
= para_malloc(sizeof(hash_sister
));
1655 result
->size
= sizeof(hash_sister
);
1656 *(struct osl_row
**)(result
->data
) = hash_sister
;
1660 static int add_one_audio_file(const char *path
, const void *private_data
)
1663 uint8_t format_num
= -1;
1664 const struct private_add_data
*pad
= private_data
;
1665 struct afh_info afhi
, *afhi_ptr
= NULL
;
1666 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1667 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
= {.data
= NULL
};
1668 HASH_TYPE hash
[HASH_SIZE
];
1670 afhi
.header_offset
= 0;
1671 afhi
.header_len
= 0;
1672 ret
= guess_audio_format(path
);
1673 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1675 query
.data
= (char *)path
;
1676 query
.size
= strlen(path
) + 1;
1677 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1678 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1681 pb
= *(struct osl_row
**)result
.data
;
1685 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1686 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1687 ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1690 /* We still want to add this file. Compute its hash. */
1691 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, NULL
);
1694 hash_function(map
.data
, map
.size
, hash
);
1696 /* Check whether database contains file with the same hash. */
1698 query
.size
= HASH_SIZE
;
1699 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1700 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1703 hs
= *(struct osl_row
**)result
.data
;
1706 /* Return success if we already know this file. */
1708 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1709 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1710 ret
= send_va_buffer(pad
->fd
,
1711 "%s exists, not forcing update\n", path
);
1715 * we won't recalculate the audio format info and the chunk table if
1716 * there is a hash sister unless in FORCE mode.
1718 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1719 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1725 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1726 ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1730 munmap(map
.data
, map
.size
);
1731 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1732 /* Ask afs to consider this entry for adding. */
1733 ret
= send_callback_request(com_add_callback
, &obj
, &result
);
1735 ret2
= send_va_buffer(pad
->fd
, "%s", (char *)result
.data
);
1737 if (ret
>= 0 && ret2
< 0)
1743 munmap(map
.data
, map
.size
);
1745 if (ret
< 0 && ret
!= -E_SEND
)
1746 send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1747 PARA_STRERROR(-ret
));
1750 free(afhi_ptr
->chunk_table
);
1751 /* it's not an error if not all files could be added */
1752 return ret
== -E_SEND
? ret
: 1;
1755 int com_add(int fd
, int argc
, char * const * const argv
)
1758 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1759 struct stat statbuf
;
1761 for (i
= 1; i
< argc
; i
++) {
1762 const char *arg
= argv
[i
];
1765 if (!strcmp(arg
, "--")) {
1769 if (!strcmp(arg
, "-a")) {
1770 pad
.flags
|= ADD_FLAG_ALL
;
1773 if (!strcmp(arg
, "-l")) {
1774 pad
.flags
|= ADD_FLAG_LAZY
;
1777 if (!strcmp(arg
, "-f")) {
1778 pad
.flags
|= ADD_FLAG_FORCE
;
1781 if (!strcmp(arg
, "-v")) {
1782 pad
.flags
|= ADD_FLAG_VERBOSE
;
1787 return -E_AFT_SYNTAX
;
1788 for (; i
< argc
; i
++) {
1790 ret
= verify_path(argv
[i
], &path
);
1792 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
], PARA_STRERROR(-ret
));
1797 ret
= stat(path
, &statbuf
);
1799 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1806 if (S_ISDIR(statbuf
.st_mode
))
1807 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1810 ret
= add_one_audio_file(path
, &pad
);
1812 send_va_buffer(fd
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
1823 * Flags used by the touch command.
1828 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1829 TOUCH_FLAG_FNM_PATHNAME
= 1,
1830 /** Activates verbose mode. */
1831 TOUCH_FLAG_VERBOSE
= 2
1834 /** Options used by com_touch(). */
1835 struct com_touch_options
{
1836 /** New num_played value. */
1838 /** New last played count. */
1839 int64_t last_played
;
1840 /** new lyrics id. */
1842 /** new image id. */
1844 /** command line flags (see \ref touch_flags). */
1848 /** Data passed to the action handler of com_touch(). */
1849 struct touch_action_data
{
1850 /** Command line options (see \ref com_touch_options). */
1851 struct com_touch_options
*cto
;
1852 /** Message buffer. */
1853 struct para_buffer pb
;
1856 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1857 struct osl_row
*row
, const char *name
, void *data
)
1859 struct touch_action_data
*tad
= data
;
1860 struct osl_object obj
;
1861 struct afs_info old_afsi
, new_afsi
;
1862 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1863 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0;
1864 struct afsi_change_event_data aced
;
1866 ret
= get_afsi_object_of_row(row
, &obj
);
1868 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1871 ret
= load_afsi(&old_afsi
, &obj
);
1873 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1876 new_afsi
= old_afsi
;
1878 new_afsi
.num_played
++;
1879 new_afsi
.last_played
= time(NULL
);
1880 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1881 para_printf(&tad
->pb
, "%s: num_played = %u, "
1882 "last_played = now()\n", name
,
1883 new_afsi
.num_played
);
1885 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1886 para_printf(&tad
->pb
, "touching %s\n", name
);
1887 if (tad
->cto
->lyrics_id
>= 0)
1888 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1889 if (tad
->cto
->image_id
>= 0)
1890 new_afsi
.image_id
= tad
->cto
->image_id
;
1891 if (tad
->cto
->num_played
>= 0)
1892 new_afsi
.num_played
= tad
->cto
->num_played
;
1893 if (tad
->cto
->last_played
>= 0)
1894 new_afsi
.last_played
= tad
->cto
->last_played
;
1896 save_afsi(&new_afsi
, &obj
); /* in-place update */
1898 aced
.old_afsi
= &old_afsi
;
1899 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
1903 static int com_touch_callback(const struct osl_object
*query
,
1904 struct osl_object
*result
)
1906 struct touch_action_data tad
= {.cto
= query
->data
};
1908 struct pattern_match_data pmd
= {
1909 .table
= audio_file_table
,
1910 .loop_col_num
= AFTCOL_HASH
,
1911 .match_col_num
= AFTCOL_PATH
,
1912 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
1913 .size
= query
->size
- sizeof(*tad
.cto
)},
1915 .action
= touch_audio_file
1917 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
1918 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1919 ret
= for_each_matching_row(&pmd
);
1921 para_printf(&tad
.pb
, "%s\n", PARA_STRERROR(-ret
));
1923 result
->data
= tad
.pb
.buf
;
1924 result
->size
= tad
.pb
.size
;
1927 return ret
< 0? ret
: 0;
1930 int com_touch(int fd
, int argc
, char * const * const argv
)
1932 struct com_touch_options cto
= {
1938 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)},
1943 for (i
= 1; i
< argc
; i
++) {
1944 const char *arg
= argv
[i
];
1947 if (!strcmp(arg
, "--")) {
1951 if (!strncmp(arg
, "-n", 2)) {
1952 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
1957 if (!strncmp(arg
, "-l", 2)) {
1958 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
1963 if (!strncmp(arg
, "-y", 2)) {
1964 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
1969 if (!strncmp(arg
, "-i", 2)) {
1970 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
1975 if (!strcmp(arg
, "-p")) {
1976 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
1979 if (!strcmp(arg
, "-v")) {
1980 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
1983 break; /* non-option starting with dash */
1986 return -E_AFT_SYNTAX
;
1987 ret
= send_option_arg_callback_request(&query
, argc
- i
,
1988 argv
+ i
, com_touch_callback
, &result
);
1990 send_buffer(fd
, (char *)result
.data
);
1993 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
1997 /** Flags for com_rm(). */
2000 RM_FLAG_VERBOSE
= 1,
2004 RM_FLAG_FNM_PATHNAME
= 4
2007 /** Data passed to the action handler of com_rm(). */
2008 struct com_rm_action_data
{
2009 /** Command line flags ((see \ref rm_flags). */
2011 /** Message buffer. */
2012 struct para_buffer pb
;
2013 /** Number of audio files removed. */
2014 unsigned num_removed
;
2017 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2018 struct osl_row
*row
, const char *name
, void *data
)
2020 struct com_rm_action_data
*crd
= data
;
2023 if (crd
->flags
& RM_FLAG_VERBOSE
)
2024 para_printf(&crd
->pb
, "removing %s\n", name
);
2025 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2026 ret
= osl_del_row(audio_file_table
, row
);
2028 para_printf(&crd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
2034 static int com_rm_callback(const struct osl_object
*query
,
2035 __a_unused
struct osl_object
*result
)
2037 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
};
2039 struct pattern_match_data pmd
= {
2040 .table
= audio_file_table
,
2041 .loop_col_num
= AFTCOL_HASH
,
2042 .match_col_num
= AFTCOL_PATH
,
2043 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2044 .size
= query
->size
- sizeof(uint32_t)},
2046 .action
= remove_audio_file
2048 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2049 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2050 ret
= for_each_matching_row(&pmd
);
2052 para_printf(&crd
.pb
, "%s\n", PARA_STRERROR(-ret
));
2053 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2054 para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2056 if (crd
.flags
& RM_FLAG_VERBOSE
)
2057 para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2060 result
->data
= crd
.pb
.buf
;
2061 result
->size
= crd
.pb
.size
;
2064 return ret
< 0? ret
: 0;
2067 /* TODO options: -r (recursive) */
2068 int com_rm(int fd
, int argc
, char * const * const argv
)
2071 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)},
2075 for (i
= 1; i
< argc
; i
++) {
2076 const char *arg
= argv
[i
];
2079 if (!strcmp(arg
, "--")) {
2083 if (!strcmp(arg
, "-f")) {
2084 flags
|= RM_FLAG_FORCE
;
2087 if (!strcmp(arg
, "-p")) {
2088 flags
|= RM_FLAG_FNM_PATHNAME
;
2091 if (!strcmp(arg
, "-v")) {
2092 flags
|= RM_FLAG_VERBOSE
;
2098 return -E_AFT_SYNTAX
;
2099 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2100 com_rm_callback
, &result
);
2102 send_buffer(fd
, (char *)result
.data
);
2105 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
2110 * Flags used by the cpsi command.
2115 /** Whether the lyrics id should be copied. */
2116 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2117 /** Whether the image id should be copied. */
2118 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2119 /** Whether the lastplayed time should be copied. */
2120 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2121 /** Whether the numplayed count should be copied. */
2122 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2123 /** Whether the attributes should be copied. */
2124 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2125 /** Activates verbose mode. */
2126 CPSI_FLAG_VERBOSE
= 32,
2129 /** Data passed to the action handler of com_cpsi(). */
2130 struct cpsi_action_data
{
2131 /** command line flags (see \ref cpsi_flags). */
2133 /** Number of audio files changed. */
2134 unsigned num_copied
;
2135 /** Message buffer. */
2136 struct para_buffer pb
;
2137 /** Values are copied from here. */
2138 struct afs_info source_afsi
;
2141 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2142 struct osl_row
*row
, const char *name
, void *data
)
2144 struct cpsi_action_data
*cad
= data
;
2145 struct osl_object target_afsi_obj
;
2147 struct afs_info old_afsi
, target_afsi
;
2148 struct afsi_change_event_data aced
;
2150 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2153 load_afsi(&target_afsi
, &target_afsi_obj
);
2154 old_afsi
= target_afsi
;
2155 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2156 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2157 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2158 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2159 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2160 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2161 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2162 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2163 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2164 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2165 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2167 if (cad
->flags
& CPSI_FLAG_VERBOSE
)
2168 para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2170 aced
.old_afsi
= &old_afsi
;
2171 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2175 static int com_cpsi_callback(const struct osl_object
*query
,
2176 struct osl_object
*result
)
2178 struct cpsi_action_data cad
= {.flags
= *(unsigned *)query
->data
};
2180 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2182 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2185 struct pattern_match_data pmd
= {
2186 .table
= audio_file_table
,
2187 .loop_col_num
= AFTCOL_HASH
,
2188 .match_col_num
= AFTCOL_PATH
,
2189 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2190 .size
= query
->size
- sizeof(cad
.flags
)
2191 - strlen(source_path
) - 1},
2193 .action
= copy_selector_info
2195 ret
= for_each_matching_row(&pmd
);
2198 para_printf(&cad
.pb
, "%s\n", PARA_STRERROR(-ret
));
2199 if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2201 para_printf(&cad
.pb
, "copied requested afsi from %s "
2203 source_path
, cad
.num_copied
);
2205 para_printf(&cad
.pb
, "nothing copied\n");
2208 result
->data
= cad
.pb
.buf
;
2209 result
->size
= cad
.pb
.size
;
2212 return ret
< 0? ret
: 0;
2215 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2219 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
2222 for (i
= 1; i
< argc
; i
++) {
2223 const char *arg
= argv
[i
];
2226 if (!strcmp(arg
, "--")) {
2230 if (!strcmp(arg
, "-y")) {
2231 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2234 if (!strcmp(arg
, "-i")) {
2235 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2238 if (!strcmp(arg
, "-l")) {
2239 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2242 if (!strcmp(arg
, "-n")) {
2243 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2246 if (!strcmp(arg
, "-a")) {
2247 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2250 if (!strcmp(arg
, "-v")) {
2251 flags
|= CPSI_FLAG_VERBOSE
;
2256 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2257 return -E_AFT_SYNTAX
;
2258 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2259 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2260 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2261 com_cpsi_callback
, &result
);
2263 send_buffer(fd
, (char *)result
.data
);
2266 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
2270 /* TODO: optionally fix problems by removing offending rows */
2271 static int check_audio_file(struct osl_row
*row
, void *data
)
2274 struct para_buffer
*pb
= data
;
2275 struct stat statbuf
;
2276 int ret
= get_audio_file_path_of_row(row
, &path
);
2277 struct afs_info afsi
;
2281 para_printf(pb
, "%s\n", PARA_STRERROR(-ret
));
2284 if (stat(path
, &statbuf
) < 0)
2285 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2287 if (!S_ISREG(statbuf
.st_mode
))
2288 para_printf(pb
, "%s: not a regular file\n", path
);
2290 ret
= get_afsi_of_row(row
, &afsi
);
2292 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
2295 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2297 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2298 PARA_STRERROR(-ret
));
2299 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2301 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2302 PARA_STRERROR(-ret
));
2307 * Check the audio file table for inconsistencies.
2309 * \param query Unused.
2310 * \param result Contains message string upon return.
2312 * This function always succeeds.
2316 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
2318 struct para_buffer pb
= {.buf
= NULL
};
2320 para_printf(&pb
, "checking audio file table...\n");
2321 audio_file_loop(&pb
, check_audio_file
);
2322 result
->data
= pb
.buf
;
2323 result
->size
= pb
.size
;
2329 * Close the audio file table.
2331 * \param flags Usual flags that are passed to osl_close_table().
2333 * \sa osl_close_table().
2335 static void aft_close(void)
2337 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2338 audio_file_table
= NULL
;
2342 * Open the audio file table.
2344 * \param dir The database directory.
2348 * \sa osl_open_table().
2350 static int aft_open(const char *dir
)
2354 audio_file_table_desc
.dir
= dir
;
2355 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2358 osl_get_num_rows(audio_file_table
, &num
);
2359 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2362 PARA_INFO_LOG("failed to open audio file table\n");
2363 audio_file_table
= NULL
;
2364 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2369 static int aft_create(const char *dir
)
2371 audio_file_table_desc
.dir
= dir
;
2372 return osl_create_table(&audio_file_table_desc
);
2375 static int clear_attribute(struct osl_row
*row
, void *data
)
2377 struct rmatt_event_data
*red
= data
;
2378 struct afs_info afsi
;
2379 struct osl_object obj
;
2380 int ret
= get_afsi_object_of_row(row
, &obj
);
2381 uint64_t mask
= ~(1ULL << red
->bitnum
);
2385 ret
= load_afsi(&afsi
, &obj
);
2388 afsi
.attributes
&= mask
;
2389 save_afsi(&afsi
, &obj
);
2393 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2397 case ATTRIBUTE_REMOVE
: {
2398 const struct rmatt_event_data
*red
= data
;
2399 para_printf(pb
, "clearing attribute %s (bit %u) from all "
2400 "entries in the audio file table\n", red
->name
,
2402 return audio_file_loop(data
, clear_attribute
);
2409 void aft_init(struct afs_table
*t
)
2411 t
->name
= audio_file_table_desc
.name
;
2413 t
->close
= aft_close
;
2414 t
->create
= aft_create
;
2415 t
->event_handler
= aft_event_handler
;