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 to the different compare functions (called by qsort()). */
101 /** Usual audio format handler information. */
102 struct afh_info afhi
;
103 /** Audio file selector information. */
104 struct afs_info afsi
;
105 /** The full path of the audio file. */
107 /** The score value (if -a was given). */
109 /** The sha1 hash of audio file. */
113 /** Data passed from the ls command handler to its callback function. */
115 /** The given command line flags. */
117 /** The sorting method given at the command line. */
118 enum ls_sorting_method sorting
;
119 /** The given listing mode (short, long, verbose, mbox). */
120 enum ls_listing_mode mode
;
121 /** The arguments passed to the ls command. */
123 /** Number of non-option arguments. */
125 /** Used for long listing mode to align the output fields. */
126 struct ls_widths widths
;
127 /** Size of the \a data array. */
129 /** Number of used entries in the data array. */
130 uint32_t num_matching_paths
;
131 /** Array of matching entries. */
132 struct ls_data
*data
;
133 /** Used to sort the array. */
134 struct ls_data
**data_ptr
;
138 * Describes the layout of the mmapped-afs info struct.
140 * \sa struct afs_info.
143 /** Where .last_played is stored. */
144 AFSI_LAST_PLAYED_OFFSET
= 0,
145 /** Storage position of the attributes bitmap. */
146 AFSI_ATTRIBUTES_OFFSET
= 8,
147 /** Storage position of the .num_played field. */
148 AFSI_NUM_PLAYED_OFFSET
= 16,
149 /** Storage position of the .image_id field. */
150 AFSI_IMAGE_ID_OFFSET
= 20,
151 /** Storage position of the .lyrics_id field. */
152 AFSI_LYRICS_ID_OFFSET
= 24,
153 /** Storage position of the .audio_format_id field. */
154 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
155 /** 3 bytes reserved space for future usage. */
156 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 29,
157 /** On-disk storage space needed. */
162 * Convert a struct afs_info to an osl object.
164 * \param afsi Pointer to the audio file info to be converted.
165 * \param obj Result pointer.
169 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
171 char *buf
= obj
->data
;
173 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
174 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
175 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
176 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
177 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
178 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
179 afsi
->audio_format_id
);
180 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 3);
184 * Get the audio file selector info struct stored in an osl object.
186 * \param afsi Points to the audio_file info structure to be filled in.
187 * \param obj The osl object holding the data.
189 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
193 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
195 char *buf
= obj
->data
;
196 if (obj
->size
< AFSI_SIZE
)
198 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
199 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
200 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
201 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
202 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
203 afsi
->audio_format_id
= read_u8(buf
+
204 AFSI_AUDIO_FORMAT_ID_OFFSET
);
208 /** The columns of the audio file table. */
209 enum audio_file_table_columns
{
210 /** The hash on the content of the audio file. */
212 /** The full path in the filesystem. */
214 /** The audio file selector info. */
216 /** The audio format handler info. */
218 /** The chunk table info and the chunk table of the audio file. */
220 /** The number of columns of this table. */
224 static struct osl_column_description aft_cols
[] = {
226 .storage_type
= OSL_MAPPED_STORAGE
,
227 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
229 .compare_function
= osl_hash_compare
,
230 .data_size
= HASH_SIZE
233 .storage_type
= OSL_MAPPED_STORAGE
,
234 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
236 .compare_function
= string_compare
,
239 .storage_type
= OSL_MAPPED_STORAGE
,
240 .storage_flags
= OSL_FIXED_SIZE
,
242 .data_size
= AFSI_SIZE
245 .storage_type
= OSL_MAPPED_STORAGE
,
249 .storage_type
= OSL_DISK_STORAGE
,
254 static struct osl_table_description audio_file_table_desc
= {
255 .name
= "audio_files",
256 .num_columns
= NUM_AFT_COLUMNS
,
257 .flags
= OSL_LARGE_TABLE
,
258 .column_descriptions
= aft_cols
261 /* We don't want * dot or dot-dot anywhere. */
262 static int verify_dotfile(const char *rest
)
265 * The first character was '.', but that has already been discarded, we
269 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
271 case '.': /* path start with /foo/.. */
272 if (rest
[1] == '\0' || rest
[1] == '/')
273 return -1; /* /foo/.. or /foo/../bar are not ok */
274 /* /foo/..bar is ok */
280 * We fundamentally don't like some paths: We don't want double slashes or
281 * slashes at the end that can make pathnames ambiguous.
283 static int verify_path(const char *orig_path
, char **resolved_path
)
289 if (*orig_path
!= '/') /* we only accept absolute paths */
291 len
= strlen(orig_path
);
292 *resolved_path
= para_strdup(orig_path
);
293 path
= *resolved_path
;
294 while (len
> 1 && path
[--len
] == '/')
295 path
[len
] = '\0'; /* remove slash at the end */
301 case '/': /* double slash */
304 if (verify_dotfile(path
) < 0)
314 free(*resolved_path
);
318 /** The on-disk layout of a afhi struct. */
320 /** Where the number of seconds is stored. */
321 AFHI_SECONDS_TOTAL_OFFSET
= 0,
322 /** Position of the bitrate. */
323 AFHI_BITRATE_OFFSET
= 4,
324 /** Position of the frequency. */
325 AFHI_FREQUENCY_OFFSET
= 8,
326 /** Location of the audio file header. */
327 AFHI_HEADER_OFFSET_OFFSET
= 12,
328 /* Length of the audio file header. Zero means: No header. */
329 AFHI_HEADER_LEN_OFFSET
= 16,
330 /** Number of channels is stored here. */
331 AFHI_CHANNELS_OFFSET
= 20,
332 /** EOF timeout in ms. */
333 AFHI_EOF_OFFSET
= 21,
334 /** The tag info position. */
335 AFHI_INFO_STRING_OFFSET
= 23,
336 /** Minimal on-disk size of a valid afhi struct. */
340 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
344 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
347 static void save_afhi(struct afh_info
*afhi
, char *buf
)
351 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
352 afhi
->seconds_total
);
353 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
354 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
355 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
356 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
357 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
358 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
359 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
360 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
363 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
365 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
366 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
367 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
368 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
369 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
370 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
371 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
372 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
375 //#define SIZEOF_CHUNK_TABLE(afhi) (((afhi)->chunks_total + 1) * sizeof(uint32_t))
377 static unsigned sizeof_chunk_info_buf(struct afh_info
*afhi
)
381 return 4 * (afhi
->chunks_total
+ 1) + 20;
385 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
386 enum chunk_info_offsets
{
387 /** The total number of chunks (4 bytes). */
388 CHUNKS_TOTAL_OFFSET
= 0,
389 /** The length of the audio file header (4 bytes). */
390 HEADER_LEN_OFFSET
= 4,
391 /** The start of the audio file header (4 bytes). */
392 HEADER_OFFSET_OFFSET
= 8,
393 /** The seconds part of the chunk time (4 bytes). */
394 CHUNK_TV_TV_SEC_OFFSET
= 12,
395 /** The microseconds part of the chunk time (4 bytes). */
396 CHUNK_TV_TV_USEC
= 16,
397 /** Chunk table entries start here. */
398 CHUNK_TABLE_OFFSET
= 20,
401 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
405 PARA_DEBUG_LOG("%lu chunks\n", afhi
->chunks_total
);
406 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
407 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
410 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
413 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
414 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
417 /* TODO: audio format handlers could just produce this */
418 static void save_chunk_info(struct afh_info
*afhi
, char *buf
)
422 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
423 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
424 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
425 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
426 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
427 save_chunk_table(afhi
, buf
+ CHUNK_TABLE_OFFSET
);
430 static int load_chunk_info(struct osl_object
*obj
, struct afh_info
*afhi
)
432 char *buf
= obj
->data
;
434 if (obj
->size
< CHUNK_TABLE_OFFSET
)
435 return -E_BAD_DATA_SIZE
;
437 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
438 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
439 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
440 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
441 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
443 if ((afhi
->chunks_total
+ 1) * 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
444 return -E_BAD_DATA_SIZE
;
445 afhi
->chunk_table
= para_malloc((afhi
->chunks_total
+ 1) * 4);
446 load_chunk_table(afhi
, buf
+ CHUNK_TABLE_OFFSET
);
451 * Get the row of the audio file table corresponding to the given path.
453 * \param path The full path of the audio file.
454 * \param row Result pointer.
456 * \return The return value of the underlying call to osl_get_row().
458 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
460 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
462 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
466 * Get the row of the audio file table corresponding to the given hash value.
468 * \param hash The hash value of the desired audio file.
469 * \param row resul pointer.
471 * \return The return value of the underlying call to osl_get_row().
473 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
475 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
476 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
480 * Get the osl object holding the audio file selector info of a row.
482 * \param row Pointer to a row in the audio file table.
483 * \param obj Result pointer.
485 * \return The return value of the underlying call to osl_get_object().
487 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
489 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
493 * Get the osl object holding the audio file selector info, given a path.
496 * \param path The full path of the audio file.
497 * \param obj Result pointer.
499 * \return Positive on success, negative on errors.
501 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
504 int ret
= aft_get_row_of_path(path
, &row
);
507 return get_afsi_object_of_row(row
, obj
);
511 * Get the audio file selector info, given a row of the audio file table.
513 * \param row Pointer to a row in the audio file table.
514 * \param afsi Result pointer.
516 * \return Positive on success, negative on errors.
518 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
520 struct osl_object obj
;
521 int ret
= get_afsi_object_of_row(row
, &obj
);
524 return load_afsi(afsi
, &obj
);
528 * Get the audio file selector info, given the path of an audio table.
530 * \param path The full path of the audio file.
531 * \param afsi Result pointer.
533 * \return Positive on success, negative on errors.
535 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
537 struct osl_object obj
;
538 int ret
= get_afsi_object_of_path(path
, &obj
);
541 return load_afsi(afsi
, &obj
);
545 * Get the path of an audio file, given a row of the audio file table.
547 * \param row Pointer to a row in the audio file table.
548 * \param path Result pointer.
550 * The result is a pointer to mmapped data. The caller must not attempt
555 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
557 struct osl_object path_obj
;
558 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
562 *path
= path_obj
.data
;
567 * Get the object containing the hash value of an audio file, given a row.
569 * \param row Pointer to a row of the audio file table.
570 * \param obj Result pointer.
572 * \return The return value of the underlying call to osl_get_object().
574 * \sa get_hash_of_row().
576 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
578 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
582 * Get the hash value of an audio file, given a row of the audio file table.
584 * \param row Pointer to a row of the audio file table.
585 * \param hash Result pointer.
587 * \a hash points to mapped data and must not be freed by the caller.
589 * \return The return value of the underlying call to
590 * get_hash_object_of_aft_row().
592 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
594 struct osl_object obj
;
595 int ret
= get_hash_object_of_aft_row(row
, &obj
);
604 * Get the audio format handler info, given a row of the audio file table.
606 * \param row Pointer to a row of the audio file table.
607 * \param afhi Result pointer.
609 * \return The return value of the underlying call to osl_get_object().
611 * \sa get_chunk_table_of_row().
613 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
615 struct osl_object obj
;
616 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
620 load_afhi(obj
.data
, afhi
);
624 /* returns shmid on success */
625 static int save_afd(struct audio_file_data
*afd
)
627 size_t size
= sizeof(*afd
)
628 + 4 * (afd
->afhi
.chunks_total
+ 1);
630 PARA_DEBUG_LOG("size: %zu\n", size
);
631 int shmid
, ret
= shm_new(size
);
638 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
641 *(struct audio_file_data
*)shm_afd
= *afd
;
644 save_chunk_table(&afd
->afhi
, buf
);
652 int load_afd(int shmid
, struct audio_file_data
*afd
)
658 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
661 *afd
= *(struct audio_file_data
*)shm_afd
;
664 afd
->afhi
.chunk_table
= para_malloc((afd
->afhi
.chunks_total
+ 1) * 4);
665 load_chunk_table(&afd
->afhi
, buf
);
671 * Mmap the given audio file and update statistics.
673 * \param aft_row Determines the audio file to be opened and updated.
674 * \param afd Result pointer.
676 * On success, the numplayed field of the audio file selector info is increased
677 * and the lastplayed time is set to the current time. Finally, the score of
678 * the audio file is updated.
680 * \return Positive on success, negative on errors.
682 int open_and_update_audio_file(struct osl_row
*aft_row
, struct audio_file_data
*afd
)
684 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
685 struct osl_object afsi_obj
;
686 struct afs_info new_afsi
;
687 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
688 struct afsi_change_event_data aced
;
689 struct osl_object map
, chunk_table_obj
;
694 ret
= get_audio_file_path_of_row(aft_row
, &path
);
697 strncpy(afd
->path
, path
, sizeof(afd
->path
) - 1);
698 afd
->path
[sizeof(afd
->path
) - 1] = '\0';
699 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
702 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
705 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
708 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
709 AFTCOL_CHUNKS
, &chunk_table_obj
);
712 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
713 &map
.size
, &afd
->fd
);
716 hash_function(map
.data
, map
.size
, file_hash
);
717 ret
= hash_compare(file_hash
, aft_hash
);
718 para_munmap(map
.data
, map
.size
);
720 ret
= -E_HASH_MISMATCH
;
723 new_afsi
= afd
->afsi
;
724 new_afsi
.num_played
++;
725 new_afsi
.last_played
= time(NULL
);
726 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
728 ret
= load_chunk_info(&chunk_table_obj
, &afd
->afhi
);
731 ret
= get_attribute_text(&afd
->afsi
.attributes
, " ", &tmp
);
735 strncpy(afd
->attributes_string
, tmp
, sizeof(afd
->attributes_string
));
736 afd
->attributes_string
[sizeof(afd
->attributes_string
) - 1] = '\0';
739 aced
.aft_row
= aft_row
;
740 aced
.old_afsi
= &afd
->afsi
;
741 afs_event(AFSI_CHANGE
, NULL
, &aced
);
745 free(afd
->afhi
.chunk_table
);
747 osl_close_disk_object(&chunk_table_obj
);
751 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
752 time_t current_time
, enum ls_listing_mode lm
)
756 if (!localtime_r((time_t *)seconds
, &t
))
758 if (lm
== LS_MODE_MBOX
) {
759 if (!strftime(buf
, size
, "%c", &t
))
763 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
764 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
768 if (!strftime(buf
, size
, "%b %e %Y", &t
))
773 /** Compute the number of (decimal) digits of a number. */
774 #define GET_NUM_DIGITS(x, num) { \
775 typeof((x)) _tmp = PARA_ABS(x); \
778 while ((_tmp) > 9) { \
784 static short unsigned get_duration_width(int seconds
)
786 short unsigned width
;
787 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
789 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
790 return 4 + (mins
> 9);
791 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
792 GET_NUM_DIGITS(hours
, &width
);
796 static void get_duration_buf(int seconds
, char *buf
, short unsigned max_width
)
798 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
800 if (!hours
) /* m:ss or mm:ss */
801 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
802 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
803 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
807 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
809 char *att_text
, *att_lines
;
811 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
813 return para_strdup(att_bitmap
);
814 att_lines
= make_message("%s\nattributes_txt: %s",
815 att_bitmap
, att_text
);
820 static char *make_lyrics_line(struct afs_info
*afsi
)
824 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
826 return make_message("%u", afsi
->lyrics_id
);
827 return make_message("%u (%s)", afsi
->lyrics_id
, lyrics_name
);
830 static char *make_image_line(struct afs_info
*afsi
)
833 img_get_name_by_id(afsi
->image_id
, &image_name
);
835 return make_message("%u", afsi
->image_id
);
836 return make_message("%u (%s)", afsi
->image_id
, image_name
);
839 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
840 struct para_buffer
*b
, time_t current_time
)
844 char last_played_time
[30];
845 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
846 char score_buf
[30] = "";
847 struct afs_info
*afsi
= &d
->afsi
;
848 struct afh_info
*afhi
= &d
->afhi
;
849 struct ls_widths
*w
= &opts
->widths
;
850 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
851 char asc_hash
[2 * HASH_SIZE
+ 1];
852 char *att_lines
, *lyrics_line
, *image_line
;
854 if (opts
->mode
== LS_MODE_SHORT
) {
855 para_printf(b
, "%s\n", d
->path
);
858 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
859 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
860 sizeof(last_played_time
), current_time
, opts
->mode
);
863 get_duration_buf(afhi
->seconds_total
, duration_buf
, w
->duration_width
);
865 if (opts
->mode
== LS_MODE_LONG
)
866 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
868 sprintf(score_buf
, "%li ", d
->score
);
871 if (opts
->mode
== LS_MODE_LONG
) {
874 "%s " /* attributes */
875 "%*d " /* image_id */
876 "%*d " /* lyrics_id */
878 "%s " /* audio format */
879 "%*d " /* frequency */
882 "%*d " /* num_played */
883 "%s " /* last_played */
887 w
->image_id_width
, afsi
->image_id
,
888 w
->lyrics_id_width
, afsi
->lyrics_id
,
889 w
->bitrate_width
, afhi
->bitrate
,
890 audio_format_name(afsi
->audio_format_id
),
891 w
->frequency_width
, afhi
->frequency
,
894 w
->num_played_width
, afsi
->num_played
,
900 hash_to_asc(d
->hash
, asc_hash
);
901 att_lines
= make_attribute_lines(att_buf
, afsi
);
902 lyrics_line
= make_lyrics_line(afsi
);
903 image_line
= make_image_line(afsi
);
904 /* TODO: Merge this with status items */
905 if (opts
->mode
== LS_MODE_VERBOSE
) {
907 "%s: %s\n" /* path */
913 "bitrate: %dkbit/s\n"
921 (opts
->flags
& LS_FLAG_FULL_PATH
)?
922 "path" : "file", d
->path
,
923 have_score
? "score: " : "", score_buf
,
924 have_score
? "\n" : "",
930 audio_format_name(afsi
->audio_format_id
),
938 } else { /* mbox mode */
939 struct osl_object lyrics_def
;
940 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
942 "From foo@localhost %s\n"
943 "Received: from\nTo: bar\nFrom: a\n"
944 "Subject: %s\n\n" /* path */
946 "%s\n" /* attributes */
950 "bitrate: %dkbit/s\n"
960 have_score
? "score: " : "", score_buf
,
961 have_score
? "\n" : "",
967 audio_format_name(afsi
->audio_format_id
),
973 lyrics_def
.data
? "Lyrics:\n~~~~~~~\n" : "",
974 lyrics_def
.data
? (char *)lyrics_def
.data
: ""
977 osl_close_disk_object(lyrics_def
.data
);
985 static int ls_audio_format_compare(const void *a
, const void *b
)
987 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
988 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
991 static int ls_duration_compare(const void *a
, const void *b
)
993 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
994 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
997 static int ls_bitrate_compare(const void *a
, const void *b
)
999 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1000 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1003 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1005 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1006 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1009 static int ls_image_id_compare(const void *a
, const void *b
)
1011 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1012 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1015 static int ls_channels_compare(const void *a
, const void *b
)
1017 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1018 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1021 static int ls_frequency_compare(const void *a
, const void *b
)
1023 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1024 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1027 static int ls_num_played_compare(const void *a
, const void *b
)
1029 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1030 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1033 static int ls_last_played_compare(const void *a
, const void *b
)
1035 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1036 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1039 static int ls_score_compare(const void *a
, const void *b
)
1041 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1042 return NUM_COMPARE(d1
->score
, d2
->score
);
1045 static int ls_path_compare(const void *a
, const void *b
)
1047 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1048 return strcmp(d1
->path
, d2
->path
);
1051 static int sort_matching_paths(struct ls_options
*options
)
1053 size_t nmemb
= options
->num_matching_paths
;
1054 size_t size
= sizeof(*options
->data_ptr
);
1055 int (*compar
)(const void *, const void *);
1058 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1059 for (i
= 0; i
< nmemb
; i
++)
1060 options
->data_ptr
[i
] = options
->data
+ i
;
1062 /* In these cases the array is already sorted */
1063 if (options
->sorting
== LS_SORT_BY_PATH
1064 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1065 && (options
->flags
& LS_FLAG_FULL_PATH
))
1067 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1068 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1071 switch (options
->sorting
) {
1072 case LS_SORT_BY_PATH
:
1073 compar
= ls_path_compare
; break;
1074 case LS_SORT_BY_SCORE
:
1075 compar
= ls_score_compare
; break;
1076 case LS_SORT_BY_LAST_PLAYED
:
1077 compar
= ls_last_played_compare
; break;
1078 case LS_SORT_BY_NUM_PLAYED
:
1079 compar
= ls_num_played_compare
; break;
1080 case LS_SORT_BY_FREQUENCY
:
1081 compar
= ls_frequency_compare
; break;
1082 case LS_SORT_BY_CHANNELS
:
1083 compar
= ls_channels_compare
; break;
1084 case LS_SORT_BY_IMAGE_ID
:
1085 compar
= ls_image_id_compare
; break;
1086 case LS_SORT_BY_LYRICS_ID
:
1087 compar
= ls_lyrics_id_compare
; break;
1088 case LS_SORT_BY_BITRATE
:
1089 compar
= ls_bitrate_compare
; break;
1090 case LS_SORT_BY_DURATION
:
1091 compar
= ls_duration_compare
; break;
1092 case LS_SORT_BY_AUDIO_FORMAT
:
1093 compar
= ls_audio_format_compare
; break;
1097 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1101 /* row is either an aft_row or a row of the score table */
1102 /* TODO: Only compute widths if we need them */
1103 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1106 struct ls_options
*options
= ls_opts
;
1108 struct ls_widths
*w
;
1109 unsigned short num_digits
;
1111 struct osl_row
*aft_row
;
1115 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1116 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1121 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1124 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1125 char *p
= strrchr(path
, '/');
1129 if (options
->num_patterns
) {
1130 for (i
= 0; i
< options
->num_patterns
; i
++) {
1131 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1134 if (ret
== FNM_NOMATCH
)
1138 if (i
>= options
->num_patterns
) /* no match */
1141 tmp
= options
->num_matching_paths
++;
1142 if (options
->num_matching_paths
> options
->array_size
) {
1143 options
->array_size
++;
1144 options
->array_size
*= 2;
1145 options
->data
= para_realloc(options
->data
, options
->array_size
1146 * sizeof(*options
->data
));
1148 d
= options
->data
+ tmp
;
1149 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1152 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1156 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1159 w
= &options
->widths
;
1160 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1161 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1162 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1163 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1164 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1165 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1166 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1167 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1168 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1169 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1170 /* get the number of chars to print this amount of time */
1171 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1172 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1173 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1174 GET_NUM_DIGITS(score
, &num_digits
);
1175 num_digits
++; /* add one for the sign (space or "-") */
1176 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1182 static int com_ls_callback(const struct osl_object
*query
,
1183 struct osl_object
*ls_output
)
1185 struct ls_options
*opts
= query
->data
;
1186 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1187 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1189 time_t current_time
;
1192 if (opts
->num_patterns
) {
1193 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1194 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1195 opts
->patterns
[i
] = p
;
1199 opts
->patterns
= NULL
;
1200 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1201 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1203 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1207 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1208 if (!opts
->num_matching_paths
)
1210 ret
= sort_matching_paths(opts
);
1213 time(¤t_time
);
1214 if (opts
->flags
& LS_FLAG_REVERSE
)
1215 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1216 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1221 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1222 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1228 ls_output
->data
= b
.buf
;
1229 ls_output
->size
= b
.size
;
1231 free(opts
->data_ptr
);
1232 free(opts
->patterns
);
1237 * TODO: flags -h (sort by hash)
1239 int com_ls(int fd
, int argc
, char * const * const argv
)
1243 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1244 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1245 struct ls_options opts
= {.patterns
= NULL
};
1246 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1249 for (i
= 1; i
< argc
; i
++) {
1250 const char *arg
= argv
[i
];
1253 if (!strcmp(arg
, "--")) {
1257 if (!strncmp(arg
, "-l", 2)) {
1259 mode
= LS_MODE_LONG
;
1263 return -E_AFT_SYNTAX
;
1264 switch(*(arg
+ 2)) {
1266 mode
= LS_MODE_SHORT
;
1269 mode
= LS_MODE_LONG
;
1272 mode
= LS_MODE_VERBOSE
;
1275 mode
= LS_MODE_MBOX
;
1278 return -E_AFT_SYNTAX
;
1281 if (!strcmp(arg
, "-p")) {
1282 flags
|= LS_FLAG_FULL_PATH
;
1285 if (!strcmp(arg
, "-a")) {
1286 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1289 if (!strcmp(arg
, "-r")) {
1290 flags
|= LS_FLAG_REVERSE
;
1293 if (!strncmp(arg
, "-s", 2)) {
1294 if (!*(arg
+ 2) || *(arg
+ 3))
1295 return -E_AFT_SYNTAX
;
1296 switch(*(arg
+ 2)) {
1298 sort
= LS_SORT_BY_PATH
;
1300 case 's': /* -ss implies -a */
1301 sort
= LS_SORT_BY_SCORE
;
1302 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1305 sort
= LS_SORT_BY_LAST_PLAYED
;
1308 sort
= LS_SORT_BY_NUM_PLAYED
;
1311 sort
= LS_SORT_BY_FREQUENCY
;
1314 sort
= LS_SORT_BY_CHANNELS
;
1317 sort
= LS_SORT_BY_IMAGE_ID
;
1320 sort
= LS_SORT_BY_LYRICS_ID
;
1323 sort
= LS_SORT_BY_BITRATE
;
1326 sort
= LS_SORT_BY_DURATION
;
1329 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1332 return -E_AFT_SYNTAX
;
1335 return -E_AFT_SYNTAX
;
1338 opts
.sorting
= sort
;
1340 opts
.num_patterns
= argc
- i
;
1341 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1342 argv
+ i
, com_ls_callback
, &ls_output
);
1344 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1345 free(ls_output
.data
);
1351 * Call the given function for each file in the audio file table.
1353 * \param private_data An arbitrary data pointer, passed to \a func.
1354 * \param func The custom function to be called.
1356 * \return The return value of the underlying call to osl_rbtree_loop().
1358 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1360 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1364 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1366 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1367 struct osl_row
*row
;
1369 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1373 enum aft_row_offsets
{
1374 AFTROW_AFHI_OFFSET_POS
= 0,
1375 AFTROW_CHUNKS_OFFSET_POS
= 2,
1376 AFTROW_AUDIO_FORMAT_OFFSET
= 4,
1377 AFTROW_FLAGS_OFFSET
= 5,
1378 AFTROW_HASH_OFFSET
= 9,
1379 AFTROW_PATH_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1382 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1383 * In this case, afhi won't be stored in the buffer */
1384 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1385 struct afh_info
*afhi
, uint32_t flags
,
1386 uint8_t audio_format_num
, struct osl_object
*obj
)
1388 size_t path_len
= strlen(path
) + 1;
1389 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1390 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1391 + sizeof_chunk_info_buf(afhi
);
1392 char *buf
= para_malloc(size
);
1395 write_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1396 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1398 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1399 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1401 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1402 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1403 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1404 pos
+ afhi_size
- 1);
1405 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1406 save_afhi(afhi
, buf
+ pos
);
1409 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1410 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1411 save_chunk_info(afhi
, buf
+ pos
);
1412 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1426 AFHI: whether afhi and chunk table are computed and sent
1427 ACTION: table modifications to be performed
1429 +---+----+-----+------+---------------------------------------------------+
1430 | HS | PB | F | AFHI | ACTION
1431 +---+----+-----+------+---------------------------------------------------+
1432 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1433 | | update path, keep afsi
1434 +---+----+-----+------+---------------------------------------------------+
1435 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1436 | | otherwise: remove PB, HS: update path, keep afhi,
1438 +---+----+-----+------+---------------------------------------------------+
1439 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1441 +---+----+-----+------+---------------------------------------------------+
1442 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1443 +---+----+-----+------+---------------------------------------------------+
1444 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1445 | | (force has no effect)
1446 +---+----+-----+------+---------------------------------------------------+
1447 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1448 +---+----+-----+------+---------------------------------------------------+
1449 | N | N | Y | Y | (new file) create new entry (force has no effect)
1450 +---+----+-----+------+---------------------------------------------------+
1451 | N | N | N | Y | (new file) create new entry
1452 +---+----+-----+------+---------------------------------------------------+
1454 afhi <=> force or no HS
1458 /** Flags passed to the add command. */
1459 enum com_add_flags
{
1460 /** Skip paths that exist already. */
1462 /** Force adding. */
1464 /** Print what is being done. */
1465 ADD_FLAG_VERBOSE
= 4,
1466 /** Try to add files with unknown suffixes. */
1470 static int com_add_callback(const struct osl_object
*query
,
1471 struct osl_object
*result
)
1473 char *buf
= query
->data
, *path
;
1474 struct osl_row
*pb
, *aft_row
;
1476 struct osl_object objs
[NUM_AFT_COLUMNS
];
1478 char asc
[2 * HASH_SIZE
+ 1];
1480 char afsi_buf
[AFSI_SIZE
];
1481 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1482 struct afs_info default_afsi
= {.last_played
= 0};
1483 struct para_buffer msg
= {.buf
= NULL
};
1485 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1486 hash_to_asc(hash
, asc
);;
1487 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1488 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1490 path
= buf
+ AFTROW_PATH_OFFSET
;
1491 objs
[AFTCOL_PATH
].data
= path
;
1492 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1494 PARA_INFO_LOG("request to add %s\n", path
);
1495 hs
= find_hash_sister(hash
);
1496 ret
= aft_get_row_of_path(path
, &pb
);
1497 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1499 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1500 if (flags
& ADD_FLAG_VERBOSE
)
1501 para_printf(&msg
, "ignoring duplicate\n");
1505 if (hs
&& hs
!= pb
) {
1506 struct osl_object obj
;
1507 if (pb
) { /* hs trumps pb, remove pb */
1508 if (flags
& ADD_FLAG_VERBOSE
)
1509 para_printf(&msg
, "removing path brother\n");
1510 ret
= osl_del_row(audio_file_table
, pb
);
1515 /* file rename, update hs' path */
1516 if (flags
& ADD_FLAG_VERBOSE
) {
1517 ret
= osl_get_object(audio_file_table
, hs
,
1521 para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1523 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1524 &objs
[AFTCOL_PATH
]);
1527 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1528 if (!(flags
& ADD_FLAG_FORCE
))
1531 /* no hs or force mode, child must have sent afhi */
1532 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1533 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1535 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1536 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1538 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1540 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1541 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1542 if (pb
&& !hs
) { /* update pb's hash */
1543 char old_asc
[2 * HASH_SIZE
+ 1];
1544 HASH_TYPE
*old_hash
;
1545 ret
= get_hash_of_row(pb
, &old_hash
);
1548 hash_to_asc(old_hash
, old_asc
);
1549 if (flags
& ADD_FLAG_VERBOSE
)
1550 para_printf(&msg
, "file change: %s -> %s\n",
1552 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1553 &objs
[AFTCOL_HASH
]);
1557 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1558 struct osl_row
*row
= pb
? pb
: hs
;
1559 /* update afhi and chunk_table */
1560 if (flags
& ADD_FLAG_VERBOSE
)
1561 para_printf(&msg
, "updating afhi and chunk table\n");
1562 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1563 &objs
[AFTCOL_AFHI
]);
1566 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1567 &objs
[AFTCOL_CHUNKS
]);
1570 afs_event(AFHI_CHANGE
, &msg
, row
);
1573 /* new entry, use default afsi */
1574 if (flags
& ADD_FLAG_VERBOSE
)
1575 para_printf(&msg
, "new file\n");
1576 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1577 default_afsi
.audio_format_id
= read_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
);
1579 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1580 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1581 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1582 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1585 para_printf(&msg
, "%s\n", PARA_STRERROR(-ret
));
1588 result
->data
= msg
.buf
;
1589 result
->size
= msg
.size
;
1590 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1594 /** Used by com_add(). */
1595 struct private_add_data
{
1596 /** The socket file descriptor. */
1598 /** The given add flags. */
1602 static int path_brother_callback(const struct osl_object
*query
,
1603 struct osl_object
*result
)
1605 char *path
= query
->data
;
1606 struct osl_row
*path_brother
;
1607 int ret
= aft_get_row_of_path(path
, &path_brother
);
1610 result
->data
= para_malloc(sizeof(path_brother
));
1611 result
->size
= sizeof(path_brother
);
1612 *(struct osl_row
**)(result
->data
) = path_brother
;
1616 static int hash_sister_callback(const struct osl_object
*query
,
1617 struct osl_object
*result
)
1619 HASH_TYPE
*hash
= query
->data
;
1620 struct osl_row
*hash_sister
;
1622 hash_sister
= find_hash_sister(hash
);
1624 return -E_RB_KEY_NOT_FOUND
;
1625 result
->data
= para_malloc(sizeof(hash_sister
));
1626 result
->size
= sizeof(hash_sister
);
1627 *(struct osl_row
**)(result
->data
) = hash_sister
;
1631 static int add_one_audio_file(const char *path
, const void *private_data
)
1634 uint8_t format_num
= -1;
1635 const struct private_add_data
*pad
= private_data
;
1636 struct afh_info afhi
, *afhi_ptr
= NULL
;
1637 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1638 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
= {.data
= NULL
};
1639 HASH_TYPE hash
[HASH_SIZE
];
1641 afhi
.header_offset
= 0;
1642 afhi
.header_len
= 0;
1643 ret
= guess_audio_format(path
);
1644 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1646 query
.data
= (char *)path
;
1647 query
.size
= strlen(path
) + 1;
1648 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1649 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1652 pb
= *(struct osl_row
**)result
.data
;
1656 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1657 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1658 ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1661 /* We still want to add this file. Compute its hash. */
1662 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, NULL
);
1665 hash_function(map
.data
, map
.size
, hash
);
1667 /* Check whether database contains file with the same hash. */
1669 query
.size
= HASH_SIZE
;
1670 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1671 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1674 hs
= *(struct osl_row
**)result
.data
;
1677 /* Return success if we already know this file. */
1679 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1680 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1681 ret
= send_va_buffer(pad
->fd
,
1682 "%s exists, not forcing update\n", path
);
1686 * we won't recalculate the audio format info and the chunk table if
1687 * there is a hash sister unless in FORCE mode.
1689 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1690 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1696 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1697 ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1701 munmap(map
.data
, map
.size
);
1702 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1703 /* Ask afs to consider this entry for adding. */
1704 ret
= send_callback_request(com_add_callback
, &obj
, &result
);
1706 ret2
= send_va_buffer(pad
->fd
, "%s", (char *)result
.data
);
1708 if (ret
>= 0 && ret2
< 0)
1714 munmap(map
.data
, map
.size
);
1716 if (ret
< 0 && ret
!= -E_SEND
)
1717 send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1718 PARA_STRERROR(-ret
));
1721 free(afhi_ptr
->chunk_table
);
1722 /* it's not an error if not all files could be added */
1723 return ret
== -E_SEND
? ret
: 1;
1726 int com_add(int fd
, int argc
, char * const * const argv
)
1729 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1730 struct stat statbuf
;
1732 for (i
= 1; i
< argc
; i
++) {
1733 const char *arg
= argv
[i
];
1736 if (!strcmp(arg
, "--")) {
1740 if (!strcmp(arg
, "-a")) {
1741 pad
.flags
|= ADD_FLAG_ALL
;
1744 if (!strcmp(arg
, "-l")) {
1745 pad
.flags
|= ADD_FLAG_LAZY
;
1748 if (!strcmp(arg
, "-f")) {
1749 pad
.flags
|= ADD_FLAG_FORCE
;
1752 if (!strcmp(arg
, "-v")) {
1753 pad
.flags
|= ADD_FLAG_VERBOSE
;
1758 return -E_AFT_SYNTAX
;
1759 for (; i
< argc
; i
++) {
1761 ret
= verify_path(argv
[i
], &path
);
1763 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
], PARA_STRERROR(-ret
));
1768 ret
= stat(path
, &statbuf
);
1770 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1777 if (S_ISDIR(statbuf
.st_mode
))
1778 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1781 ret
= add_one_audio_file(path
, &pad
);
1783 send_va_buffer(fd
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
1794 * Flags used by the touch command.
1799 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1800 TOUCH_FLAG_FNM_PATHNAME
= 1,
1801 /** Activates verbose mode. */
1802 TOUCH_FLAG_VERBOSE
= 2
1805 /** Options used by com_touch(). */
1806 struct com_touch_options
{
1807 /** New num_played value. */
1809 /** New last played count. */
1810 int64_t last_played
;
1811 /** new lyrics id. */
1813 /** new image id. */
1815 /** command line flags (see \ref touch_flags). */
1819 /** Data passed to the action handler of com_touch(). */
1820 struct touch_action_data
{
1821 /** Command line options (see \ref com_touch_options). */
1822 struct com_touch_options
*cto
;
1823 /** Message buffer. */
1824 struct para_buffer pb
;
1827 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1828 struct osl_row
*row
, const char *name
, void *data
)
1830 struct touch_action_data
*tad
= data
;
1831 struct osl_object obj
;
1832 struct afs_info old_afsi
, new_afsi
;
1833 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1834 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0;
1835 struct afsi_change_event_data aced
;
1837 ret
= get_afsi_object_of_row(row
, &obj
);
1839 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1842 ret
= load_afsi(&old_afsi
, &obj
);
1844 para_printf(&tad
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
1847 new_afsi
= old_afsi
;
1849 new_afsi
.num_played
++;
1850 new_afsi
.last_played
= time(NULL
);
1851 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1852 para_printf(&tad
->pb
, "%s: num_played = %u, "
1853 "last_played = now()\n", name
,
1854 new_afsi
.num_played
);
1856 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1857 para_printf(&tad
->pb
, "touching %s\n", name
);
1858 if (tad
->cto
->lyrics_id
>= 0)
1859 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1860 if (tad
->cto
->image_id
>= 0)
1861 new_afsi
.image_id
= tad
->cto
->image_id
;
1862 if (tad
->cto
->num_played
>= 0)
1863 new_afsi
.num_played
= tad
->cto
->num_played
;
1864 if (tad
->cto
->last_played
>= 0)
1865 new_afsi
.last_played
= tad
->cto
->last_played
;
1867 save_afsi(&new_afsi
, &obj
); /* in-place update */
1869 aced
.old_afsi
= &old_afsi
;
1870 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
1874 static int com_touch_callback(const struct osl_object
*query
,
1875 struct osl_object
*result
)
1877 struct touch_action_data tad
= {.cto
= query
->data
};
1879 struct pattern_match_data pmd
= {
1880 .table
= audio_file_table
,
1881 .loop_col_num
= AFTCOL_HASH
,
1882 .match_col_num
= AFTCOL_PATH
,
1883 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
1884 .size
= query
->size
- sizeof(*tad
.cto
)},
1886 .action
= touch_audio_file
1888 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
1889 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1890 ret
= for_each_matching_row(&pmd
);
1892 para_printf(&tad
.pb
, "%s\n", PARA_STRERROR(-ret
));
1894 result
->data
= tad
.pb
.buf
;
1895 result
->size
= tad
.pb
.size
;
1898 return ret
< 0? ret
: 0;
1901 int com_touch(int fd
, int argc
, char * const * const argv
)
1903 struct com_touch_options cto
= {
1909 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)},
1914 for (i
= 1; i
< argc
; i
++) {
1915 const char *arg
= argv
[i
];
1918 if (!strcmp(arg
, "--")) {
1922 if (!strncmp(arg
, "-n", 2)) {
1923 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
1928 if (!strncmp(arg
, "-l", 2)) {
1929 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
1934 if (!strncmp(arg
, "-y", 2)) {
1935 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
1940 if (!strncmp(arg
, "-i", 2)) {
1941 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
1946 if (!strcmp(arg
, "-p")) {
1947 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
1950 if (!strcmp(arg
, "-v")) {
1951 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
1954 break; /* non-option starting with dash */
1957 return -E_AFT_SYNTAX
;
1958 ret
= send_option_arg_callback_request(&query
, argc
- i
,
1959 argv
+ i
, com_touch_callback
, &result
);
1961 send_buffer(fd
, (char *)result
.data
);
1964 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
1968 /** Flags for com_rm(). */
1971 RM_FLAG_VERBOSE
= 1,
1975 RM_FLAG_FNM_PATHNAME
= 4
1978 /** Data passed to the action handler of com_rm(). */
1979 struct com_rm_action_data
{
1980 /** Command line flags ((see \ref rm_flags). */
1982 /** Message buffer. */
1983 struct para_buffer pb
;
1984 /** Number of audio files removed. */
1985 unsigned num_removed
;
1988 static int remove_audio_file(__a_unused
struct osl_table
*table
,
1989 struct osl_row
*row
, const char *name
, void *data
)
1991 struct com_rm_action_data
*crd
= data
;
1994 if (crd
->flags
& RM_FLAG_VERBOSE
)
1995 para_printf(&crd
->pb
, "removing %s\n", name
);
1996 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
1997 ret
= osl_del_row(audio_file_table
, row
);
1999 para_printf(&crd
->pb
, "%s: %s\n", name
, PARA_STRERROR(-ret
));
2005 static int com_rm_callback(const struct osl_object
*query
,
2006 __a_unused
struct osl_object
*result
)
2008 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
};
2010 struct pattern_match_data pmd
= {
2011 .table
= audio_file_table
,
2012 .loop_col_num
= AFTCOL_HASH
,
2013 .match_col_num
= AFTCOL_PATH
,
2014 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2015 .size
= query
->size
- sizeof(uint32_t)},
2017 .action
= remove_audio_file
2019 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2020 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2021 ret
= for_each_matching_row(&pmd
);
2023 para_printf(&crd
.pb
, "%s\n", PARA_STRERROR(-ret
));
2024 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2025 para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2027 if (crd
.flags
& RM_FLAG_VERBOSE
)
2028 para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2031 result
->data
= crd
.pb
.buf
;
2032 result
->size
= crd
.pb
.size
;
2035 return ret
< 0? ret
: 0;
2038 /* TODO options: -r (recursive) */
2039 int com_rm(int fd
, int argc
, char * const * const argv
)
2042 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)},
2046 for (i
= 1; i
< argc
; i
++) {
2047 const char *arg
= argv
[i
];
2050 if (!strcmp(arg
, "--")) {
2054 if (!strcmp(arg
, "-f")) {
2055 flags
|= RM_FLAG_FORCE
;
2058 if (!strcmp(arg
, "-p")) {
2059 flags
|= RM_FLAG_FNM_PATHNAME
;
2062 if (!strcmp(arg
, "-v")) {
2063 flags
|= RM_FLAG_VERBOSE
;
2069 return -E_AFT_SYNTAX
;
2070 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2071 com_rm_callback
, &result
);
2073 send_buffer(fd
, (char *)result
.data
);
2076 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
2081 * Flags used by the cpsi command.
2086 /** Whether the lyrics id should be copied. */
2087 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2088 /** Whether the image id should be copied. */
2089 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2090 /** Whether the lastplayed time should be copied. */
2091 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2092 /** Whether the numplayed count should be copied. */
2093 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2094 /** Whether the attributes should be copied. */
2095 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2096 /** Activates verbose mode. */
2097 CPSI_FLAG_VERBOSE
= 32,
2100 /** Data passed to the action handler of com_cpsi(). */
2101 struct cpsi_action_data
{
2102 /** command line flags (see \ref cpsi_flags). */
2104 /** Number of audio files changed. */
2105 unsigned num_copied
;
2106 /** Message buffer. */
2107 struct para_buffer pb
;
2108 /** Values are copied from here. */
2109 struct afs_info source_afsi
;
2112 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2113 struct osl_row
*row
, const char *name
, void *data
)
2115 struct cpsi_action_data
*cad
= data
;
2116 struct osl_object target_afsi_obj
;
2118 struct afs_info old_afsi
, target_afsi
;
2119 struct afsi_change_event_data aced
;
2121 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2124 load_afsi(&target_afsi
, &target_afsi_obj
);
2125 old_afsi
= target_afsi
;
2126 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2127 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2128 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2129 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2130 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2131 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2132 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2133 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2134 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2135 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2136 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2138 if (cad
->flags
& CPSI_FLAG_VERBOSE
)
2139 para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2141 aced
.old_afsi
= &old_afsi
;
2142 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2146 static int com_cpsi_callback(const struct osl_object
*query
,
2147 struct osl_object
*result
)
2149 struct cpsi_action_data cad
= {.flags
= *(unsigned *)query
->data
};
2151 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2153 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2156 struct pattern_match_data pmd
= {
2157 .table
= audio_file_table
,
2158 .loop_col_num
= AFTCOL_HASH
,
2159 .match_col_num
= AFTCOL_PATH
,
2160 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2161 .size
= query
->size
- sizeof(cad
.flags
)
2162 - strlen(source_path
) - 1},
2164 .action
= copy_selector_info
2166 ret
= for_each_matching_row(&pmd
);
2169 para_printf(&cad
.pb
, "%s\n", PARA_STRERROR(-ret
));
2170 if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2172 para_printf(&cad
.pb
, "copied requested afsi from %s "
2174 source_path
, cad
.num_copied
);
2176 para_printf(&cad
.pb
, "nothing copied\n");
2179 result
->data
= cad
.pb
.buf
;
2180 result
->size
= cad
.pb
.size
;
2183 return ret
< 0? ret
: 0;
2186 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2190 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
2193 for (i
= 1; i
< argc
; i
++) {
2194 const char *arg
= argv
[i
];
2197 if (!strcmp(arg
, "--")) {
2201 if (!strcmp(arg
, "-y")) {
2202 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2205 if (!strcmp(arg
, "-i")) {
2206 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2209 if (!strcmp(arg
, "-l")) {
2210 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2213 if (!strcmp(arg
, "-n")) {
2214 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2217 if (!strcmp(arg
, "-a")) {
2218 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2221 if (!strcmp(arg
, "-v")) {
2222 flags
|= CPSI_FLAG_VERBOSE
;
2227 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2228 return -E_AFT_SYNTAX
;
2229 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2230 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2231 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2232 com_cpsi_callback
, &result
);
2234 send_buffer(fd
, (char *)result
.data
);
2237 send_va_buffer(fd
, "%s\n", PARA_STRERROR(-ret
));
2241 /* TODO: optionally fix problems by removing offending rows */
2242 static int check_audio_file(struct osl_row
*row
, void *data
)
2245 struct para_buffer
*pb
= data
;
2246 struct stat statbuf
;
2247 int ret
= get_audio_file_path_of_row(row
, &path
);
2248 struct afs_info afsi
;
2252 para_printf(pb
, "%s\n", PARA_STRERROR(-ret
));
2255 if (stat(path
, &statbuf
) < 0)
2256 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2258 if (!S_ISREG(statbuf
.st_mode
))
2259 para_printf(pb
, "%s: not a regular file\n", path
);
2261 ret
= get_afsi_of_row(row
, &afsi
);
2263 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
2266 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2268 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2269 PARA_STRERROR(-ret
));
2270 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2272 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2273 PARA_STRERROR(-ret
));
2278 * Check the audio file table for inconsistencies.
2280 * \param query Unused.
2281 * \param result Contains message string upon return.
2283 * This function always succeeds.
2287 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
2289 struct para_buffer pb
= {.buf
= NULL
};
2291 para_printf(&pb
, "checking audio file table...\n");
2292 audio_file_loop(&pb
, check_audio_file
);
2293 result
->data
= pb
.buf
;
2294 result
->size
= pb
.size
;
2300 * Close the audio file table.
2302 * \param flags Usual flags that are passed to osl_close_table().
2304 * \sa osl_close_table().
2306 static void aft_close(void)
2308 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2309 audio_file_table
= NULL
;
2313 * Open the audio file table.
2315 * \param dir The database directory.
2319 * \sa osl_open_table().
2321 static int aft_open(const char *dir
)
2325 audio_file_table_desc
.dir
= dir
;
2326 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2329 osl_get_num_rows(audio_file_table
, &num
);
2330 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2333 PARA_INFO_LOG("failed to open audio file table\n");
2334 audio_file_table
= NULL
;
2335 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2340 static int aft_create(const char *dir
)
2342 audio_file_table_desc
.dir
= dir
;
2343 return osl_create_table(&audio_file_table_desc
);
2346 static int clear_attribute(struct osl_row
*row
, void *data
)
2348 struct rmatt_event_data
*red
= data
;
2349 struct afs_info afsi
;
2350 struct osl_object obj
;
2351 int ret
= get_afsi_object_of_row(row
, &obj
);
2352 uint64_t mask
= ~(1ULL << red
->bitnum
);
2356 ret
= load_afsi(&afsi
, &obj
);
2359 afsi
.attributes
&= mask
;
2360 save_afsi(&afsi
, &obj
);
2364 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2368 case ATTRIBUTE_REMOVE
: {
2369 const struct rmatt_event_data
*red
= data
;
2370 para_printf(pb
, "clearing attribute %s (bit %u) from all "
2371 "entries in the audio file table\n", red
->name
,
2373 return audio_file_loop(data
, clear_attribute
);
2380 void aft_init(struct afs_table
*t
)
2382 t
->name
= audio_file_table_desc
.name
;
2384 t
->close
= aft_close
;
2385 t
->create
= aft_create
;
2386 t
->event_handler
= aft_event_handler
;