2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
9 #include <dirent.h> /* readdir() */
24 #include "portable_io.h"
26 static struct osl_table
*audio_file_table
;
27 static char *current_status_items
;
29 /** The different sorting methods of the ls command. */
30 enum ls_sorting_method
{
36 LS_SORT_BY_LAST_PLAYED
,
38 LS_SORT_BY_NUM_PLAYED
,
52 LS_SORT_BY_AUDIO_FORMAT
,
57 /** The different listing modes of the ls command. */
58 enum ls_listing_mode
{
59 /** Default listing mode. */
71 /** The flags accepted by the ls command. */
74 LS_FLAG_FULL_PATH
= 1,
76 LS_FLAG_ADMISSIBLE_ONLY
= 2,
82 * The size of the individual output fields of the ls command.
84 * These depend on the actual content being listed. If, for instance only files
85 * with duration less than an hour are being listed, then the duration with is
86 * made smaller because then the duration is listed as mm:ss rather than
90 /** size of the score field. */
91 unsigned short score_width
;
92 /** size of the image id field. */
93 unsigned short image_id_width
;
94 /** size of the lyrics id field. */
95 unsigned short lyrics_id_width
;
96 /** size of the bitrate field. */
97 unsigned short bitrate_width
;
98 /** size of the frequency field. */
99 unsigned short frequency_width
;
100 /** size of the duration field. */
101 unsigned short duration_width
;
102 /** size of the num played field. */
103 unsigned short num_played_width
;
104 /** size of the amp field. */
105 unsigned short amp_width
;
108 /** Data passed from the ls command handler to its callback function. */
110 /** The given command line flags. */
112 /** The sorting method given at the command line. */
113 enum ls_sorting_method sorting
;
114 /** The given listing mode (short, long, verbose, mbox). */
115 enum ls_listing_mode mode
;
116 /** The arguments passed to the ls command. */
118 /** Number of non-option arguments. */
120 /** Used for long listing mode to align the output fields. */
121 struct ls_widths widths
;
122 /** Size of the \a data array. */
124 /** Number of used entries in the data array. */
125 uint32_t num_matching_paths
;
126 /** Array of matching entries. */
127 struct ls_data
*data
;
128 /** Used to sort the array. */
129 struct ls_data
**data_ptr
;
133 * Describes the layout of the mmapped-afs info struct.
135 * \sa struct afs_info.
138 /** Where .last_played is stored. */
139 AFSI_LAST_PLAYED_OFFSET
= 0,
140 /** Storage position of the attributes bitmap. */
141 AFSI_ATTRIBUTES_OFFSET
= 8,
142 /** Storage position of the .num_played field. */
143 AFSI_NUM_PLAYED_OFFSET
= 16,
144 /** Storage position of the .image_id field. */
145 AFSI_IMAGE_ID_OFFSET
= 20,
146 /** Storage position of the .lyrics_id field. */
147 AFSI_LYRICS_ID_OFFSET
= 24,
148 /** Storage position of the .audio_format_id field. */
149 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
150 /** Storage position of the amplification field. */
151 AFSI_AMP_OFFSET
= 29,
152 /** 2 bytes reserved space for future usage. */
153 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 30,
154 /** On-disk storage space needed. */
159 * Convert a struct afs_info to an osl object.
161 * \param afsi Pointer to the audio file info to be converted.
162 * \param obj Result pointer.
166 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
168 char *buf
= obj
->data
;
170 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
171 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
172 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
173 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
174 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
175 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
176 afsi
->audio_format_id
);
177 write_u8(buf
+ AFSI_AMP_OFFSET
, afsi
->amp
);
178 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 2);
182 * Get the audio file selector info struct stored in an osl object.
184 * \param afsi Points to the audio_file info structure to be filled in.
185 * \param obj The osl object holding the data.
187 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
191 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
193 char *buf
= obj
->data
;
194 if (obj
->size
< AFSI_SIZE
)
196 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
197 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
198 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
199 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
200 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
201 afsi
->audio_format_id
= read_u8(buf
+
202 AFSI_AUDIO_FORMAT_ID_OFFSET
);
203 afsi
->amp
= read_u8(buf
+ AFSI_AMP_OFFSET
);
207 /** The columns of the audio file table. */
208 enum audio_file_table_columns
{
209 /** The hash on the content of the audio file. */
211 /** The full path in the filesystem. */
213 /** The audio file selector info. */
215 /** The audio format handler info. */
217 /** The chunk table info and the chunk table of the audio file. */
219 /** The number of columns of this table. */
224 * Compare two osl objects pointing to hash values.
226 * \param obj1 Pointer to the first hash object.
227 * \param obj2 Pointer to the second hash object.
229 * \return The values required for an osl compare function.
231 * \sa osl_compare_func, uint32_compare().
233 int aft_hash_compare(const struct osl_object
*obj1
, const struct osl_object
*obj2
)
235 return hash_compare((HASH_TYPE
*)obj1
->data
, (HASH_TYPE
*)obj2
->data
);
238 static struct osl_column_description aft_cols
[] = {
240 .storage_type
= OSL_MAPPED_STORAGE
,
241 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
243 .compare_function
= aft_hash_compare
,
244 .data_size
= HASH_SIZE
247 .storage_type
= OSL_MAPPED_STORAGE
,
248 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
250 .compare_function
= string_compare
,
253 .storage_type
= OSL_MAPPED_STORAGE
,
254 .storage_flags
= OSL_FIXED_SIZE
,
256 .data_size
= AFSI_SIZE
259 .storage_type
= OSL_MAPPED_STORAGE
,
263 .storage_type
= OSL_DISK_STORAGE
,
268 static struct osl_table_description audio_file_table_desc
= {
269 .name
= "audio_files",
270 .num_columns
= NUM_AFT_COLUMNS
,
271 .flags
= OSL_LARGE_TABLE
,
272 .column_descriptions
= aft_cols
275 /* We don't want * dot or dot-dot anywhere. */
276 static int verify_dotfile(const char *rest
)
279 * The first character was '.', but that has already been discarded, we
283 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
285 case '.': /* path start with /foo/.. */
286 if (rest
[1] == '\0' || rest
[1] == '/')
287 return -1; /* /foo/.. or /foo/../bar are not ok */
288 /* /foo/..bar is ok */
294 * We fundamentally don't like some paths: We don't want double slashes or
295 * slashes at the end that can make pathnames ambiguous.
297 static int verify_path(const char *orig_path
, char **resolved_path
)
303 if (*orig_path
!= '/') /* we only accept absolute paths */
305 len
= strlen(orig_path
);
306 *resolved_path
= para_strdup(orig_path
);
307 path
= *resolved_path
;
308 while (len
> 1 && path
[--len
] == '/')
309 path
[len
] = '\0'; /* remove slash at the end */
315 case '/': /* double slash */
318 if (verify_dotfile(path
) < 0)
328 free(*resolved_path
);
332 /** The on-disk layout of a afhi struct. */
334 /** Where the number of seconds is stored. */
335 AFHI_SECONDS_TOTAL_OFFSET
= 0,
336 /** Position of the bitrate. */
337 AFHI_BITRATE_OFFSET
= 4,
338 /** Position of the frequency. */
339 AFHI_FREQUENCY_OFFSET
= 8,
340 /** Location of the audio file header. */
341 AFHI_HEADER_OFFSET_OFFSET
= 12,
342 /* Length of the audio file header. Zero means: No header. */
343 AFHI_HEADER_LEN_OFFSET
= 16,
344 /** The total number of chunks (4 bytes). */
345 CHUNKS_TOTAL_OFFSET
= 20,
346 /** The length of the audio file header (4 bytes). */
347 HEADER_LEN_OFFSET
= 24,
348 /** The start of the audio file header (4 bytes). */
349 HEADER_OFFSET_OFFSET
= 28,
350 /** The seconds part of the chunk time (4 bytes). */
351 CHUNK_TV_TV_SEC_OFFSET
= 32,
352 /** The microseconds part of the chunk time (4 bytes). */
353 CHUNK_TV_TV_USEC_OFFSET
= 36,
354 /** Number of channels is stored here. (1 byte) */
355 AFHI_CHANNELS_OFFSET
= 40,
356 /** The tag info position. */
357 AFHI_INFO_STRING_OFFSET
= 41,
358 /** Minimal on-disk size of a valid afhi struct. */
359 MIN_AFHI_SIZE
= 47, /* at least 6 null bytes for techinfo/tags */
362 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
367 + strlen(afhi
->techinfo
)
368 + strlen(afhi
->tags
.artist
)
369 + strlen(afhi
->tags
.title
)
370 + strlen(afhi
->tags
.year
)
371 + strlen(afhi
->tags
.album
)
372 + strlen(afhi
->tags
.comment
);
375 static void save_afhi(struct afh_info
*afhi
, char *buf
)
381 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
, afhi
->seconds_total
);
382 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
383 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
384 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
385 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
386 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
387 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
388 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
389 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
390 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
391 write_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
, afhi
->chunk_tv
.tv_usec
);
392 p
= buf
+ AFHI_INFO_STRING_OFFSET
;
393 /* The sprintf's below are OK as our caller made sure that buf is large enough */
394 p
+= sprintf(p
, "%s", afhi
->techinfo
) + 1;
395 p
+= sprintf(p
, "%s", afhi
->tags
.artist
) + 1;
396 p
+= sprintf(p
, "%s", afhi
->tags
.title
) + 1;
397 p
+= sprintf(p
, "%s", afhi
->tags
.year
) + 1;
398 p
+= sprintf(p
, "%s", afhi
->tags
.album
) + 1;
399 sprintf(p
, "%s", afhi
->tags
.comment
);
402 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
404 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
405 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
406 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
407 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
408 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
409 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
410 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
411 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
412 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
413 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
414 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
);
415 afhi
->techinfo
= buf
+ AFHI_INFO_STRING_OFFSET
;
416 afhi
->tags
.artist
= afhi
->techinfo
+ strlen(afhi
->techinfo
) + 1;
417 afhi
->tags
.title
= afhi
->tags
.artist
+ strlen(afhi
->tags
.artist
) + 1;
418 afhi
->tags
.year
= afhi
->tags
.title
+ strlen(afhi
->tags
.title
) + 1;
419 afhi
->tags
.album
= afhi
->tags
.year
+ strlen(afhi
->tags
.year
) + 1;
420 afhi
->tags
.comment
= afhi
->tags
.album
+ strlen(afhi
->tags
.album
) + 1;
423 static unsigned sizeof_chunk_table(struct afh_info
*afhi
)
427 return 4 * (afhi
->chunks_total
+ 1);
430 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
434 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
435 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
438 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
442 afhi
->chunk_table
= para_malloc(sizeof_chunk_table(afhi
));
443 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
444 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
448 * Get the row of the audio file table corresponding to the given path.
450 * \param path The full path of the audio file.
451 * \param row Result pointer.
455 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
457 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
459 return osl(osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
));
463 * Get the row of the audio file table corresponding to the given hash value.
465 * \param hash The hash value of the desired audio file.
466 * \param row Result pointer.
470 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
472 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
473 return osl(osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
));
477 * Get the osl object holding the audio file selector info of a row.
479 * \param row Pointer to a row in the audio file table.
480 * \param obj Result pointer.
484 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
486 return osl(osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
));
490 * Get the osl object holding the audio file selector info, given a path.
493 * \param path The full path of the audio file.
494 * \param obj Result pointer.
496 * \return Positive on success, negative on errors.
498 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
501 int ret
= aft_get_row_of_path(path
, &row
);
504 return get_afsi_object_of_row(row
, obj
);
508 * Get the audio file selector info, given a row of the audio file table.
510 * \param row Pointer to a row in the audio file table.
511 * \param afsi Result pointer.
513 * \return Positive on success, negative on errors.
515 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
517 struct osl_object obj
;
518 int ret
= get_afsi_object_of_row(row
, &obj
);
521 return load_afsi(afsi
, &obj
);
525 * Get the audio file selector info, given the path of an audio table.
527 * \param path The full path of the audio file.
528 * \param afsi Result pointer.
530 * \return Positive on success, negative on errors.
532 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
534 struct osl_object obj
;
535 int ret
= get_afsi_object_of_path(path
, &obj
);
538 return load_afsi(afsi
, &obj
);
542 * Get the path of an audio file, given a row of the audio file table.
544 * \param row Pointer to a row in the audio file table.
545 * \param path Result pointer.
547 * The result is a pointer to mmapped data. The caller must not attempt
552 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
554 struct osl_object path_obj
;
555 int ret
= osl(osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
559 *path
= path_obj
.data
;
564 * Get the object containing the hash value of an audio file, given a row.
566 * \param row Pointer to a row of the audio file table.
567 * \param obj Result pointer.
569 * \return The return value of the underlying call to osl_get_object().
571 * \sa get_hash_of_row().
573 static int get_hash_object_of_aft_row(const struct osl_row
*row
,
574 struct osl_object
*obj
)
576 return osl(osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
));
580 * Get the hash value of an audio file, given a row of the audio file table.
582 * \param row Pointer to a row of the audio file table.
583 * \param hash Result pointer.
585 * \a hash points to mapped data and must not be freed by the caller.
587 * \return The return value of the underlying call to
588 * get_hash_object_of_aft_row().
590 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
592 struct osl_object obj
;
593 int ret
= get_hash_object_of_aft_row(row
, &obj
);
602 * Get the audio format handler info, given a row of the audio file table.
604 * \param row Pointer to a row of the audio file table.
605 * \param afhi Result pointer.
607 * \return The return value of the underlying call to osl_get_object().
609 * \sa get_chunk_table_of_row().
611 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
613 struct osl_object obj
;
614 int ret
= osl(osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
618 load_afhi(obj
.data
, afhi
);
622 /* returns shmid on success */
623 static int save_afd(struct audio_file_data
*afd
)
625 size_t size
= sizeof(*afd
) + sizeof_chunk_table(&afd
->afhi
);
626 int shmid
, ret
= shm_new(size
);
633 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
636 *(struct audio_file_data
*)shm_afd
= *afd
;
639 save_chunk_table(&afd
->afhi
, buf
);
647 int load_afd(int shmid
, struct audio_file_data
*afd
)
653 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
656 *afd
= *(struct audio_file_data
*)shm_afd
;
659 load_chunk_table(&afd
->afhi
, buf
);
664 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
665 time_t current_time
, enum ls_listing_mode lm
)
669 if (!localtime_r((time_t *)seconds
, &t
))
671 if (lm
== LS_MODE_MBOX
) {
672 if (!strftime(buf
, size
, "%c", &t
))
676 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
677 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
681 if (!strftime(buf
, size
, "%b %e %Y", &t
))
686 /** Compute the number of (decimal) digits of a number. */
687 #define GET_NUM_DIGITS(x, num) { \
688 typeof((x)) _tmp = PARA_ABS(x); \
691 while ((_tmp) > 9) { \
697 static short unsigned get_duration_width(int seconds
)
699 short unsigned width
;
700 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
702 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
703 return 4 + (mins
> 9);
704 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
705 GET_NUM_DIGITS(hours
, &width
);
709 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
711 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
712 short unsigned max_width
;
714 if (!hours
) { /* m:ss or mm:ss */
715 max_width
= opts
->mode
== LS_MODE_LONG
?
716 opts
->widths
.duration_width
: 4;
717 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
718 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
719 max_width
= opts
->mode
== LS_MODE_LONG
?
720 opts
->widths
.duration_width
: 7;
721 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
726 static int make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
,
730 int ret
= get_attribute_text(&afsi
->attributes
, " ", &att_text
);
734 *result
= make_message("%s: %s\n%s: %s",
735 status_item_list
[SI_ATTRIBUTES_BITMAP
], att_bitmap
,
736 status_item_list
[SI_ATTRIBUTES_TXT
], att_text
);
741 static char *make_lyrics_lines(struct afs_info
*afsi
)
745 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
746 return make_message("%s: %u\n%s: %s\n",
747 status_item_list
[SI_LYRICS_ID
], afsi
->lyrics_id
,
748 status_item_list
[SI_LYRICS_NAME
], lyrics_name
?
749 lyrics_name
: "(none)");
752 static char *make_image_lines(struct afs_info
*afsi
)
755 img_get_name_by_id(afsi
->image_id
, &image_name
);
756 return make_message("%s: %u\n%s: %s\n",
757 status_item_list
[SI_IMAGE_ID
], afsi
->image_id
,
758 status_item_list
[SI_IMAGE_NAME
], image_name
?
759 image_name
: "(none)");
762 static char *make_filename_lines(const char *path
, unsigned flags
)
765 const char *basename
;
767 if (!(flags
& LS_FLAG_FULL_PATH
))
768 return make_message("%s: %s\n",
769 status_item_list
[SI_BASENAME
], path
);
770 basename
= para_basename(path
),
771 dirname
= para_dirname(path
);
772 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n",
773 status_item_list
[SI_PATH
], path
,
774 status_item_list
[SI_DIRECTORY
], dirname
? dirname
: "?",
775 status_item_list
[SI_BASENAME
], basename
? basename
: "?");
780 static int print_chunk_table(struct ls_data
*d
, struct para_buffer
*b
)
782 struct osl_object chunk_table_obj
;
783 struct osl_row
*aft_row
;
787 ret
= aft_get_row_of_hash(d
->hash
, &aft_row
);
790 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
791 AFTCOL_CHUNKS
, &chunk_table_obj
);
794 ret
= para_printf(b
, "%s\n"
795 "chunk_time: %lu:%lu\nchunk_offsets: ",
797 (long unsigned) d
->afhi
.chunk_tv
.tv_sec
,
798 (long unsigned) d
->afhi
.chunk_tv
.tv_usec
802 buf
= chunk_table_obj
.data
;
803 for (i
= 0; i
<= d
->afhi
.chunks_total
; i
++) {
804 ret
= para_printf(b
, "%u ", (unsigned) read_u32(buf
+ 4 * i
));
808 ret
= para_printf(b
, "\n");
810 osl_close_disk_object(&chunk_table_obj
);
814 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
815 struct para_buffer
*b
, time_t current_time
)
819 char last_played_time
[30];
820 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
821 char score_buf
[30] = "";
822 struct afs_info
*afsi
= &d
->afsi
;
823 struct afh_info
*afhi
= &d
->afhi
;
824 struct ls_widths
*w
= &opts
->widths
;
825 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
826 char asc_hash
[2 * HASH_SIZE
+ 1];
827 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
829 if (opts
->mode
== LS_MODE_SHORT
) {
830 ret
= para_printf(b
, "%s\n", d
->path
);
833 if (opts
->mode
== LS_MODE_CHUNKS
) {
834 ret
= print_chunk_table(d
, b
);
837 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
838 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
839 sizeof(last_played_time
), current_time
, opts
->mode
);
842 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
844 if (opts
->mode
== LS_MODE_LONG
)
845 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
847 sprintf(score_buf
, "%li ", d
->score
);
850 if (opts
->mode
== LS_MODE_LONG
) {
853 "%s " /* attributes */
855 "%*d " /* image_id */
856 "%*d " /* lyrics_id */
858 "%s " /* audio format */
859 "%*d " /* frequency */
862 "%*d " /* num_played */
863 "%s " /* last_played */
867 w
->amp_width
, afsi
->amp
,
868 w
->image_id_width
, afsi
->image_id
,
869 w
->lyrics_id_width
, afsi
->lyrics_id
,
870 w
->bitrate_width
, afhi
->bitrate
,
871 audio_format_name(afsi
->audio_format_id
),
872 w
->frequency_width
, afhi
->frequency
,
875 w
->num_played_width
, afsi
->num_played
,
881 hash_to_asc(d
->hash
, asc_hash
);
882 ret
= make_attribute_lines(att_buf
, afsi
, &att_lines
);
885 lyrics_lines
= make_lyrics_lines(afsi
);
886 image_lines
= make_image_lines(afsi
);
887 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
888 if (opts
->mode
== LS_MODE_MBOX
) {
889 const char *bn
= para_basename(d
->path
);
891 "From foo@localhost %s\n"
892 "Received: from\nTo: bar\nFrom: a\n"
900 "%s" /* filename stuff */
901 "%s%s%s%s" /* score */
902 "%s\n" /* attributes */
903 "%s: %s\n" /* hash */
904 "%s" /* image id, image name */
906 "%s: %dkbit/s\n" /* bitrate */
907 "%s: %s\n" /* format */
908 "%s: %dHz\n" /* frequency */
909 "%s: %d\n" /* channels */
910 "%s: %s\n" /* duration */
911 "%s: %lu\n" /* seconds total */
912 "%s: %s\n" /* last played time */
913 "%s: %d\n" /* num_played */
914 "%s: %u\n" /* amplification */
915 "%s: %lu\n" /* chunk time */
916 "%s: %lu\n" /* num chunks */
917 "%s: %s\n" /* techinfo */
918 "%s: %s\n" /* artist */
919 "%s: %s\n" /* title */
920 "%s: %s\n" /* year */
921 "%s: %s\n" /* album */
922 "%s: %s\n", /* comment */
924 have_score
? status_item_list
[SI_SCORE
] : "",
925 have_score
? ": " : "",
927 have_score
? "\n" : "",
929 status_item_list
[SI_HASH
], asc_hash
,
932 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
933 status_item_list
[SI_FORMAT
], audio_format_name(afsi
->audio_format_id
),
934 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
935 status_item_list
[SI_CHANNELS
], afhi
->channels
,
936 status_item_list
[SI_DURATION
], duration_buf
,
937 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
938 status_item_list
[SI_LAST_PLAYED
], last_played_time
,
939 status_item_list
[SI_NUM_PLAYED
], afsi
->num_played
,
940 status_item_list
[SI_AMPLIFICATION
], afsi
->amp
,
941 status_item_list
[SI_CHUNK_TIME
], tv2ms(&afhi
->chunk_tv
),
942 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
,
943 status_item_list
[SI_TECHINFO
], afhi
->techinfo
,
944 status_item_list
[SI_ARTIST
], afhi
->tags
.artist
,
945 status_item_list
[SI_TITLE
], afhi
->tags
.title
,
946 status_item_list
[SI_YEAR
], afhi
->tags
.year
,
947 status_item_list
[SI_ALBUM
], afhi
->tags
.album
,
948 status_item_list
[SI_COMMENT
], afhi
->tags
.comment
952 if (opts
->mode
== LS_MODE_MBOX
) {
953 struct osl_object lyrics_def
;
954 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
955 if (lyrics_def
.data
) {
956 ret
= para_printf(b
, "Lyrics:\n~~~~~~~\n%s",
957 (char *)lyrics_def
.data
);
958 osl_close_disk_object(&lyrics_def
);
964 free(filename_lines
);
969 static int make_status_items(struct audio_file_data
*afd
,
970 struct afs_info
*afsi
, char *path
, long score
,
980 struct ls_options opts
= {
981 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
982 .mode
= LS_MODE_VERBOSE
,
984 struct para_buffer pb
= {.max_size
= SHMMAX
- 1};
989 ret
= print_list_item(&d
, &opts
, &pb
, current_time
);
992 free(current_status_items
);
993 current_status_items
= pb
.buf
;
998 * Mmap the given audio file and update statistics.
1000 * \param aft_row Determines the audio file to be opened and updated.
1001 * \param score The score of the audio file.
1002 * \param afd Result pointer.
1004 * On success, the numplayed field of the audio file selector info is increased
1005 * and the lastplayed time is set to the current time. Finally, the score of
1006 * the audio file is updated.
1008 * \return Positive shmid on success, negative on errors.
1010 int open_and_update_audio_file(struct osl_row
*aft_row
, long score
,
1011 struct audio_file_data
*afd
)
1013 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
1014 struct osl_object afsi_obj
;
1015 struct afs_info old_afsi
, new_afsi
;
1016 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
1017 struct afsi_change_event_data aced
;
1018 struct osl_object map
, chunk_table_obj
;
1023 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1026 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
1029 ret
= load_afsi(&old_afsi
, &afsi_obj
);
1032 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
1035 afd
->afhi
.chunk_table
= NULL
;
1036 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
1037 AFTCOL_CHUNKS
, &chunk_table_obj
);
1040 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
1041 &map
.size
, &afd
->fd
);
1044 hash_function(map
.data
, map
.size
, file_hash
);
1045 ret
= hash_compare(file_hash
, aft_hash
);
1046 para_munmap(map
.data
, map
.size
);
1048 ret
= -E_HASH_MISMATCH
;
1051 new_afsi
= old_afsi
;
1052 new_afsi
.num_played
++;
1053 new_afsi
.last_played
= time(NULL
);
1054 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
1056 load_chunk_table(&afd
->afhi
, chunk_table_obj
.data
);
1057 ret
= make_status_items(afd
, &old_afsi
, path
, score
, file_hash
);
1060 aced
.aft_row
= aft_row
;
1061 aced
.old_afsi
= &old_afsi
;
1062 afs_event(AFSI_CHANGE
, NULL
, &aced
);
1063 ret
= save_afd(afd
);
1065 free(afd
->afhi
.chunk_table
);
1066 osl_close_disk_object(&chunk_table_obj
);
1070 static int ls_audio_format_compare(const void *a
, const void *b
)
1072 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1073 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1076 static int ls_duration_compare(const void *a
, const void *b
)
1078 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1079 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1082 static int ls_bitrate_compare(const void *a
, const void *b
)
1084 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1085 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1088 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1090 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1091 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1094 static int ls_image_id_compare(const void *a
, const void *b
)
1096 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1097 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1100 static int ls_channels_compare(const void *a
, const void *b
)
1102 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1103 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1106 static int ls_frequency_compare(const void *a
, const void *b
)
1108 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1109 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1112 static int ls_num_played_compare(const void *a
, const void *b
)
1114 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1115 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1118 static int ls_last_played_compare(const void *a
, const void *b
)
1120 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1121 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1124 static int ls_score_compare(const void *a
, const void *b
)
1126 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1127 return NUM_COMPARE(d1
->score
, d2
->score
);
1130 static int ls_path_compare(const void *a
, const void *b
)
1132 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1133 return strcmp(d1
->path
, d2
->path
);
1136 static int sort_matching_paths(struct ls_options
*options
)
1138 size_t nmemb
= options
->num_matching_paths
;
1139 size_t size
= sizeof(*options
->data_ptr
);
1140 int (*compar
)(const void *, const void *);
1143 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1144 for (i
= 0; i
< nmemb
; i
++)
1145 options
->data_ptr
[i
] = options
->data
+ i
;
1147 /* In these cases the array is already sorted */
1148 if (options
->sorting
== LS_SORT_BY_PATH
1149 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1150 && (options
->flags
& LS_FLAG_FULL_PATH
))
1152 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1153 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1156 switch (options
->sorting
) {
1157 case LS_SORT_BY_PATH
:
1158 compar
= ls_path_compare
; break;
1159 case LS_SORT_BY_SCORE
:
1160 compar
= ls_score_compare
; break;
1161 case LS_SORT_BY_LAST_PLAYED
:
1162 compar
= ls_last_played_compare
; break;
1163 case LS_SORT_BY_NUM_PLAYED
:
1164 compar
= ls_num_played_compare
; break;
1165 case LS_SORT_BY_FREQUENCY
:
1166 compar
= ls_frequency_compare
; break;
1167 case LS_SORT_BY_CHANNELS
:
1168 compar
= ls_channels_compare
; break;
1169 case LS_SORT_BY_IMAGE_ID
:
1170 compar
= ls_image_id_compare
; break;
1171 case LS_SORT_BY_LYRICS_ID
:
1172 compar
= ls_lyrics_id_compare
; break;
1173 case LS_SORT_BY_BITRATE
:
1174 compar
= ls_bitrate_compare
; break;
1175 case LS_SORT_BY_DURATION
:
1176 compar
= ls_duration_compare
; break;
1177 case LS_SORT_BY_AUDIO_FORMAT
:
1178 compar
= ls_audio_format_compare
; break;
1182 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1186 /* row is either an aft_row or a row of the score table */
1187 /* TODO: Only compute widths if we need them */
1188 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1191 struct ls_options
*options
= ls_opts
;
1193 struct ls_widths
*w
;
1194 unsigned short num_digits
;
1196 struct osl_row
*aft_row
;
1200 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1201 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1206 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1209 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1210 char *p
= strrchr(path
, '/');
1214 if (options
->num_patterns
) {
1215 for (i
= 0; i
< options
->num_patterns
; i
++) {
1216 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1219 if (ret
== FNM_NOMATCH
)
1223 if (i
>= options
->num_patterns
) /* no match */
1226 tmp
= options
->num_matching_paths
++;
1227 if (options
->num_matching_paths
> options
->array_size
) {
1228 options
->array_size
++;
1229 options
->array_size
*= 2;
1230 options
->data
= para_realloc(options
->data
, options
->array_size
1231 * sizeof(*options
->data
));
1233 d
= options
->data
+ tmp
;
1234 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1237 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1241 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1244 w
= &options
->widths
;
1245 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1246 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1247 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1248 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1249 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1250 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1251 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1252 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1253 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1254 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1255 /* get the number of chars to print this amount of time */
1256 num_digits
= get_duration_width(d
->afhi
.seconds_total
);
1257 w
->duration_width
= PARA_MAX(w
->duration_width
, num_digits
);
1258 GET_NUM_DIGITS(d
->afsi
.amp
, &num_digits
);
1259 w
->amp_width
= PARA_MAX(w
->amp_width
, num_digits
);
1260 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1261 GET_NUM_DIGITS(score
, &num_digits
);
1262 num_digits
++; /* add one for the sign (space or "-") */
1263 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1271 static void com_ls_callback(int fd
, const struct osl_object
*query
)
1273 struct ls_options
*opts
= query
->data
;
1274 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1275 struct para_buffer b
= {.max_size
= SHMMAX
,
1276 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1278 time_t current_time
;
1281 if (opts
->num_patterns
) {
1282 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1283 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1284 opts
->patterns
[i
] = p
;
1288 opts
->patterns
= NULL
;
1289 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1290 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1292 ret
= osl(osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1296 if (!opts
->num_matching_paths
)
1298 ret
= sort_matching_paths(opts
);
1301 time(¤t_time
);
1302 if (opts
->flags
& LS_FLAG_REVERSE
)
1303 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1304 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1309 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1310 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1316 pass_buffer_as_shm(b
.buf
, b
.offset
, &fd
);
1319 free(opts
->data_ptr
);
1320 free(opts
->patterns
);
1324 * TODO: flags -h (sort by hash)
1326 int com_ls(int fd
, int argc
, char * const * const argv
)
1330 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1331 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1332 struct ls_options opts
= {.patterns
= NULL
};
1333 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)};
1335 for (i
= 1; i
< argc
; i
++) {
1336 const char *arg
= argv
[i
];
1339 if (!strcmp(arg
, "--")) {
1343 if (!strncmp(arg
, "-l", 2)) {
1345 mode
= LS_MODE_LONG
;
1349 return -E_AFT_SYNTAX
;
1350 switch(*(arg
+ 2)) {
1352 mode
= LS_MODE_SHORT
;
1355 mode
= LS_MODE_LONG
;
1358 mode
= LS_MODE_VERBOSE
;
1361 mode
= LS_MODE_MBOX
;
1364 mode
= LS_MODE_CHUNKS
;
1367 return -E_AFT_SYNTAX
;
1370 if (!strcmp(arg
, "-p")) {
1371 flags
|= LS_FLAG_FULL_PATH
;
1374 if (!strcmp(arg
, "-a")) {
1375 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1378 if (!strcmp(arg
, "-r")) {
1379 flags
|= LS_FLAG_REVERSE
;
1382 if (!strncmp(arg
, "-s", 2)) {
1383 if (!*(arg
+ 2) || *(arg
+ 3))
1384 return -E_AFT_SYNTAX
;
1385 switch(*(arg
+ 2)) {
1387 sort
= LS_SORT_BY_PATH
;
1389 case 's': /* -ss implies -a */
1390 sort
= LS_SORT_BY_SCORE
;
1391 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1394 sort
= LS_SORT_BY_LAST_PLAYED
;
1397 sort
= LS_SORT_BY_NUM_PLAYED
;
1400 sort
= LS_SORT_BY_FREQUENCY
;
1403 sort
= LS_SORT_BY_CHANNELS
;
1406 sort
= LS_SORT_BY_IMAGE_ID
;
1409 sort
= LS_SORT_BY_LYRICS_ID
;
1412 sort
= LS_SORT_BY_BITRATE
;
1415 sort
= LS_SORT_BY_DURATION
;
1418 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1421 return -E_AFT_SYNTAX
;
1424 return -E_AFT_SYNTAX
;
1427 opts
.sorting
= sort
;
1429 opts
.num_patterns
= argc
- i
;
1430 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1431 argv
+ i
, com_ls_callback
, send_result
, &fd
);
1436 * Call the given function for each file in the audio file table.
1438 * \param private_data An arbitrary data pointer, passed to \a func.
1439 * \param func The custom function to be called.
1443 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1445 return osl(osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1449 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1451 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1452 struct osl_row
*row
;
1454 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1458 /** The format of the data stored by save_audio_file_data(). */
1459 enum com_add_buffer_offsets
{
1460 /** afhi (if present) starts here. */
1461 CAB_AFHI_OFFSET_POS
= 0,
1462 /** Start of the chunk table (if present). */
1463 CAB_CHUNKS_OFFSET_POS
= 2,
1464 /** Audio format id. */
1465 CAB_AUDIO_FORMAT_OFFSET
= 4,
1466 /** Flags given to the add command. */
1467 CAB_FLAGS_OFFSET
= 5,
1468 /** The hash of the audio file being added. */
1469 CAB_HASH_OFFSET
= 9,
1470 /** Start of the path of the audio file. */
1471 CAB_PATH_OFFSET
= (CAB_HASH_OFFSET
+ HASH_SIZE
),
1475 * Store the given data to a single buffer. Doesn't need the audio file selector
1476 * info struct as the server knows it as well.
1478 * It's OK to call this with afhi == NULL. In this case, the audio format
1479 * handler info won't be stored in the buffer.
1481 static void save_add_callback_buffer(HASH_TYPE
*hash
, const char *path
,
1482 struct afh_info
*afhi
, uint32_t flags
,
1483 uint8_t audio_format_num
, struct osl_object
*obj
)
1485 size_t path_len
= strlen(path
) + 1;
1486 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1487 size_t size
= CAB_PATH_OFFSET
+ path_len
+ afhi_size
1488 + sizeof_chunk_table(afhi
);
1489 char *buf
= para_malloc(size
);
1492 write_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1493 write_u32(buf
+ CAB_FLAGS_OFFSET
, flags
);
1495 memcpy(buf
+ CAB_HASH_OFFSET
, hash
, HASH_SIZE
);
1496 strcpy(buf
+ CAB_PATH_OFFSET
, path
);
1498 pos
= CAB_PATH_OFFSET
+ path_len
;
1499 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1500 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1501 pos
+ afhi_size
- 1);
1502 write_u16(buf
+ CAB_AFHI_OFFSET_POS
, pos
);
1503 save_afhi(afhi
, buf
+ pos
);
1506 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1507 write_u16(buf
+ CAB_CHUNKS_OFFSET_POS
, pos
);
1509 save_chunk_table(afhi
, buf
+ pos
);
1510 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1517 Overview of the add command.
1519 Input: What was passed to the callback by the command handler.
1521 HS: Hash sister. Whether an audio file with identical hash
1522 already exists in the osl database.
1524 PB: Path brother. Whether a file with the given path exists
1527 F: Force flag given. Whether add was called with -f.
1529 output: Action performed by the callback.
1531 AFHI: Whether afhi and chunk table are computed and sent.
1532 ACTION: Table modifications to be done by the callback.
1534 +----+----+---+------+---------------------------------------------------+
1535 | HS | PB | F | AFHI | ACTION
1536 +----+----+---+------+---------------------------------------------------+
1537 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1538 | | update path, keep afsi
1539 +----+----+---+------+---------------------------------------------------+
1540 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1541 | | otherwise: remove PB, HS: update path, keep afhi,
1543 +----+----+---+------+---------------------------------------------------+
1544 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1546 +----+----+---+------+---------------------------------------------------+
1547 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1548 +----+----+---+------+---------------------------------------------------+
1549 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1550 | | (force has no effect)
1551 +----+----+---+------+---------------------------------------------------+
1552 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1553 +----+----+---+------+---------------------------------------------------+
1554 | N | N | Y | Y | (new file) create new entry (force has no effect)
1555 +----+----+---+------+---------------------------------------------------+
1556 | N | N | N | Y | (new file) create new entry
1557 +----+----+---+------+---------------------------------------------------+
1561 afhi <=> force or no HS
1566 /** Flags passed to the add command. */
1567 enum com_add_flags
{
1568 /** Skip paths that exist already. */
1570 /** Force adding. */
1572 /** Print what is being done. */
1573 ADD_FLAG_VERBOSE
= 4,
1574 /** Try to add files with unknown suffixes. */
1578 static void com_add_callback(int fd
, const struct osl_object
*query
)
1580 char *buf
= query
->data
, *path
;
1581 struct osl_row
*pb
, *aft_row
;
1583 struct osl_object objs
[NUM_AFT_COLUMNS
];
1585 char asc
[2 * HASH_SIZE
+ 1];
1587 char afsi_buf
[AFSI_SIZE
];
1588 uint32_t flags
= read_u32(buf
+ CAB_FLAGS_OFFSET
);
1589 struct afs_info default_afsi
= {.last_played
= 0};
1590 struct para_buffer msg
= {.max_size
= SHMMAX
,
1591 .max_size_handler
= pass_buffer_as_shm
, .private_data
= &fd
};
1592 uint16_t afhi_offset
, chunks_offset
;
1594 hash
= (HASH_TYPE
*)buf
+ CAB_HASH_OFFSET
;
1595 hash_to_asc(hash
, asc
);;
1596 objs
[AFTCOL_HASH
].data
= buf
+ CAB_HASH_OFFSET
;
1597 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1599 path
= buf
+ CAB_PATH_OFFSET
;
1600 objs
[AFTCOL_PATH
].data
= path
;
1601 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1603 PARA_INFO_LOG("request to add %s\n", path
);
1604 hs
= find_hash_sister(hash
);
1605 ret
= aft_get_row_of_path(path
, &pb
);
1606 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
1608 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1609 if (flags
& ADD_FLAG_VERBOSE
)
1610 ret
= para_printf(&msg
, "ignoring duplicate\n");
1615 if (hs
&& hs
!= pb
) {
1616 struct osl_object obj
;
1617 if (pb
) { /* hs trumps pb, remove pb */
1618 if (flags
& ADD_FLAG_VERBOSE
) {
1619 ret
= para_printf(&msg
, "removing path brother\n");
1623 ret
= osl(osl_del_row(audio_file_table
, pb
));
1628 /* file rename, update hs' path */
1629 if (flags
& ADD_FLAG_VERBOSE
) {
1630 ret
= osl(osl_get_object(audio_file_table
, hs
,
1631 AFTCOL_PATH
, &obj
));
1634 ret
= para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1638 ret
= osl(osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1639 &objs
[AFTCOL_PATH
]));
1642 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1643 if (!(flags
& ADD_FLAG_FORCE
))
1646 /* no hs or force mode, child must have sent afhi */
1647 afhi_offset
= read_u16(buf
+ CAB_AFHI_OFFSET_POS
);
1648 chunks_offset
= read_u16(buf
+ CAB_CHUNKS_OFFSET_POS
);
1650 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1651 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1653 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1655 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1656 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1657 if (pb
&& !hs
) { /* update pb's hash */
1658 char old_asc
[2 * HASH_SIZE
+ 1];
1659 HASH_TYPE
*old_hash
;
1660 ret
= get_hash_of_row(pb
, &old_hash
);
1663 hash_to_asc(old_hash
, old_asc
);
1664 if (flags
& ADD_FLAG_VERBOSE
) {
1665 ret
= para_printf(&msg
, "file change: %s -> %s\n",
1670 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1671 &objs
[AFTCOL_HASH
]);
1675 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1676 struct osl_row
*row
= pb
? pb
: hs
;
1677 /* update afhi and chunk_table */
1678 if (flags
& ADD_FLAG_VERBOSE
) {
1679 ret
= para_printf(&msg
, "updating afhi and chunk table\n");
1683 ret
= osl(osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1684 &objs
[AFTCOL_AFHI
]));
1687 ret
= osl(osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1688 &objs
[AFTCOL_CHUNKS
]));
1691 afs_event(AFHI_CHANGE
, &msg
, row
);
1694 /* new entry, use default afsi */
1695 if (flags
& ADD_FLAG_VERBOSE
) {
1696 ret
= para_printf(&msg
, "new file\n");
1700 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1701 default_afsi
.audio_format_id
= read_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
);
1703 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1704 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1705 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1706 ret
= osl(osl_add_and_get_row(audio_file_table
, objs
, &aft_row
));
1707 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1710 para_printf(&msg
, "%s\n", para_strerror(-ret
));
1712 pass_buffer_as_shm(msg
.buf
, msg
.offset
, &fd
);
1716 /** Used by com_add(). */
1717 struct private_add_data
{
1718 /** The socket file descriptor. */
1720 /** The given add flags. */
1724 static void path_brother_callback(int fd
, const struct osl_object
*query
)
1726 char *path
= query
->data
;
1727 struct osl_row
*path_brother
;
1728 int ret
= aft_get_row_of_path(path
, &path_brother
);
1731 pass_buffer_as_shm((char *)&path_brother
, sizeof(path_brother
), &fd
);
1734 static void hash_sister_callback(int fd
, const struct osl_object
*query
)
1736 HASH_TYPE
*hash
= query
->data
;
1737 struct osl_row
*hash_sister
;
1739 hash_sister
= find_hash_sister(hash
);
1742 pass_buffer_as_shm((char *)&hash_sister
, sizeof(hash_sister
), &fd
);
1745 static int get_row_pointer_from_result(struct osl_object
*result
, void *private)
1747 struct osl_row
**row
= private;
1748 *row
= result
->data
;
1752 static int add_one_audio_file(const char *path
, void *private_data
)
1754 int ret
, send_ret
= 1, fd
;
1755 uint8_t format_num
= -1;
1756 struct private_add_data
*pad
= private_data
;
1757 struct afh_info afhi
, *afhi_ptr
= NULL
;
1758 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1759 struct osl_object map
, obj
= {.data
= NULL
}, query
;
1760 HASH_TYPE hash
[HASH_SIZE
];
1762 ret
= guess_audio_format(path
);
1763 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1765 query
.data
= (char *)path
;
1766 query
.size
= strlen(path
) + 1;
1767 ret
= send_callback_request(path_brother_callback
, &query
,
1768 get_row_pointer_from_result
, &pb
);
1769 if (ret
< 0 && ret
!= -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND
))
1772 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1773 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1774 send_ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1777 /* We still want to add this file. Compute its hash. */
1778 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, &fd
);
1781 hash_function(map
.data
, map
.size
, hash
);
1783 /* Check whether the database contains a file with the same hash. */
1785 query
.size
= HASH_SIZE
;
1786 ret
= send_callback_request(hash_sister_callback
, &query
,
1787 get_row_pointer_from_result
, &hs
);
1790 /* Return success if we already know this file. */
1792 if (pb
&& hs
&& hs
== pb
&& !(pad
->flags
& ADD_FLAG_FORCE
)) {
1793 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1794 send_ret
= send_va_buffer(pad
->fd
,
1795 "%s exists, not forcing update\n", path
);
1799 * We won't recalculate the audio format info and the chunk table if
1800 * there is a hash sister and FORCE was not given.
1802 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1803 ret
= compute_afhi(path
, map
.data
, map
.size
, fd
, &afhi
);
1809 munmap(map
.data
, map
.size
);
1811 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1812 send_ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1816 save_add_callback_buffer(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1817 /* Ask afs to consider this entry for adding. */
1818 ret
= send_callback_request(com_add_callback
, &obj
, send_result
, &pad
->fd
);
1823 munmap(map
.data
, map
.size
);
1825 if (ret
< 0 && send_ret
>= 0)
1826 send_ret
= send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1827 para_strerror(-ret
));
1830 free(afhi_ptr
->chunk_table
);
1831 free(afhi_ptr
->techinfo
);
1832 free(afhi_ptr
->tags
.artist
);
1833 free(afhi_ptr
->tags
.title
);
1834 free(afhi_ptr
->tags
.year
);
1835 free(afhi_ptr
->tags
.album
);
1836 free(afhi_ptr
->tags
.comment
);
1838 /* Stop adding files only on send errors. */
1842 int com_add(int fd
, int argc
, char * const * const argv
)
1845 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1846 struct stat statbuf
;
1848 for (i
= 1; i
< argc
; i
++) {
1849 const char *arg
= argv
[i
];
1852 if (!strcmp(arg
, "--")) {
1856 if (!strcmp(arg
, "-a")) {
1857 pad
.flags
|= ADD_FLAG_ALL
;
1860 if (!strcmp(arg
, "-l")) {
1861 pad
.flags
|= ADD_FLAG_LAZY
;
1864 if (!strcmp(arg
, "-f")) {
1865 pad
.flags
|= ADD_FLAG_FORCE
;
1868 if (!strcmp(arg
, "-v")) {
1869 pad
.flags
|= ADD_FLAG_VERBOSE
;
1874 return -E_AFT_SYNTAX
;
1875 for (; i
< argc
; i
++) {
1877 ret
= verify_path(argv
[i
], &path
);
1879 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
],
1880 para_strerror(-ret
));
1885 ret
= stat(path
, &statbuf
);
1887 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1894 if (S_ISDIR(statbuf
.st_mode
))
1895 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1898 ret
= add_one_audio_file(path
, &pad
);
1900 send_va_buffer(fd
, "%s: %s\n", path
, para_strerror(-ret
));
1911 * Flags used by the touch command.
1916 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1917 TOUCH_FLAG_FNM_PATHNAME
= 1,
1918 /** Activates verbose mode. */
1919 TOUCH_FLAG_VERBOSE
= 2
1922 /** Options used by com_touch(). */
1923 struct com_touch_options
{
1924 /** New num_played value. */
1926 /** New last played count. */
1927 int64_t last_played
;
1928 /** New lyrics id. */
1930 /** New image id. */
1932 /** New amplification value. */
1934 /** Command line flags (see \ref touch_flags). */
1938 /** Data passed to the action handler of com_touch(). */
1939 struct touch_action_data
{
1940 /** Command line options (see \ref com_touch_options). */
1941 struct com_touch_options
*cto
;
1942 /** Message buffer. */
1943 struct para_buffer pb
;
1944 /** How many audio files matched the given pattern. */
1945 unsigned num_matches
;
1948 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1949 struct osl_row
*row
, const char *name
, void *data
)
1951 struct touch_action_data
*tad
= data
;
1952 struct osl_object obj
;
1953 struct afs_info old_afsi
, new_afsi
;
1954 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1955 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0 && tad
->cto
->amp
< 0;
1956 struct afsi_change_event_data aced
;
1958 ret
= get_afsi_object_of_row(row
, &obj
);
1960 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1961 ret
= load_afsi(&old_afsi
, &obj
);
1963 return para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1964 new_afsi
= old_afsi
;
1966 new_afsi
.num_played
++;
1967 new_afsi
.last_played
= time(NULL
);
1968 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
1969 ret
= para_printf(&tad
->pb
, "%s: num_played = %u, "
1970 "last_played = now()\n", name
,
1971 new_afsi
.num_played
);
1976 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
) {
1977 ret
= para_printf(&tad
->pb
, "touching %s\n", name
);
1981 if (tad
->cto
->lyrics_id
>= 0)
1982 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1983 if (tad
->cto
->image_id
>= 0)
1984 new_afsi
.image_id
= tad
->cto
->image_id
;
1985 if (tad
->cto
->num_played
>= 0)
1986 new_afsi
.num_played
= tad
->cto
->num_played
;
1987 if (tad
->cto
->last_played
>= 0)
1988 new_afsi
.last_played
= tad
->cto
->last_played
;
1989 if (tad
->cto
->amp
>= 0)
1990 new_afsi
.amp
= tad
->cto
->amp
;
1993 save_afsi(&new_afsi
, &obj
); /* in-place update */
1995 aced
.old_afsi
= &old_afsi
;
1996 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
2000 static void com_touch_callback(int fd
, const struct osl_object
*query
)
2002 struct touch_action_data tad
= {.cto
= query
->data
,
2005 .private_data
= &fd
,
2006 .max_size_handler
= pass_buffer_as_shm
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(*tad
.cto
),
2015 .size
= query
->size
- sizeof(*tad
.cto
)},
2017 .action
= touch_audio_file
2019 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
2020 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2021 ret
= for_each_matching_row(&pmd
);
2023 ret2
= para_printf(&tad
.pb
, "%s\n", para_strerror(-ret
));
2025 if (!tad
.num_matches
)
2026 ret2
= para_printf(&tad
.pb
, "no matches\n");
2027 if (ret2
>= 0 && tad
.pb
.offset
)
2028 pass_buffer_as_shm(tad
.pb
.buf
, tad
.pb
.offset
, &fd
);
2032 int com_touch(int fd
, int argc
, char * const * const argv
)
2034 struct com_touch_options cto
= {
2041 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)};
2045 for (i
= 1; i
< argc
; i
++) {
2046 const char *arg
= argv
[i
];
2049 if (!strcmp(arg
, "--")) {
2053 if (!strncmp(arg
, "-n", 2)) {
2054 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
2059 if (!strncmp(arg
, "-l", 2)) {
2060 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
2065 if (!strncmp(arg
, "-y", 2)) {
2066 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
2071 if (!strncmp(arg
, "-i", 2)) {
2072 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
2077 if (!strncmp(arg
, "-a", 2)) {
2079 ret
= para_atoi32(arg
+ 2, &val
);
2082 if (val
< 0 || val
> 255)
2083 return -ERRNO_TO_PARA_ERROR(EINVAL
);
2087 if (!strcmp(arg
, "-p")) {
2088 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
2091 if (!strcmp(arg
, "-v")) {
2092 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
2095 break; /* non-option starting with dash */
2098 return -E_AFT_SYNTAX
;
2099 ret
= send_option_arg_callback_request(&query
, argc
- i
,
2100 argv
+ i
, com_touch_callback
, send_result
, &fd
);
2102 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2106 /** Flags for com_rm(). */
2109 RM_FLAG_VERBOSE
= 1,
2113 RM_FLAG_FNM_PATHNAME
= 4
2116 /** Data passed to the action handler of com_rm(). */
2117 struct com_rm_action_data
{
2118 /** Command line flags ((see \ref rm_flags). */
2120 /** Message buffer. */
2121 struct para_buffer pb
;
2122 /** Number of audio files removed. */
2123 unsigned num_removed
;
2126 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2127 struct osl_row
*row
, const char *name
, void *data
)
2129 struct com_rm_action_data
*crd
= data
;
2132 if (crd
->flags
& RM_FLAG_VERBOSE
) {
2133 ret
= para_printf(&crd
->pb
, "removing %s\n", name
);
2137 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2138 ret
= osl(osl_del_row(audio_file_table
, row
));
2140 para_printf(&crd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2146 static void com_rm_callback(int fd
, const struct osl_object
*query
)
2148 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
,
2151 .private_data
= &fd
,
2152 .max_size_handler
= pass_buffer_as_shm
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
= (char *)query
->data
+ sizeof(uint32_t),
2161 .size
= query
->size
- sizeof(uint32_t)},
2163 .action
= remove_audio_file
2165 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2166 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2167 ret
= for_each_matching_row(&pmd
);
2169 para_printf(&crd
.pb
, "%s\n", para_strerror(-ret
));
2172 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2173 ret
= para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2175 if (crd
.flags
& RM_FLAG_VERBOSE
)
2176 ret
= para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2178 if (ret
>= 0 && crd
.pb
.offset
)
2179 pass_buffer_as_shm(crd
.pb
.buf
, crd
.pb
.offset
, &fd
);
2183 /* TODO options: -r (recursive) */
2184 int com_rm(int fd
, int argc
, char * const * const argv
)
2187 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)};
2190 for (i
= 1; i
< argc
; i
++) {
2191 const char *arg
= argv
[i
];
2194 if (!strcmp(arg
, "--")) {
2198 if (!strcmp(arg
, "-f")) {
2199 flags
|= RM_FLAG_FORCE
;
2202 if (!strcmp(arg
, "-p")) {
2203 flags
|= RM_FLAG_FNM_PATHNAME
;
2206 if (!strcmp(arg
, "-v")) {
2207 flags
|= RM_FLAG_VERBOSE
;
2213 return -E_AFT_SYNTAX
;
2214 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2215 com_rm_callback
, send_result
, &fd
);
2217 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2222 * Flags used by the cpsi command.
2227 /** Whether the lyrics id should be copied. */
2228 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2229 /** Whether the image id should be copied. */
2230 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2231 /** Whether the lastplayed time should be copied. */
2232 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2233 /** Whether the numplayed count should be copied. */
2234 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2235 /** Whether the attributes should be copied. */
2236 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2237 /** Activates verbose mode. */
2238 CPSI_FLAG_VERBOSE
= 32,
2241 /** Data passed to the action handler of com_cpsi(). */
2242 struct cpsi_action_data
{
2243 /** command line flags (see \ref cpsi_flags). */
2245 /** Number of audio files changed. */
2246 unsigned num_copied
;
2247 /** Message buffer. */
2248 struct para_buffer pb
;
2249 /** Values are copied from here. */
2250 struct afs_info source_afsi
;
2253 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2254 struct osl_row
*row
, const char *name
, void *data
)
2256 struct cpsi_action_data
*cad
= data
;
2257 struct osl_object target_afsi_obj
;
2259 struct afs_info old_afsi
, target_afsi
;
2260 struct afsi_change_event_data aced
;
2262 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2265 load_afsi(&target_afsi
, &target_afsi_obj
);
2266 old_afsi
= target_afsi
;
2267 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2268 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2269 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2270 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2271 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2272 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2273 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2274 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2275 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2276 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2277 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2279 if (cad
->flags
& CPSI_FLAG_VERBOSE
) {
2280 ret
= para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2285 aced
.old_afsi
= &old_afsi
;
2286 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2290 static void com_cpsi_callback(int fd
, const struct osl_object
*query
)
2292 struct cpsi_action_data cad
= {
2293 .flags
= *(unsigned *)query
->data
,
2296 .private_data
= &fd
,
2297 .max_size_handler
= pass_buffer_as_shm
2301 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2302 struct pattern_match_data pmd
= {
2303 .table
= audio_file_table
,
2304 .loop_col_num
= AFTCOL_HASH
,
2305 .match_col_num
= AFTCOL_PATH
,
2306 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2307 .size
= query
->size
- sizeof(cad
.flags
)
2308 - strlen(source_path
) - 1},
2310 .action
= copy_selector_info
2313 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2316 ret
= for_each_matching_row(&pmd
);
2319 para_printf(&cad
.pb
, "%s\n", para_strerror(-ret
));
2320 else if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2322 para_printf(&cad
.pb
, "copied requested afsi from %s "
2323 "to %u files\n", source_path
, cad
.num_copied
);
2325 para_printf(&cad
.pb
, "nothing copied\n");
2328 pass_buffer_as_shm(cad
.pb
.buf
, cad
.pb
.offset
, &fd
);
2332 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2336 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)};
2338 for (i
= 1; i
< argc
; i
++) {
2339 const char *arg
= argv
[i
];
2342 if (!strcmp(arg
, "--")) {
2346 if (!strcmp(arg
, "-y")) {
2347 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2350 if (!strcmp(arg
, "-i")) {
2351 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2354 if (!strcmp(arg
, "-l")) {
2355 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2358 if (!strcmp(arg
, "-n")) {
2359 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2362 if (!strcmp(arg
, "-a")) {
2363 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2366 if (!strcmp(arg
, "-v")) {
2367 flags
|= CPSI_FLAG_VERBOSE
;
2372 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2373 return -E_AFT_SYNTAX
;
2374 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2375 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2376 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2377 com_cpsi_callback
, send_result
, &fd
);
2379 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2383 void afs_stat_callback(int fd
, __a_unused
const struct osl_object
*query
)
2385 if (current_status_items
)
2386 pass_buffer_as_shm(current_status_items
,
2387 strlen(current_status_items
), &fd
);
2390 int send_afs_status(int fd
)
2392 return send_callback_request(afs_stat_callback
, NULL
, send_result
, &fd
);
2395 /* TODO: optionally fix problems by removing offending rows */
2396 static int check_audio_file(struct osl_row
*row
, void *data
)
2399 struct para_buffer
*pb
= data
;
2400 struct stat statbuf
;
2401 int ret
= get_audio_file_path_of_row(row
, &path
);
2402 struct afs_info afsi
;
2406 return para_printf(pb
, "%s\n", para_strerror(-ret
));
2407 if (stat(path
, &statbuf
) < 0) {
2408 ret
= para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2412 if (!S_ISREG(statbuf
.st_mode
)) {
2413 ret
= para_printf(pb
, "%s: not a regular file\n", path
);
2418 ret
= get_afsi_of_row(row
, &afsi
);
2420 return para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
2421 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2423 ret
= para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2424 para_strerror(-ret
));
2428 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2430 ret
= para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2431 para_strerror(-ret
));
2436 * Check the audio file table for inconsistencies.
2438 * \param fd The afs socket.
2439 * \param query Unused.
2441 * This function always succeeds.
2445 void aft_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
2447 struct para_buffer pb
= {
2449 .private_data
= &fd
,
2450 .max_size_handler
= pass_buffer_as_shm
2452 int ret
= para_printf(&pb
, "checking audio file table...\n");
2456 audio_file_loop(&pb
, check_audio_file
);
2458 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
2463 * Close the audio file table.
2465 * \param flags Usual flags that are passed to osl_close_table().
2467 * \sa osl_close_table().
2469 static void aft_close(void)
2471 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2472 audio_file_table
= NULL
;
2473 free(current_status_items
);
2474 current_status_items
= NULL
;
2478 * Open the audio file table.
2480 * \param dir The database directory.
2484 * \sa osl_open_table().
2486 static int aft_open(const char *dir
)
2490 audio_file_table_desc
.dir
= dir
;
2491 ret
= osl(osl_open_table(&audio_file_table_desc
, &audio_file_table
));
2494 osl_get_num_rows(audio_file_table
, &num
);
2495 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2498 PARA_INFO_LOG("failed to open audio file table\n");
2499 audio_file_table
= NULL
;
2500 if (ret
>= 0 || ret
== -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT
))
2505 static int aft_create(const char *dir
)
2507 audio_file_table_desc
.dir
= dir
;
2508 return osl(osl_create_table(&audio_file_table_desc
));
2511 static int clear_attribute(struct osl_row
*row
, void *data
)
2513 struct rmatt_event_data
*red
= data
;
2514 struct afs_info afsi
;
2515 struct osl_object obj
;
2516 int ret
= get_afsi_object_of_row(row
, &obj
);
2517 uint64_t mask
= ~(1ULL << red
->bitnum
);
2521 ret
= load_afsi(&afsi
, &obj
);
2524 afsi
.attributes
&= mask
;
2525 save_afsi(&afsi
, &obj
);
2529 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2535 case ATTRIBUTE_REMOVE
: {
2536 const struct rmatt_event_data
*red
= data
;
2537 ret
= para_printf(pb
, "clearing attribute %s (bit %u) from all "
2538 "entries in the audio file table\n", red
->name
,
2542 return audio_file_loop(data
, clear_attribute
);
2549 void aft_init(struct afs_table
*t
)
2551 t
->name
= audio_file_table_desc
.name
;
2553 t
->close
= aft_close
;
2554 t
->create
= aft_create
;
2555 t
->event_handler
= aft_event_handler
;