2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file aft.c Audio file table functions. */
19 #define AFS_AUDIO_FILE_DIR "/home/mp3" /* FIXME: Use cwd instead */
21 static struct osl_table
*audio_file_table
;
23 /** The different sorting methods of the ls command. */
24 enum ls_sorting_method
{
30 LS_SORT_BY_LAST_PLAYED
,
32 LS_SORT_BY_NUM_PLAYED
,
46 LS_SORT_BY_AUDIO_FORMAT
,
51 /** The different listing modes of the ls command. */
52 enum ls_listing_mode
{
53 /** Default listing mode. */
63 /** The flags accepted by the ls command. */
66 LS_FLAG_FULL_PATH
= 1,
68 LS_FLAG_ADMISSIBLE_ONLY
= 2,
74 * The size of the individual output fields of the ls command.
76 * These depend on the actual content being listed. If, for instance only files
77 * with duration less than an hour are being listed, then the duration with is
78 * made smaller because then the duration is listed as mm:ss rather than
82 /** size of the score field. */
83 unsigned short score_width
;
84 /** size of the image id field. */
85 unsigned short image_id_width
;
86 /** size of the lyrics id field. */
87 unsigned short lyrics_id_width
;
88 /** size of the bitrate field. */
89 unsigned short bitrate_width
;
90 /** size of the frequency field. */
91 unsigned short frequency_width
;
92 /** size of the duration field. */
93 unsigned short duration_width
;
94 /** size of the num played field. */
95 unsigned short num_played_width
;
98 /** Data passed to the different compare functions (called by qsort()). */
100 /** Usual audio format handler information. */
101 struct audio_format_info afhi
;
102 /** Audio file selector information. */
103 struct afs_info afsi
;
104 /** The full path of the audio file. */
106 /** The score value (if -a was given). */
108 /** The sha1 hash of audio file. */
114 enum ls_sorting_method sorting
;
115 enum ls_listing_mode mode
;
118 struct ls_widths widths
;
120 uint32_t num_matching_paths
;
121 struct ls_data
*data
;
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 /** On-disk storage space needed. */
148 * Convert a struct afs_info to an osl object.
150 * \param afsi Pointer to the audio file info to be converted.
151 * \param obj Result pointer.
155 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
157 char *buf
= obj
->data
;
159 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
160 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
161 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
162 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
163 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
164 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
165 afsi
->audio_format_id
);
169 * Get the audio file selector info struct stored in an osl object.
171 * \param afsi Points to the audio_file info structure to be filled in.
172 * \param obj The osl object holding the data.
174 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
178 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
180 char *buf
= obj
->data
;
181 if (obj
->size
< AFSI_SIZE
)
183 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
184 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
185 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
186 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
187 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
188 afsi
->audio_format_id
= read_u8(buf
+
189 AFSI_AUDIO_FORMAT_ID_OFFSET
);
193 /** The columns of the audio file table. */
194 enum audio_file_table_columns
{
195 /** The hash on the content of the audio file. */
197 /** The full path in the filesystem. */
199 /** The audio file selector info. */
201 /** The audio format handler info. */
203 /** The chunk table info and the chunk table of the audio file. */
205 /** The number of columns of this table. */
209 static struct osl_column_description aft_cols
[] = {
211 .storage_type
= OSL_MAPPED_STORAGE
,
212 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
214 .compare_function
= osl_hash_compare
,
215 .data_size
= HASH_SIZE
218 .storage_type
= OSL_MAPPED_STORAGE
,
219 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
221 .compare_function
= string_compare
,
224 .storage_type
= OSL_MAPPED_STORAGE
,
225 .storage_flags
= OSL_FIXED_SIZE
,
227 .data_size
= AFSI_SIZE
230 .storage_type
= OSL_MAPPED_STORAGE
,
234 .storage_type
= OSL_DISK_STORAGE
,
239 static struct osl_table_description audio_file_table_desc
= {
240 .name
= "audio_files",
241 .num_columns
= NUM_AFT_COLUMNS
,
242 .flags
= OSL_LARGE_TABLE
,
243 .column_descriptions
= aft_cols
246 static char *prefix_path(const char *prefix
, int len
, const char *path
)
276 /* Remove last component of the prefix */
281 } while (len
&& prefix
[len
-1] != '/');
285 return para_strdup(path
);
286 speclen
= strlen(path
);
287 n
= para_malloc(speclen
+ len
+ 1);
288 memcpy(n
, prefix
, len
);
289 memcpy(n
+ len
, path
, speclen
+1);
294 * We fundamentally don't like some paths: we don't want
295 * dot or dot-dot anywhere.
297 * Also, we don't want double slashes or slashes at the
298 * end that can make pathnames ambiguous.
300 static int verify_dotfile(const char *rest
)
303 * The first character was '.', but that has already been discarded, we
307 /* "." is not allowed */
312 if (rest
[1] == '\0' || rest
[1] == '/')
318 static int verify_path(const char *orig_path
, char **resolved_path
)
321 const char prefix
[] = AFS_AUDIO_FILE_DIR
"/";
322 const char *path
= orig_path
;
323 const size_t prefix_len
= strlen(prefix
);
334 case '/': /* double slash */
337 if (verify_dotfile(path
) < 0)
343 if (*orig_path
!= '/')
344 *resolved_path
= prefix_path(prefix
, prefix_len
, orig_path
);
346 *resolved_path
= para_strdup(orig_path
);
353 AFHI_SECONDS_TOTAL_OFFSET
= 0,
354 AFHI_BITRATE_OFFSET
= 4,
355 AFHI_FREQUENCY_OFFSET
= 8,
356 AFHI_CHANNELS_OFFSET
= 12,
357 AFHI_INFO_STRING_OFFSET
= 13,
361 static unsigned sizeof_afhi_buf(const struct audio_format_info
*afhi
)
365 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
368 static void save_afhi(struct audio_format_info
*afhi
, char *buf
)
372 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
373 afhi
->seconds_total
);
374 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
375 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
376 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
377 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
378 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
381 static void load_afhi(const char *buf
, struct audio_format_info
*afhi
)
383 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
384 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
385 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
386 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
387 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
390 static unsigned sizeof_chunk_info_buf(struct audio_format_info
*afhi
)
394 return 4 * afhi
->chunks_total
+ 20;
398 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
399 enum chunk_info_offsets
{
400 /** The total number of chunks (4 bytes). */
401 CHUNKS_TOTAL_OFFSET
= 0,
402 /** The length of the audio file header (4 bytes). */
403 HEADER_LEN_OFFSET
= 4,
404 /** The start of the audio file header (4 bytes). */
405 HEADER_OFFSET_OFFSET
= 8,
406 /** The seconds part of the chunk time (4 bytes). */
407 CHUNK_TV_TV_SEC_OFFSET
= 12,
408 /** The microseconds part of the chunk time (4 bytes). */
409 CHUNK_TV_TV_USEC
= 16,
410 /** Chunk table entries start here. */
411 CHUNK_TABLE_OFFSET
= 20,
414 /* TODO: audio format handlers could just produce this */
415 static void save_chunk_info(struct audio_format_info
*afhi
, char *buf
)
421 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
422 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
423 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
424 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
425 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
426 for (i
= 0; i
< afhi
->chunks_total
; i
++)
427 write_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
, afhi
->chunk_table
[i
]);
430 static int load_chunk_info(struct osl_object
*obj
, struct audio_format_info
*afhi
)
432 char *buf
= obj
->data
;
435 if (obj
->size
< CHUNK_TABLE_OFFSET
)
436 return -E_BAD_DATA_SIZE
;
438 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
439 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
440 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
441 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
442 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
444 if (afhi
->chunks_total
* 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
445 return -E_BAD_DATA_SIZE
;
446 afhi
->chunk_table
= para_malloc(afhi
->chunks_total
* sizeof(size_t));
447 for (i
= 0; i
< afhi
->chunks_total
; i
++)
448 afhi
->chunk_table
[i
] = read_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
);
453 * Get the row of the audio file table corresponding to the given path.
455 * \param path The full path of the audio file.
456 * \param row Result pointer.
458 * \return The return value of the underlying call to osl_get_row().
460 int aft_get_row_of_path(char *path
, struct osl_row
**row
)
462 struct osl_object obj
= {.data
= path
, .size
= strlen(path
) + 1};
464 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
468 * Get the row of the audio file table corresponding to the given hash value.
470 * \param hash The hash value of the desired audio file.
471 * \param row resul pointer.
473 * \return The return value of the underlying call to osl_get_row().
475 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
477 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
478 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
482 * Get the osl object holding the audio file selector info of a row.
484 * \param row Pointer to a row in the audio file table.
485 * \param obj Result pointer.
487 * \return The return value of the underlying call to osl_get_object().
489 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
491 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
495 * Get the osl object holding the audio file selector info, given a path.
498 * \param path The full path of the audio file.
499 * \param obj Result pointer.
501 * \return Positive on success, negative on errors.
503 int get_afsi_object_of_path(char *path
, struct osl_object
*obj
)
506 int ret
= aft_get_row_of_path(path
, &row
);
509 return get_afsi_object_of_row(row
, obj
);
513 * Get the audio file selector info, given a row of the audio file table.
515 * \param row Pointer to a row in the audio file table.
516 * \param afsi Result pointer.
518 * \return Positive on success, negative on errors.
520 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
522 struct osl_object obj
;
523 int ret
= get_afsi_object_of_row(row
, &obj
);
526 return load_afsi(afsi
, &obj
);
530 * Get the path of an audio file, given a row of the audio file table.
532 * \param row Pointer to a row in the audio file table.
533 * \param path Result pointer.
535 * \return Positive on success, negative on errors.
537 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
539 struct osl_object path_obj
;
540 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
544 *path
= path_obj
.data
;
549 * Get the object containing the hash value of an audio file, given a row.
551 * \param row Pointer to a row of the audio file table.
552 * \param obj Result pointer.
554 * \return The return value of the underlying call to osl_get_object().
556 * \sa get_hash_of_row().
558 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
560 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
564 * Get the hash value of an audio file, given a row of the audio file table.
566 * \param row Pointer to a row of the audio file table.
567 * \param hash Result pointer.
569 * \a hash points to mapped data and must not be freed by the caller.
571 * \return The return value of the underlying call to
572 * get_hash_object_of_aft_row().
574 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
576 struct osl_object obj
;
577 int ret
= get_hash_object_of_aft_row(row
, &obj
);
586 * Get the audio format handler info, given a row of the audio file table.
588 * \param row Pointer to a row of the audio file table.
589 * \param afhi Result pointer.
591 * \return The return value of the underlying call to osl_get_object().
593 * \sa get_chunk_table_of_row().
595 static int get_afhi_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
597 struct osl_object obj
;
598 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
602 load_afhi(obj
.data
, afhi
);
607 * Get the chunk table of an audio file, given a row of the audio file table.
609 * \param row Pointer to a row of the audio file table.
610 * \param afhi Result pointer.
612 * \return The return value of the underlying call to osl_open_disk_object().
614 * \sa get_afhi_of_row().
616 static int get_chunk_table_of_row(const struct osl_row
*row
, struct audio_format_info
*afhi
)
618 struct osl_object obj
;
619 int ret
= osl_open_disk_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
623 ret
= load_chunk_info(&obj
, afhi
);
624 osl_close_disk_object(&obj
);
629 * Mmap the given audio file and update statistics.
631 * \param aft_row Determines the audio file to be opened and updated.
632 * \param afd Result pointer.
634 * On success, the numplayed field of the audio file selector info is increased
635 * and the lastplayed time is set to the current time. Finally, the score of
636 * the audio file is updated.
638 * \return Positive on success, negative on errors.
640 int open_and_update_audio_file(struct osl_row
*aft_row
, struct audio_file_data
*afd
)
642 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
643 struct osl_object afsi_obj
;
644 struct afs_info new_afsi
;
645 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
649 ret
= get_audio_file_path_of_row(aft_row
, &afd
->path
);
652 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
655 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
658 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
661 ret
= get_chunk_table_of_row(aft_row
, &afd
->afhi
);
664 ret
= mmap_full_file(afd
->path
, O_RDONLY
, &afd
->map
);
667 hash_function(afd
->map
.data
, afd
->map
.size
, file_hash
);
668 ret
= -E_HASH_MISMATCH
;
669 if (hash_compare(file_hash
, aft_hash
))
671 new_afsi
= afd
->afsi
;
672 new_afsi
.num_played
++;
673 new_afsi
.last_played
= time(NULL
);
674 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
675 if (afd
->current_play_mode
== PLAY_MODE_PLAYLIST
)
676 ret
= playlist_update_audio_file(aft_row
);
678 ret
= mood_update_audio_file(aft_row
, &afd
->afsi
);
681 free(afd
->afhi
.chunk_table
);
685 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
690 if (!localtime_r((time_t *)seconds
, &t
))
692 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
693 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
697 if (!strftime(buf
, size
, "%b %e %Y", &t
))
702 #define GET_NUM_DIGITS(x, num) { \
703 typeof((x)) _tmp = PARA_ABS(x); \
706 while ((_tmp) > 9) { \
712 static short unsigned get_duration_width(int seconds
)
714 short unsigned width
;
715 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
717 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
718 return 4 + (mins
> 9);
719 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
720 GET_NUM_DIGITS(hours
, &width
);
724 static void get_duration_buf(int seconds
, char *buf
, short unsigned max_width
)
726 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
728 if (!hours
) /* m:ss or mm:ss */
729 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
730 else /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
731 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
735 static char *make_attribute_line(const char *att_bitmap
, struct afs_info
*afsi
)
737 char *att_text
, *att_line
;
739 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
741 return para_strdup(att_bitmap
);
742 att_line
= make_message("%s (%s)", att_bitmap
, att_text
);
747 static char *make_lyrics_line(struct afs_info
*afsi
)
750 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
752 return make_message("%u", afsi
->lyrics_id
);
753 return make_message("%u (%s)", afsi
->lyrics_id
, lyrics_name
);
756 static char *make_image_line(struct afs_info
*afsi
)
759 img_get_name_by_id(afsi
->image_id
, &image_name
);
761 return make_message("%u", afsi
->image_id
);
762 return make_message("%u (%s)", afsi
->image_id
, image_name
);
765 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
766 struct para_buffer
*b
, time_t current_time
)
770 char last_played_time
[30];
771 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
772 char score_buf
[30] = "";
773 struct afs_info
*afsi
= &d
->afsi
;
774 struct audio_format_info
*afhi
= &d
->afhi
;
775 struct ls_widths
*w
= &opts
->widths
;
776 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
778 if (opts
->mode
== LS_MODE_SHORT
) {
779 para_printf(b
, "%s\n", d
->path
);
782 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
783 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
784 sizeof(last_played_time
), current_time
);
787 get_duration_buf(afhi
->seconds_total
, duration_buf
, w
->duration_width
);
789 if (opts
->mode
== LS_MODE_LONG
)
790 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
792 sprintf(score_buf
, "%li ", d
->score
);
795 PARA_NOTICE_LOG("id: %s, %d\n", d
->path
, afsi
->audio_format_id
);
796 if (opts
->mode
== LS_MODE_LONG
) {
799 "%s " /* attributes */
800 "%*d " /* image_id */
801 "%*d " /* lyrics_id */
803 "%s " /* audio format */
804 "%*d " /* frequency */
807 "%*d " /* num_played */
808 "%s " /* last_played */
812 w
->image_id_width
, afsi
->image_id
,
813 w
->lyrics_id_width
, afsi
->lyrics_id
,
814 w
->bitrate_width
, afhi
->bitrate
,
815 audio_format_name(afsi
->audio_format_id
),
816 w
->frequency_width
, afhi
->frequency
,
819 w
->num_played_width
, afsi
->num_played
,
825 if (opts
->mode
== LS_MODE_VERBOSE
) {
826 char asc_hash
[2 * HASH_SIZE
+ 1];
827 char *att_line
, *lyrics_line
, *image_line
;
829 hash_to_asc(d
->hash
, asc_hash
);
830 att_line
= make_attribute_line(att_buf
, afsi
);
831 lyrics_line
= make_lyrics_line(afsi
);
832 image_line
= make_image_line(afsi
);
834 "%s: %s\n" /* path */
840 "bitrate: %dkbit/s\n"
848 (opts
->flags
& LS_FLAG_FULL_PATH
)?
849 "path" : "file", d
->path
,
850 have_score
? "score: " : "", score_buf
,
851 have_score
? "\n" : "",
857 audio_format_name(afsi
->audio_format_id
),
873 static int ls_audio_format_compare(const void *a
, const void *b
)
875 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
876 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
879 static int ls_duration_compare(const void *a
, const void *b
)
881 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
882 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
885 static int ls_bitrate_compare(const void *a
, const void *b
)
887 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
888 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
891 static int ls_lyrics_id_compare(const void *a
, const void *b
)
893 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
894 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
897 static int ls_image_id_compare(const void *a
, const void *b
)
899 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
900 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
903 static int ls_channels_compare(const void *a
, const void *b
)
905 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
906 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
909 static int ls_frequency_compare(const void *a
, const void *b
)
911 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
912 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
915 static int ls_num_played_compare(const void *a
, const void *b
)
917 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
918 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
921 static int ls_last_played_compare(const void *a
, const void *b
)
923 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
924 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
927 static int ls_score_compare(const void *a
, const void *b
)
929 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
930 return NUM_COMPARE(d1
->score
, d2
->score
);
933 static int ls_path_compare(const void *a
, const void *b
)
935 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
936 return strcmp(d1
->path
, d2
->path
);
939 static int sort_matching_paths(struct ls_options
*options
)
941 size_t nmemb
= options
->num_matching_paths
;
942 size_t size
= sizeof(*options
->data_ptr
);
943 int (*compar
)(const void *, const void *);
946 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
947 for (i
= 0; i
< nmemb
; i
++)
948 options
->data_ptr
[i
] = options
->data
+ i
;
950 /* In these cases the array is already sorted */
951 if (options
->sorting
== LS_SORT_BY_PATH
952 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
953 && (options
->flags
& LS_FLAG_FULL_PATH
))
955 if (options
->sorting
== LS_SORT_BY_SCORE
&&
956 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
959 switch (options
->sorting
) {
960 case LS_SORT_BY_PATH
:
961 compar
= ls_path_compare
; break;
962 case LS_SORT_BY_SCORE
:
963 compar
= ls_score_compare
; break;
964 case LS_SORT_BY_LAST_PLAYED
:
965 compar
= ls_last_played_compare
; break;
966 case LS_SORT_BY_NUM_PLAYED
:
967 compar
= ls_num_played_compare
; break;
968 case LS_SORT_BY_FREQUENCY
:
969 compar
= ls_frequency_compare
; break;
970 case LS_SORT_BY_CHANNELS
:
971 compar
= ls_channels_compare
; break;
972 case LS_SORT_BY_IMAGE_ID
:
973 compar
= ls_image_id_compare
; break;
974 case LS_SORT_BY_LYRICS_ID
:
975 compar
= ls_lyrics_id_compare
; break;
976 case LS_SORT_BY_BITRATE
:
977 compar
= ls_bitrate_compare
; break;
978 case LS_SORT_BY_DURATION
:
979 compar
= ls_duration_compare
; break;
980 case LS_SORT_BY_AUDIO_FORMAT
:
981 compar
= ls_audio_format_compare
; break;
985 qsort(options
->data_ptr
, nmemb
, size
, compar
);
989 /* row is either an aft_row or a row of the score table */
990 /* TODO: Only compute widths if we need them */
991 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
994 struct ls_options
*options
= ls_opts
;
997 unsigned short num_digits
;
999 struct osl_row
*aft_row
;
1003 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1004 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1009 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1012 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1013 char *p
= strrchr(path
, '/');
1017 if (options
->num_patterns
) {
1018 for (i
= 0; i
< options
->num_patterns
; i
++) {
1019 ret
= fnmatch(options
->patterns
[i
], path
, FNM_PATHNAME
);
1022 if (ret
== FNM_NOMATCH
)
1026 if (i
>= options
->num_patterns
) /* no match */
1029 tmp
= options
->num_matching_paths
++;
1030 if (options
->num_matching_paths
> options
->array_size
) {
1031 options
->array_size
++;
1032 options
->array_size
*= 2;
1033 options
->data
= para_realloc(options
->data
, options
->array_size
1034 * sizeof(*options
->data
));
1036 d
= options
->data
+ tmp
;
1037 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1040 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1044 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1047 w
= &options
->widths
;
1048 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1049 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1050 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1051 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1052 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1053 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1054 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1055 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1056 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1057 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1058 /* get the number of chars to print this amount of time */
1059 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1060 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1061 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1062 GET_NUM_DIGITS(score
, &num_digits
);
1063 num_digits
++; /* add one for the sign (space or "-") */
1064 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1070 static int com_ls_callback(const struct osl_object
*query
,
1071 struct osl_object
*ls_output
)
1073 struct ls_options
*opts
= query
->data
;
1074 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1075 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1077 time_t current_time
;
1080 PARA_NOTICE_LOG("%d patterns\n", opts
->num_patterns
);
1081 if (opts
->num_patterns
) {
1082 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1083 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1084 opts
->patterns
[i
] = p
;
1086 PARA_NOTICE_LOG("pattern %d: %s\n", i
, opts
->patterns
[i
]);
1089 opts
->patterns
= NULL
;
1090 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1091 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1093 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1097 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1098 if (!opts
->num_matching_paths
) {
1099 PARA_NOTICE_LOG("no match, ret: %d\n", ret
);
1102 ret
= sort_matching_paths(opts
);
1105 time(¤t_time
);
1106 if (opts
->flags
& LS_FLAG_REVERSE
)
1107 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1108 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1113 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1114 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1120 ls_output
->data
= b
.buf
;
1121 PARA_NOTICE_LOG("ls_outoute.data: %p\n", ls_output
->data
);
1122 ls_output
->size
= b
.size
;
1124 free(opts
->data_ptr
);
1125 free(opts
->patterns
);
1130 * TODO: flags -h (sort by hash) -lm (list in mbox format)
1132 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1133 * full list: list everything, including afsi, afhi, atts as clear text
1136 int com_afs_ls(int fd
, int argc
, char * const * const argv
)
1140 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1141 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1142 struct ls_options opts
= {.patterns
= NULL
};
1143 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1146 for (i
= 1; i
< argc
; i
++) {
1147 const char *arg
= argv
[i
];
1150 if (!strcmp(arg
, "--")) {
1154 if (!strncmp(arg
, "-l", 2)) {
1156 mode
= LS_MODE_LONG
;
1160 return -E_AFT_SYNTAX
;
1161 switch(*(arg
+ 2)) {
1163 mode
= LS_MODE_SHORT
;
1166 mode
= LS_MODE_LONG
;
1169 mode
= LS_MODE_VERBOSE
;
1172 mode
= LS_MODE_MBOX
;
1175 return -E_AFT_SYNTAX
;
1178 if (!strcmp(arg
, "-p")) {
1179 flags
|= LS_FLAG_FULL_PATH
;
1182 if (!strcmp(arg
, "-a")) {
1183 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1186 if (!strcmp(arg
, "-r")) {
1187 flags
|= LS_FLAG_REVERSE
;
1190 if (!strncmp(arg
, "-s", 2)) {
1191 if (!*(arg
+ 2) || *(arg
+ 3))
1192 return -E_AFT_SYNTAX
;
1193 switch(*(arg
+ 2)) {
1195 sort
= LS_SORT_BY_PATH
;
1197 case 's': /* -ss implies -a */
1198 sort
= LS_SORT_BY_SCORE
;
1199 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1202 sort
= LS_SORT_BY_LAST_PLAYED
;
1205 sort
= LS_SORT_BY_NUM_PLAYED
;
1208 sort
= LS_SORT_BY_FREQUENCY
;
1211 sort
= LS_SORT_BY_CHANNELS
;
1214 sort
= LS_SORT_BY_IMAGE_ID
;
1217 sort
= LS_SORT_BY_LYRICS_ID
;
1220 sort
= LS_SORT_BY_BITRATE
;
1223 sort
= LS_SORT_BY_DURATION
;
1226 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1229 return -E_AFT_SYNTAX
;
1232 return -E_AFT_SYNTAX
;
1235 opts
.sorting
= sort
;
1237 opts
.num_patterns
= argc
- i
;
1238 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1239 argv
+ i
, com_ls_callback
, &ls_output
);
1241 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1242 free(ls_output
.data
);
1248 * Call the given function for each file in the audio file table.
1250 * \param private_data An arbitrary data pointer, passed to \a func.
1251 * \param func The custom function to be called.
1253 * \return The return value of the underlying call to osl_rbtree_loop().
1255 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1257 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1261 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1263 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1264 struct osl_row
*row
;
1266 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1270 enum aft_row_offsets
{
1271 AFTROW_AFHI_OFFSET_POS
= 0,
1272 AFTROW_CHUNKS_OFFSET_POS
= 2,
1273 AFTROW_AUDIO_FORMAT_OFFSET
= 4,
1274 AFTROW_FLAGS_OFFSET
= 5,
1275 AFTROW_HASH_OFFSET
= 9,
1276 AFTROW_PATH_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1279 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1280 * In this case, afhi won't be stored in the buffer */
1281 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1282 struct audio_format_info
*afhi
, uint32_t flags
,
1283 uint8_t audio_format_num
, struct osl_object
*obj
)
1285 size_t path_len
= strlen(path
) + 1;
1286 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1287 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1288 + sizeof_chunk_info_buf(afhi
);
1289 char *buf
= para_malloc(size
);
1292 write_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1293 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1295 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1296 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1298 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1299 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1300 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1301 pos
+ afhi_size
- 1);
1302 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1303 save_afhi(afhi
, buf
+ pos
);
1306 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1307 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1308 save_chunk_info(afhi
, buf
+ pos
);
1309 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1323 AFHI: whether afhi and chunk table are computed and sent
1324 ACTION: table modifications to be performed
1326 +---+----+-----+------+---------------------------------------------------+
1327 | HS | PB | F | AFHI | ACTION
1328 +---+----+-----+------+---------------------------------------------------+
1329 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1330 | | update path, keep afsi
1331 +---+----+-----+------+---------------------------------------------------+
1332 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1333 | | otherwise: remove PB, HS: update path, keep afhi,
1335 +---+----+-----+------+---------------------------------------------------+
1336 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1338 +---+----+-----+------+---------------------------------------------------+
1339 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1340 +---+----+-----+------+---------------------------------------------------+
1341 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1342 | | (force has no effect)
1343 +---+----+-----+------+---------------------------------------------------+
1344 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1345 +---+----+-----+------+---------------------------------------------------+
1346 | N | N | Y | Y | (new file) create new entry (force has no effect)
1347 +---+----+-----+------+---------------------------------------------------+
1348 | N | N | N | Y | (new file) create new entry
1349 +---+----+-----+------+---------------------------------------------------+
1351 afhi <=> force or no HS
1356 #define ADD_FLAG_LAZY 1
1357 #define ADD_FLAG_FORCE 2
1358 #define ADD_FLAG_VERBOSE 4
1360 /* TODO: change log messages so that they get written to the result buffer */
1362 static int com_add_callback(const struct osl_object
*query
,
1363 __a_unused
struct osl_object
*result
)
1365 char *buf
= query
->data
, *path
;
1366 struct osl_row
*pb
, *aft_row
;
1367 const struct osl_row
*hs
;
1368 struct osl_object objs
[NUM_AFT_COLUMNS
];
1370 char asc
[2 * HASH_SIZE
+ 1];
1372 char afsi_buf
[AFSI_SIZE
];
1373 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1374 struct afs_info default_afsi
= {.last_played
= 0};
1376 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1377 hash_to_asc(hash
, asc
);;
1378 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1379 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1381 path
= buf
+ AFTROW_PATH_OFFSET
;
1382 objs
[AFTCOL_PATH
].data
= path
;
1383 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1385 PARA_INFO_LOG("request to add %s\n", path
);
1386 hs
= find_hash_sister(hash
);
1387 ret
= aft_get_row_of_path(path
, &pb
);
1388 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1390 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1391 if (flags
& ADD_FLAG_VERBOSE
)
1392 PARA_NOTICE_LOG("ignoring duplicate %p\n", path
);
1395 if (hs
&& hs
!= pb
) {
1396 struct osl_object obj
;
1397 if (pb
) { /* hs trumps pb, remove pb */
1398 if (flags
& ADD_FLAG_VERBOSE
)
1399 PARA_NOTICE_LOG("removing path brother\n");
1400 ret
= osl_del_row(audio_file_table
, pb
);
1405 /* file rename, update hs' path */
1406 ret
= osl_get_object(audio_file_table
, hs
, AFTCOL_PATH
, &obj
);
1407 if (flags
& ADD_FLAG_VERBOSE
)
1408 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj
.data
, path
);
1409 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1410 &objs
[AFTCOL_PATH
]);
1413 if (!(flags
& ADD_FLAG_FORCE
))
1416 /* no hs or force mode, child must have sent afhi */
1417 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1418 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1420 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1421 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1422 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1424 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1425 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1426 if (pb
&& !hs
) { /* update pb's hash */
1427 char old_asc
[2 * HASH_SIZE
+ 1];
1428 HASH_TYPE
*old_hash
;
1429 ret
= get_hash_of_row(pb
, &old_hash
);
1432 hash_to_asc(old_hash
, old_asc
);
1433 if (flags
& ADD_FLAG_VERBOSE
)
1434 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path
,
1436 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1437 &objs
[AFTCOL_HASH
]);
1441 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1442 const struct osl_row
*row
= pb
? pb
: hs
;
1443 /* update afhi and chunk_table */
1444 if (flags
& ADD_FLAG_VERBOSE
)
1445 PARA_DEBUG_LOG("updating audio format handler info (%zd bytes)\n",
1446 objs
[AFTCOL_AFHI
].size
);
1447 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1448 &objs
[AFTCOL_AFHI
]);
1451 if (flags
& ADD_FLAG_VERBOSE
)
1452 PARA_DEBUG_LOG("updating chunk table\n");
1453 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1454 &objs
[AFTCOL_CHUNKS
]);
1457 return mood_update_audio_file(row
, NULL
);
1459 /* new entry, use default afsi */
1460 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1461 default_afsi
.audio_format_id
= read_u8(buf
+ AFTROW_AUDIO_FORMAT_OFFSET
);
1463 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1464 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1465 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1466 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1469 return mood_update_audio_file(aft_row
, NULL
);
1472 struct private_add_data
{
1477 static int path_brother_callback(const struct osl_object
*query
,
1478 struct osl_object
*result
)
1480 char *path
= query
->data
;
1481 struct osl_row
*path_brother
;
1482 int ret
= aft_get_row_of_path(path
, &path_brother
);
1485 result
->data
= para_malloc(sizeof(path_brother
));
1486 result
->size
= sizeof(path_brother
);
1487 *(struct osl_row
**)(result
->data
) = path_brother
;
1491 static int hash_sister_callback(const struct osl_object
*query
,
1492 struct osl_object
*result
)
1494 HASH_TYPE
*hash
= query
->data
;
1495 struct osl_row
*hash_sister
;
1497 hash_sister
= find_hash_sister(hash
);
1499 return -E_RB_KEY_NOT_FOUND
;
1500 result
->data
= para_malloc(sizeof(hash_sister
));
1501 result
->size
= sizeof(hash_sister
);
1502 *(struct osl_row
**)(result
->data
) = hash_sister
;
1506 static int add_one_audio_file(const char *arg
, const void *private_data
)
1509 uint8_t format_num
= -1;
1510 const struct private_add_data
*pad
= private_data
;
1511 struct audio_format_info afhi
, *afhi_ptr
= NULL
;
1512 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1513 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
;
1515 HASH_TYPE hash
[HASH_SIZE
];
1517 afhi
.header_offset
= 0;
1518 afhi
.header_len
= 0;
1519 ret
= verify_path(arg
, &path
);
1523 query
.size
= strlen(path
) + 1;
1524 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1525 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1528 pb
= *(struct osl_row
**)result
.data
;
1532 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1533 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1534 ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1537 /* We still want to add this file. Compute its hash. */
1538 ret
= mmap_full_file(path
, O_RDONLY
, &map
);
1541 hash_function(map
.data
, map
.size
, hash
);
1543 /* Check whether database contains file with the same hash. */
1545 query
.size
= HASH_SIZE
;
1546 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1547 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1550 hs
= *(struct osl_row
**)result
.data
;
1553 /* Return success if we already know this file. */
1555 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1556 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1557 ret
= send_va_buffer(pad
->fd
,
1558 "not forcing update: %s\n", path
);
1562 * we won't recalculate the audio format info and the chunk table if
1563 * there is a hash sister unless in FORCE mode.
1565 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1566 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1572 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1573 send_va_buffer(pad
->fd
, "adding %s\n", path
);
1574 munmap(map
.data
, map
.size
);
1575 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1576 /* Ask afs to consider this entry for adding. */
1577 ret
= send_callback_request(com_add_callback
, &obj
, NULL
);
1581 munmap(map
.data
, map
.size
);
1584 send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
?
1585 path
: arg
, PARA_STRERROR(-ret
));
1589 free(afhi_ptr
->chunk_table
);
1590 return 1; /* it's not an error if not all files could be added */
1593 int com_add(int fd
, int argc
, char * const * const argv
)
1596 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1597 struct stat statbuf
;
1599 for (i
= 1; i
< argc
; i
++) {
1600 const char *arg
= argv
[i
];
1603 if (!strcmp(arg
, "--")) {
1607 if (!strcmp(arg
, "-l")) {
1608 pad
.flags
|= ADD_FLAG_LAZY
;
1611 if (!strcmp(arg
, "-f")) {
1612 pad
.flags
|= ADD_FLAG_FORCE
;
1615 if (!strcmp(arg
, "-v")) {
1616 pad
.flags
|= ADD_FLAG_VERBOSE
;
1621 return -E_AFT_SYNTAX
;
1622 for (; i
< argc
; i
++) {
1623 char *path
= para_strdup(argv
[i
]);
1624 size_t len
= strlen(path
);
1625 while (len
> 1 && path
[--len
] == '/')
1627 ret
= stat(path
, &statbuf
);
1629 PARA_NOTICE_LOG("failed to stat %s (%s)", path
,
1632 if (S_ISDIR(statbuf
.st_mode
))
1633 for_each_file_in_dir(path
, add_one_audio_file
,
1636 add_one_audio_file(path
, &pad
);
1644 struct com_touch_options
{
1651 static int com_touch_callback(const struct osl_object
*query
,
1652 __a_unused
struct osl_object
*result
)
1654 struct com_touch_options
*cto
= query
->data
;
1655 char *p
= (char *)query
->data
+ sizeof(*cto
);
1657 int ret
, no_options
= cto
->num_played
< 0 && cto
->last_played
< 0 &&
1658 cto
->lyrics_id
< 0 && cto
->image_id
< 0;
1660 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1661 struct afs_info old_afsi
, new_afsi
;
1662 struct osl_object obj
;
1663 struct osl_row
*row
;
1666 ret
= aft_get_row_of_path(p
, &row
);
1669 ret
= get_afsi_object_of_row(row
, &obj
);
1672 ret
= load_afsi(&old_afsi
, &obj
);
1675 new_afsi
= old_afsi
;
1677 new_afsi
.num_played
++;
1678 new_afsi
.last_played
= time(NULL
);
1680 if (cto
->lyrics_id
>= 0)
1681 new_afsi
.lyrics_id
= cto
->lyrics_id
;
1682 if (cto
->image_id
>= 0)
1683 new_afsi
.image_id
= cto
->image_id
;
1684 if (cto
->num_played
>= 0)
1685 new_afsi
.num_played
= cto
->num_played
;
1686 if (cto
->last_played
>= 0)
1687 new_afsi
.last_played
= cto
->last_played
;
1689 save_afsi(&new_afsi
, &obj
); /* in-place update */
1690 ret
= mood_update_audio_file(row
, &old_afsi
);
1697 int com_touch(__a_unused
int fd
, int argc
, char * const * const argv
)
1699 struct com_touch_options cto
= {
1705 struct osl_object options
= {.data
= &cto
, .size
= sizeof(cto
)};
1709 for (i
= 1; i
< argc
; i
++) {
1710 const char *arg
= argv
[i
];
1713 if (!strcmp(arg
, "--")) {
1717 if (!strncmp(arg
, "-n", 2)) {
1718 ret
= para_atol(arg
+ 2, &cto
.num_played
);
1723 if (!strncmp(arg
, "-l", 2)) {
1724 ret
= para_atol(arg
+ 2, &cto
.last_played
);
1729 if (!strncmp(arg
, "-y", 2)) {
1730 ret
= para_atol(arg
+ 2, &cto
.lyrics_id
);
1735 if (!strncmp(arg
, "-i", 2)) {
1736 ret
= para_atol(arg
+ 2, &cto
.image_id
);
1742 ret
= -E_AFT_SYNTAX
;
1745 return send_option_arg_callback_request(&options
, argc
- i
,
1746 argv
+ i
, com_touch_callback
, NULL
);
1751 struct com_rm_options
{
1755 static int com_rm_callback(const struct osl_object
*query
,
1756 __a_unused
struct osl_object
*result
)
1758 struct com_rm_options
*cro
= query
->data
;
1759 char *p
= (char *)query
->data
+ sizeof(*cro
);
1763 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1764 struct osl_row
*row
;
1767 ret
= aft_get_row_of_path(p
, &row
);
1770 ret
= mood_delete_audio_file(row
);
1773 ret
= osl_del_row(audio_file_table
, row
);
1781 * TODO options: -v verbose, -f dont stop if file not found
1782 * -h remove by hash, use fnmatch
1786 int com_afs_rm(__a_unused
int fd
, int argc
, char * const * const argv
)
1788 struct com_rm_options cro
= {.flags
= 0};
1789 struct osl_object options
= {.data
= &cro
, .size
= sizeof(cro
)};
1792 for (i
= 1; i
< argc
; i
++) {
1793 const char *arg
= argv
[i
];
1796 if (!strcmp(arg
, "--")) {
1801 ret
= -E_AFT_SYNTAX
;
1804 return send_option_arg_callback_request(&options
, argc
- i
,
1805 argv
+ i
, com_rm_callback
, NULL
);
1810 /* TODO: optionally fix problems by removing offending rows */
1811 static int check_audio_file(struct osl_row
*row
, void *data
)
1814 struct para_buffer
*pb
= data
;
1815 struct stat statbuf
;
1816 int ret
= get_audio_file_path_of_row(row
, &path
);
1817 struct afs_info afsi
;
1821 para_printf(pb
, "%s\n", PARA_STRERROR(-ret
));
1824 if (stat(path
, &statbuf
) < 0)
1825 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
1827 if (!S_ISREG(statbuf
.st_mode
))
1828 para_printf(pb
, "%s: not a regular file\n", path
);
1830 ret
= get_afsi_of_row(row
, &afsi
);
1832 para_printf(pb
, "%s: %s\n", path
, PARA_STRERROR(-ret
));
1835 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
1837 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
1838 PARA_STRERROR(-ret
));
1839 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
1841 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
1842 PARA_STRERROR(-ret
));
1846 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
1848 struct para_buffer pb
= {.buf
= NULL
};
1850 para_printf(&pb
, "checking audio file table...\n");
1851 audio_file_loop(&pb
, check_audio_file
);
1852 result
->data
= pb
.buf
;
1853 result
->size
= pb
.size
;
1859 * Close the audio file table.
1861 * \param flags Ususal flags that are passed to osl_close_table().
1863 * \sa osl_close_table().
1865 void aft_shutdown(enum osl_close_flags flags
)
1867 osl_close_table(audio_file_table
, flags
);
1868 audio_file_table
= NULL
;
1872 * Open the audio file table.
1874 * \param ti Gets initialized by this function.
1875 * \param db The database directory.
1877 * \return Positive on success, negative on errors.
1879 * \sa osl_open_table().
1881 int aft_init(struct table_info
*ti
, const char *db
)
1885 audio_file_table_desc
.dir
= db
;
1886 ti
->desc
= &audio_file_table_desc
;
1887 ret
= osl_open_table(ti
->desc
, &audio_file_table
);
1890 osl_get_num_rows(audio_file_table
, &num
);
1891 PARA_INFO_LOG("audio file table contains %d files\n", num
);
1894 PARA_INFO_LOG("failed to open audio file table\n");
1895 audio_file_table
= NULL
;
1896 return ret
== -E_NOENT
? 1 : ret
;