9 int mp3_get_file_info(char *map
, size_t numbytes
,
10 struct audio_format_info
*afi
); /* FXIME */
12 #define AFS_AUDIO_FILE_DIR "/home/mp3"
14 static void *audio_file_table
;
17 * Describes the structure of the mmapped-afs info struct.
19 * \sa struct afs_info.
22 /** Where .last_played is stored. */
23 AFSI_LAST_PLAYED_OFFSET
= 0,
24 /** Storage position of the attributes bitmap. */
25 AFSI_ATTRIBUTES_OFFSET
= 8,
26 /** Storage position of the .num_played field. */
27 AFSI_NUM_PLAYED_OFFSET
= 16,
28 /** Storage position of the .image_id field. */
29 AFSI_IMAGE_ID_OFFSET
= 20,
30 /** Storage position of the .lyrics_id field. */
31 AFSI_LYRICS_ID_OFFSET
= 24,
32 /** Storage position of the .audio_format_id field. */
33 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
34 /** On-disk storage space needed. */
39 * Convert a struct afs_info to an osl object.
41 * \param afsi Pointer to the audio file info to be converted.
42 * \param obj Result pointer.
46 void save_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
48 struct afs_info default_afs_info
= {
49 .last_played
= time(NULL
) - 365 * 24 * 60 * 60,
54 .audio_format_id
= 5, /* FIXME */
56 char *buf
= obj
->data
;
59 afsi
= &default_afs_info
;
61 write_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
, afsi
->last_played
);
62 write_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
, afsi
->attributes
);
63 write_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
, afsi
->num_played
);
64 write_u32(buf
+ AFSI_IMAGE_ID_OFFSET
, afsi
->image_id
);
65 write_u32(buf
+ AFSI_LYRICS_ID_OFFSET
, afsi
->lyrics_id
);
66 write_u8(buf
+ AFSI_AUDIO_FORMAT_ID_OFFSET
,
67 afsi
->audio_format_id
);
71 * Get the audio file selector info struct stored in an osl object.
73 * \param afsi Points to the audio_file info structure to be filled in.
74 * \param obj The osl object holding the data.
76 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
80 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
82 char *buf
= obj
->data
;
83 if (obj
->size
< AFSI_SIZE
)
85 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
86 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
87 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
88 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
89 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
90 afsi
->audio_format_id
= read_u8(buf
+
91 AFSI_AUDIO_FORMAT_ID_OFFSET
);
95 /** The columns of the audio file table. */
96 enum audio_file_table_columns
{
97 /** The hash on the content of the audio file. */
99 /** The full path in the filesystem. */
101 /** The audio file selector info. */
103 /** The audio format handler info. */
105 /** The chunk table info and the chunk table of the audio file. */
107 /** The number of columns of this table. */
111 static struct osl_column_description aft_cols
[] = {
113 .storage_type
= OSL_MAPPED_STORAGE
,
114 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
116 .compare_function
= osl_hash_compare
,
117 .data_size
= HASH_SIZE
120 .storage_type
= OSL_MAPPED_STORAGE
,
121 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
123 .compare_function
= string_compare
,
126 .storage_type
= OSL_MAPPED_STORAGE
,
127 .storage_flags
= OSL_FIXED_SIZE
,
129 .data_size
= AFSI_SIZE
132 .storage_type
= OSL_MAPPED_STORAGE
,
136 .storage_type
= OSL_DISK_STORAGE
,
141 static const struct osl_table_description audio_file_table_desc
= {
143 .name
= "audio_files",
144 .num_columns
= NUM_AFT_COLUMNS
,
145 .flags
= OSL_LARGE_TABLE
,
146 .column_descriptions
= aft_cols
149 static char *prefix_path(const char *prefix
, int len
, const char *path
)
179 /* Remove last component of the prefix */
184 } while (len
&& prefix
[len
-1] != '/');
188 return para_strdup(path
);
189 speclen
= strlen(path
);
190 n
= para_malloc(speclen
+ len
+ 1);
191 memcpy(n
, prefix
, len
);
192 memcpy(n
+ len
, path
, speclen
+1);
197 * We fundamentally don't like some paths: we don't want
198 * dot or dot-dot anywhere.
200 * Also, we don't want double slashes or slashes at the
201 * end that can make pathnames ambiguous.
203 static int verify_dotfile(const char *rest
)
206 * The first character was '.', but that has already been discarded, we
210 /* "." is not allowed */
215 if (rest
[1] == '\0' || rest
[1] == '/')
221 static int verify_path(const char *path
, char **resolved_path
)
223 const char *orig_path
= path
;
225 const char prefix
[] = AFS_AUDIO_FILE_DIR
"/";
226 const size_t prefix_len
= strlen(prefix
);
228 PARA_DEBUG_LOG("path: %s\n", path
);
241 if (verify_dotfile(path
) > 0)
244 *resolved_path
= NULL
;
249 if (*orig_path
== '/')
250 *resolved_path
= prefix_path("", 0, orig_path
);
252 *resolved_path
= prefix_path(prefix
, prefix_len
, orig_path
);
253 PARA_DEBUG_LOG("resolved: %s\n", *resolved_path
);
254 return *resolved_path
? 1: -E_BAD_PATH
;
258 AFHI_SECONDS_TOTAL_OFFSET
= 0,
259 AFHI_BITRATE_OFFSET
= 4,
260 AFHI_FREQUENCY_OFFSET
= 8,
261 AFHI_CHANNELS_OFFSET
= 12,
262 AFHI_INFO_STRING_OFFSET
= 13,
266 static unsigned sizeof_afhi_buf(const struct audio_format_info
*afhi
)
270 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
273 static void save_afhi(struct audio_format_info
*afhi
, char *buf
)
277 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
,
278 afhi
->seconds_total
);
279 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
280 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
281 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
282 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
283 PARA_DEBUG_LOG("last byte written: %p\n", buf
+ AFHI_INFO_STRING_OFFSET
+ strlen(afhi
->info_string
));
286 static void load_afhi(const char *buf
, struct audio_format_info
*afhi
)
288 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
289 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
290 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
291 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
292 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
295 static unsigned sizeof_chunk_info_buf(struct audio_format_info
*afhi
)
299 return 4 * afhi
->chunks_total
+ 20;
303 /** The offsets of the data contained in the AFTCOL_CHUNKS column. */
304 enum chunk_info_offsets
{
305 /** The total number of chunks (4 bytes). */
306 CHUNKS_TOTAL_OFFSET
= 0,
307 /** The length of the audio file header (4 bytes). */
308 HEADER_LEN_OFFSET
= 4,
309 /** The start of the audio file header (4 bytes). */
310 HEADER_OFFSET_OFFSET
= 8,
311 /** The seconds part of the chunk time (4 bytes). */
312 CHUNK_TV_TV_SEC_OFFSET
= 12,
313 /** The microseconds part of the chunk time (4 bytes). */
314 CHUNK_TV_TV_USEC
= 16,
315 /** Chunk table entries start here. */
316 CHUNK_TABLE_OFFSET
= 20,
319 /* TODO: audio format handlers could just produce this */
320 static void save_chunk_info(struct audio_format_info
*afhi
, char *buf
)
326 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
327 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
328 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
329 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
330 write_u32(buf
+ CHUNK_TV_TV_USEC
, afhi
->chunk_tv
.tv_usec
);
331 for (i
= 0; i
< afhi
->chunks_total
; i
++)
332 write_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
, afhi
->chunk_table
[i
]);
335 static int load_chunk_info(struct osl_object
*obj
, struct audio_format_info
*afhi
)
337 char *buf
= obj
->data
;
340 if (obj
->size
< CHUNK_TABLE_OFFSET
)
341 return -E_BAD_DATA_SIZE
;
343 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
344 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
345 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
346 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
347 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC
);
349 if (afhi
->chunks_total
* 4 + CHUNK_TABLE_OFFSET
> obj
->size
)
350 return -E_BAD_DATA_SIZE
;
351 afhi
->chunk_table
= para_malloc(afhi
->chunks_total
* sizeof(size_t));
352 for (i
= 0; i
< afhi
->chunks_total
; i
++)
353 afhi
->chunk_table
[i
] = read_u32(buf
+ CHUNK_TABLE_OFFSET
+ 4 * i
);
358 * Get the row of the audio file table corresponding to the given path.
360 * \param path The full path of the audio file.
361 * \param row Result pointer.
363 * \return The return value of the underlying call to osl_get_row().
365 int aft_get_row_of_path(char *path
, struct osl_row
**row
)
367 struct osl_object obj
= {.data
= path
, .size
= strlen(path
) + 1};
368 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
372 * Get the row of the audio file table corresponding to the given hash value.
374 * \param hash The hash value of the desired audio file.
375 * \param row resul pointer.
377 * \return The return value of the underlying call to osl_get_row().
379 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
381 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
382 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
386 * Get the osl object holding the audio file selector info of a row.
388 * \param row Pointer to a row in the audio file table.
389 * \param obj Result pointer.
391 * \return The return value of the underlying call to osl_get_object().
393 int get_afsi_object_of_row(const void *row
, struct osl_object
*obj
)
395 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
399 * Get the osl object holding the audio file selector info, given a path.
402 * \param path The full path of the audio file.
403 * \param obj Result pointer.
405 * \return Positive on success, negative on errors.
407 int get_afsi_object_of_path(char *path
, struct osl_object
*obj
)
410 int ret
= aft_get_row_of_path(path
, &row
);
413 return get_afsi_object_of_row(row
, obj
);
417 * Get the audio file selector info, given a row of the audio file table.
419 * \param row Pointer to a row in the audio file table.
420 * \param afsi Result pointer.
422 * \return Positive on success, negative on errors.
424 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
426 struct osl_object obj
;
427 int ret
= get_afsi_object_of_row(row
, &obj
);
430 return load_afsi(afsi
, &obj
);
434 * Get the path of an audio file, given a row of the audio file table.
436 * \param row Pointer to a row in the audio file table.
437 * \param path Result pointer.
439 * \return Positive on success, negative on errors.
441 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
443 struct osl_object path_obj
;
444 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
448 *path
= path_obj
.data
;
453 * Get the object containing the hash value of an audio file, given a row.
455 * \param row Pointer to a row of the audio file table.
456 * \param obj Result pointer.
458 * \return The return value of the underlying call to osl_get_object().
460 * \sa get_hash_of_row().
462 int get_hash_object_of_aft_row(const void *row
, struct osl_object
*obj
)
464 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
468 * Get the hash value of an audio file, given a row of the audio file table.
470 * \param row Pointer to a row of the audio file table.
471 * \param hash Result pointer.
473 * \a hash points to mapped data and must not be freed by the caller.
475 * \return The return value of the underlying call to
476 * get_hash_object_of_aft_row().
478 static int get_hash_of_row(const void *row
, HASH_TYPE
**hash
)
480 struct osl_object obj
;
481 int ret
= get_hash_object_of_aft_row(row
, &obj
);
490 * Get the audio format handler info, given a row of the audio file table.
492 * \param row Pointer to a row of the audio file table.
493 * \param afhi Result pointer.
495 * \return The return value of the underlying call to osl_get_object().
497 * \sa get_chunk_table_of_row().
499 int get_afhi_of_row(const void *row
, struct audio_format_info
*afhi
)
501 struct osl_object obj
;
502 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
506 load_afhi(obj
.data
, afhi
);
511 * Get the chunk table of an audio file, given a row of the audio file table.
513 * \param row Pointer to a row of the audio file table.
514 * \param afhi Result pointer.
516 * \return The return value of the underlying call to osl_open_disk_object().
518 * \sa get_afhi_of_row().
520 int get_chunk_table_of_row(const void *row
, struct audio_format_info
*afhi
)
522 struct osl_object obj
;
523 int ret
= osl_open_disk_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
527 ret
= load_chunk_info(&obj
, afhi
);
528 osl_close_disk_object(&obj
);
533 * Mmap the given audio file and update statistics.
535 * \param aft_row Determines the audio file to be opened and updated.
536 * \param afd Result pointer.
538 * On success, the numplayed field of the audio file selector info is increased
539 * and the lastplayed time is set to the current time. Finally, the score of
540 * the audio file is updated.
542 * \return Positive on success, negative on errors.
544 int open_and_update_audio_file(struct osl_row
*aft_row
, struct audio_file_data
*afd
)
546 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
547 struct osl_object afsi_obj
;
548 struct afs_info new_afsi
;
549 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
553 ret
= get_audio_file_path_of_row(aft_row
, &afd
->path
);
556 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
559 ret
= load_afsi(&afd
->afsi
, &afsi_obj
);
562 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
565 ret
= get_chunk_table_of_row(aft_row
, &afd
->afhi
);
568 ret
= mmap_full_file(afd
->path
, O_RDONLY
, &afd
->map
);
571 hash_function(afd
->map
.data
, afd
->map
.size
, file_hash
);
572 ret
= -E_HASH_MISMATCH
;
573 if (hash_compare(file_hash
, aft_hash
))
575 new_afsi
= afd
->afsi
;
576 new_afsi
.num_played
++;
577 new_afsi
.last_played
= time(NULL
);
578 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
579 if (afd
->current_play_mode
== PLAY_MODE_PLAYLIST
)
580 ret
= playlist_update_audio_file(aft_row
);
582 ret
= mood_update_audio_file(aft_row
, &afd
->afsi
);
585 free(afd
->afhi
.chunk_table
);
591 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
)
595 if (!localtime_r((time_t *)seconds
, &t
))
597 if (*seconds
+ 6 * 30 * 24 * 3600 > now
) {
598 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
602 if (!strftime(buf
, size
, "%b %e %Y", &t
))
607 #define GET_NUM_DIGITS(x, num) { \
608 typeof((x)) _tmp = PARA_ABS(x); \
611 while ((_tmp) > 9) { \
617 static short unsigned get_duration(int seconds_total
, char *buf
, short unsigned max_width
)
619 short unsigned width
;
620 int s
= seconds_total
;
621 unsigned hours
= s
/ 3600, mins
= (s
% 3600) / 60, secs
= s
% 60;
623 if (s
< 3600) { /* less than one hour => m:ss or mm:ss */
624 GET_NUM_DIGITS(mins
, &width
); /* 1 or 2 */
625 width
+= 3; /* 4 or 5 */
627 sprintf(buf
, "%*u:%02u", max_width
- width
+ 1, mins
, secs
);
630 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
631 GET_NUM_DIGITS(hours
, &width
);
634 sprintf(buf
, "%*u:%02u:%02u", max_width
- width
+ 1, hours
, mins
, secs
);
638 static char *make_attribute_line(const char *att_bitmap
, struct afs_info
*afsi
)
640 char *att_text
, *att_line
;
642 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
644 return para_strdup(att_bitmap
);
645 att_line
= make_message("%s (%s)", att_bitmap
, att_text
);
650 static char *make_lyrics_line(struct afs_info
*afsi
)
653 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
655 return make_message("%u", afsi
->lyrics_id
);
656 return make_message("%u (%s)", afsi
->lyrics_id
, lyrics_name
);
659 static char *make_image_line(struct afs_info
*afsi
)
662 img_get_name_by_id(afsi
->image_id
, &image_name
);
664 return make_message("%u", afsi
->image_id
);
665 return make_message("%u (%s)", afsi
->image_id
, image_name
);
668 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
669 struct para_buffer
*b
)
673 char last_played_time
[30];
674 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
675 char score_buf
[30] = "";
676 struct afs_info
*afsi
= &d
->afsi
;
677 struct audio_format_info
*afhi
= &d
->afhi
;
678 struct ls_widths
*w
= &opts
->widths
;
679 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
681 if (opts
->mode
== LS_MODE_SHORT
) {
682 para_printf(b
, "%s\n", d
->path
);
685 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
686 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
687 sizeof(last_played_time
));
690 get_duration(afhi
->seconds_total
, duration_buf
, w
->duration_width
);
692 if (opts
->mode
== LS_MODE_LONG
)
693 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
695 sprintf(score_buf
, "%li ", d
->score
);
698 if (opts
->mode
== LS_MODE_LONG
) {
701 "%s " /* attributes */
702 "%*d " /* image_id */
703 "%*d " /* lyrics_id */
705 "%s " /* audio format */
706 "%*d " /* frequency */
709 "%*d " /* num_played */
710 "%s " /* last_played */
714 w
->image_id_width
, afsi
->image_id
,
715 w
->lyrics_id_width
, afsi
->lyrics_id
,
716 w
->bitrate_width
, afhi
->bitrate
,
718 w
->frequency_width
, afhi
->frequency
,
721 w
->num_played_width
, afsi
->num_played
,
727 if (opts
->mode
== LS_MODE_VERBOSE
) {
728 char asc_hash
[2 * HASH_SIZE
+ 1];
729 char *att_line
, *lyrics_line
, *image_line
;
731 hash_to_asc(d
->hash
, asc_hash
);
732 att_line
= make_attribute_line(att_buf
, afsi
);
733 lyrics_line
= make_lyrics_line(afsi
);
734 image_line
= make_image_line(afsi
);
736 "%s: %s\n" /* path */
742 "bitrate: %dkbit/s\n"
748 "last_played: %s\n\n",
749 (opts
->flags
& LS_FLAG_FULL_PATH
)?
750 "path" : "file", d
->path
,
751 have_score
? "score: " : "", score_buf
,
752 have_score
? "\n" : "",
773 static int ls_audio_format_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
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
779 static int ls_duration_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
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
785 static int ls_bitrate_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
->afhi
.bitrate
, d2
->afhi
.bitrate
);
791 static int ls_lyrics_id_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
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
797 static int ls_image_id_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
->afsi
.image_id
, d2
->afsi
.image_id
);
803 static int ls_channels_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
->afhi
.channels
, d2
->afhi
.channels
);
809 static int ls_frequency_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
->afhi
.frequency
, d2
->afhi
.frequency
);
815 static int ls_num_played_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
->afsi
.num_played
, d2
->afsi
.num_played
);
821 static int ls_last_played_compare(const void *a
, const void *b
)
823 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
824 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
827 static int ls_score_compare(const void *a
, const void *b
)
829 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
830 return NUM_COMPARE(d1
->score
, d2
->score
);
833 static int ls_path_compare(const void *a
, const void *b
)
835 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
836 return strcmp(d1
->path
, d2
->path
);
839 static int sort_matching_paths(struct ls_options
*options
)
841 size_t nmemb
= options
->num_matching_paths
;
842 size_t size
= sizeof(uint32_t);
843 int (*compar
)(const void *, const void *);
846 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
847 for (i
= 0; i
< nmemb
; i
++)
848 options
->data_ptr
[i
] = options
->data
+ i
;
850 /* In these cases the array is already sorted */
851 if (options
->sorting
== LS_SORT_BY_PATH
852 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
853 && (options
->flags
& LS_FLAG_FULL_PATH
))
855 if (options
->sorting
== LS_SORT_BY_SCORE
&&
856 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
859 switch (options
->sorting
) {
860 case LS_SORT_BY_PATH
:
861 compar
= ls_path_compare
; break;
862 case LS_SORT_BY_SCORE
:
863 compar
= ls_score_compare
; break;
864 case LS_SORT_BY_LAST_PLAYED
:
865 compar
= ls_last_played_compare
; break;
866 case LS_SORT_BY_NUM_PLAYED
:
867 compar
= ls_num_played_compare
; break;
868 case LS_SORT_BY_FREQUENCY
:
869 compar
= ls_frequency_compare
; break;
870 case LS_SORT_BY_CHANNELS
:
871 compar
= ls_channels_compare
; break;
872 case LS_SORT_BY_IMAGE_ID
:
873 compar
= ls_image_id_compare
; break;
874 case LS_SORT_BY_LYRICS_ID
:
875 compar
= ls_lyrics_id_compare
; break;
876 case LS_SORT_BY_BITRATE
:
877 compar
= ls_bitrate_compare
; break;
878 case LS_SORT_BY_DURATION
:
879 compar
= ls_duration_compare
; break;
880 case LS_SORT_BY_AUDIO_FORMAT
:
881 compar
= ls_audio_format_compare
; break;
885 qsort(options
->data_ptr
, nmemb
, size
, compar
);
889 /* row is either an aft_row or a row of the score table */
890 /* TODO: Only compute widths if we need them */
891 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
894 struct ls_options
*options
= ls_opts
;
897 unsigned short num_digits
;
899 struct osl_row
*aft_row
;
903 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
904 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
909 ret
= get_audio_file_path_of_row(aft_row
, &path
);
912 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
913 char *p
= strrchr(path
, '/');
917 if (options
->num_patterns
) {
918 for (i
= 0; i
< options
->num_patterns
; i
++) {
919 ret
= fnmatch(options
->patterns
[i
], path
, FNM_PATHNAME
);
922 if (ret
== FNM_NOMATCH
)
926 if (i
>= options
->num_patterns
) /* no match */
929 tmp
= options
->num_matching_paths
++;
930 if (options
->num_matching_paths
> options
->array_size
) {
931 options
->array_size
++;
932 options
->array_size
*= 2;
933 options
->data
= para_realloc(options
->data
, options
->array_size
934 * sizeof(*options
->data
));
936 d
= options
->data
+ tmp
;
937 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
940 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
944 ret
= get_hash_of_row(aft_row
, &d
->hash
);
947 w
= &options
->widths
;
948 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
949 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
950 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
951 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
952 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
953 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
954 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
955 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
956 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
957 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
958 /* just get the number of chars to print this amount of time */
959 tmp
= get_duration(d
->afhi
.seconds_total
, NULL
, 0);
960 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
961 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
962 GET_NUM_DIGITS(score
, &num_digits
);
963 num_digits
++; /* add one for the sign (space or "-") */
964 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
970 static int com_ls_callback(const struct osl_object
*query
,
971 struct osl_object
*ls_output
)
973 struct ls_options
*opts
= query
->data
;
974 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
975 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
978 PARA_NOTICE_LOG("%d patterns\n", opts
->num_patterns
);
979 if (opts
->num_patterns
) {
980 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
981 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
982 opts
->patterns
[i
] = p
;
984 PARA_NOTICE_LOG("pattern %d: %s\n", i
, opts
->patterns
[i
]);
987 opts
->patterns
= NULL
;
988 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
989 ret
= admissible_file_loop(opts
, prepare_ls_row
);
991 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
995 ret
= opts
->num_patterns
? -E_NO_MATCH
: 1;
996 if (!opts
->num_matching_paths
) {
997 PARA_NOTICE_LOG("no match, ret: %d\n", ret
);
1000 ret
= sort_matching_paths(opts
);
1003 if (opts
->flags
& LS_FLAG_REVERSE
)
1004 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1005 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
);
1010 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1011 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
);
1017 ls_output
->data
= b
.buf
;
1018 ls_output
->size
= b
.size
;
1020 free(opts
->data_ptr
);
1021 free(opts
->patterns
);
1026 * TODO: flags -h (sort by hash)
1028 * long list: list hash, attributes as (xx--x-x-), file size, lastplayed
1029 * full list: list everything, including afsi, afhi, atts as clear text
1032 int com_afs_ls(__a_unused
int fd
, int argc
, const char **argv
)
1036 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1037 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1038 struct ls_options opts
= {.patterns
= NULL
};
1039 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1042 for (i
= 1; i
< argc
; i
++) {
1043 const char *arg
= argv
[i
];
1046 if (!strcmp(arg
, "--")) {
1050 if (!strncmp(arg
, "-l", 2)) {
1052 mode
= LS_MODE_LONG
;
1056 return -E_AFT_SYNTAX
;
1057 switch(*(arg
+ 2)) {
1059 mode
= LS_MODE_SHORT
;
1062 mode
= LS_MODE_LONG
;
1065 mode
= LS_MODE_VERBOSE
;
1068 mode
= LS_MODE_MBOX
;
1071 return -E_AFT_SYNTAX
;
1074 if (!strcmp(arg
, "-p")) {
1075 flags
|= LS_FLAG_FULL_PATH
;
1078 if (!strcmp(arg
, "-a")) {
1079 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1082 if (!strcmp(arg
, "-r")) {
1083 flags
|= LS_FLAG_REVERSE
;
1086 if (!strncmp(arg
, "-s", 2)) {
1087 if (!*(arg
+ 2) || *(arg
+ 3))
1088 return -E_AFT_SYNTAX
;
1089 switch(*(arg
+ 2)) {
1091 sort
= LS_SORT_BY_PATH
;
1093 case 's': /* -ss implies -a */
1094 sort
= LS_SORT_BY_SCORE
;
1095 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1098 sort
= LS_SORT_BY_LAST_PLAYED
;
1101 sort
= LS_SORT_BY_NUM_PLAYED
;
1104 sort
= LS_SORT_BY_FREQUENCY
;
1107 sort
= LS_SORT_BY_CHANNELS
;
1110 sort
= LS_SORT_BY_IMAGE_ID
;
1113 sort
= LS_SORT_BY_LYRICS_ID
;
1116 sort
= LS_SORT_BY_BITRATE
;
1119 sort
= LS_SORT_BY_DURATION
;
1122 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1125 return -E_AFT_SYNTAX
;
1128 return -E_AFT_SYNTAX
;
1132 opts
.sorting
= sort
;
1134 opts
.num_patterns
= argc
- i
;
1135 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1136 argv
+ i
, com_ls_callback
, &ls_output
);
1137 if (ret
>= 0 && ls_output
.data
) {
1138 printf("%s\n", (char *)ls_output
.data
);
1139 free(ls_output
.data
);
1145 * Call the given function for each file in the audio file table.
1147 * \param private_data An arbitrary data pointer, passed to \a func.
1148 * \param func The custom function to be called.
1150 * \return The return value of the underlying call to osl_rbtree_loop().
1152 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1154 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1158 static void *find_hash_sister(HASH_TYPE
*hash
)
1160 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1161 struct osl_row
*row
;
1163 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1167 #define AFTROW_HEADER_SIZE 4
1169 enum aft_row_offsets
{
1170 AFTROW_AFHI_OFFSET_POS
= 0,
1171 AFTROW_CHUNKS_OFFSET_POS
= 2,
1172 AFTROW_HASH_OFFSET
= AFTROW_HEADER_SIZE
,
1173 AFTROW_FLAGS_OFFSET
= (AFTROW_HASH_OFFSET
+ HASH_SIZE
),
1174 AFTROW_PATH_OFFSET
= (AFTROW_FLAGS_OFFSET
+ 4)
1177 /* never save the afsi, as the server knows it too. Note that afhi might be NULL.
1178 * In this case, afhi won't be stored in the buffer */
1179 static void save_audio_file_info(HASH_TYPE
*hash
, const char *path
,
1180 struct audio_format_info
*afhi
,
1181 uint32_t flags
, struct osl_object
*obj
)
1183 size_t path_len
= strlen(path
) + 1;
1184 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1185 size_t size
= AFTROW_PATH_OFFSET
+ path_len
+ afhi_size
1186 + sizeof_chunk_info_buf(afhi
);
1187 char *buf
= para_malloc(size
);
1190 memcpy(buf
+ AFTROW_HASH_OFFSET
, hash
, HASH_SIZE
);
1191 write_u32(buf
+ AFTROW_FLAGS_OFFSET
, flags
);
1192 strcpy(buf
+ AFTROW_PATH_OFFSET
, path
);
1194 pos
= AFTROW_PATH_OFFSET
+ path_len
;
1195 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1196 PARA_DEBUG_LOG("last afhi byte: %p, pos %d\n", buf
+ pos
+ afhi_size
- 1,
1197 pos
+ afhi_size
- 1);
1198 write_u16(buf
+ AFTROW_AFHI_OFFSET_POS
, pos
);
1199 save_afhi(afhi
, buf
+ pos
);
1202 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1203 write_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
, pos
);
1204 save_chunk_info(afhi
, buf
+ pos
);
1205 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1219 AFHI: whether afhi and chunk table are computed and sent
1220 ACTION: table modifications to be performed
1222 +---+----+-----+------+---------------------------------------------------+
1223 | HS | PB | F | AFHI | ACTION
1224 +---+----+-----+------+---------------------------------------------------+
1225 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1226 | | update path, keep afsi
1227 +---+----+-----+------+---------------------------------------------------+
1228 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1229 | | otherwise: remove PB, HS: update path, keep afhi,
1231 +---+----+-----+------+---------------------------------------------------+
1232 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1234 +---+----+-----+------+---------------------------------------------------+
1235 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1236 +---+----+-----+------+---------------------------------------------------+
1237 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1238 | | (force has no effect)
1239 +---+----+-----+------+---------------------------------------------------+
1240 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1241 +---+----+-----+------+---------------------------------------------------+
1242 | N | N | Y | Y | (new file) create new entry (force has no effect)
1243 +---+----+-----+------+---------------------------------------------------+
1244 | N | N | N | Y | (new file) create new entry
1245 +---+----+-----+------+---------------------------------------------------+
1247 afhi <=> force or no HS
1252 #define ADD_FLAG_LAZY 1
1253 #define ADD_FLAG_FORCE 2
1254 #define ADD_FLAG_VERBOSE 4
1256 static int com_add_callback(const struct osl_object
*query
,
1257 __a_unused
struct osl_object
*result
)
1259 char *buf
= query
->data
, *path
;
1260 struct osl_row
*pb
, *aft_row
;
1261 const struct osl_row
*hs
;
1262 struct osl_object objs
[NUM_AFT_COLUMNS
];
1264 char asc
[2 * HASH_SIZE
+ 1];
1266 char afsi_buf
[AFSI_SIZE
];
1267 uint32_t flags
= read_u32(buf
+ AFTROW_FLAGS_OFFSET
);
1269 hash
= (HASH_TYPE
*)buf
+ AFTROW_HASH_OFFSET
;
1270 hash_to_asc(hash
, asc
);;
1271 objs
[AFTCOL_HASH
].data
= buf
+ AFTROW_HASH_OFFSET
;
1272 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1274 path
= buf
+ AFTROW_PATH_OFFSET
;
1275 objs
[AFTCOL_PATH
].data
= path
;
1276 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1278 PARA_DEBUG_LOG("request to add %s with hash %s\n", path
, asc
);
1279 hs
= find_hash_sister(hash
);
1280 ret
= aft_get_row_of_path(path
, &pb
);
1281 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1283 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1284 if (flags
& ADD_FLAG_VERBOSE
)
1285 PARA_NOTICE_LOG("ignoring duplicate %p\n", path
);
1288 if (hs
&& hs
!= pb
) {
1289 struct osl_object obj
;
1290 if (pb
) { /* hs trumps pb, remove pb */
1291 if (flags
& ADD_FLAG_VERBOSE
)
1292 PARA_NOTICE_LOG("removing path brother\n");
1293 ret
= osl_del_row(audio_file_table
, pb
);
1298 /* file rename, update hs' path */
1299 ret
= osl_get_object(audio_file_table
, hs
, AFTCOL_PATH
, &obj
);
1300 if (flags
& ADD_FLAG_VERBOSE
)
1301 PARA_NOTICE_LOG("rename %s -> %s\n", (char *)obj
.data
, path
);
1302 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1303 &objs
[AFTCOL_PATH
]);
1306 if (!(flags
& ADD_FLAG_FORCE
))
1309 /* no hs or force mode, child must have sent afhi */
1310 uint16_t afhi_offset
= read_u16(buf
+ AFTROW_AFHI_OFFSET_POS
);
1311 uint16_t chunks_offset
= read_u16(buf
+ AFTROW_CHUNKS_OFFSET_POS
);
1313 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1314 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1315 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1317 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1318 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1319 if (pb
&& !hs
) { /* update pb's hash */
1320 char old_asc
[2 * HASH_SIZE
+ 1];
1321 HASH_TYPE
*old_hash
;
1322 ret
= get_hash_of_row(pb
, &old_hash
);
1325 hash_to_asc(old_hash
, old_asc
);
1326 if (flags
& ADD_FLAG_VERBOSE
)
1327 PARA_NOTICE_LOG("file change: %s %s -> %s\n", path
,
1329 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1330 &objs
[AFTCOL_HASH
]);
1334 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1335 const void *row
= pb
? pb
: hs
;
1336 /* update afhi and chunk_table */
1337 if (flags
& ADD_FLAG_VERBOSE
)
1338 PARA_NOTICE_LOG("updating audio format handler info (%zd bytes)\n",
1339 objs
[AFTCOL_AFHI
].size
);
1340 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1341 &objs
[AFTCOL_AFHI
]);
1344 if (flags
& ADD_FLAG_VERBOSE
)
1345 PARA_NOTICE_LOG("updating chunk table\n");
1346 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1347 &objs
[AFTCOL_CHUNKS
]);
1350 ret
= mood_update_audio_file(row
, NULL
);
1354 /* new entry, use default afsi */
1355 if (flags
& ADD_FLAG_VERBOSE
)
1356 PARA_NOTICE_LOG("adding %s\n", path
);
1357 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1358 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1359 save_afsi(NULL
, &objs
[AFTCOL_AFSI
]);
1360 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1363 return mood_update_audio_file(aft_row
, NULL
);
1366 struct private_add_data
{
1371 static int add_one_audio_file(const char *arg
, const void *private_data
)
1374 const struct private_add_data
*pad
= private_data
;
1375 struct audio_format_info afhi
, *afhi_ptr
= NULL
;
1376 struct osl_row
*pb
, *hs
; /* path brother/hash sister */
1377 struct osl_object map
, obj
= {.data
= NULL
};
1379 HASH_TYPE hash
[HASH_SIZE
];
1381 afhi
.header_offset
= 0;
1382 afhi
.header_len
= 0;
1383 ret
= verify_path(arg
, &path
);
1386 ret
= aft_get_row_of_path(path
, &pb
);
1387 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1390 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1391 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1392 PARA_NOTICE_LOG("lazy-ignore: %s\n", path
);
1395 /* we still want to add this file. Compute its hash and look it up */
1396 ret
= mmap_full_file(path
, O_RDONLY
, &map
);
1399 hash_function(map
.data
, map
.size
, hash
);
1400 hs
= find_hash_sister(hash
);
1402 * return success if we're pretty sure that we already know this file
1405 if (pb
&& hs
&& hs
== pb
&& (!(pad
->flags
& ADD_FLAG_FORCE
))) {
1406 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1407 PARA_NOTICE_LOG("not forcing update: %s\n", path
);
1411 * we won't recalculate the audio format info and the chunk table if
1412 * there is a hash sister unless in FORCE mode.
1414 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1415 ret
= mp3_get_file_info(map
.data
, map
.size
, &afhi
);
1417 PARA_WARNING_LOG("audio format of %s not recognized, skipping\n", path
);
1423 munmap(map
.data
, map
.size
);
1424 save_audio_file_info(hash
, path
, afhi_ptr
, pad
->flags
, &obj
);
1425 /* ask parent to consider this entry for adding */
1426 ret
= send_callback_request(com_add_callback
, &obj
, NULL
);
1430 munmap(map
.data
, map
.size
);
1435 free(afhi_ptr
->chunk_table
);
1439 int com_add(int fd
, int argc
, char **argv
)
1442 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1443 struct stat statbuf
;
1445 for (i
= 1; i
< argc
; i
++) {
1446 const char *arg
= argv
[i
];
1449 if (!strcmp(arg
, "--")) {
1453 if (!strcmp(arg
, "-l")) {
1454 pad
.flags
|= ADD_FLAG_LAZY
;
1457 if (!strcmp(arg
, "-f")) {
1458 pad
.flags
|= ADD_FLAG_FORCE
;
1461 if (!strcmp(arg
, "-v")) {
1462 pad
.flags
|= ADD_FLAG_VERBOSE
;
1467 return -E_AFT_SYNTAX
;
1468 for (; i
< argc
; i
++) {
1469 ret
= stat(argv
[i
], &statbuf
);
1472 if (S_ISDIR(statbuf
.st_mode
)) {
1473 ret
= for_each_file_in_dir(argv
[i
],
1474 add_one_audio_file
, &pad
);
1479 ret
= add_one_audio_file(argv
[i
], &pad
);
1489 struct com_touch_options
{
1496 static int com_touch_callback(const struct osl_object
*query
,
1497 __a_unused
struct osl_object
*result
)
1499 struct com_touch_options
*cto
= query
->data
;
1500 char *p
= (char *)query
->data
+ sizeof(*cto
);
1502 int ret
, no_options
= cto
->num_played
< 0 && cto
->last_played
< 0 &&
1503 cto
->lyrics_id
< 0 && cto
->image_id
< 0;
1505 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1506 struct afs_info old_afsi
, new_afsi
;
1507 struct osl_object obj
;
1508 struct osl_row
*row
;
1511 ret
= aft_get_row_of_path(p
, &row
);
1514 ret
= get_afsi_object_of_row(row
, &obj
);
1517 ret
= load_afsi(&old_afsi
, &obj
);
1520 new_afsi
= old_afsi
;
1522 new_afsi
.num_played
++;
1523 new_afsi
.last_played
= time(NULL
);
1525 if (cto
->lyrics_id
>= 0)
1526 new_afsi
.lyrics_id
= cto
->lyrics_id
;
1527 if (cto
->image_id
>= 0)
1528 new_afsi
.image_id
= cto
->image_id
;
1529 if (cto
->num_played
>= 0)
1530 new_afsi
.num_played
= cto
->num_played
;
1531 if (cto
->last_played
>= 0)
1532 new_afsi
.last_played
= cto
->last_played
;
1534 save_afsi(&new_afsi
, &obj
); /* in-place update */
1535 ret
= mood_update_audio_file(row
, &old_afsi
);
1542 int com_touch(__a_unused
int fd
, int argc
, const char **argv
)
1544 struct com_touch_options cto
= {
1550 struct osl_object options
= {.data
= &cto
, .size
= sizeof(cto
)};
1554 for (i
= 1; i
< argc
; i
++) {
1555 const char *arg
= argv
[i
];
1558 if (!strcmp(arg
, "--")) {
1562 if (!strncmp(arg
, "-n", 2)) {
1563 ret
= para_atol(arg
+ 2, &cto
.num_played
);
1568 if (!strncmp(arg
, "-l", 2)) {
1569 ret
= para_atol(arg
+ 2, &cto
.last_played
);
1574 if (!strncmp(arg
, "-y", 2)) {
1575 ret
= para_atol(arg
+ 2, &cto
.lyrics_id
);
1580 if (!strncmp(arg
, "-i", 2)) {
1581 ret
= para_atol(arg
+ 2, &cto
.image_id
);
1587 ret
= -E_AFT_SYNTAX
;
1590 return send_option_arg_callback_request(&options
, argc
- i
,
1591 argv
+ i
, com_touch_callback
, NULL
);
1596 struct com_rm_options
{
1600 static int com_rm_callback(const struct osl_object
*query
,
1601 __a_unused
struct osl_object
*result
)
1603 struct com_rm_options
*cro
= query
->data
;
1604 char *p
= (char *)query
->data
+ sizeof(*cro
);
1608 for (;p
< (char *)query
->data
+ query
->size
; p
+= len
+ 1) {
1609 struct osl_row
*row
;
1612 ret
= aft_get_row_of_path(p
, &row
);
1615 ret
= mood_delete_audio_file(row
);
1618 ret
= osl_del_row(audio_file_table
, row
);
1626 * TODO options: -v verbose, -f dont stop if file not found
1627 * -h remove by hash, use fnmatch
1631 int com_afs_rm(__a_unused
int fd
, int argc
, const char **argv
)
1633 struct com_rm_options cro
= {.flags
= 0};
1634 struct osl_object options
= {.data
= &cro
, .size
= sizeof(cro
)};
1637 for (i
= 1; i
< argc
; i
++) {
1638 const char *arg
= argv
[i
];
1641 if (!strcmp(arg
, "--")) {
1646 ret
= -E_AFT_SYNTAX
;
1649 return send_option_arg_callback_request(&options
, argc
- i
,
1650 argv
+ i
, com_rm_callback
, NULL
);
1656 * Close the audio file table.
1658 * \param flags Ususal flags that are passed to osl_close_table().
1660 * \sa osl_close_table().
1662 void aft_shutdown(enum osl_close_flags flags
)
1664 osl_close_table(audio_file_table
, flags
);
1668 * Open the audio file table.
1670 * \param ti Gets initialized by this function
1672 * \return Positive on success, negative on errors.
1674 * \sa osl_open_table().
1676 int aft_init(struct table_info
*ti
)
1680 ti
->desc
= &audio_file_table_desc
;
1681 ret
= osl_open_table(ti
->desc
, &ti
->table
);
1684 audio_file_table
= ti
->table
;
1685 osl_get_num_rows(audio_file_table
, &num
);
1686 PARA_INFO_LOG("audio file table contains %d files\n", num
);
1689 audio_file_table
= NULL
;
1690 return ret
== -E_NOENT
? 1 : ret
;