11 #define AFS_AUDIO_FILE_DIR "/home/mp3"
13 static struct osl_table
*audio_file_table
;
16 * Describes the layout of the mmapped-afs info struct.
18 * \sa struct afs_info.
21 /** Where .last_played is stored. */
22 AFSI_LAST_PLAYED_OFFSET
= 0,
23 /** Storage position of the attributes bitmap. */
24 AFSI_ATTRIBUTES_OFFSET
= 8,
25 /** Storage position of the .num_played field. */
26 AFSI_NUM_PLAYED_OFFSET
= 16,
27 /** Storage position of the .image_id field. */
28 AFSI_IMAGE_ID_OFFSET
= 20,
29 /** Storage position of the .lyrics_id field. */
30 AFSI_LYRICS_ID_OFFSET
= 24,
31 /** Storage position of the .audio_format_id field. */
32 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
33 /** On-disk storage space needed. */
38 * Convert a struct afs_info to an osl object.
40 * \param afsi Pointer to the audio file info to be converted.
41 * \param obj Result pointer.
45 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
47 char *buf
= obj
->data
;
49 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
50 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
51 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
52 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
53 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
54 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
55 afsi
->audio_format_id
);
59 * Get the audio file selector info struct stored in an osl object.
61 * \param afsi Points to the audio_file info structure to be filled in.
62 * \param obj The osl object holding the data.
64 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
68 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
70 char *buf
= obj
->data
;
71 if (obj
->size
< AFSI_SIZE
)
73 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
74 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
75 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
76 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
77 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
78 afsi
->audio_format_id
= read_u8(buf
+
79 AFSI_AUDIO_FORMAT_ID_OFFSET
);
83 /** The columns of the audio file table. */
84 enum audio_file_table_columns
{
85 /** The hash on the content of the audio file. */
87 /** The full path in the filesystem. */
89 /** The audio file selector info. */
91 /** The audio format handler info. */
93 /** The chunk table info and the chunk table of the audio file. */
95 /** The number of columns of this table. */
99 static struct osl_column_description aft_cols
[] = {
101 .storage_type
= OSL_MAPPED_STORAGE
,
102 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
104 .compare_function
= osl_hash_compare
,
105 .data_size
= HASH_SIZE
108 .storage_type
= OSL_MAPPED_STORAGE
,
109 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
111 .compare_function
= string_compare
,
114 .storage_type
= OSL_MAPPED_STORAGE
,
115 .storage_flags
= OSL_FIXED_SIZE
,
117 .data_size
= AFSI_SIZE
120 .storage_type
= OSL_MAPPED_STORAGE
,
124 .storage_type
= OSL_DISK_STORAGE
,
129 static struct osl_table_description audio_file_table_desc
= {
130 .name
= "audio_files",
131 .num_columns
= NUM_AFT_COLUMNS
,
132 .flags
= OSL_LARGE_TABLE
,
133 .column_descriptions
= aft_cols
136 static char *prefix_path(const char *prefix
, int len
, const char *path
)
166 /* Remove last component of the prefix */
171 } while (len
&& prefix
[len
-1] != '/');
175 return para_strdup(path
);
176 speclen
= strlen(path
);
177 n
= para_malloc(speclen
+ len
+ 1);
178 memcpy(n
, prefix
, len
);
179 memcpy(n
+ len
, path
, speclen
+1);
184 * We fundamentally don't like some paths: we don't want
185 * dot or dot-dot anywhere.
187 * Also, we don't want double slashes or slashes at the
188 * end that can make pathnames ambiguous.
190 static int verify_dotfile(const char *rest
)
193 * The first character was '.', but that has already been discarded, we
197 /* "." is not allowed */
202 if (rest
[1] == '\0' || rest
[1] == '/')
208 static int verify_path(const char *orig_path
, char **resolved_path
)
211 const char prefix
[] = AFS_AUDIO_FILE_DIR
"/";
212 const char *path
= orig_path
;
213 const size_t prefix_len
= strlen(prefix
);
224 case '/': /* double slash */
227 if (verify_dotfile(path
) < 0)
233 if (*orig_path
!= '/')
234 *resolved_path
= prefix_path(prefix
, prefix_len
, orig_path
);
236 *resolved_path
= para_strdup(orig_path
);
243 AFHI_SECONDS_TOTAL_OFFSET
= 0,
244 AFHI_BITRATE_OFFSET
= 4,
245 AFHI_FREQUENCY_OFFSET
= 8,
246 AFHI_CHANNELS_OFFSET
= 12,
247 AFHI_INFO_STRING_OFFSET
= 13,
251 static unsigned sizeof_afhi_buf(const struct audio_format_info
*afhi
)
255 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
258 static void save_afhi(struct audio_format_info
*afhi
, char *buf
)
262 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
263 afhi
->seconds_total
);
264 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
265 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
266 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
267 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
268 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
271 static void load_afhi(const char *buf
, struct audio_format_info
*afhi
)
273 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
274 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
275 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
276 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
277 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
280 static unsigned sizeof_chunk_info_buf(struct audio_format_info
*afhi
)
284 return 4 * afhi
->chunks_total
+ 20;
288 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
289 enum chunk_info_offsets
{
290 /** The total number of chunks (4 bytes). */
291 CHUNKS_TOTAL_OFFSET
= 0,
292 /** The length of the audio file header (4 bytes). */
293 HEADER_LEN_OFFSET
= 4,
294 /** The start of the audio file header (4 bytes). */
295 HEADER_OFFSET_OFFSET
= 8,
296 /** The seconds part of the chunk time (4 bytes). */
297 CHUNK_TV_TV_SEC_OFFSET
= 12,
298 /** The microseconds part of the chunk time (4 bytes). */
299 CHUNK_TV_TV_USEC
= 16,
300 /** Chunk table entries start here. */
301 CHUNK_TABLE_OFFSET
= 20,
304 /* TODO: audio format handlers could just produce this */
305 static void save_chunk_info(struct audio_format_info
*afhi
, char *buf
)
311 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
312 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
313 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
314 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
315 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
316 for (i
= 0; i
< afhi
->chunks_total
; i
++)
317 write_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
, afhi
->chunk_table
[i
]);
320 static int load_chunk_info(struct osl_object
*obj
, struct audio_format_info
*afhi
)
322 char *buf
= obj
->data
;
325 if (obj
->size
< CHUNK_TABLE_OFFSET
)
326 return -E_BAD_DATA_SIZE
;
328 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
329 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
330 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
331 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
332 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
334 if (afhi
->chunks_total
* 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
335 return -E_BAD_DATA_SIZE
;
336 afhi
->chunk_table
= para_malloc(afhi
->chunks_total
* sizeof(size_t));
337 for (i
= 0; i
< afhi
->chunks_total
; i
++)
338 afhi
->chunk_table
[i
] = read_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
);
343 * Get the row of the audio file table corresponding to the given path.
345 * \param path The full path of the audio file.
346 * \param row Result pointer.
348 * \return The return value of the underlying call to osl_get_row().
350 int aft_get_row_of_path(char *path
, struct osl_row
**row
)
352 struct osl_object obj
= {.data
= path
, .size
= strlen(path
) + 1};
354 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
358 * Get the row of the audio file table corresponding to the given hash value.
360 * \param hash The hash value of the desired audio file.
361 * \param row resul pointer.
363 * \return The return value of the underlying call to osl_get_row().
365 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
367 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
368 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
372 * Get the osl object holding the audio file selector info of a row.
374 * \param row Pointer to a row in the audio file table.
375 * \param obj Result pointer.
377 * \return The return value of the underlying call to osl_get_object().
379 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
381 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
385 * Get the osl object holding the audio file selector info, given a path.
388 * \param path The full path of the audio file.
389 * \param obj Result pointer.
391 * \return Positive on success, negative on errors.
393 int get_afsi_object_of_path(char *path
, struct osl_object
*obj
)
396 int ret
= aft_get_row_of_path(path
, &row
);
399 return get_afsi_object_of_row(row
, obj
);
403 * Get the audio file selector info, given a row of the audio file table.
405 * \param row Pointer to a row in the audio file table.
406 * \param afsi Result pointer.
408 * \return Positive on success, negative on errors.
410 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
412 struct osl_object obj
;
413 int ret
= get_afsi_object_of_row(row
, &obj
);
416 return load_afsi(afsi
, &obj
);
420 * Get the path of an audio file, given a row of the audio file table.
422 * \param row Pointer to a row in the audio file table.
423 * \param path Result pointer.
425 * \return Positive on success, negative on errors.
427 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
429 struct osl_object path_obj
;
430 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
434 *path
= path_obj
.data
;
439 * Get the object containing the hash value of an audio file, given a row.
441 * \param row Pointer to a row of the audio file table.
442 * \param obj Result pointer.
444 * \return The return value of the underlying call to osl_get_object().
446 * \sa get_hash_of_row().
448 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
450 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
454 * Get the hash value of an audio file, given a row of the audio file table.
456 * \param row Pointer to a row of the audio file table.
457 * \param hash Result pointer.
459 * \a hash points to mapped data and must not be freed by the caller.
461 * \return The return value of the underlying call to
462 * get_hash_object_of_aft_row().
464 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
466 struct osl_object obj
;
467 int ret
= get_hash_object_of_aft_row(row
, &obj
);
476 * Get the audio format handler info, given a row of the audio file table.
478 * \param row Pointer to a row of the audio file table.
479 * \param afhi Result pointer.
481 * \return The return value of the underlying call to osl_get_object().
483 * \sa get_chunk_table_of_row().
485 static int get_afhi_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
487 struct osl_object obj
;
488 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
492 load_afhi(obj
.data
, afhi
);
497 * Get the chunk table of an audio file, given a row of the audio file table.
499 * \param row Pointer to a row of the audio file table.
500 * \param afhi Result pointer.
502 * \return The return value of the underlying call to osl_open_disk_object().
504 * \sa get_afhi_of_row().
506 static int get_chunk_table_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
508 struct osl_object obj
;
509 int ret
= osl_open_disk_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
513 ret
= load_chunk_info(&obj
, afhi
);
514 osl_close_disk_object(&obj
);
519 * Mmap the given audio file and update statistics.
521 * \param aft_row Determines the audio file to be opened and updated.
522 * \param afd Result pointer.
524 * On success, the numplayed field of the audio file selector info is increased
525 * and the lastplayed time is set to the current time. Finally, the score of
526 * the audio file is updated.
528 * \return Positive on success, negative on errors.
530 int open_and_update_audio_file(struct osl_row
*aft_row
, struct audio_file_data
*afd
)
532 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
533 struct osl_object afsi_obj
;
534 struct afs_info new_afsi
;
535 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
539 ret
= get_audio_file_path_of_row(aft_row
, &afd
->path
);
542 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
545 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
548 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
551 ret
= get_chunk_table_of_row(aft_row
, &afd
->afhi
);
554 ret
= mmap_full_file(afd
->path
, O_RDONLY
, &afd
->map
);
557 hash_function(afd
->map
.data
, afd
->map
.size
, file_hash
);
558 ret
= -E_HASH_MISMATCH
;
559 if (hash_compare(file_hash
, aft_hash
))
561 new_afsi
= afd
->afsi
;
562 new_afsi
.num_played
++;
563 new_afsi
.last_played
= time(NULL
);
564 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
565 if (afd
->current_play_mode
== PLAY_MODE_PLAYLIST
)
566 ret
= playlist_update_audio_file(aft_row
);
568 ret
= mood_update_audio_file(aft_row
, &afd
->afsi
);
571 free(afd
->afhi
.chunk_table
);
575 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
580 if (!localtime_r((time_t *)seconds
, &t
))
582 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
583 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
587 if (!strftime(buf
, size
, "%b %e %Y", &t
))
592 #define GET_NUM_DIGITS(x, num) { \
593 typeof((x)) _tmp = PARA_ABS(x); \
596 while ((_tmp) > 9) { \
602 static short unsigned get_duration_width(int seconds
)
604 short unsigned width
;
605 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
607 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
608 return 4 + (mins
> 9);
609 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
610 GET_NUM_DIGITS(hours
, &width
);
614 static void get_duration_buf(int seconds
, char *buf
, short unsigned max_width
)
616 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
618 if (!hours
) /* m:ss or mm:ss */
619 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
620 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
621 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
625 static char *make_attribute_line(const char *att_bitmap
, struct afs_info
*afsi
)
627 char *att_text
, *att_line
;
629 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
631 return para_strdup(att_bitmap
);
632 att_line
= make_message("%s (%s)", att_bitmap
, att_text
);
637 static char *make_lyrics_line(struct afs_info
*afsi
)
640 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
642 return make_message("%u", afsi
->lyrics_id
);
643 return make_message("%u (%s)", afsi
->lyrics_id
, lyrics_name
);
646 static char *make_image_line(struct afs_info
*afsi
)
649 img_get_name_by_id(afsi
->image_id
, &image_name
);
651 return make_message("%u", afsi
->image_id
);
652 return make_message("%u (%s)", afsi
->image_id
, image_name
);
655 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
656 struct para_buffer
*b
, time_t current_time
)
660 char last_played_time
[30];
661 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
662 char score_buf
[30] = "";
663 struct afs_info
*afsi
= &d
->afsi
;
664 struct audio_format_info
*afhi
= &d
->afhi
;
665 struct ls_widths
*w
= &opts
->widths
;
666 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
668 if (opts
->mode
== LS_MODE_SHORT
) {
669 para_printf(b
, "%s\n", d
->path
);
672 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
673 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
674 sizeof(last_played_time
), current_time
);
677 get_duration_buf(afhi
->seconds_total
, duration_buf
, w
->duration_width
);
679 if (opts
->mode
== LS_MODE_LONG
)
680 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
682 sprintf(score_buf
, "%li ", d
->score
);
685 PARA_NOTICE_LOG("id: %s, %d\n", d
->path
, afsi
->audio_format_id
);
686 if (opts
->mode
== LS_MODE_LONG
) {
689 "%s " /* attributes */
690 "%*d " /* image_id */
691 "%*d " /* lyrics_id */
693 "%s " /* audio format */
694 "%*d " /* frequency */
697 "%*d " /* num_played */
698 "%s " /* last_played */
702 w
->image_id_width
, afsi
->image_id
,
703 w
->lyrics_id_width
, afsi
->lyrics_id
,
704 w
->bitrate_width
, afhi
->bitrate
,
705 audio_format_name(afsi
->audio_format_id
),
706 w
->frequency_width
, afhi
->frequency
,
709 w
->num_played_width
, afsi
->num_played
,
715 if (opts
->mode
== LS_MODE_VERBOSE
) {
716 char asc_hash
[2 * HASH_SIZE
+ 1];
717 char *att_line
, *lyrics_line
, *image_line
;
719 hash_to_asc(d
->hash
, asc_hash
);
720 att_line
= make_attribute_line(att_buf
, afsi
);
721 lyrics_line
= make_lyrics_line(afsi
);
722 image_line
= make_image_line(afsi
);
724 "%s: %s\n" /* path */
730 "bitrate: %dkbit/s\n"
736 "last_played: %s\n\n",
737 (opts
->flags
& LS_FLAG_FULL_PATH
)?
738 "path" : "file", d
->path
,
739 have_score
? "score: " : "", score_buf
,
740 have_score
? "\n" : "",
746 audio_format_name(afsi
->audio_format_id
),
761 static int ls_audio_format_compare(const void *a
, const void *b
)
763 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
764 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
767 static int ls_duration_compare(const void *a
, const void *b
)
769 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
770 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
773 static int ls_bitrate_compare(const void *a
, const void *b
)
775 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
776 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
779 static int ls_lyrics_id_compare(const void *a
, const void *b
)
781 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
782 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
785 static int ls_image_id_compare(const void *a
, const void *b
)
787 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
788 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
791 static int ls_channels_compare(const void *a
, const void *b
)
793 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
794 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
797 static int ls_frequency_compare(const void *a
, const void *b
)
799 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
800 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
803 static int ls_num_played_compare(const void *a
, const void *b
)
805 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
806 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
809 static int ls_last_played_compare(const void *a
, const void *b
)
811 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
812 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
815 static int ls_score_compare(const void *a
, const void *b
)
817 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
818 return NUM_COMPARE(d1
->score
, d2
->score
);
821 static int ls_path_compare(const void *a
, const void *b
)
823 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
824 return strcmp(d1
->path
, d2
->path
);
827 static int sort_matching_paths(struct ls_options
*options
)
829 size_t nmemb
= options
->num_matching_paths
;
830 size_t size
= sizeof(*options
->data_ptr
);
831 int (*compar
)(const void *, const void *);
834 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
835 for (i
= 0; i
< nmemb
; i
++)
836 options
->data_ptr
[i
] = options
->data
+ i
;
838 /* In these cases the array is already sorted */
839 if (options
->sorting
== LS_SORT_BY_PATH
840 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
841 && (options
->flags
& LS_FLAG_FULL_PATH
))
843 if (options
->sorting
== LS_SORT_BY_SCORE
&&
844 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
847 switch (options
->sorting
) {
848 case LS_SORT_BY_PATH
:
849 compar
= ls_path_compare
; break;
850 case LS_SORT_BY_SCORE
:
851 compar
= ls_score_compare
; break;
852 case LS_SORT_BY_LAST_PLAYED
:
853 compar
= ls_last_played_compare
; break;
854 case LS_SORT_BY_NUM_PLAYED
:
855 compar
= ls_num_played_compare
; break;
856 case LS_SORT_BY_FREQUENCY
:
857 compar
= ls_frequency_compare
; break;
858 case LS_SORT_BY_CHANNELS
:
859 compar
= ls_channels_compare
; break;
860 case LS_SORT_BY_IMAGE_ID
:
861 compar
= ls_image_id_compare
; break;
862 case LS_SORT_BY_LYRICS_ID
:
863 compar
= ls_lyrics_id_compare
; break;
864 case LS_SORT_BY_BITRATE
:
865 compar
= ls_bitrate_compare
; break;
866 case LS_SORT_BY_DURATION
:
867 compar
= ls_duration_compare
; break;
868 case LS_SORT_BY_AUDIO_FORMAT
:
869 compar
= ls_audio_format_compare
; break;
873 qsort(options
->data_ptr
, nmemb
, size
, compar
);
877 /* row is either an aft_row or a row of the score table */
878 /* TODO: Only compute widths if we need them */
879 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
882 struct ls_options
*options
= ls_opts
;
885 unsigned short num_digits
;
887 struct osl_row
*aft_row
;
891 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
892 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
897 ret
= get_audio_file_path_of_row(aft_row
, &path
);
900 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
901 char *p
= strrchr(path
, '/');
905 if (options
->num_patterns
) {
906 for (i
= 0; i
< options
->num_patterns
; i
++) {
907 ret
= fnmatch(options
->patterns
[i
], path
, FNM_PATHNAME
);
910 if (ret
== FNM_NOMATCH
)
914 if (i
>= options
->num_patterns
) /* no match */
917 tmp
= options
->num_matching_paths
++;
918 if (options
->num_matching_paths
> options
->array_size
) {
919 options
->array_size
++;
920 options
->array_size
*= 2;
921 options
->data
= para_realloc(options
->data
, options
->array_size
922 * sizeof(*options
->data
));
924 d
= options
->data
+ tmp
;
925 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
928 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
932 ret
= get_hash_of_row(aft_row
, &d
->hash
);
935 w
= &options
->widths
;
936 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
937 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
938 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
939 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
940 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
941 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
942 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
943 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
944 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
945 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
946 /* get the number of chars to print this amount of time */
947 tmp
= get_duration_width(d
->afhi
.seconds_total
);
948 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
949 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
950 GET_NUM_DIGITS(score
, &num_digits
);
951 num_digits
++; /* add one for the sign (space or "-") */
952 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
958 static int com_ls_callback(const struct osl_object
*query
,
959 struct osl_object
*ls_output
)
961 struct ls_options
*opts
= query
->data
;
962 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
963 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
968 PARA_NOTICE_LOG("%d patterns\n", opts
->num_patterns
);
969 if (opts
->num_patterns
) {
970 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
971 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
972 opts
->patterns
[i
] = p
;
974 PARA_NOTICE_LOG("pattern %d: %s\n", i
, opts
->patterns
[i
]);
977 opts
->patterns
= NULL
;
978 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
979 ret
= admissible_file_loop(opts
, prepare_ls_row
);
981 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
985 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
986 if (!opts
->num_matching_paths
) {
987 PARA_NOTICE_LOG("no match, ret: %d\n", ret
);
990 ret
= sort_matching_paths(opts
);
994 if (opts
->flags
& LS_FLAG_REVERSE
)
995 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
996 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1001 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1002 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1008 ls_output
->data
= b
.buf
;
1009 PARA_NOTICE_LOG("ls_outoute.data: %p\n", ls_output
->data
);
1010 ls_output
->size
= b
.size
;
1012 free(opts
->data_ptr
);
1013 free(opts
->patterns
);
1018 * TODO: flags -h (sort by hash) -lm (list in mbox format)
1020 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1021 * full list: list everything, including afsi, afhi, atts as clear text
1024 int com_afs_ls(int fd
, int argc
, char * const * const argv
)
1028 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1029 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1030 struct ls_options opts
= {.patterns
= NULL
};
1031 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1034 for (i
= 1; i
< argc
; i
++) {
1035 const char *arg
= argv
[i
];
1038 if (!strcmp(arg
, "--")) {
1042 if (!strncmp(arg
, "-l", 2)) {
1044 mode
= LS_MODE_LONG
;
1048 return -E_AFT_SYNTAX
;
1049 switch(*(arg
+ 2)) {
1051 mode
= LS_MODE_SHORT
;
1054 mode
= LS_MODE_LONG
;
1057 mode
= LS_MODE_VERBOSE
;
1060 mode
= LS_MODE_MBOX
;
1063 return -E_AFT_SYNTAX
;
1066 if (!strcmp(arg
, "-p")) {
1067 flags
|= LS_FLAG_FULL_PATH
;
1070 if (!strcmp(arg
, "-a")) {
1071 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1074 if (!strcmp(arg
, "-r")) {
1075 flags
|= LS_FLAG_REVERSE
;
1078 if (!strncmp(arg
, "-s", 2)) {
1079 if (!*(arg
+ 2) || *(arg
+ 3))
1080 return -E_AFT_SYNTAX
;
1081 switch(*(arg
+ 2)) {
1083 sort
= LS_SORT_BY_PATH
;
1085 case 's': /* -ss implies -a */
1086 sort
= LS_SORT_BY_SCORE
;
1087 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1090 sort
= LS_SORT_BY_LAST_PLAYED
;
1093 sort
= LS_SORT_BY_NUM_PLAYED
;
1096 sort
= LS_SORT_BY_FREQUENCY
;
1099 sort
= LS_SORT_BY_CHANNELS
;
1102 sort
= LS_SORT_BY_IMAGE_ID
;
1105 sort
= LS_SORT_BY_LYRICS_ID
;
1108 sort
= LS_SORT_BY_BITRATE
;
1111 sort
= LS_SORT_BY_DURATION
;
1114 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1117 return -E_AFT_SYNTAX
;
1120 return -E_AFT_SYNTAX
;
1123 opts
.sorting
= sort
;
1125 opts
.num_patterns
= argc
- i
;
1126 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1127 argv
+ i
, com_ls_callback
, &ls_output
);
1129 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1130 free(ls_output
.data
);
1136 * Call the given function for each file in the audio file table.
1138 * \param private_data An arbitrary data pointer, passed to \a func.
1139 * \param func The custom function to be called.
1141 * \return The return value of the underlying call to osl_rbtree_loop().
1143 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1145 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1149 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1151 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1152 struct osl_row
*row
;
1154 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1158 enum aft_row_offsets
{
1159 AFTROW_AFHI_OFFSET_POS
= 0,
1160 AFTROW_CHUNKS_OFFSET_POS
= 2,
1161 AFTROW_AUDIO_FORMAT_OFFSET
= 4,
1162 AFTROW_FLAGS_OFFSET
= 5,
1163 AFTROW_HASH_OFFSET
= 9,
1164 AFTROW_PATH_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1167 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1168 * In this case, afhi won't be stored in the buffer */
1169 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1170 struct audio_format_info
*afhi
, uint32_t flags
,
1171 uint8_t audio_format_num
, struct osl_object
*obj
)
1173 size_t path_len
= strlen(path
) + 1;
1174 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1175 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1176 + sizeof_chunk_info_buf(afhi
);
1177 char *buf
= para_malloc(size
);
1180 write_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1181 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1183 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1184 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1186 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1187 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1188 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1189 pos
+ afhi_size
- 1);
1190 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1191 save_afhi(afhi
, buf
+ pos
);
1194 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1195 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1196 save_chunk_info(afhi
, buf
+ pos
);
1197 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1211 AFHI: whether afhi and chunk table are computed and sent
1212 ACTION: table modifications to be performed
1214 +---+----+-----+------+---------------------------------------------------+
1215 | HS | PB | F | AFHI | ACTION
1216 +---+----+-----+------+---------------------------------------------------+
1217 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1218 | | update path, keep afsi
1219 +---+----+-----+------+---------------------------------------------------+
1220 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1221 | | otherwise: remove PB, HS: update path, keep afhi,
1223 +---+----+-----+------+---------------------------------------------------+
1224 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1226 +---+----+-----+------+---------------------------------------------------+
1227 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1228 +---+----+-----+------+---------------------------------------------------+
1229 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1230 | | (force has no effect)
1231 +---+----+-----+------+---------------------------------------------------+
1232 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1233 +---+----+-----+------+---------------------------------------------------+
1234 | N | N | Y | Y | (new file) create new entry (force has no effect)
1235 +---+----+-----+------+---------------------------------------------------+
1236 | N | N | N | Y | (new file) create new entry
1237 +---+----+-----+------+---------------------------------------------------+
1239 afhi <=> force or no HS
1244 #define ADD_FLAG_LAZY 1
1245 #define ADD_FLAG_FORCE 2
1246 #define ADD_FLAG_VERBOSE 4
1248 /* TODO: change log messages so that they get written to the result buffer */
1250 static int com_add_callback(const struct osl_object
*query
,
1251 __a_unused
struct osl_object
*result
)
1253 char *buf
= query
->data
, *path
;
1254 struct osl_row
*pb
, *aft_row
;
1255 const struct osl_row
*hs
;
1256 struct osl_object objs
[NUM_AFT_COLUMNS
];
1258 char asc
[2 * HASH_SIZE
+ 1];
1260 char afsi_buf
[AFSI_SIZE
];
1261 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1262 struct afs_info default_afsi
= {.last_played
= 0};
1264 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1265 hash_to_asc(hash
, asc
);;
1266 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1267 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1269 path
= buf
+ AFTROW_PATH_OFFSET
;
1270 objs
[AFTCOL_PATH
].data
= path
;
1271 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1273 PARA_INFO_LOG("request to add %s\n", path
);
1274 hs
= find_hash_sister(hash
);
1275 ret
= aft_get_row_of_path(path
, &pb
);
1276 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1278 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1279 if (flags
& ADD_FLAG_VERBOSE
)
1280 PARA_NOTICE_LOG("ignoring duplicate %p\n", path
);
1283 if (hs
&& hs
!= pb
) {
1284 struct osl_object obj
;
1285 if (pb
) { /* hs trumps pb, remove pb */
1286 if (flags
& ADD_FLAG_VERBOSE
)
1287 PARA_NOTICE_LOG("removing path brother\n");
1288 ret
= osl_del_row(audio_file_table
, pb
);
1293 /* file rename, update hs' path */
1294 ret
= osl_get_object(audio_file_table
, hs
, AFTCOL_PATH
, &obj
);
1295 if (flags
& ADD_FLAG_VERBOSE
)
1296 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj
.data
, path
);
1297 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1298 &objs
[AFTCOL_PATH
]);
1301 if (!(flags
& ADD_FLAG_FORCE
))
1304 /* no hs or force mode, child must have sent afhi */
1305 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1306 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1308 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1309 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1310 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1312 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1313 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1314 if (pb
&& !hs
) { /* update pb's hash */
1315 char old_asc
[2 * HASH_SIZE
+ 1];
1316 HASH_TYPE
*old_hash
;
1317 ret
= get_hash_of_row(pb
, &old_hash
);
1320 hash_to_asc(old_hash
, old_asc
);
1321 if (flags
& ADD_FLAG_VERBOSE
)
1322 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path
,
1324 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1325 &objs
[AFTCOL_HASH
]);
1329 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1330 const struct osl_row
*row
= pb
? pb
: hs
;
1331 /* update afhi and chunk_table */
1332 if (flags
& ADD_FLAG_VERBOSE
)
1333 PARA_DEBUG_LOG("updating audio format handler info (%zd bytes)\n",
1334 objs
[AFTCOL_AFHI
].size
);
1335 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1336 &objs
[AFTCOL_AFHI
]);
1339 if (flags
& ADD_FLAG_VERBOSE
)
1340 PARA_DEBUG_LOG("updating chunk table\n");
1341 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1342 &objs
[AFTCOL_CHUNKS
]);
1345 return mood_update_audio_file(row
, NULL
);
1347 /* new entry, use default afsi */
1348 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1349 default_afsi
.audio_format_id
= read_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
);
1351 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1352 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1353 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1354 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1357 return mood_update_audio_file(aft_row
, NULL
);
1360 struct private_add_data
{
1365 static int path_brother_callback(const struct osl_object
*query
,
1366 struct osl_object
*result
)
1368 char *path
= query
->data
;
1369 struct osl_row
*path_brother
;
1370 int ret
= aft_get_row_of_path(path
, &path_brother
);
1373 result
->data
= para_malloc(sizeof(path_brother
));
1374 result
->size
= sizeof(path_brother
);
1375 *(struct osl_row
**)(result
->data
) = path_brother
;
1379 static int hash_sister_callback(const struct osl_object
*query
,
1380 struct osl_object
*result
)
1382 HASH_TYPE
*hash
= query
->data
;
1383 struct osl_row
*hash_sister
;
1385 hash_sister
= find_hash_sister(hash
);
1387 return -E_RB_KEY_NOT_FOUND
;
1388 result
->data
= para_malloc(sizeof(hash_sister
));
1389 result
->size
= sizeof(hash_sister
);
1390 *(struct osl_row
**)(result
->data
) = hash_sister
;
1394 static int add_one_audio_file(const char *arg
, const void *private_data
)
1397 uint8_t format_num
= -1;
1398 const struct private_add_data
*pad
= private_data
;
1399 struct audio_format_info afhi
, *afhi_ptr
= NULL
;
1400 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1401 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
;
1403 HASH_TYPE hash
[HASH_SIZE
];
1405 afhi
.header_offset
= 0;
1406 afhi
.header_len
= 0;
1407 ret
= verify_path(arg
, &path
);
1411 query
.size
= strlen(path
) + 1;
1412 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1413 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1416 pb
= *(struct osl_row
**)result
.data
;
1420 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1421 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1422 ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1425 /* We still want to add this file. Compute its hash. */
1426 ret
= mmap_full_file(path
, O_RDONLY
, &map
);
1429 hash_function(map
.data
, map
.size
, hash
);
1431 /* Check whether database contains file with the same hash. */
1433 query
.size
= HASH_SIZE
;
1434 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1435 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1438 hs
= *(struct osl_row
**)result
.data
;
1441 /* Return success if we already know this file. */
1443 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1444 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1445 ret
= send_va_buffer(pad
->fd
,
1446 "not forcing update: %s\n", path
);
1450 * we won't recalculate the audio format info and the chunk table if
1451 * there is a hash sister unless in FORCE mode.
1453 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1454 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1460 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1461 send_va_buffer(pad
->fd
, "adding %s\n", path
);
1462 munmap(map
.data
, map
.size
);
1463 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1464 /* Ask afs to consider this entry for adding. */
1465 ret
= send_callback_request(com_add_callback
, &obj
, NULL
);
1469 munmap(map
.data
, map
.size
);
1472 send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
?
1473 path
: arg
, PARA_STRERROR(-ret
));
1477 free(afhi_ptr
->chunk_table
);
1478 return 1; /* it's not an error if not all files could be added */
1481 int com_add(int fd
, int argc
, char * const * const argv
)
1484 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1485 struct stat statbuf
;
1487 for (i
= 1; i
< argc
; i
++) {
1488 const char *arg
= argv
[i
];
1491 if (!strcmp(arg
, "--")) {
1495 if (!strcmp(arg
, "-l")) {
1496 pad
.flags
|= ADD_FLAG_LAZY
;
1499 if (!strcmp(arg
, "-f")) {
1500 pad
.flags
|= ADD_FLAG_FORCE
;
1503 if (!strcmp(arg
, "-v")) {
1504 pad
.flags
|= ADD_FLAG_VERBOSE
;
1509 return -E_AFT_SYNTAX
;
1510 for (; i
< argc
; i
++) {
1511 char *path
= para_strdup(argv
[i
]);
1512 size_t len
= strlen(path
);
1513 while (len
> 1 && path
[--len
] == '/')
1515 ret
= stat(path
, &statbuf
);
1517 PARA_NOTICE_LOG("failed to stat %s (%s)", path
,
1520 if (S_ISDIR(statbuf
.st_mode
))
1521 for_each_file_in_dir(path
, add_one_audio_file
,
1524 add_one_audio_file(path
, &pad
);
1532 struct com_touch_options
{
1539 static int com_touch_callback(const struct osl_object
*query
,
1540 __a_unused
struct osl_object
*result
)
1542 struct com_touch_options
*cto
= query
->data
;
1543 char *p
= (char *)query
->data
+ sizeof(*cto
);
1545 int ret
, no_options
= cto
->num_played
< 0 && cto
->last_played
< 0 &&
1546 cto
->lyrics_id
< 0 && cto
->image_id
< 0;
1548 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1549 struct afs_info old_afsi
, new_afsi
;
1550 struct osl_object obj
;
1551 struct osl_row
*row
;
1554 ret
= aft_get_row_of_path(p
, &row
);
1557 ret
= get_afsi_object_of_row(row
, &obj
);
1560 ret
= load_afsi(&old_afsi
, &obj
);
1563 new_afsi
= old_afsi
;
1565 new_afsi
.num_played
++;
1566 new_afsi
.last_played
= time(NULL
);
1568 if (cto
->lyrics_id
>= 0)
1569 new_afsi
.lyrics_id
= cto
->lyrics_id
;
1570 if (cto
->image_id
>= 0)
1571 new_afsi
.image_id
= cto
->image_id
;
1572 if (cto
->num_played
>= 0)
1573 new_afsi
.num_played
= cto
->num_played
;
1574 if (cto
->last_played
>= 0)
1575 new_afsi
.last_played
= cto
->last_played
;
1577 save_afsi(&new_afsi
, &obj
); /* in-place update */
1578 ret
= mood_update_audio_file(row
, &old_afsi
);
1585 int com_touch(__a_unused
int fd
, int argc
, char * const * const argv
)
1587 struct com_touch_options cto
= {
1593 struct osl_object options
= {.data
= &cto
, .size
= sizeof(cto
)};
1597 for (i
= 1; i
< argc
; i
++) {
1598 const char *arg
= argv
[i
];
1601 if (!strcmp(arg
, "--")) {
1605 if (!strncmp(arg
, "-n", 2)) {
1606 ret
= para_atol(arg
+ 2, &cto
.num_played
);
1611 if (!strncmp(arg
, "-l", 2)) {
1612 ret
= para_atol(arg
+ 2, &cto
.last_played
);
1617 if (!strncmp(arg
, "-y", 2)) {
1618 ret
= para_atol(arg
+ 2, &cto
.lyrics_id
);
1623 if (!strncmp(arg
, "-i", 2)) {
1624 ret
= para_atol(arg
+ 2, &cto
.image_id
);
1630 ret
= -E_AFT_SYNTAX
;
1633 return send_option_arg_callback_request(&options
, argc
- i
,
1634 argv
+ i
, com_touch_callback
, NULL
);
1639 struct com_rm_options
{
1643 static int com_rm_callback(const struct osl_object
*query
,
1644 __a_unused
struct osl_object
*result
)
1646 struct com_rm_options
*cro
= query
->data
;
1647 char *p
= (char *)query
->data
+ sizeof(*cro
);
1651 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1652 struct osl_row
*row
;
1655 ret
= aft_get_row_of_path(p
, &row
);
1658 ret
= mood_delete_audio_file(row
);
1661 ret
= osl_del_row(audio_file_table
, row
);
1669 * TODO options: -v verbose, -f dont stop if file not found
1670 * -h remove by hash, use fnmatch
1674 int com_afs_rm(__a_unused
int fd
, int argc
, char * const * const argv
)
1676 struct com_rm_options cro
= {.flags
= 0};
1677 struct osl_object options
= {.data
= &cro
, .size
= sizeof(cro
)};
1680 for (i
= 1; i
< argc
; i
++) {
1681 const char *arg
= argv
[i
];
1684 if (!strcmp(arg
, "--")) {
1689 ret
= -E_AFT_SYNTAX
;
1692 return send_option_arg_callback_request(&options
, argc
- i
,
1693 argv
+ i
, com_rm_callback
, NULL
);
1699 * Close the audio file table.
1701 * \param flags Ususal flags that are passed to osl_close_table().
1703 * \sa osl_close_table().
1705 void aft_shutdown(enum osl_close_flags flags
)
1707 osl_close_table(audio_file_table
, flags
);
1708 audio_file_table
= NULL
;
1712 * Open the audio file table.
1714 * \param ti Gets initialized by this function.
1715 * \param db The database directory.
1717 * \return Positive on success, negative on errors.
1719 * \sa osl_open_table().
1721 int aft_init(struct table_info
*ti
, const char *db
)
1725 audio_file_table_desc
.dir
= db
;
1726 ti
->desc
= &audio_file_table_desc
;
1727 ret
= osl_open_table(ti
->desc
, &ti
->table
);
1730 audio_file_table
= ti
->table
;
1731 osl_get_num_rows(audio_file_table
, &num
);
1732 PARA_INFO_LOG("audio file table contains %d files\n", num
);
1735 PARA_INFO_LOG("failed to open audio file table\n");
1736 audio_file_table
= NULL
;
1737 return ret
== -E_NOENT
? 1 : ret
;