2 * Copyright (C) 2007-2008 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. */
66 /** The flags accepted by the ls command. */
69 LS_FLAG_FULL_PATH
= 1,
71 LS_FLAG_ADMISSIBLE_ONLY
= 2,
77 * The size of the individual output fields of the ls command.
79 * These depend on the actual content being listed. If, for instance only files
80 * with duration less than an hour are being listed, then the duration with is
81 * made smaller because then the duration is listed as mm:ss rather than
85 /** size of the score field. */
86 unsigned short score_width
;
87 /** size of the image id field. */
88 unsigned short image_id_width
;
89 /** size of the lyrics id field. */
90 unsigned short lyrics_id_width
;
91 /** size of the bitrate field. */
92 unsigned short bitrate_width
;
93 /** size of the frequency field. */
94 unsigned short frequency_width
;
95 /** size of the duration field. */
96 unsigned short duration_width
;
97 /** size of the num played field. */
98 unsigned short num_played_width
;
101 /** Data passed from the ls command handler to its callback function. */
103 /** The given command line flags. */
105 /** The sorting method given at the command line. */
106 enum ls_sorting_method sorting
;
107 /** The given listing mode (short, long, verbose, mbox). */
108 enum ls_listing_mode mode
;
109 /** The arguments passed to the ls command. */
111 /** Number of non-option arguments. */
113 /** Used for long listing mode to align the output fields. */
114 struct ls_widths widths
;
115 /** Size of the \a data array. */
117 /** Number of used entries in the data array. */
118 uint32_t num_matching_paths
;
119 /** Array of matching entries. */
120 struct ls_data
*data
;
121 /** Used to sort the array. */
122 struct ls_data
**data_ptr
;
126 * Describes the layout of the mmapped-afs info struct.
128 * \sa struct afs_info.
131 /** Where .last_played is stored. */
132 AFSI_LAST_PLAYED_OFFSET
= 0,
133 /** Storage position of the attributes bitmap. */
134 AFSI_ATTRIBUTES_OFFSET
= 8,
135 /** Storage position of the .num_played field. */
136 AFSI_NUM_PLAYED_OFFSET
= 16,
137 /** Storage position of the .image_id field. */
138 AFSI_IMAGE_ID_OFFSET
= 20,
139 /** Storage position of the .lyrics_id field. */
140 AFSI_LYRICS_ID_OFFSET
= 24,
141 /** Storage position of the .audio_format_id field. */
142 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
143 /** 3 bytes reserved space for future usage. */
144 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 29,
145 /** On-disk storage space needed. */
150 * Convert a struct afs_info to an osl object.
152 * \param afsi Pointer to the audio file info to be converted.
153 * \param obj Result pointer.
157 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
159 char *buf
= obj
->data
;
161 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
162 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
163 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
164 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
165 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
166 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
167 afsi
->audio_format_id
);
168 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 3);
172 * Get the audio file selector info struct stored in an osl object.
174 * \param afsi Points to the audio_file info structure to be filled in.
175 * \param obj The osl object holding the data.
177 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
181 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
183 char *buf
= obj
->data
;
184 if (obj
->size
< AFSI_SIZE
)
186 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
187 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
188 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
189 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
190 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
191 afsi
->audio_format_id
= read_u8(buf
+
192 AFSI_AUDIO_FORMAT_ID_OFFSET
);
196 /** The columns of the audio file table. */
197 enum audio_file_table_columns
{
198 /** The hash on the content of the audio file. */
200 /** The full path in the filesystem. */
202 /** The audio file selector info. */
204 /** The audio format handler info. */
206 /** The chunk table info and the chunk table of the audio file. */
208 /** The number of columns of this table. */
212 static struct osl_column_description aft_cols
[] = {
214 .storage_type
= OSL_MAPPED_STORAGE
,
215 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
217 .compare_function
= osl_hash_compare
,
218 .data_size
= HASH_SIZE
221 .storage_type
= OSL_MAPPED_STORAGE
,
222 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
224 .compare_function
= string_compare
,
227 .storage_type
= OSL_MAPPED_STORAGE
,
228 .storage_flags
= OSL_FIXED_SIZE
,
230 .data_size
= AFSI_SIZE
233 .storage_type
= OSL_MAPPED_STORAGE
,
237 .storage_type
= OSL_DISK_STORAGE
,
242 static struct osl_table_description audio_file_table_desc
= {
243 .name
= "audio_files",
244 .num_columns
= NUM_AFT_COLUMNS
,
245 .flags
= OSL_LARGE_TABLE
,
246 .column_descriptions
= aft_cols
249 /* We don't want * dot or dot-dot anywhere. */
250 static int verify_dotfile(const char *rest
)
253 * The first character was '.', but that has already been discarded, we
257 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
259 case '.': /* path start with /foo/.. */
260 if (rest
[1] == '\0' || rest
[1] == '/')
261 return -1; /* /foo/.. or /foo/../bar are not ok */
262 /* /foo/..bar is ok */
268 * We fundamentally don't like some paths: We don't want double slashes or
269 * slashes at the end that can make pathnames ambiguous.
271 static int verify_path(const char *orig_path
, char **resolved_path
)
277 if (*orig_path
!= '/') /* we only accept absolute paths */
279 len
= strlen(orig_path
);
280 *resolved_path
= para_strdup(orig_path
);
281 path
= *resolved_path
;
282 while (len
> 1 && path
[--len
] == '/')
283 path
[len
] = '\0'; /* remove slash at the end */
289 case '/': /* double slash */
292 if (verify_dotfile(path
) < 0)
302 free(*resolved_path
);
306 /** The on-disk layout of a afhi struct. */
308 /** Where the number of seconds is stored. */
309 AFHI_SECONDS_TOTAL_OFFSET
= 0,
310 /** Position of the bitrate. */
311 AFHI_BITRATE_OFFSET
= 4,
312 /** Position of the frequency. */
313 AFHI_FREQUENCY_OFFSET
= 8,
314 /** Location of the audio file header. */
315 AFHI_HEADER_OFFSET_OFFSET
= 12,
316 /* Length of the audio file header. Zero means: No header. */
317 AFHI_HEADER_LEN_OFFSET
= 16,
318 /** The total number of chunks (4 bytes). */
319 CHUNKS_TOTAL_OFFSET
= 20,
320 /** The length of the audio file header (4 bytes). */
321 HEADER_LEN_OFFSET
= 24,
322 /** The start of the audio file header (4 bytes). */
323 HEADER_OFFSET_OFFSET
= 28,
324 /** The seconds part of the chunk time (4 bytes). */
325 CHUNK_TV_TV_SEC_OFFSET
= 32,
326 /** The microseconds part of the chunk time (4 bytes). */
327 CHUNK_TV_TV_USEC_OFFSET
= 36,
328 /** Number of channels is stored here. (1 byte) */
329 AFHI_CHANNELS_OFFSET
= 40,
330 /** EOF timeout in ms. (2 byte) */
331 AFHI_EOF_OFFSET
= 41,
332 /** The tag info position. */
333 AFHI_INFO_STRING_OFFSET
= 43,
334 /** Minimal on-disk size of a valid afhi struct. */
338 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
342 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
345 static void save_afhi(struct afh_info
*afhi
, char *buf
)
349 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
, afhi
->seconds_total
);
350 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
351 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
352 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
353 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
354 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
355 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
356 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
357 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
358 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
359 write_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
, afhi
->chunk_tv
.tv_usec
);
360 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
361 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
364 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
366 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
367 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
368 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
369 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
370 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
371 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
372 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
373 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
374 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
375 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
376 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
);
377 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
378 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
381 static unsigned sizeof_chunk_table(struct afh_info
*afhi
)
385 return 4 * (afhi
->chunks_total
+ 1);
388 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
392 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
393 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
396 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
400 afhi
->chunk_table
= para_malloc(sizeof_chunk_table(afhi
));
401 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
402 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
406 * Get the row of the audio file table corresponding to the given path.
408 * \param path The full path of the audio file.
409 * \param row Result pointer.
411 * \return The return value of the underlying call to osl_get_row().
413 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
415 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
417 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
421 * Get the row of the audio file table corresponding to the given hash value.
423 * \param hash The hash value of the desired audio file.
424 * \param row resul pointer.
426 * \return The return value of the underlying call to osl_get_row().
428 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
430 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
431 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
435 * Get the osl object holding the audio file selector info of a row.
437 * \param row Pointer to a row in the audio file table.
438 * \param obj Result pointer.
440 * \return The return value of the underlying call to osl_get_object().
442 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
444 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
448 * Get the osl object holding the audio file selector info, given a path.
451 * \param path The full path of the audio file.
452 * \param obj Result pointer.
454 * \return Positive on success, negative on errors.
456 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
459 int ret
= aft_get_row_of_path(path
, &row
);
462 return get_afsi_object_of_row(row
, obj
);
466 * Get the audio file selector info, given a row of the audio file table.
468 * \param row Pointer to a row in the audio file table.
469 * \param afsi Result pointer.
471 * \return Positive on success, negative on errors.
473 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
475 struct osl_object obj
;
476 int ret
= get_afsi_object_of_row(row
, &obj
);
479 return load_afsi(afsi
, &obj
);
483 * Get the audio file selector info, given the path of an audio table.
485 * \param path The full path of the audio file.
486 * \param afsi Result pointer.
488 * \return Positive on success, negative on errors.
490 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
492 struct osl_object obj
;
493 int ret
= get_afsi_object_of_path(path
, &obj
);
496 return load_afsi(afsi
, &obj
);
500 * Get the path of an audio file, given a row of the audio file table.
502 * \param row Pointer to a row in the audio file table.
503 * \param path Result pointer.
505 * The result is a pointer to mmapped data. The caller must not attempt
510 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
512 struct osl_object path_obj
;
513 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
517 *path
= path_obj
.data
;
522 * Get the object containing the hash value of an audio file, given a row.
524 * \param row Pointer to a row of the audio file table.
525 * \param obj Result pointer.
527 * \return The return value of the underlying call to osl_get_object().
529 * \sa get_hash_of_row().
531 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
533 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
537 * Get the hash value of an audio file, given a row of the audio file table.
539 * \param row Pointer to a row of the audio file table.
540 * \param hash Result pointer.
542 * \a hash points to mapped data and must not be freed by the caller.
544 * \return The return value of the underlying call to
545 * get_hash_object_of_aft_row().
547 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
549 struct osl_object obj
;
550 int ret
= get_hash_object_of_aft_row(row
, &obj
);
559 * Get the audio format handler info, given a row of the audio file table.
561 * \param row Pointer to a row of the audio file table.
562 * \param afhi Result pointer.
564 * \return The return value of the underlying call to osl_get_object().
566 * \sa get_chunk_table_of_row().
568 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
570 struct osl_object obj
;
571 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
575 load_afhi(obj
.data
, afhi
);
579 /* returns shmid on success */
580 static int save_afd(struct audio_file_data
*afd
)
582 size_t size
= sizeof(*afd
) + sizeof_chunk_table(&afd
->afhi
);
584 PARA_DEBUG_LOG("size: %zu\n", size
);
585 int shmid
, ret
= shm_new(size
);
592 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
595 *(struct audio_file_data
*)shm_afd
= *afd
;
598 save_chunk_table(&afd
->afhi
, buf
);
606 int load_afd(int shmid
, struct audio_file_data
*afd
)
612 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
615 *afd
= *(struct audio_file_data
*)shm_afd
;
618 load_chunk_table(&afd
->afhi
, buf
);
624 * Mmap the given audio file and update statistics.
626 * \param aft_row Determines the audio file to be opened and updated.
627 * \param score The score of the audio file.
628 * \param afd Result pointer.
630 * On success, the numplayed field of the audio file selector info is increased
631 * and the lastplayed time is set to the current time. Finally, the score of
632 * the audio file is updated.
634 * \return Positive shmid on success, negative on errors.
636 int open_and_update_audio_file(struct osl_row
*aft_row
, long score
,
637 struct audio_file_data
*afd
)
639 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
640 struct osl_object afsi_obj
;
641 struct afs_info old_afsi
, new_afsi
;
642 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
643 struct afsi_change_event_data aced
;
644 struct osl_object map
, chunk_table_obj
;
649 ret
= get_audio_file_path_of_row(aft_row
, &path
);
652 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
655 ret
= load_afsi(&old_afsi
, &afsi_obj
);
658 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
661 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
662 AFTCOL_CHUNKS
, &chunk_table_obj
);
665 afd
->afhi
.chunk_table
= NULL
;
666 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
667 &map
.size
, &afd
->fd
);
670 hash_function(map
.data
, map
.size
, file_hash
);
671 ret
= hash_compare(file_hash
, aft_hash
);
672 para_munmap(map
.data
, map
.size
);
674 ret
= -E_HASH_MISMATCH
;
678 new_afsi
.num_played
++;
679 new_afsi
.last_played
= time(NULL
);
680 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
682 load_chunk_table(&afd
->afhi
, chunk_table_obj
.data
);
691 struct para_buffer pb
= {.buf
= NULL
};
692 ret
= make_status_items(&d
, &pb
);
695 strncpy(afd
->verbose_ls_output
, pb
.buf
, VERBOSE_LS_OUTPUT_SIZE
);
696 afd
->verbose_ls_output
[VERBOSE_LS_OUTPUT_SIZE
- 1] = '\0';
699 aced
.aft_row
= aft_row
;
700 aced
.old_afsi
= &old_afsi
;
701 afs_event(AFSI_CHANGE
, NULL
, &aced
);
704 free(afd
->afhi
.chunk_table
);
705 osl_close_disk_object(&chunk_table_obj
);
709 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
710 time_t current_time
, enum ls_listing_mode lm
)
714 if (!localtime_r((time_t *)seconds
, &t
))
716 if (lm
== LS_MODE_MBOX
) {
717 if (!strftime(buf
, size
, "%c", &t
))
721 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
722 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
726 if (!strftime(buf
, size
, "%b %e %Y", &t
))
731 /** Compute the number of (decimal) digits of a number. */
732 #define GET_NUM_DIGITS(x, num) { \
733 typeof((x)) _tmp = PARA_ABS(x); \
736 while ((_tmp) > 9) { \
742 static short unsigned get_duration_width(int seconds
)
744 short unsigned width
;
745 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
747 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
748 return 4 + (mins
> 9);
749 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
750 GET_NUM_DIGITS(hours
, &width
);
754 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
756 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
757 short unsigned max_width
;
759 if (!hours
) { /* m:ss or mm:ss */
760 max_width
= opts
->mode
== LS_MODE_LONG
?
761 opts
->widths
.duration_width
: 4;
762 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
763 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
764 max_width
= opts
->mode
== LS_MODE_LONG
?
765 opts
->widths
.duration_width
: 7;
766 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
771 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
773 char *att_text
, *att_lines
;
775 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
777 return para_strdup(att_bitmap
);
778 att_lines
= make_message("%s: %s\n%s: %s",
779 status_item_list
[SI_ATTRIBUTES_BITMAP
], att_bitmap
,
780 status_item_list
[SI_ATTRIBUTES_TXT
], att_text
);
785 static char *make_lyrics_lines(struct afs_info
*afsi
)
789 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
790 return make_message("%s: %u\n%s: %s\n",
791 status_item_list
[SI_LYRICS_ID
], afsi
->lyrics_id
,
792 status_item_list
[SI_LYRICS_NAME
], lyrics_name
?
793 lyrics_name
: "(none)");
796 static char *make_image_lines(struct afs_info
*afsi
)
799 img_get_name_by_id(afsi
->image_id
, &image_name
);
800 return make_message("%s: %u\n%s: %s\n",
801 status_item_list
[SI_IMAGE_ID
], afsi
->image_id
,
802 status_item_list
[SI_IMAGE_NAME
], image_name
?
803 image_name
: "(none)");
806 static char *make_filename_lines(const char *path
, unsigned flags
)
809 const char *basename
;
811 if (!(flags
& LS_FLAG_FULL_PATH
))
812 return make_message("%s: %s\n",
813 status_item_list
[SI_BASENAME
], path
);
814 basename
= para_basename(path
),
815 dirname
= para_dirname(path
);
816 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n",
817 status_item_list
[SI_PATH
], path
,
818 status_item_list
[SI_DIRECTORY
], dirname
? dirname
: "?",
819 status_item_list
[SI_BASENAME
], basename
? basename
: "?");
824 static int print_chunk_table(struct ls_data
*d
, struct para_buffer
*b
)
826 struct osl_object chunk_table_obj
;
827 struct osl_row
*aft_row
;
831 ret
= aft_get_row_of_hash(d
->hash
, &aft_row
);
834 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
835 AFTCOL_CHUNKS
, &chunk_table_obj
);
838 buf
= chunk_table_obj
.data
;
839 para_printf(b
, "%s\n"
840 "chunk_time: %lu:%lu\nchunk_offsets: ",
842 (long unsigned) d
->afhi
.chunk_tv
.tv_sec
,
843 (long unsigned) d
->afhi
.chunk_tv
.tv_usec
845 for (i
= 0; i
<= d
->afhi
.chunks_total
; i
++)
846 para_printf(b
, "%u ",
847 (unsigned) read_u32(buf
+ 4 * i
));
848 osl_close_disk_object(&chunk_table_obj
);
849 para_printf(b
, "\n");
853 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
854 struct para_buffer
*b
, time_t current_time
)
858 char last_played_time
[30];
859 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
860 char score_buf
[30] = "";
861 struct afs_info
*afsi
= &d
->afsi
;
862 struct afh_info
*afhi
= &d
->afhi
;
863 struct ls_widths
*w
= &opts
->widths
;
864 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
865 char asc_hash
[2 * HASH_SIZE
+ 1];
866 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
868 if (opts
->mode
== LS_MODE_SHORT
) {
869 para_printf(b
, "%s\n", d
->path
);
872 if (opts
->mode
== LS_MODE_CHUNKS
)
873 return print_chunk_table(d
, b
);
874 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
875 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
876 sizeof(last_played_time
), current_time
, opts
->mode
);
879 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
881 if (opts
->mode
== LS_MODE_LONG
)
882 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
884 sprintf(score_buf
, "%li ", d
->score
);
887 if (opts
->mode
== LS_MODE_LONG
) {
890 "%s " /* attributes */
891 "%*d " /* image_id */
892 "%*d " /* lyrics_id */
894 "%s " /* audio format */
895 "%*d " /* frequency */
898 "%*d " /* num_played */
899 "%s " /* last_played */
903 w
->image_id_width
, afsi
->image_id
,
904 w
->lyrics_id_width
, afsi
->lyrics_id
,
905 w
->bitrate_width
, afhi
->bitrate
,
906 audio_format_name(afsi
->audio_format_id
),
907 w
->frequency_width
, afhi
->frequency
,
910 w
->num_played_width
, afsi
->num_played
,
916 hash_to_asc(d
->hash
, asc_hash
);
917 att_lines
= make_attribute_lines(att_buf
, afsi
);
918 lyrics_lines
= make_lyrics_lines(afsi
);
919 image_lines
= make_image_lines(afsi
);
920 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
921 if (opts
->mode
== LS_MODE_MBOX
) {
922 const char *bn
= para_basename(d
->path
);
924 "From foo@localhost %s\n"
925 "Received: from\nTo: bar\nFrom: a\n"
931 "%s" /* filename stuff */
932 "%s%s%s%s" /* score */
933 "%s\n" /* attributes */
934 "%s: %s\n" /* hash */
935 "%s" /* image id, image name */
937 "%s: %dkbit/s\n" /* bitrate */
938 "%s: %s\n" /* format */
939 "%s: %dHz\n" /* frequency */
940 "%s: %d\n" /* channels */
941 "%s: %s\n" /* duration */
942 "%s: %lu\n" /* seconds total */
943 "%s: %s\n" /* last played time */
944 "%s: %d\n" /* num_played */
946 "%s: %lu\n" /* chunk time */
947 "%s: %lu\n", /* num chunks */
949 have_score
? status_item_list
[SI_SCORE
] : "",
950 have_score
? ": " : "",
952 have_score
? "\n" : "",
954 status_item_list
[SI_HASH
], asc_hash
,
957 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
958 status_item_list
[SI_FORMAT
], audio_format_name(afsi
->audio_format_id
),
959 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
960 status_item_list
[SI_CHANNELS
], afhi
->channels
,
961 status_item_list
[SI_DURATION
], duration_buf
,
962 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
963 status_item_list
[SI_LAST_PLAYED
], last_played_time
,
964 status_item_list
[SI_NUM_PLAYED
], afsi
->num_played
,
966 status_item_list
[SI_CHUNK_TIME
], tv2ms(&afhi
->chunk_tv
),
967 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
969 if (opts
->mode
== LS_MODE_MBOX
) {
970 struct osl_object lyrics_def
;
971 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
972 if (lyrics_def
.data
) {
973 para_printf(b
, "Lyrics:\n~~~~~~~\n%s",
974 (char *)lyrics_def
.data
);
975 osl_close_disk_object(&lyrics_def
);
981 free(filename_lines
);
985 void make_empty_status_items(char *buf
)
989 "%s: \n" /* dirname */
990 "%s: \n" /* basename */
992 "%s: \n" /* attributes bitmap */
993 "%s: \n" /* attributes txt */
995 "%s: \n" /* image id */
996 "%s: \n" /* image name */
997 "%s: \n" /* lyrics id */
998 "%s: \n" /* lyrics name */
999 "%s: \n" /* bitrate */
1000 "%s: \n" /* format */
1001 "%s: \n" /* frequency */
1002 "%s: \n" /* channels */
1003 "%s: \n" /* duration */
1004 "%s: \n" /* seconds total */
1005 "%s: \n" /* num played */
1006 "%s: \n" /* last played */
1008 status_item_list
[SI_PATH
],
1009 status_item_list
[SI_DIRECTORY
],
1010 status_item_list
[SI_BASENAME
],
1011 status_item_list
[SI_SCORE
],
1012 status_item_list
[SI_ATTRIBUTES_BITMAP
],
1013 status_item_list
[SI_ATTRIBUTES_TXT
],
1014 status_item_list
[SI_HASH
],
1015 status_item_list
[SI_IMAGE_ID
],
1016 status_item_list
[SI_IMAGE_NAME
],
1017 status_item_list
[SI_LYRICS_ID
],
1018 status_item_list
[SI_LYRICS_NAME
],
1019 status_item_list
[SI_BITRATE
],
1020 status_item_list
[SI_FORMAT
],
1021 status_item_list
[SI_FREQUENCY
],
1022 status_item_list
[SI_CHANNELS
],
1023 status_item_list
[SI_DURATION
],
1024 status_item_list
[SI_SECONDS_TOTAL
],
1025 status_item_list
[SI_NUM_PLAYED
],
1026 status_item_list
[SI_LAST_PLAYED
]
1030 int make_status_items(struct ls_data
*d
, struct para_buffer
*pb
)
1032 struct ls_options opts
= {
1033 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
1034 .mode
= LS_MODE_VERBOSE
,
1036 time_t current_time
;
1038 time(¤t_time
);
1039 return print_list_item(d
, &opts
, pb
, current_time
);
1043 static int ls_audio_format_compare(const void *a
, const void *b
)
1045 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1046 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1049 static int ls_duration_compare(const void *a
, const void *b
)
1051 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1052 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1055 static int ls_bitrate_compare(const void *a
, const void *b
)
1057 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1058 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1061 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1063 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1064 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1067 static int ls_image_id_compare(const void *a
, const void *b
)
1069 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1070 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1073 static int ls_channels_compare(const void *a
, const void *b
)
1075 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1076 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1079 static int ls_frequency_compare(const void *a
, const void *b
)
1081 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1082 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1085 static int ls_num_played_compare(const void *a
, const void *b
)
1087 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1088 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1091 static int ls_last_played_compare(const void *a
, const void *b
)
1093 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1094 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1097 static int ls_score_compare(const void *a
, const void *b
)
1099 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1100 return NUM_COMPARE(d1
->score
, d2
->score
);
1103 static int ls_path_compare(const void *a
, const void *b
)
1105 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1106 return strcmp(d1
->path
, d2
->path
);
1109 static int sort_matching_paths(struct ls_options
*options
)
1111 size_t nmemb
= options
->num_matching_paths
;
1112 size_t size
= sizeof(*options
->data_ptr
);
1113 int (*compar
)(const void *, const void *);
1116 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1117 for (i
= 0; i
< nmemb
; i
++)
1118 options
->data_ptr
[i
] = options
->data
+ i
;
1120 /* In these cases the array is already sorted */
1121 if (options
->sorting
== LS_SORT_BY_PATH
1122 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1123 && (options
->flags
& LS_FLAG_FULL_PATH
))
1125 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1126 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1129 switch (options
->sorting
) {
1130 case LS_SORT_BY_PATH
:
1131 compar
= ls_path_compare
; break;
1132 case LS_SORT_BY_SCORE
:
1133 compar
= ls_score_compare
; break;
1134 case LS_SORT_BY_LAST_PLAYED
:
1135 compar
= ls_last_played_compare
; break;
1136 case LS_SORT_BY_NUM_PLAYED
:
1137 compar
= ls_num_played_compare
; break;
1138 case LS_SORT_BY_FREQUENCY
:
1139 compar
= ls_frequency_compare
; break;
1140 case LS_SORT_BY_CHANNELS
:
1141 compar
= ls_channels_compare
; break;
1142 case LS_SORT_BY_IMAGE_ID
:
1143 compar
= ls_image_id_compare
; break;
1144 case LS_SORT_BY_LYRICS_ID
:
1145 compar
= ls_lyrics_id_compare
; break;
1146 case LS_SORT_BY_BITRATE
:
1147 compar
= ls_bitrate_compare
; break;
1148 case LS_SORT_BY_DURATION
:
1149 compar
= ls_duration_compare
; break;
1150 case LS_SORT_BY_AUDIO_FORMAT
:
1151 compar
= ls_audio_format_compare
; break;
1155 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1159 /* row is either an aft_row or a row of the score table */
1160 /* TODO: Only compute widths if we need them */
1161 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1164 struct ls_options
*options
= ls_opts
;
1166 struct ls_widths
*w
;
1167 unsigned short num_digits
;
1169 struct osl_row
*aft_row
;
1173 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1174 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1179 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1182 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1183 char *p
= strrchr(path
, '/');
1187 if (options
->num_patterns
) {
1188 for (i
= 0; i
< options
->num_patterns
; i
++) {
1189 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1192 if (ret
== FNM_NOMATCH
)
1196 if (i
>= options
->num_patterns
) /* no match */
1199 tmp
= options
->num_matching_paths
++;
1200 if (options
->num_matching_paths
> options
->array_size
) {
1201 options
->array_size
++;
1202 options
->array_size
*= 2;
1203 options
->data
= para_realloc(options
->data
, options
->array_size
1204 * sizeof(*options
->data
));
1206 d
= options
->data
+ tmp
;
1207 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1210 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1214 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1217 w
= &options
->widths
;
1218 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1219 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1220 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1221 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1222 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1223 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1224 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1225 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1226 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1227 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1228 /* get the number of chars to print this amount of time */
1229 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1230 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1231 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1232 GET_NUM_DIGITS(score
, &num_digits
);
1233 num_digits
++; /* add one for the sign (space or "-") */
1234 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1240 static int com_ls_callback(const struct osl_object
*query
,
1241 struct osl_object
*ls_output
)
1243 struct ls_options
*opts
= query
->data
;
1244 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1245 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1247 time_t current_time
;
1250 if (opts
->num_patterns
) {
1251 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1252 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1253 opts
->patterns
[i
] = p
;
1257 opts
->patterns
= NULL
;
1258 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1259 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1261 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1265 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1266 if (!opts
->num_matching_paths
)
1268 ret
= sort_matching_paths(opts
);
1271 time(¤t_time
);
1272 if (opts
->flags
& LS_FLAG_REVERSE
)
1273 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1274 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1279 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1280 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1286 ls_output
->data
= b
.buf
;
1287 ls_output
->size
= b
.size
;
1289 free(opts
->data_ptr
);
1290 free(opts
->patterns
);
1295 * TODO: flags -h (sort by hash)
1297 int com_ls(int fd
, int argc
, char * const * const argv
)
1301 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1302 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1303 struct ls_options opts
= {.patterns
= NULL
};
1304 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1307 for (i
= 1; i
< argc
; i
++) {
1308 const char *arg
= argv
[i
];
1311 if (!strcmp(arg
, "--")) {
1315 if (!strncmp(arg
, "-l", 2)) {
1317 mode
= LS_MODE_LONG
;
1321 return -E_AFT_SYNTAX
;
1322 switch(*(arg
+ 2)) {
1324 mode
= LS_MODE_SHORT
;
1327 mode
= LS_MODE_LONG
;
1330 mode
= LS_MODE_VERBOSE
;
1333 mode
= LS_MODE_MBOX
;
1336 mode
= LS_MODE_CHUNKS
;
1339 return -E_AFT_SYNTAX
;
1342 if (!strcmp(arg
, "-p")) {
1343 flags
|= LS_FLAG_FULL_PATH
;
1346 if (!strcmp(arg
, "-a")) {
1347 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1350 if (!strcmp(arg
, "-r")) {
1351 flags
|= LS_FLAG_REVERSE
;
1354 if (!strncmp(arg
, "-s", 2)) {
1355 if (!*(arg
+ 2) || *(arg
+ 3))
1356 return -E_AFT_SYNTAX
;
1357 switch(*(arg
+ 2)) {
1359 sort
= LS_SORT_BY_PATH
;
1361 case 's': /* -ss implies -a */
1362 sort
= LS_SORT_BY_SCORE
;
1363 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1366 sort
= LS_SORT_BY_LAST_PLAYED
;
1369 sort
= LS_SORT_BY_NUM_PLAYED
;
1372 sort
= LS_SORT_BY_FREQUENCY
;
1375 sort
= LS_SORT_BY_CHANNELS
;
1378 sort
= LS_SORT_BY_IMAGE_ID
;
1381 sort
= LS_SORT_BY_LYRICS_ID
;
1384 sort
= LS_SORT_BY_BITRATE
;
1387 sort
= LS_SORT_BY_DURATION
;
1390 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1393 return -E_AFT_SYNTAX
;
1396 return -E_AFT_SYNTAX
;
1399 opts
.sorting
= sort
;
1401 opts
.num_patterns
= argc
- i
;
1402 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1403 argv
+ i
, com_ls_callback
, &ls_output
);
1405 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1406 free(ls_output
.data
);
1412 * Call the given function for each file in the audio file table.
1414 * \param private_data An arbitrary data pointer, passed to \a func.
1415 * \param func The custom function to be called.
1417 * \return The return value of the underlying call to osl_rbtree_loop().
1419 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1421 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1425 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1427 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1428 struct osl_row
*row
;
1430 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1434 /** The format of the data stored by save_audio_file_data(). */
1435 enum com_add_buffer_offsets
{
1436 /** afhi (if present) starts here. */
1437 CAB_AFHI_OFFSET_POS
= 0,
1438 /** Start of the chunk table (if present). */
1439 CAB_CHUNKS_OFFSET_POS
= 2,
1440 /** Audio format id. */
1441 CAB_AUDIO_FORMAT_OFFSET
= 4,
1442 /** Flags given to the add command. */
1443 CAB_FLAGS_OFFSET
= 5,
1444 /** The hash of the audio file being added. */
1445 CAB_HASH_OFFSET
= 9,
1446 /** Start of the path of the audio file. */
1447 CAB_PATH_OFFSET
= (CAB_HASH_OFFSET
+ HASH_SIZE
),
1451 * Store the given data to a single buffer. Doesn't need the audio file selector
1452 * info struct as the server knows it as well.
1454 * It's OK to call this with afhi == NULL. In this case, the audio format
1455 * handler info won't be stored in the buffer.
1457 static void save_add_callback_buffer(HASH_TYPE
*hash
, const char *path
,
1458 struct afh_info
*afhi
, uint32_t flags
,
1459 uint8_t audio_format_num
, struct osl_object
*obj
)
1461 size_t path_len
= strlen(path
) + 1;
1462 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1463 size_t size
= CAB_PATH_OFFSET
+ path_len
+ afhi_size
1464 + sizeof_chunk_table(afhi
);
1465 char *buf
= para_malloc(size
);
1468 write_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1469 write_u32(buf
+ CAB_FLAGS_OFFSET
, flags
);
1471 memcpy(buf
+ CAB_HASH_OFFSET
, hash
, HASH_SIZE
);
1472 strcpy(buf
+ CAB_PATH_OFFSET
, path
);
1474 pos
= CAB_PATH_OFFSET
+ path_len
;
1475 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1476 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1477 pos
+ afhi_size
- 1);
1478 write_u16(buf
+ CAB_AFHI_OFFSET_POS
, pos
);
1479 save_afhi(afhi
, buf
+ pos
);
1482 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1483 write_u16(buf
+ CAB_CHUNKS_OFFSET_POS
, pos
);
1485 save_chunk_table(afhi
, buf
+ pos
);
1486 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1493 Overview of the add command.
1495 Input: What was passed to the callback by the command handler.
1497 HS: Hash sister. Whether an audio file with identical hash
1498 already exists in the osl database.
1500 PB: Path brother. Whether a file with the given path exists
1503 F: Force flag given. Whether add was called with -f.
1505 output: Action performed by the callback.
1507 AFHI: Whether afhi and chunk table are computed and sent.
1508 ACTION: Table modifications to be done by the callback.
1510 +----+----+---+------+---------------------------------------------------+
1511 | HS | PB | F | AFHI | ACTION
1512 +----+----+---+------+---------------------------------------------------+
1513 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1514 | | update path, keep afsi
1515 +----+----+---+------+---------------------------------------------------+
1516 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1517 | | otherwise: remove PB, HS: update path, keep afhi,
1519 +----+----+---+------+---------------------------------------------------+
1520 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1522 +----+----+---+------+---------------------------------------------------+
1523 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1524 +----+----+---+------+---------------------------------------------------+
1525 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1526 | | (force has no effect)
1527 +----+----+---+------+---------------------------------------------------+
1528 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1529 +----+----+---+------+---------------------------------------------------+
1530 | N | N | Y | Y | (new file) create new entry (force has no effect)
1531 +----+----+---+------+---------------------------------------------------+
1532 | N | N | N | Y | (new file) create new entry
1533 +----+----+---+------+---------------------------------------------------+
1537 afhi <=> force or no HS
1542 /** Flags passed to the add command. */
1543 enum com_add_flags
{
1544 /** Skip paths that exist already. */
1546 /** Force adding. */
1548 /** Print what is being done. */
1549 ADD_FLAG_VERBOSE
= 4,
1550 /** Try to add files with unknown suffixes. */
1554 static int com_add_callback(const struct osl_object
*query
,
1555 struct osl_object
*result
)
1557 char *buf
= query
->data
, *path
;
1558 struct osl_row
*pb
, *aft_row
;
1560 struct osl_object objs
[NUM_AFT_COLUMNS
];
1562 char asc
[2 * HASH_SIZE
+ 1];
1564 char afsi_buf
[AFSI_SIZE
];
1565 uint32_t flags
= read_u32(buf
+ CAB_FLAGS_OFFSET
);
1566 struct afs_info default_afsi
= {.last_played
= 0};
1567 struct para_buffer msg
= {.buf
= NULL
};
1569 hash
= (HASH_TYPE
*)buf
+ CAB_HASH_OFFSET
;
1570 hash_to_asc(hash
, asc
);;
1571 objs
[AFTCOL_HASH
].data
= buf
+ CAB_HASH_OFFSET
;
1572 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1574 path
= buf
+ CAB_PATH_OFFSET
;
1575 objs
[AFTCOL_PATH
].data
= path
;
1576 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1578 PARA_INFO_LOG("request to add %s\n", path
);
1579 hs
= find_hash_sister(hash
);
1580 ret
= aft_get_row_of_path(path
, &pb
);
1581 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1583 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1584 if (flags
& ADD_FLAG_VERBOSE
)
1585 para_printf(&msg
, "ignoring duplicate\n");
1589 if (hs
&& hs
!= pb
) {
1590 struct osl_object obj
;
1591 if (pb
) { /* hs trumps pb, remove pb */
1592 if (flags
& ADD_FLAG_VERBOSE
)
1593 para_printf(&msg
, "removing path brother\n");
1594 ret
= osl_del_row(audio_file_table
, pb
);
1599 /* file rename, update hs' path */
1600 if (flags
& ADD_FLAG_VERBOSE
) {
1601 ret
= osl_get_object(audio_file_table
, hs
,
1605 para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1607 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1608 &objs
[AFTCOL_PATH
]);
1611 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1612 if (!(flags
& ADD_FLAG_FORCE
))
1615 /* no hs or force mode, child must have sent afhi */
1616 uint16_t afhi_offset
= read_u16(buf
+ CAB_AFHI_OFFSET_POS
);
1617 uint16_t chunks_offset
= read_u16(buf
+ CAB_CHUNKS_OFFSET_POS
);
1619 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1620 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1622 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1624 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1625 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1626 if (pb
&& !hs
) { /* update pb's hash */
1627 char old_asc
[2 * HASH_SIZE
+ 1];
1628 HASH_TYPE
*old_hash
;
1629 ret
= get_hash_of_row(pb
, &old_hash
);
1632 hash_to_asc(old_hash
, old_asc
);
1633 if (flags
& ADD_FLAG_VERBOSE
)
1634 para_printf(&msg
, "file change: %s -> %s\n",
1636 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1637 &objs
[AFTCOL_HASH
]);
1641 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1642 struct osl_row
*row
= pb
? pb
: hs
;
1643 /* update afhi and chunk_table */
1644 if (flags
& ADD_FLAG_VERBOSE
)
1645 para_printf(&msg
, "updating afhi and chunk table\n");
1646 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1647 &objs
[AFTCOL_AFHI
]);
1650 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1651 &objs
[AFTCOL_CHUNKS
]);
1654 afs_event(AFHI_CHANGE
, &msg
, row
);
1657 /* new entry, use default afsi */
1658 if (flags
& ADD_FLAG_VERBOSE
)
1659 para_printf(&msg
, "new file\n");
1660 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1661 default_afsi
.audio_format_id
= read_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
);
1663 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1664 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1665 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1666 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1667 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1670 para_printf(&msg
, "%s\n", para_strerror(-ret
));
1673 result
->data
= msg
.buf
;
1674 result
->size
= msg
.size
;
1678 /** Used by com_add(). */
1679 struct private_add_data
{
1680 /** The socket file descriptor. */
1682 /** The given add flags. */
1686 static int path_brother_callback(const struct osl_object
*query
,
1687 struct osl_object
*result
)
1689 char *path
= query
->data
;
1690 struct osl_row
*path_brother
;
1691 int ret
= aft_get_row_of_path(path
, &path_brother
);
1694 result
->data
= para_malloc(sizeof(path_brother
));
1695 result
->size
= sizeof(path_brother
);
1696 *(struct osl_row
**)(result
->data
) = path_brother
;
1700 static int hash_sister_callback(const struct osl_object
*query
,
1701 struct osl_object
*result
)
1703 HASH_TYPE
*hash
= query
->data
;
1704 struct osl_row
*hash_sister
;
1706 hash_sister
= find_hash_sister(hash
);
1708 return -E_RB_KEY_NOT_FOUND
;
1709 result
->data
= para_malloc(sizeof(hash_sister
));
1710 result
->size
= sizeof(hash_sister
);
1711 *(struct osl_row
**)(result
->data
) = hash_sister
;
1715 static int add_one_audio_file(const char *path
, const void *private_data
)
1717 int ret
, send_ret
= 1;
1718 uint8_t format_num
= -1;
1719 const struct private_add_data
*pad
= private_data
;
1720 struct afh_info afhi
, *afhi_ptr
= NULL
;
1721 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1722 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
= {.data
= NULL
};
1723 HASH_TYPE hash
[HASH_SIZE
];
1725 afhi
.header_offset
= 0;
1726 afhi
.header_len
= 0;
1727 ret
= guess_audio_format(path
);
1728 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1730 query
.data
= (char *)path
;
1731 query
.size
= strlen(path
) + 1;
1732 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1733 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1736 pb
= *(struct osl_row
**)result
.data
;
1740 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1741 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1742 send_ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1745 /* We still want to add this file. Compute its hash. */
1746 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, NULL
);
1749 hash_function(map
.data
, map
.size
, hash
);
1751 /* Check whether the database contains a file with the same hash. */
1753 query
.size
= HASH_SIZE
;
1754 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1755 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1758 hs
= *(struct osl_row
**)result
.data
;
1761 /* Return success if we already know this file. */
1763 if (pb
&& hs
&& hs
== pb
&& !(pad
->flags
& ADD_FLAG_FORCE
)) {
1764 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1765 send_ret
= send_va_buffer(pad
->fd
,
1766 "%s exists, not forcing update\n", path
);
1770 * We won't recalculate the audio format info and the chunk table if
1771 * there is a hash sister and FORCE was not given.
1773 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1774 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1780 munmap(map
.data
, map
.size
);
1781 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1782 send_ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1786 save_add_callback_buffer(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1787 /* Ask afs to consider this entry for adding. */
1788 ret
= send_callback_request(com_add_callback
, &obj
, &result
);
1790 send_ret
= send_va_buffer(pad
->fd
, "%s", (char *)result
.data
);
1796 munmap(map
.data
, map
.size
);
1798 if (ret
< 0 && send_ret
>= 0)
1799 send_ret
= send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1800 para_strerror(-ret
));
1803 free(afhi_ptr
->chunk_table
);
1804 /* Stop adding files only on send errors. */
1808 int com_add(int fd
, int argc
, char * const * const argv
)
1811 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1812 struct stat statbuf
;
1814 for (i
= 1; i
< argc
; i
++) {
1815 const char *arg
= argv
[i
];
1818 if (!strcmp(arg
, "--")) {
1822 if (!strcmp(arg
, "-a")) {
1823 pad
.flags
|= ADD_FLAG_ALL
;
1826 if (!strcmp(arg
, "-l")) {
1827 pad
.flags
|= ADD_FLAG_LAZY
;
1830 if (!strcmp(arg
, "-f")) {
1831 pad
.flags
|= ADD_FLAG_FORCE
;
1834 if (!strcmp(arg
, "-v")) {
1835 pad
.flags
|= ADD_FLAG_VERBOSE
;
1840 return -E_AFT_SYNTAX
;
1841 for (; i
< argc
; i
++) {
1843 ret
= verify_path(argv
[i
], &path
);
1845 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
],
1846 para_strerror(-ret
));
1851 ret
= stat(path
, &statbuf
);
1853 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1860 if (S_ISDIR(statbuf
.st_mode
))
1861 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1864 ret
= add_one_audio_file(path
, &pad
);
1866 send_va_buffer(fd
, "%s: %s\n", path
, para_strerror(-ret
));
1877 * Flags used by the touch command.
1882 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1883 TOUCH_FLAG_FNM_PATHNAME
= 1,
1884 /** Activates verbose mode. */
1885 TOUCH_FLAG_VERBOSE
= 2
1888 /** Options used by com_touch(). */
1889 struct com_touch_options
{
1890 /** New num_played value. */
1892 /** New last played count. */
1893 int64_t last_played
;
1894 /** new lyrics id. */
1896 /** new image id. */
1898 /** command line flags (see \ref touch_flags). */
1902 /** Data passed to the action handler of com_touch(). */
1903 struct touch_action_data
{
1904 /** Command line options (see \ref com_touch_options). */
1905 struct com_touch_options
*cto
;
1906 /** Message buffer. */
1907 struct para_buffer pb
;
1908 /** How many audio files matched the given pattern. */
1909 unsigned num_matches
;
1912 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1913 struct osl_row
*row
, const char *name
, void *data
)
1915 struct touch_action_data
*tad
= data
;
1916 struct osl_object obj
;
1917 struct afs_info old_afsi
, new_afsi
;
1918 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1919 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0;
1920 struct afsi_change_event_data aced
;
1922 ret
= get_afsi_object_of_row(row
, &obj
);
1924 para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1927 ret
= load_afsi(&old_afsi
, &obj
);
1929 para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1932 new_afsi
= old_afsi
;
1934 new_afsi
.num_played
++;
1935 new_afsi
.last_played
= time(NULL
);
1936 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1937 para_printf(&tad
->pb
, "%s: num_played = %u, "
1938 "last_played = now()\n", name
,
1939 new_afsi
.num_played
);
1941 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1942 para_printf(&tad
->pb
, "touching %s\n", name
);
1943 if (tad
->cto
->lyrics_id
>= 0)
1944 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1945 if (tad
->cto
->image_id
>= 0)
1946 new_afsi
.image_id
= tad
->cto
->image_id
;
1947 if (tad
->cto
->num_played
>= 0)
1948 new_afsi
.num_played
= tad
->cto
->num_played
;
1949 if (tad
->cto
->last_played
>= 0)
1950 new_afsi
.last_played
= tad
->cto
->last_played
;
1953 save_afsi(&new_afsi
, &obj
); /* in-place update */
1955 aced
.old_afsi
= &old_afsi
;
1956 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
1960 static int com_touch_callback(const struct osl_object
*query
,
1961 struct osl_object
*result
)
1963 struct touch_action_data tad
= {.cto
= query
->data
};
1965 struct pattern_match_data pmd
= {
1966 .table
= audio_file_table
,
1967 .loop_col_num
= AFTCOL_HASH
,
1968 .match_col_num
= AFTCOL_PATH
,
1969 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
1970 .size
= query
->size
- sizeof(*tad
.cto
)},
1972 .action
= touch_audio_file
1974 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
1975 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1976 ret
= for_each_matching_row(&pmd
);
1978 para_printf(&tad
.pb
, "%s\n", para_strerror(-ret
));
1980 if (!tad
.num_matches
)
1981 para_printf(&tad
.pb
, "no matches\n");
1983 result
->data
= tad
.pb
.buf
;
1984 result
->size
= tad
.pb
.size
;
1987 return ret
< 0? ret
: 0;
1990 int com_touch(int fd
, int argc
, char * const * const argv
)
1992 struct com_touch_options cto
= {
1998 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)},
2003 for (i
= 1; i
< argc
; i
++) {
2004 const char *arg
= argv
[i
];
2007 if (!strcmp(arg
, "--")) {
2011 if (!strncmp(arg
, "-n", 2)) {
2012 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
2017 if (!strncmp(arg
, "-l", 2)) {
2018 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
2023 if (!strncmp(arg
, "-y", 2)) {
2024 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
2029 if (!strncmp(arg
, "-i", 2)) {
2030 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
2035 if (!strcmp(arg
, "-p")) {
2036 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
2039 if (!strcmp(arg
, "-v")) {
2040 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
2043 break; /* non-option starting with dash */
2046 return -E_AFT_SYNTAX
;
2047 ret
= send_option_arg_callback_request(&query
, argc
- i
,
2048 argv
+ i
, com_touch_callback
, &result
);
2052 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2055 ret
= send_buffer(fd
, (char *)result
.data
);
2060 /** Flags for com_rm(). */
2063 RM_FLAG_VERBOSE
= 1,
2067 RM_FLAG_FNM_PATHNAME
= 4
2070 /** Data passed to the action handler of com_rm(). */
2071 struct com_rm_action_data
{
2072 /** Command line flags ((see \ref rm_flags). */
2074 /** Message buffer. */
2075 struct para_buffer pb
;
2076 /** Number of audio files removed. */
2077 unsigned num_removed
;
2080 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2081 struct osl_row
*row
, const char *name
, void *data
)
2083 struct com_rm_action_data
*crd
= data
;
2086 if (crd
->flags
& RM_FLAG_VERBOSE
)
2087 para_printf(&crd
->pb
, "removing %s\n", name
);
2088 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2089 ret
= osl_del_row(audio_file_table
, row
);
2091 para_printf(&crd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2097 static int com_rm_callback(const struct osl_object
*query
,
2098 __a_unused
struct osl_object
*result
)
2100 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
};
2102 struct pattern_match_data pmd
= {
2103 .table
= audio_file_table
,
2104 .loop_col_num
= AFTCOL_HASH
,
2105 .match_col_num
= AFTCOL_PATH
,
2106 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2107 .size
= query
->size
- sizeof(uint32_t)},
2109 .action
= remove_audio_file
2111 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2112 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2113 ret
= for_each_matching_row(&pmd
);
2115 para_printf(&crd
.pb
, "%s\n", para_strerror(-ret
));
2116 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2117 para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2119 if (crd
.flags
& RM_FLAG_VERBOSE
)
2120 para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2123 result
->data
= crd
.pb
.buf
;
2124 result
->size
= crd
.pb
.size
;
2127 return ret
< 0? ret
: 0;
2130 /* TODO options: -r (recursive) */
2131 int com_rm(int fd
, int argc
, char * const * const argv
)
2134 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)},
2138 for (i
= 1; i
< argc
; i
++) {
2139 const char *arg
= argv
[i
];
2142 if (!strcmp(arg
, "--")) {
2146 if (!strcmp(arg
, "-f")) {
2147 flags
|= RM_FLAG_FORCE
;
2150 if (!strcmp(arg
, "-p")) {
2151 flags
|= RM_FLAG_FNM_PATHNAME
;
2154 if (!strcmp(arg
, "-v")) {
2155 flags
|= RM_FLAG_VERBOSE
;
2161 return -E_AFT_SYNTAX
;
2162 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2163 com_rm_callback
, &result
);
2167 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2170 ret
= send_buffer(fd
, (char *)result
.data
);
2176 * Flags used by the cpsi command.
2181 /** Whether the lyrics id should be copied. */
2182 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2183 /** Whether the image id should be copied. */
2184 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2185 /** Whether the lastplayed time should be copied. */
2186 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2187 /** Whether the numplayed count should be copied. */
2188 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2189 /** Whether the attributes should be copied. */
2190 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2191 /** Activates verbose mode. */
2192 CPSI_FLAG_VERBOSE
= 32,
2195 /** Data passed to the action handler of com_cpsi(). */
2196 struct cpsi_action_data
{
2197 /** command line flags (see \ref cpsi_flags). */
2199 /** Number of audio files changed. */
2200 unsigned num_copied
;
2201 /** Message buffer. */
2202 struct para_buffer pb
;
2203 /** Values are copied from here. */
2204 struct afs_info source_afsi
;
2207 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2208 struct osl_row
*row
, const char *name
, void *data
)
2210 struct cpsi_action_data
*cad
= data
;
2211 struct osl_object target_afsi_obj
;
2213 struct afs_info old_afsi
, target_afsi
;
2214 struct afsi_change_event_data aced
;
2216 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2219 load_afsi(&target_afsi
, &target_afsi_obj
);
2220 old_afsi
= target_afsi
;
2221 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2222 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2223 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2224 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2225 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2226 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2227 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2228 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2229 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2230 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2231 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2233 if (cad
->flags
& CPSI_FLAG_VERBOSE
)
2234 para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2236 aced
.old_afsi
= &old_afsi
;
2237 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2241 static int com_cpsi_callback(const struct osl_object
*query
,
2242 struct osl_object
*result
)
2244 struct cpsi_action_data cad
= {.flags
= *(unsigned *)query
->data
};
2246 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2248 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2251 struct pattern_match_data pmd
= {
2252 .table
= audio_file_table
,
2253 .loop_col_num
= AFTCOL_HASH
,
2254 .match_col_num
= AFTCOL_PATH
,
2255 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2256 .size
= query
->size
- sizeof(cad
.flags
)
2257 - strlen(source_path
) - 1},
2259 .action
= copy_selector_info
2261 ret
= for_each_matching_row(&pmd
);
2264 para_printf(&cad
.pb
, "%s\n", para_strerror(-ret
));
2265 if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2267 para_printf(&cad
.pb
, "copied requested afsi from %s "
2269 source_path
, cad
.num_copied
);
2271 para_printf(&cad
.pb
, "nothing copied\n");
2274 result
->data
= cad
.pb
.buf
;
2275 result
->size
= cad
.pb
.size
;
2278 return ret
< 0? ret
: 0;
2281 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2285 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
2288 for (i
= 1; i
< argc
; i
++) {
2289 const char *arg
= argv
[i
];
2292 if (!strcmp(arg
, "--")) {
2296 if (!strcmp(arg
, "-y")) {
2297 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2300 if (!strcmp(arg
, "-i")) {
2301 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2304 if (!strcmp(arg
, "-l")) {
2305 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2308 if (!strcmp(arg
, "-n")) {
2309 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2312 if (!strcmp(arg
, "-a")) {
2313 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2316 if (!strcmp(arg
, "-v")) {
2317 flags
|= CPSI_FLAG_VERBOSE
;
2322 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2323 return -E_AFT_SYNTAX
;
2324 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2325 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2326 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2327 com_cpsi_callback
, &result
);
2331 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2334 ret
= send_buffer(fd
, (char *)result
.data
);
2339 /* TODO: optionally fix problems by removing offending rows */
2340 static int check_audio_file(struct osl_row
*row
, void *data
)
2343 struct para_buffer
*pb
= data
;
2344 struct stat statbuf
;
2345 int ret
= get_audio_file_path_of_row(row
, &path
);
2346 struct afs_info afsi
;
2350 para_printf(pb
, "%s\n", para_strerror(-ret
));
2353 if (stat(path
, &statbuf
) < 0)
2354 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2356 if (!S_ISREG(statbuf
.st_mode
))
2357 para_printf(pb
, "%s: not a regular file\n", path
);
2359 ret
= get_afsi_of_row(row
, &afsi
);
2361 para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
2364 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2366 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2367 para_strerror(-ret
));
2368 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2370 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2371 para_strerror(-ret
));
2376 * Check the audio file table for inconsistencies.
2378 * \param query Unused.
2379 * \param result Contains message string upon return.
2381 * This function always succeeds.
2385 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
2387 struct para_buffer pb
= {.buf
= NULL
};
2389 para_printf(&pb
, "checking audio file table...\n");
2390 audio_file_loop(&pb
, check_audio_file
);
2391 result
->data
= pb
.buf
;
2392 result
->size
= pb
.size
;
2398 * Close the audio file table.
2400 * \param flags Usual flags that are passed to osl_close_table().
2402 * \sa osl_close_table().
2404 static void aft_close(void)
2406 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2407 audio_file_table
= NULL
;
2411 * Open the audio file table.
2413 * \param dir The database directory.
2417 * \sa osl_open_table().
2419 static int aft_open(const char *dir
)
2423 audio_file_table_desc
.dir
= dir
;
2424 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2427 osl_get_num_rows(audio_file_table
, &num
);
2428 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2431 PARA_INFO_LOG("failed to open audio file table\n");
2432 audio_file_table
= NULL
;
2433 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2438 static int aft_create(const char *dir
)
2440 audio_file_table_desc
.dir
= dir
;
2441 return osl_create_table(&audio_file_table_desc
);
2444 static int clear_attribute(struct osl_row
*row
, void *data
)
2446 struct rmatt_event_data
*red
= data
;
2447 struct afs_info afsi
;
2448 struct osl_object obj
;
2449 int ret
= get_afsi_object_of_row(row
, &obj
);
2450 uint64_t mask
= ~(1ULL << red
->bitnum
);
2454 ret
= load_afsi(&afsi
, &obj
);
2457 afsi
.attributes
&= mask
;
2458 save_afsi(&afsi
, &obj
);
2462 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2466 case ATTRIBUTE_REMOVE
: {
2467 const struct rmatt_event_data
*red
= data
;
2468 para_printf(pb
, "clearing attribute %s (bit %u) from all "
2469 "entries in the audio file table\n", red
->name
,
2471 return audio_file_loop(data
, clear_attribute
);
2478 void aft_init(struct afs_table
*t
)
2480 t
->name
= audio_file_table_desc
.name
;
2482 t
->close
= aft_close
;
2483 t
->create
= aft_create
;
2484 t
->event_handler
= aft_event_handler
;