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. */
9 #include <dirent.h> /* readdir() */
22 static struct osl_table
*audio_file_table
;
24 /** The different sorting methods of the ls command. */
25 enum ls_sorting_method
{
31 LS_SORT_BY_LAST_PLAYED
,
33 LS_SORT_BY_NUM_PLAYED
,
47 LS_SORT_BY_AUDIO_FORMAT
,
52 /** The different listing modes of the ls command. */
53 enum ls_listing_mode
{
54 /** Default listing mode. */
64 /** The flags accepted by the ls command. */
67 LS_FLAG_FULL_PATH
= 1,
69 LS_FLAG_ADMISSIBLE_ONLY
= 2,
75 * The size of the individual output fields of the ls command.
77 * These depend on the actual content being listed. If, for instance only files
78 * with duration less than an hour are being listed, then the duration with is
79 * made smaller because then the duration is listed as mm:ss rather than
83 /** size of the score field. */
84 unsigned short score_width
;
85 /** size of the image id field. */
86 unsigned short image_id_width
;
87 /** size of the lyrics id field. */
88 unsigned short lyrics_id_width
;
89 /** size of the bitrate field. */
90 unsigned short bitrate_width
;
91 /** size of the frequency field. */
92 unsigned short frequency_width
;
93 /** size of the duration field. */
94 unsigned short duration_width
;
95 /** size of the num played field. */
96 unsigned short num_played_width
;
99 /** Data passed from the ls command handler to its callback function. */
101 /** The given command line flags. */
103 /** The sorting method given at the command line. */
104 enum ls_sorting_method sorting
;
105 /** The given listing mode (short, long, verbose, mbox). */
106 enum ls_listing_mode mode
;
107 /** The arguments passed to the ls command. */
109 /** Number of non-option arguments. */
111 /** Used for long listing mode to align the output fields. */
112 struct ls_widths widths
;
113 /** Size of the \a data array. */
115 /** Number of used entries in the data array. */
116 uint32_t num_matching_paths
;
117 /** Array of matching entries. */
118 struct ls_data
*data
;
119 /** Used to sort the array. */
120 struct ls_data
**data_ptr
;
124 * Describes the layout of the mmapped-afs info struct.
126 * \sa struct afs_info.
129 /** Where .last_played is stored. */
130 AFSI_LAST_PLAYED_OFFSET
= 0,
131 /** Storage position of the attributes bitmap. */
132 AFSI_ATTRIBUTES_OFFSET
= 8,
133 /** Storage position of the .num_played field. */
134 AFSI_NUM_PLAYED_OFFSET
= 16,
135 /** Storage position of the .image_id field. */
136 AFSI_IMAGE_ID_OFFSET
= 20,
137 /** Storage position of the .lyrics_id field. */
138 AFSI_LYRICS_ID_OFFSET
= 24,
139 /** Storage position of the .audio_format_id field. */
140 AFSI_AUDIO_FORMAT_ID_OFFSET
= 28,
141 /** 3 bytes reserved space for future usage. */
142 AFSI_AUDIO_FORMAT_UNUSED_OFFSET
= 29,
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
);
166 memset(buf
+ AFSI_AUDIO_FORMAT_UNUSED_OFFSET
, 0, 3);
170 * Get the audio file selector info struct stored in an osl object.
172 * \param afsi Points to the audio_file info structure to be filled in.
173 * \param obj The osl object holding the data.
175 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
179 int load_afsi(struct afs_info
*afsi
, struct osl_object
*obj
)
181 char *buf
= obj
->data
;
182 if (obj
->size
< AFSI_SIZE
)
184 afsi
->last_played
= read_u64(buf
+ AFSI_LAST_PLAYED_OFFSET
);
185 afsi
->attributes
= read_u64(buf
+ AFSI_ATTRIBUTES_OFFSET
);
186 afsi
->num_played
= read_u32(buf
+ AFSI_NUM_PLAYED_OFFSET
);
187 afsi
->image_id
= read_u32(buf
+ AFSI_IMAGE_ID_OFFSET
);
188 afsi
->lyrics_id
= read_u32(buf
+ AFSI_LYRICS_ID_OFFSET
);
189 afsi
->audio_format_id
= read_u8(buf
+
190 AFSI_AUDIO_FORMAT_ID_OFFSET
);
194 /** The columns of the audio file table. */
195 enum audio_file_table_columns
{
196 /** The hash on the content of the audio file. */
198 /** The full path in the filesystem. */
200 /** The audio file selector info. */
202 /** The audio format handler info. */
204 /** The chunk table info and the chunk table of the audio file. */
206 /** The number of columns of this table. */
210 static struct osl_column_description aft_cols
[] = {
212 .storage_type
= OSL_MAPPED_STORAGE
,
213 .storage_flags
= OSL_RBTREE
| OSL_FIXED_SIZE
| OSL_UNIQUE
,
215 .compare_function
= osl_hash_compare
,
216 .data_size
= HASH_SIZE
219 .storage_type
= OSL_MAPPED_STORAGE
,
220 .storage_flags
= OSL_RBTREE
| OSL_UNIQUE
,
222 .compare_function
= string_compare
,
225 .storage_type
= OSL_MAPPED_STORAGE
,
226 .storage_flags
= OSL_FIXED_SIZE
,
228 .data_size
= AFSI_SIZE
231 .storage_type
= OSL_MAPPED_STORAGE
,
235 .storage_type
= OSL_DISK_STORAGE
,
240 static struct osl_table_description audio_file_table_desc
= {
241 .name
= "audio_files",
242 .num_columns
= NUM_AFT_COLUMNS
,
243 .flags
= OSL_LARGE_TABLE
,
244 .column_descriptions
= aft_cols
247 /* We don't want * dot or dot-dot anywhere. */
248 static int verify_dotfile(const char *rest
)
251 * The first character was '.', but that has already been discarded, we
255 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
257 case '.': /* path start with /foo/.. */
258 if (rest
[1] == '\0' || rest
[1] == '/')
259 return -1; /* /foo/.. or /foo/../bar are not ok */
260 /* /foo/..bar is ok */
266 * We fundamentally don't like some paths: We don't want double slashes or
267 * slashes at the end that can make pathnames ambiguous.
269 static int verify_path(const char *orig_path
, char **resolved_path
)
275 if (*orig_path
!= '/') /* we only accept absolute paths */
277 len
= strlen(orig_path
);
278 *resolved_path
= para_strdup(orig_path
);
279 path
= *resolved_path
;
280 while (len
> 1 && path
[--len
] == '/')
281 path
[len
] = '\0'; /* remove slash at the end */
287 case '/': /* double slash */
290 if (verify_dotfile(path
) < 0)
300 free(*resolved_path
);
304 /** The on-disk layout of a afhi struct. */
306 /** Where the number of seconds is stored. */
307 AFHI_SECONDS_TOTAL_OFFSET
= 0,
308 /** Position of the bitrate. */
309 AFHI_BITRATE_OFFSET
= 4,
310 /** Position of the frequency. */
311 AFHI_FREQUENCY_OFFSET
= 8,
312 /** Location of the audio file header. */
313 AFHI_HEADER_OFFSET_OFFSET
= 12,
314 /* Length of the audio file header. Zero means: No header. */
315 AFHI_HEADER_LEN_OFFSET
= 16,
316 /** The total number of chunks (4 bytes). */
317 CHUNKS_TOTAL_OFFSET
= 20,
318 /** The length of the audio file header (4 bytes). */
319 HEADER_LEN_OFFSET
= 24,
320 /** The start of the audio file header (4 bytes). */
321 HEADER_OFFSET_OFFSET
= 28,
322 /** The seconds part of the chunk time (4 bytes). */
323 CHUNK_TV_TV_SEC_OFFSET
= 32,
324 /** The microseconds part of the chunk time (4 bytes). */
325 CHUNK_TV_TV_USEC_OFFSET
= 36,
326 /** Number of channels is stored here. (1 byte) */
327 AFHI_CHANNELS_OFFSET
= 40,
328 /** EOF timeout in ms. (2 byte) */
329 AFHI_EOF_OFFSET
= 41,
330 /** The tag info position. */
331 AFHI_INFO_STRING_OFFSET
= 43,
332 /** Minimal on-disk size of a valid afhi struct. */
336 static unsigned sizeof_afhi_buf(const struct afh_info
*afhi
)
340 return strlen(afhi
->info_string
) + MIN_AFHI_SIZE
;
343 static void save_afhi(struct afh_info
*afhi
, char *buf
)
347 write_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
, afhi
->seconds_total
);
348 write_u32(buf
+ AFHI_BITRATE_OFFSET
, afhi
->bitrate
);
349 write_u32(buf
+ AFHI_FREQUENCY_OFFSET
, afhi
->frequency
);
350 write_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
351 write_u32(buf
+ AFHI_HEADER_LEN_OFFSET
, afhi
->header_len
);
352 write_u8(buf
+ AFHI_CHANNELS_OFFSET
, afhi
->channels
);
353 write_u32(buf
+ CHUNKS_TOTAL_OFFSET
, afhi
->chunks_total
);
354 write_u32(buf
+ HEADER_LEN_OFFSET
, afhi
->header_len
);
355 write_u32(buf
+ HEADER_OFFSET_OFFSET
, afhi
->header_offset
);
356 write_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
, afhi
->chunk_tv
.tv_sec
);
357 write_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
, afhi
->chunk_tv
.tv_usec
);
358 write_u16(buf
+ AFHI_EOF_OFFSET
, tv2ms(&afhi
->eof_tv
));
359 strcpy(buf
+ AFHI_INFO_STRING_OFFSET
, afhi
->info_string
); /* OK */
362 static void load_afhi(const char *buf
, struct afh_info
*afhi
)
364 afhi
->seconds_total
= read_u32(buf
+ AFHI_SECONDS_TOTAL_OFFSET
);
365 afhi
->bitrate
= read_u32(buf
+ AFHI_BITRATE_OFFSET
);
366 afhi
->frequency
= read_u32(buf
+ AFHI_FREQUENCY_OFFSET
);
367 afhi
->header_offset
= read_u32(buf
+ AFHI_HEADER_OFFSET_OFFSET
);
368 afhi
->header_len
= read_u32(buf
+ AFHI_HEADER_LEN_OFFSET
);
369 afhi
->channels
= read_u8(buf
+ AFHI_CHANNELS_OFFSET
);
370 afhi
->chunks_total
= read_u32(buf
+ CHUNKS_TOTAL_OFFSET
);
371 afhi
->header_len
= read_u32(buf
+ HEADER_LEN_OFFSET
);
372 afhi
->header_offset
= read_u32(buf
+ HEADER_OFFSET_OFFSET
);
373 afhi
->chunk_tv
.tv_sec
= read_u32(buf
+ CHUNK_TV_TV_SEC_OFFSET
);
374 afhi
->chunk_tv
.tv_usec
= read_u32(buf
+ CHUNK_TV_TV_USEC_OFFSET
);
375 ms2tv(read_u16(buf
+ AFHI_EOF_OFFSET
), &afhi
->eof_tv
);
376 strcpy(afhi
->info_string
, buf
+ AFHI_INFO_STRING_OFFSET
);
379 static unsigned sizeof_chunk_table(struct afh_info
*afhi
)
383 return 4 * (afhi
->chunks_total
+ 1);
386 static void save_chunk_table(struct afh_info
*afhi
, char *buf
)
390 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
391 write_u32(buf
+ 4 * i
, afhi
->chunk_table
[i
]);
394 static void load_chunk_table(struct afh_info
*afhi
, char *buf
)
398 afhi
->chunk_table
= para_malloc(sizeof_chunk_table(afhi
));
399 for (i
= 0; i
<= afhi
->chunks_total
; i
++)
400 afhi
->chunk_table
[i
] = read_u32(buf
+ 4 * i
);
404 * Get the row of the audio file table corresponding to the given path.
406 * \param path The full path of the audio file.
407 * \param row Result pointer.
409 * \return The return value of the underlying call to osl_get_row().
411 int aft_get_row_of_path(const char *path
, struct osl_row
**row
)
413 struct osl_object obj
= {.data
= (char *)path
, .size
= strlen(path
) + 1};
415 return osl_get_row(audio_file_table
, AFTCOL_PATH
, &obj
, row
);
419 * Get the row of the audio file table corresponding to the given hash value.
421 * \param hash The hash value of the desired audio file.
422 * \param row resul pointer.
424 * \return The return value of the underlying call to osl_get_row().
426 int aft_get_row_of_hash(HASH_TYPE
*hash
, struct osl_row
**row
)
428 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
429 return osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, row
);
433 * Get the osl object holding the audio file selector info of a row.
435 * \param row Pointer to a row in the audio file table.
436 * \param obj Result pointer.
438 * \return The return value of the underlying call to osl_get_object().
440 int get_afsi_object_of_row(const struct osl_row
*row
, struct osl_object
*obj
)
442 return osl_get_object(audio_file_table
, row
, AFTCOL_AFSI
, obj
);
446 * Get the osl object holding the audio file selector info, given a path.
449 * \param path The full path of the audio file.
450 * \param obj Result pointer.
452 * \return Positive on success, negative on errors.
454 int get_afsi_object_of_path(const char *path
, struct osl_object
*obj
)
457 int ret
= aft_get_row_of_path(path
, &row
);
460 return get_afsi_object_of_row(row
, obj
);
464 * Get the audio file selector info, given a row of the audio file table.
466 * \param row Pointer to a row in the audio file table.
467 * \param afsi Result pointer.
469 * \return Positive on success, negative on errors.
471 int get_afsi_of_row(const struct osl_row
*row
, struct afs_info
*afsi
)
473 struct osl_object obj
;
474 int ret
= get_afsi_object_of_row(row
, &obj
);
477 return load_afsi(afsi
, &obj
);
481 * Get the audio file selector info, given the path of an audio table.
483 * \param path The full path of the audio file.
484 * \param afsi Result pointer.
486 * \return Positive on success, negative on errors.
488 int get_afsi_of_path(const char *path
, struct afs_info
*afsi
)
490 struct osl_object obj
;
491 int ret
= get_afsi_object_of_path(path
, &obj
);
494 return load_afsi(afsi
, &obj
);
498 * Get the path of an audio file, given a row of the audio file table.
500 * \param row Pointer to a row in the audio file table.
501 * \param path Result pointer.
503 * The result is a pointer to mmapped data. The caller must not attempt
508 int get_audio_file_path_of_row(const struct osl_row
*row
, char **path
)
510 struct osl_object path_obj
;
511 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_PATH
,
515 *path
= path_obj
.data
;
520 * Get the object containing the hash value of an audio file, given a row.
522 * \param row Pointer to a row of the audio file table.
523 * \param obj Result pointer.
525 * \return The return value of the underlying call to osl_get_object().
527 * \sa get_hash_of_row().
529 static int get_hash_object_of_aft_row(const struct osl_row
*row
, struct osl_object
*obj
)
531 return osl_get_object(audio_file_table
, row
, AFTCOL_HASH
, obj
);
535 * Get the hash value of an audio file, given a row of the audio file table.
537 * \param row Pointer to a row of the audio file table.
538 * \param hash Result pointer.
540 * \a hash points to mapped data and must not be freed by the caller.
542 * \return The return value of the underlying call to
543 * get_hash_object_of_aft_row().
545 static int get_hash_of_row(const struct osl_row
*row
, HASH_TYPE
**hash
)
547 struct osl_object obj
;
548 int ret
= get_hash_object_of_aft_row(row
, &obj
);
557 * Get the audio format handler info, given a row of the audio file table.
559 * \param row Pointer to a row of the audio file table.
560 * \param afhi Result pointer.
562 * \return The return value of the underlying call to osl_get_object().
564 * \sa get_chunk_table_of_row().
566 int get_afhi_of_row(const struct osl_row
*row
, struct afh_info
*afhi
)
568 struct osl_object obj
;
569 int ret
= osl_get_object(audio_file_table
, row
, AFTCOL_AFHI
,
573 load_afhi(obj
.data
, afhi
);
577 /* returns shmid on success */
578 static int save_afd(struct audio_file_data
*afd
)
580 size_t size
= sizeof(*afd
) + sizeof_chunk_table(&afd
->afhi
);
582 PARA_DEBUG_LOG("size: %zu\n", size
);
583 int shmid
, ret
= shm_new(size
);
590 ret
= shm_attach(shmid
, ATTACH_RW
, &shm_afd
);
593 *(struct audio_file_data
*)shm_afd
= *afd
;
596 save_chunk_table(&afd
->afhi
, buf
);
604 int load_afd(int shmid
, struct audio_file_data
*afd
)
610 ret
= shm_attach(shmid
, ATTACH_RO
, &shm_afd
);
613 *afd
= *(struct audio_file_data
*)shm_afd
;
616 load_chunk_table(&afd
->afhi
, buf
);
622 * Mmap the given audio file and update statistics.
624 * \param aft_row Determines the audio file to be opened and updated.
625 * \param score The score of the audio file.
626 * \param afd Result pointer.
628 * On success, the numplayed field of the audio file selector info is increased
629 * and the lastplayed time is set to the current time. Finally, the score of
630 * the audio file is updated.
632 * \return Positive shmid on success, negative on errors.
634 int open_and_update_audio_file(struct osl_row
*aft_row
, long score
,
635 struct audio_file_data
*afd
)
637 HASH_TYPE
*aft_hash
, file_hash
[HASH_SIZE
];
638 struct osl_object afsi_obj
;
639 struct afs_info old_afsi
, new_afsi
;
640 int ret
= get_hash_of_row(aft_row
, &aft_hash
);
641 struct afsi_change_event_data aced
;
642 struct osl_object map
, chunk_table_obj
;
647 ret
= get_audio_file_path_of_row(aft_row
, &path
);
650 ret
= get_afsi_object_of_row(aft_row
, &afsi_obj
);
653 ret
= load_afsi(&old_afsi
, &afsi_obj
);
656 ret
= get_afhi_of_row(aft_row
, &afd
->afhi
);
659 ret
= osl_open_disk_object(audio_file_table
, aft_row
,
660 AFTCOL_CHUNKS
, &chunk_table_obj
);
663 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
,
664 &map
.size
, &afd
->fd
);
667 hash_function(map
.data
, map
.size
, file_hash
);
668 ret
= hash_compare(file_hash
, aft_hash
);
669 para_munmap(map
.data
, map
.size
);
671 ret
= -E_HASH_MISMATCH
;
675 new_afsi
.num_played
++;
676 new_afsi
.last_played
= time(NULL
);
677 save_afsi(&new_afsi
, &afsi_obj
); /* in-place update */
679 load_chunk_table(&afd
->afhi
, chunk_table_obj
.data
);
688 struct para_buffer pb
= {.buf
= NULL
};
689 ret
= make_status_items(&d
, &pb
);
692 strncpy(afd
->verbose_ls_output
, pb
.buf
, VERBOSE_LS_OUTPUT_SIZE
);
693 afd
->verbose_ls_output
[VERBOSE_LS_OUTPUT_SIZE
- 1] = '\0';
696 aced
.aft_row
= aft_row
;
697 aced
.old_afsi
= &old_afsi
;
698 afs_event(AFSI_CHANGE
, NULL
, &aced
);
700 free(afd
->afhi
.chunk_table
);
704 osl_close_disk_object(&chunk_table_obj
);
708 static int get_local_time(uint64_t *seconds
, char *buf
, size_t size
,
709 time_t current_time
, enum ls_listing_mode lm
)
713 if (!localtime_r((time_t *)seconds
, &t
))
715 if (lm
== LS_MODE_MBOX
) {
716 if (!strftime(buf
, size
, "%c", &t
))
720 if (*seconds
+ 6 * 30 * 24 * 3600 > current_time
) {
721 if (!strftime(buf
, size
, "%b %e %k:%M", &t
))
725 if (!strftime(buf
, size
, "%b %e %Y", &t
))
730 /** Compute the number of (decimal) digits of a number. */
731 #define GET_NUM_DIGITS(x, num) { \
732 typeof((x)) _tmp = PARA_ABS(x); \
735 while ((_tmp) > 9) { \
741 static short unsigned get_duration_width(int seconds
)
743 short unsigned width
;
744 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
746 if (!hours
) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
747 return 4 + (mins
> 9);
748 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
749 GET_NUM_DIGITS(hours
, &width
);
753 static void get_duration_buf(int seconds
, char *buf
, struct ls_options
*opts
)
755 unsigned hours
= seconds
/ 3600, mins
= (seconds
% 3600) / 60;
756 short unsigned max_width
;
758 if (!hours
) { /* m:ss or mm:ss */
759 max_width
= opts
->mode
== LS_MODE_LONG
?
760 opts
->widths
.duration_width
: 4;
761 sprintf(buf
, "%*u:%02u", max_width
- 3, mins
, seconds
% 60);
762 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
763 max_width
= opts
->mode
== LS_MODE_LONG
?
764 opts
->widths
.duration_width
: 7;
765 sprintf(buf
, "%*u:%02u:%02u", max_width
- 6, hours
, mins
,
770 static char *make_attribute_lines(const char *att_bitmap
, struct afs_info
*afsi
)
772 char *att_text
, *att_lines
;
774 get_attribute_text(&afsi
->attributes
, " ", &att_text
);
776 return para_strdup(att_bitmap
);
777 att_lines
= make_message("%s: %s\n%s: %s",
778 status_item_list
[SI_ATTRIBUTES_BITMAP
], att_bitmap
,
779 status_item_list
[SI_ATTRIBUTES_TXT
], att_text
);
784 static char *make_lyrics_lines(struct afs_info
*afsi
)
788 lyr_get_name_by_id(afsi
->lyrics_id
, &lyrics_name
);
789 return make_message("%s: %u\n%s: %s\n",
790 status_item_list
[SI_LYRICS_ID
], afsi
->lyrics_id
,
791 status_item_list
[SI_LYRICS_NAME
], lyrics_name
?
792 lyrics_name
: "(none)");
795 static char *make_image_lines(struct afs_info
*afsi
)
798 img_get_name_by_id(afsi
->image_id
, &image_name
);
799 return make_message("%s: %u\n%s: %s\n",
800 status_item_list
[SI_IMAGE_ID
], afsi
->image_id
,
801 status_item_list
[SI_IMAGE_NAME
], image_name
?
802 image_name
: "(none)");
805 static char *make_filename_lines(const char *path
, unsigned flags
)
807 char *basename
, *dirname
;
810 if (!(flags
& LS_FLAG_FULL_PATH
))
811 return make_message("%s: %s\n",
812 status_item_list
[SI_BASENAME
], path
);
813 basename
= para_basename(path
),
814 dirname
= para_dirname(path
);
815 ret
= make_message("%s: %s\n%s: %s\n%s: %s\n",
816 status_item_list
[SI_PATH
], path
,
817 status_item_list
[SI_DIRECTORY
], dirname
,
818 status_item_list
[SI_BASENAME
], basename
);
824 static int print_list_item(struct ls_data
*d
, struct ls_options
*opts
,
825 struct para_buffer
*b
, time_t current_time
)
829 char last_played_time
[30];
830 char duration_buf
[30]; /* nobody has an audio file long enough to overflow this */
831 char score_buf
[30] = "";
832 struct afs_info
*afsi
= &d
->afsi
;
833 struct afh_info
*afhi
= &d
->afhi
;
834 struct ls_widths
*w
= &opts
->widths
;
835 int have_score
= opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
;
836 char asc_hash
[2 * HASH_SIZE
+ 1];
837 char *att_lines
, *lyrics_lines
, *image_lines
, *filename_lines
;
839 if (opts
->mode
== LS_MODE_SHORT
) {
840 para_printf(b
, "%s\n", d
->path
);
843 get_attribute_bitmap(&afsi
->attributes
, att_buf
);
844 ret
= get_local_time(&afsi
->last_played
, last_played_time
,
845 sizeof(last_played_time
), current_time
, opts
->mode
);
848 get_duration_buf(afhi
->seconds_total
, duration_buf
, opts
);
850 if (opts
->mode
== LS_MODE_LONG
)
851 sprintf(score_buf
, "%*li ", w
->score_width
, d
->score
);
853 sprintf(score_buf
, "%li ", d
->score
);
856 if (opts
->mode
== LS_MODE_LONG
) {
859 "%s " /* attributes */
860 "%*d " /* image_id */
861 "%*d " /* lyrics_id */
863 "%s " /* audio format */
864 "%*d " /* frequency */
867 "%*d " /* num_played */
868 "%s " /* last_played */
872 w
->image_id_width
, afsi
->image_id
,
873 w
->lyrics_id_width
, afsi
->lyrics_id
,
874 w
->bitrate_width
, afhi
->bitrate
,
875 audio_format_name(afsi
->audio_format_id
),
876 w
->frequency_width
, afhi
->frequency
,
879 w
->num_played_width
, afsi
->num_played
,
885 hash_to_asc(d
->hash
, asc_hash
);
886 att_lines
= make_attribute_lines(att_buf
, afsi
);
887 lyrics_lines
= make_lyrics_lines(afsi
);
888 image_lines
= make_image_lines(afsi
);
889 filename_lines
= make_filename_lines(d
->path
, opts
->flags
);
890 if (opts
->mode
== LS_MODE_MBOX
)
892 "From foo@localhost %s\n"
893 "Received: from\nTo: bar\nFrom: a\n"
898 "%s" /* filename stuff */
899 "%s%s%s%s" /* score */
900 "%s\n" /* attributes */
901 "%s: %s\n" /* hash */
902 "%s" /* image id, image name */
904 "%s: %dkbit/s\n" /* bitrate */
905 "%s: %s\n" /* format */
906 "%s: %dHz\n" /* frequency */
907 "%s: %d\n" /* channels */
908 "%s: %s\n" /* duration */
909 "%s: %lu\n" /* seconds total */
910 "%s: %s\n" /* last played time */
911 "%s: %d\n" /* num_played */
913 "%s: %lu\n" /* chunk time */
914 "%s: %lu\n", /* num chunks */
916 have_score
? status_item_list
[SI_SCORE
] : "",
917 have_score
? ": " : "",
919 have_score
? "\n" : "",
921 status_item_list
[SI_HASH
], asc_hash
,
924 status_item_list
[SI_BITRATE
], afhi
->bitrate
,
925 status_item_list
[SI_FORMAT
], audio_format_name(afsi
->audio_format_id
),
926 status_item_list
[SI_FREQUENCY
], afhi
->frequency
,
927 status_item_list
[SI_CHANNELS
], afhi
->channels
,
928 status_item_list
[SI_DURATION
], duration_buf
,
929 status_item_list
[SI_SECONDS_TOTAL
], afhi
->seconds_total
,
930 status_item_list
[SI_LAST_PLAYED
], last_played_time
,
931 status_item_list
[SI_NUM_PLAYED
], afsi
->num_played
,
933 status_item_list
[SI_CHUNK_TIME
], tv2ms(&afhi
->chunk_tv
),
934 status_item_list
[SI_NUM_CHUNKS
], afhi
->chunks_total
936 if (opts
->mode
== LS_MODE_MBOX
) {
937 struct osl_object lyrics_def
;
938 lyr_get_def_by_id(afsi
->lyrics_id
, &lyrics_def
);
939 if (lyrics_def
.data
) {
940 para_printf(b
, "Lyrics:\n~~~~~~~\n%s",
941 (char *)lyrics_def
.data
);
942 osl_close_disk_object(&lyrics_def
);
948 free(filename_lines
);
952 void make_empty_status_items(char *buf
)
956 "%s: \n" /* dirname */
957 "%s: \n" /* basename */
959 "%s: \n" /* attributes bitmap */
960 "%s: \n" /* attributes txt */
962 "%s: \n" /* image id */
963 "%s: \n" /* image name */
964 "%s: \n" /* lyrics id */
965 "%s: \n" /* lyrics name */
966 "%s: \n" /* bitrate */
967 "%s: \n" /* format */
968 "%s: \n" /* frequency */
969 "%s: \n" /* channels */
970 "%s: \n" /* duration */
971 "%s: \n" /* seconds total */
972 "%s: \n" /* num played */
973 "%s: \n" /* last played */
975 status_item_list
[SI_PATH
],
976 status_item_list
[SI_DIRECTORY
],
977 status_item_list
[SI_BASENAME
],
978 status_item_list
[SI_SCORE
],
979 status_item_list
[SI_ATTRIBUTES_BITMAP
],
980 status_item_list
[SI_ATTRIBUTES_TXT
],
981 status_item_list
[SI_HASH
],
982 status_item_list
[SI_IMAGE_ID
],
983 status_item_list
[SI_IMAGE_NAME
],
984 status_item_list
[SI_LYRICS_ID
],
985 status_item_list
[SI_LYRICS_NAME
],
986 status_item_list
[SI_BITRATE
],
987 status_item_list
[SI_FORMAT
],
988 status_item_list
[SI_FREQUENCY
],
989 status_item_list
[SI_CHANNELS
],
990 status_item_list
[SI_DURATION
],
991 status_item_list
[SI_SECONDS_TOTAL
],
992 status_item_list
[SI_NUM_PLAYED
],
993 status_item_list
[SI_LAST_PLAYED
]
997 int make_status_items(struct ls_data
*d
, struct para_buffer
*pb
)
999 struct ls_options opts
= {
1000 .flags
= LS_FLAG_FULL_PATH
| LS_FLAG_ADMISSIBLE_ONLY
,
1001 .mode
= LS_MODE_VERBOSE
,
1003 time_t current_time
;
1005 time(¤t_time
);
1006 return print_list_item(d
, &opts
, pb
, current_time
);
1010 static int ls_audio_format_compare(const void *a
, const void *b
)
1012 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1013 return NUM_COMPARE(d1
->afsi
.audio_format_id
, d2
->afsi
.audio_format_id
);
1016 static int ls_duration_compare(const void *a
, const void *b
)
1018 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1019 return NUM_COMPARE(d1
->afhi
.seconds_total
, d2
->afhi
.seconds_total
);
1022 static int ls_bitrate_compare(const void *a
, const void *b
)
1024 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1025 return NUM_COMPARE(d1
->afhi
.bitrate
, d2
->afhi
.bitrate
);
1028 static int ls_lyrics_id_compare(const void *a
, const void *b
)
1030 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1031 return NUM_COMPARE(d1
->afsi
.lyrics_id
, d2
->afsi
.lyrics_id
);
1034 static int ls_image_id_compare(const void *a
, const void *b
)
1036 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1037 return NUM_COMPARE(d1
->afsi
.image_id
, d2
->afsi
.image_id
);
1040 static int ls_channels_compare(const void *a
, const void *b
)
1042 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1043 return NUM_COMPARE(d1
->afhi
.channels
, d2
->afhi
.channels
);
1046 static int ls_frequency_compare(const void *a
, const void *b
)
1048 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1049 return NUM_COMPARE(d1
->afhi
.frequency
, d2
->afhi
.frequency
);
1052 static int ls_num_played_compare(const void *a
, const void *b
)
1054 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1055 return NUM_COMPARE(d1
->afsi
.num_played
, d2
->afsi
.num_played
);
1058 static int ls_last_played_compare(const void *a
, const void *b
)
1060 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1061 return NUM_COMPARE(d1
->afsi
.last_played
, d2
->afsi
.last_played
);
1064 static int ls_score_compare(const void *a
, const void *b
)
1066 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1067 return NUM_COMPARE(d1
->score
, d2
->score
);
1070 static int ls_path_compare(const void *a
, const void *b
)
1072 struct ls_data
*d1
= *(struct ls_data
**)a
, *d2
= *(struct ls_data
**)b
;
1073 return strcmp(d1
->path
, d2
->path
);
1076 static int sort_matching_paths(struct ls_options
*options
)
1078 size_t nmemb
= options
->num_matching_paths
;
1079 size_t size
= sizeof(*options
->data_ptr
);
1080 int (*compar
)(const void *, const void *);
1083 options
->data_ptr
= para_malloc(nmemb
* sizeof(*options
->data_ptr
));
1084 for (i
= 0; i
< nmemb
; i
++)
1085 options
->data_ptr
[i
] = options
->data
+ i
;
1087 /* In these cases the array is already sorted */
1088 if (options
->sorting
== LS_SORT_BY_PATH
1089 && !(options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1090 && (options
->flags
& LS_FLAG_FULL_PATH
))
1092 if (options
->sorting
== LS_SORT_BY_SCORE
&&
1093 options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1096 switch (options
->sorting
) {
1097 case LS_SORT_BY_PATH
:
1098 compar
= ls_path_compare
; break;
1099 case LS_SORT_BY_SCORE
:
1100 compar
= ls_score_compare
; break;
1101 case LS_SORT_BY_LAST_PLAYED
:
1102 compar
= ls_last_played_compare
; break;
1103 case LS_SORT_BY_NUM_PLAYED
:
1104 compar
= ls_num_played_compare
; break;
1105 case LS_SORT_BY_FREQUENCY
:
1106 compar
= ls_frequency_compare
; break;
1107 case LS_SORT_BY_CHANNELS
:
1108 compar
= ls_channels_compare
; break;
1109 case LS_SORT_BY_IMAGE_ID
:
1110 compar
= ls_image_id_compare
; break;
1111 case LS_SORT_BY_LYRICS_ID
:
1112 compar
= ls_lyrics_id_compare
; break;
1113 case LS_SORT_BY_BITRATE
:
1114 compar
= ls_bitrate_compare
; break;
1115 case LS_SORT_BY_DURATION
:
1116 compar
= ls_duration_compare
; break;
1117 case LS_SORT_BY_AUDIO_FORMAT
:
1118 compar
= ls_audio_format_compare
; break;
1122 qsort(options
->data_ptr
, nmemb
, size
, compar
);
1126 /* row is either an aft_row or a row of the score table */
1127 /* TODO: Only compute widths if we need them */
1128 static int prepare_ls_row(struct osl_row
*row
, void *ls_opts
)
1131 struct ls_options
*options
= ls_opts
;
1133 struct ls_widths
*w
;
1134 unsigned short num_digits
;
1136 struct osl_row
*aft_row
;
1140 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1141 ret
= get_score_and_aft_row(row
, &score
, &aft_row
);
1146 ret
= get_audio_file_path_of_row(aft_row
, &path
);
1149 if (!(options
->flags
& LS_FLAG_FULL_PATH
)) {
1150 char *p
= strrchr(path
, '/');
1154 if (options
->num_patterns
) {
1155 for (i
= 0; i
< options
->num_patterns
; i
++) {
1156 ret
= fnmatch(options
->patterns
[i
], path
, 0);
1159 if (ret
== FNM_NOMATCH
)
1163 if (i
>= options
->num_patterns
) /* no match */
1166 tmp
= options
->num_matching_paths
++;
1167 if (options
->num_matching_paths
> options
->array_size
) {
1168 options
->array_size
++;
1169 options
->array_size
*= 2;
1170 options
->data
= para_realloc(options
->data
, options
->array_size
1171 * sizeof(*options
->data
));
1173 d
= options
->data
+ tmp
;
1174 ret
= get_afsi_of_row(aft_row
, &d
->afsi
);
1177 ret
= get_afhi_of_row(aft_row
, &d
->afhi
);
1181 ret
= get_hash_of_row(aft_row
, &d
->hash
);
1184 w
= &options
->widths
;
1185 GET_NUM_DIGITS(d
->afsi
.image_id
, &num_digits
);
1186 w
->image_id_width
= PARA_MAX(w
->image_id_width
, num_digits
);
1187 GET_NUM_DIGITS(d
->afsi
.lyrics_id
, &num_digits
);
1188 w
->lyrics_id_width
= PARA_MAX(w
->lyrics_id_width
, num_digits
);
1189 GET_NUM_DIGITS(d
->afhi
.bitrate
, &num_digits
);
1190 w
->bitrate_width
= PARA_MAX(w
->bitrate_width
, num_digits
);
1191 GET_NUM_DIGITS(d
->afhi
.frequency
, &num_digits
);
1192 w
->frequency_width
= PARA_MAX(w
->frequency_width
, num_digits
);
1193 GET_NUM_DIGITS(d
->afsi
.num_played
, &num_digits
);
1194 w
->num_played_width
= PARA_MAX(w
->num_played_width
, num_digits
);
1195 /* get the number of chars to print this amount of time */
1196 tmp
= get_duration_width(d
->afhi
.seconds_total
);
1197 w
->duration_width
= PARA_MAX(w
->duration_width
, tmp
);
1198 if (options
->flags
& LS_FLAG_ADMISSIBLE_ONLY
) {
1199 GET_NUM_DIGITS(score
, &num_digits
);
1200 num_digits
++; /* add one for the sign (space or "-") */
1201 w
->score_width
= PARA_MAX(w
->score_width
, num_digits
);
1207 static int com_ls_callback(const struct osl_object
*query
,
1208 struct osl_object
*ls_output
)
1210 struct ls_options
*opts
= query
->data
;
1211 char *p
, *pattern_start
= (char *)query
->data
+ sizeof(*opts
);
1212 struct para_buffer b
= {.buf
= NULL
, .size
= 0};
1214 time_t current_time
;
1217 if (opts
->num_patterns
) {
1218 opts
->patterns
= para_malloc(opts
->num_patterns
* sizeof(char *));
1219 for (i
= 0, p
= pattern_start
; i
< opts
->num_patterns
; i
++) {
1220 opts
->patterns
[i
] = p
;
1224 opts
->patterns
= NULL
;
1225 if (opts
->flags
& LS_FLAG_ADMISSIBLE_ONLY
)
1226 ret
= admissible_file_loop(opts
, prepare_ls_row
);
1228 ret
= osl_rbtree_loop(audio_file_table
, AFTCOL_PATH
, opts
,
1232 ret
= opts
->num_patterns
? -E_NO_MATCH
: 0;
1233 if (!opts
->num_matching_paths
)
1235 ret
= sort_matching_paths(opts
);
1238 time(¤t_time
);
1239 if (opts
->flags
& LS_FLAG_REVERSE
)
1240 for (i
= opts
->num_matching_paths
- 1; i
>= 0; i
--) {
1241 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1246 for (i
= 0; i
< opts
->num_matching_paths
; i
++) {
1247 ret
= print_list_item(opts
->data_ptr
[i
], opts
, &b
, current_time
);
1253 ls_output
->data
= b
.buf
;
1254 ls_output
->size
= b
.size
;
1256 free(opts
->data_ptr
);
1257 free(opts
->patterns
);
1262 * TODO: flags -h (sort by hash)
1264 int com_ls(int fd
, int argc
, char * const * const argv
)
1268 enum ls_sorting_method sort
= LS_SORT_BY_PATH
;
1269 enum ls_listing_mode mode
= LS_MODE_SHORT
;
1270 struct ls_options opts
= {.patterns
= NULL
};
1271 struct osl_object query
= {.data
= &opts
, .size
= sizeof(opts
)},
1274 for (i
= 1; i
< argc
; i
++) {
1275 const char *arg
= argv
[i
];
1278 if (!strcmp(arg
, "--")) {
1282 if (!strncmp(arg
, "-l", 2)) {
1284 mode
= LS_MODE_LONG
;
1288 return -E_AFT_SYNTAX
;
1289 switch(*(arg
+ 2)) {
1291 mode
= LS_MODE_SHORT
;
1294 mode
= LS_MODE_LONG
;
1297 mode
= LS_MODE_VERBOSE
;
1300 mode
= LS_MODE_MBOX
;
1303 return -E_AFT_SYNTAX
;
1306 if (!strcmp(arg
, "-p")) {
1307 flags
|= LS_FLAG_FULL_PATH
;
1310 if (!strcmp(arg
, "-a")) {
1311 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1314 if (!strcmp(arg
, "-r")) {
1315 flags
|= LS_FLAG_REVERSE
;
1318 if (!strncmp(arg
, "-s", 2)) {
1319 if (!*(arg
+ 2) || *(arg
+ 3))
1320 return -E_AFT_SYNTAX
;
1321 switch(*(arg
+ 2)) {
1323 sort
= LS_SORT_BY_PATH
;
1325 case 's': /* -ss implies -a */
1326 sort
= LS_SORT_BY_SCORE
;
1327 flags
|= LS_FLAG_ADMISSIBLE_ONLY
;
1330 sort
= LS_SORT_BY_LAST_PLAYED
;
1333 sort
= LS_SORT_BY_NUM_PLAYED
;
1336 sort
= LS_SORT_BY_FREQUENCY
;
1339 sort
= LS_SORT_BY_CHANNELS
;
1342 sort
= LS_SORT_BY_IMAGE_ID
;
1345 sort
= LS_SORT_BY_LYRICS_ID
;
1348 sort
= LS_SORT_BY_BITRATE
;
1351 sort
= LS_SORT_BY_DURATION
;
1354 sort
= LS_SORT_BY_AUDIO_FORMAT
;
1357 return -E_AFT_SYNTAX
;
1360 return -E_AFT_SYNTAX
;
1363 opts
.sorting
= sort
;
1365 opts
.num_patterns
= argc
- i
;
1366 ret
= send_option_arg_callback_request(&query
, opts
.num_patterns
,
1367 argv
+ i
, com_ls_callback
, &ls_output
);
1369 ret
= send_buffer(fd
, (char *)ls_output
.data
);
1370 free(ls_output
.data
);
1376 * Call the given function for each file in the audio file table.
1378 * \param private_data An arbitrary data pointer, passed to \a func.
1379 * \param func The custom function to be called.
1381 * \return The return value of the underlying call to osl_rbtree_loop().
1383 int audio_file_loop(void *private_data
, osl_rbtree_loop_func
*func
)
1385 return osl_rbtree_loop(audio_file_table
, AFTCOL_HASH
, private_data
,
1389 static struct osl_row
*find_hash_sister(HASH_TYPE
*hash
)
1391 const struct osl_object obj
= {.data
= hash
, .size
= HASH_SIZE
};
1392 struct osl_row
*row
;
1394 osl_get_row(audio_file_table
, AFTCOL_HASH
, &obj
, &row
);
1398 /** The format of the data stored by save_audio_file_data(). */
1399 enum com_add_buffer_offsets
{
1400 /** afhi (if present) starts here. */
1401 CAB_AFHI_OFFSET_POS
= 0,
1402 /** Start of the chunk table (if present). */
1403 CAB_CHUNKS_OFFSET_POS
= 2,
1404 /** Audio format id. */
1405 CAB_AUDIO_FORMAT_OFFSET
= 4,
1406 /** Flags given to the add command. */
1407 CAB_FLAGS_OFFSET
= 5,
1408 /** The hash of the audio file being added. */
1409 CAB_HASH_OFFSET
= 9,
1410 /** Start of the path of the audio file. */
1411 CAB_PATH_OFFSET
= (CAB_HASH_OFFSET
+ HASH_SIZE
),
1415 * Store the given data to a single buffer. Doesn't need the audio file selector
1416 * info struct as the server knows it as well.
1418 * It's OK to call this with afhi == NULL. In this case, the audio format
1419 * handler info won't be stored in the buffer.
1421 static void save_add_callback_buffer(HASH_TYPE
*hash
, const char *path
,
1422 struct afh_info
*afhi
, uint32_t flags
,
1423 uint8_t audio_format_num
, struct osl_object
*obj
)
1425 size_t path_len
= strlen(path
) + 1;
1426 size_t afhi_size
= sizeof_afhi_buf(afhi
);
1427 size_t size
= CAB_PATH_OFFSET
+ path_len
+ afhi_size
1428 + sizeof_chunk_table(afhi
);
1429 char *buf
= para_malloc(size
);
1432 write_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
, audio_format_num
);
1433 write_u32(buf
+ CAB_FLAGS_OFFSET
, flags
);
1435 memcpy(buf
+ CAB_HASH_OFFSET
, hash
, HASH_SIZE
);
1436 strcpy(buf
+ CAB_PATH_OFFSET
, path
);
1438 pos
= CAB_PATH_OFFSET
+ path_len
;
1439 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size
, pos
);
1440 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf
+ pos
+ afhi_size
- 1,
1441 pos
+ afhi_size
- 1);
1442 write_u16(buf
+ CAB_AFHI_OFFSET_POS
, pos
);
1443 save_afhi(afhi
, buf
+ pos
);
1446 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size
, pos
);
1447 write_u16(buf
+ CAB_CHUNKS_OFFSET_POS
, pos
);
1449 save_chunk_table(afhi
, buf
+ pos
);
1450 PARA_DEBUG_LOG("last byte in buf: %p\n", buf
+ size
- 1);
1457 Overview of the add command.
1459 Input: What was passed to the callback by the command handler.
1461 HS: Hash sister. Whether an audio file with identical hash
1462 already exists in the osl database.
1464 PB: Path brother. Whether a file with the given path exists
1467 F: Force flag given. Whether add was called with -f.
1469 output: Action performed by the callback.
1471 AFHI: Whether afhi and chunk table are computed and sent.
1472 ACTION: Table modifications to be done by the callback.
1474 +----+----+---+------+---------------------------------------------------+
1475 | HS | PB | F | AFHI | ACTION
1476 +----+----+---+------+---------------------------------------------------+
1477 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1478 | | update path, keep afsi
1479 +----+----+---+------+---------------------------------------------------+
1480 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1481 | | otherwise: remove PB, HS: update path, keep afhi,
1483 +----+----+---+------+---------------------------------------------------+
1484 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1486 +----+----+---+------+---------------------------------------------------+
1487 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1488 +----+----+---+------+---------------------------------------------------+
1489 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1490 | | (force has no effect)
1491 +----+----+---+------+---------------------------------------------------+
1492 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1493 +----+----+---+------+---------------------------------------------------+
1494 | N | N | Y | Y | (new file) create new entry (force has no effect)
1495 +----+----+---+------+---------------------------------------------------+
1496 | N | N | N | Y | (new file) create new entry
1497 +----+----+---+------+---------------------------------------------------+
1501 afhi <=> force or no HS
1506 /** Flags passed to the add command. */
1507 enum com_add_flags
{
1508 /** Skip paths that exist already. */
1510 /** Force adding. */
1512 /** Print what is being done. */
1513 ADD_FLAG_VERBOSE
= 4,
1514 /** Try to add files with unknown suffixes. */
1518 static int com_add_callback(const struct osl_object
*query
,
1519 struct osl_object
*result
)
1521 char *buf
= query
->data
, *path
;
1522 struct osl_row
*pb
, *aft_row
;
1524 struct osl_object objs
[NUM_AFT_COLUMNS
];
1526 char asc
[2 * HASH_SIZE
+ 1];
1528 char afsi_buf
[AFSI_SIZE
];
1529 uint32_t flags
= read_u32(buf
+ CAB_FLAGS_OFFSET
);
1530 struct afs_info default_afsi
= {.last_played
= 0};
1531 struct para_buffer msg
= {.buf
= NULL
};
1533 hash
= (HASH_TYPE
*)buf
+ CAB_HASH_OFFSET
;
1534 hash_to_asc(hash
, asc
);;
1535 objs
[AFTCOL_HASH
].data
= buf
+ CAB_HASH_OFFSET
;
1536 objs
[AFTCOL_HASH
].size
= HASH_SIZE
;
1538 path
= buf
+ CAB_PATH_OFFSET
;
1539 objs
[AFTCOL_PATH
].data
= path
;
1540 objs
[AFTCOL_PATH
].size
= strlen(path
) + 1;
1542 PARA_INFO_LOG("request to add %s\n", path
);
1543 hs
= find_hash_sister(hash
);
1544 ret
= aft_get_row_of_path(path
, &pb
);
1545 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1547 if (hs
&& pb
&& hs
== pb
&& !(flags
& ADD_FLAG_FORCE
)) {
1548 if (flags
& ADD_FLAG_VERBOSE
)
1549 para_printf(&msg
, "ignoring duplicate\n");
1553 if (hs
&& hs
!= pb
) {
1554 struct osl_object obj
;
1555 if (pb
) { /* hs trumps pb, remove pb */
1556 if (flags
& ADD_FLAG_VERBOSE
)
1557 para_printf(&msg
, "removing path brother\n");
1558 ret
= osl_del_row(audio_file_table
, pb
);
1563 /* file rename, update hs' path */
1564 if (flags
& ADD_FLAG_VERBOSE
) {
1565 ret
= osl_get_object(audio_file_table
, hs
,
1569 para_printf(&msg
, "renamed from %s\n", (char *)obj
.data
);
1571 ret
= osl_update_object(audio_file_table
, hs
, AFTCOL_PATH
,
1572 &objs
[AFTCOL_PATH
]);
1575 afs_event(AUDIO_FILE_RENAME
, &msg
, hs
);
1576 if (!(flags
& ADD_FLAG_FORCE
))
1579 /* no hs or force mode, child must have sent afhi */
1580 uint16_t afhi_offset
= read_u16(buf
+ CAB_AFHI_OFFSET_POS
);
1581 uint16_t chunks_offset
= read_u16(buf
+ CAB_CHUNKS_OFFSET_POS
);
1583 objs
[AFTCOL_AFHI
].data
= buf
+ afhi_offset
;
1584 objs
[AFTCOL_AFHI
].size
= chunks_offset
- afhi_offset
;
1586 if (!objs
[AFTCOL_AFHI
].size
) /* "impossible" */
1588 objs
[AFTCOL_CHUNKS
].data
= buf
+ chunks_offset
;
1589 objs
[AFTCOL_CHUNKS
].size
= query
->size
- chunks_offset
;
1590 if (pb
&& !hs
) { /* update pb's hash */
1591 char old_asc
[2 * HASH_SIZE
+ 1];
1592 HASH_TYPE
*old_hash
;
1593 ret
= get_hash_of_row(pb
, &old_hash
);
1596 hash_to_asc(old_hash
, old_asc
);
1597 if (flags
& ADD_FLAG_VERBOSE
)
1598 para_printf(&msg
, "file change: %s -> %s\n",
1600 ret
= osl_update_object(audio_file_table
, pb
, AFTCOL_HASH
,
1601 &objs
[AFTCOL_HASH
]);
1605 if (hs
|| pb
) { /* (hs != NULL and pb != NULL) implies hs == pb */
1606 struct osl_row
*row
= pb
? pb
: hs
;
1607 /* update afhi and chunk_table */
1608 if (flags
& ADD_FLAG_VERBOSE
)
1609 para_printf(&msg
, "updating afhi and chunk table\n");
1610 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_AFHI
,
1611 &objs
[AFTCOL_AFHI
]);
1614 ret
= osl_update_object(audio_file_table
, row
, AFTCOL_CHUNKS
,
1615 &objs
[AFTCOL_CHUNKS
]);
1618 afs_event(AFHI_CHANGE
, &msg
, row
);
1621 /* new entry, use default afsi */
1622 if (flags
& ADD_FLAG_VERBOSE
)
1623 para_printf(&msg
, "new file\n");
1624 default_afsi
.last_played
= time(NULL
) - 365 * 24 * 60 * 60;
1625 default_afsi
.audio_format_id
= read_u8(buf
+ CAB_AUDIO_FORMAT_OFFSET
);
1627 objs
[AFTCOL_AFSI
].data
= &afsi_buf
;
1628 objs
[AFTCOL_AFSI
].size
= AFSI_SIZE
;
1629 save_afsi(&default_afsi
, &objs
[AFTCOL_AFSI
]);
1630 ret
= osl_add_and_get_row(audio_file_table
, objs
, &aft_row
);
1631 afs_event(AUDIO_FILE_ADD
, &msg
, aft_row
);
1634 para_printf(&msg
, "%s\n", para_strerror(-ret
));
1637 result
->data
= msg
.buf
;
1638 result
->size
= msg
.size
;
1642 /** Used by com_add(). */
1643 struct private_add_data
{
1644 /** The socket file descriptor. */
1646 /** The given add flags. */
1650 static int path_brother_callback(const struct osl_object
*query
,
1651 struct osl_object
*result
)
1653 char *path
= query
->data
;
1654 struct osl_row
*path_brother
;
1655 int ret
= aft_get_row_of_path(path
, &path_brother
);
1658 result
->data
= para_malloc(sizeof(path_brother
));
1659 result
->size
= sizeof(path_brother
);
1660 *(struct osl_row
**)(result
->data
) = path_brother
;
1664 static int hash_sister_callback(const struct osl_object
*query
,
1665 struct osl_object
*result
)
1667 HASH_TYPE
*hash
= query
->data
;
1668 struct osl_row
*hash_sister
;
1670 hash_sister
= find_hash_sister(hash
);
1672 return -E_RB_KEY_NOT_FOUND
;
1673 result
->data
= para_malloc(sizeof(hash_sister
));
1674 result
->size
= sizeof(hash_sister
);
1675 *(struct osl_row
**)(result
->data
) = hash_sister
;
1679 static int add_one_audio_file(const char *path
, const void *private_data
)
1681 int ret
, send_ret
= 1;
1682 uint8_t format_num
= -1;
1683 const struct private_add_data
*pad
= private_data
;
1684 struct afh_info afhi
, *afhi_ptr
= NULL
;
1685 struct osl_row
*pb
= NULL
, *hs
= NULL
; /* path brother/hash sister */
1686 struct osl_object map
, obj
= {.data
= NULL
}, query
, result
= {.data
= NULL
};
1687 HASH_TYPE hash
[HASH_SIZE
];
1689 afhi
.header_offset
= 0;
1690 afhi
.header_len
= 0;
1691 ret
= guess_audio_format(path
);
1692 if (ret
< 0 && !(pad
->flags
& ADD_FLAG_ALL
))
1694 query
.data
= (char *)path
;
1695 query
.size
= strlen(path
) + 1;
1696 ret
= send_callback_request(path_brother_callback
, &query
, &result
);
1697 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1700 pb
= *(struct osl_row
**)result
.data
;
1704 if (pb
&& (pad
->flags
& ADD_FLAG_LAZY
)) { /* lazy is really cheap */
1705 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1706 send_ret
= send_va_buffer(pad
->fd
, "lazy-ignore: %s\n", path
);
1709 /* We still want to add this file. Compute its hash. */
1710 ret
= mmap_full_file(path
, O_RDONLY
, &map
.data
, &map
.size
, NULL
);
1713 hash_function(map
.data
, map
.size
, hash
);
1715 /* Check whether the database contains a file with the same hash. */
1717 query
.size
= HASH_SIZE
;
1718 ret
= send_callback_request(hash_sister_callback
, &query
, &result
);
1719 if (ret
< 0 && ret
!= -E_RB_KEY_NOT_FOUND
)
1722 hs
= *(struct osl_row
**)result
.data
;
1725 /* Return success if we already know this file. */
1727 if (pb
&& hs
&& hs
== pb
&& !(pad
->flags
& ADD_FLAG_FORCE
)) {
1728 if (pad
->flags
& ADD_FLAG_VERBOSE
)
1729 send_ret
= send_va_buffer(pad
->fd
,
1730 "%s exists, not forcing update\n", path
);
1734 * We won't recalculate the audio format info and the chunk table if
1735 * there is a hash sister and FORCE was not given.
1737 if (!hs
|| (pad
->flags
& ADD_FLAG_FORCE
)) {
1738 ret
= compute_afhi(path
, map
.data
, map
.size
, &afhi
);
1744 munmap(map
.data
, map
.size
);
1745 if (pad
->flags
& ADD_FLAG_VERBOSE
) {
1746 send_ret
= send_va_buffer(pad
->fd
, "adding %s\n", path
);
1750 save_add_callback_buffer(hash
, path
, afhi_ptr
, pad
->flags
, format_num
, &obj
);
1751 /* Ask afs to consider this entry for adding. */
1752 ret
= send_callback_request(com_add_callback
, &obj
, &result
);
1754 send_ret
= send_va_buffer(pad
->fd
, "%s", (char *)result
.data
);
1760 munmap(map
.data
, map
.size
);
1762 if (ret
< 0 && send_ret
>= 0)
1763 send_ret
= send_va_buffer(pad
->fd
, "failed to add %s (%s)\n", path
,
1764 para_strerror(-ret
));
1767 free(afhi_ptr
->chunk_table
);
1768 /* Stop adding files only on send errors. */
1772 int com_add(int fd
, int argc
, char * const * const argv
)
1775 struct private_add_data pad
= {.fd
= fd
, .flags
= 0};
1776 struct stat statbuf
;
1778 for (i
= 1; i
< argc
; i
++) {
1779 const char *arg
= argv
[i
];
1782 if (!strcmp(arg
, "--")) {
1786 if (!strcmp(arg
, "-a")) {
1787 pad
.flags
|= ADD_FLAG_ALL
;
1790 if (!strcmp(arg
, "-l")) {
1791 pad
.flags
|= ADD_FLAG_LAZY
;
1794 if (!strcmp(arg
, "-f")) {
1795 pad
.flags
|= ADD_FLAG_FORCE
;
1798 if (!strcmp(arg
, "-v")) {
1799 pad
.flags
|= ADD_FLAG_VERBOSE
;
1804 return -E_AFT_SYNTAX
;
1805 for (; i
< argc
; i
++) {
1807 ret
= verify_path(argv
[i
], &path
);
1809 ret
= send_va_buffer(fd
, "%s: %s\n", argv
[i
],
1810 para_strerror(-ret
));
1815 ret
= stat(path
, &statbuf
);
1817 ret
= send_va_buffer(fd
, "failed to stat %s (%s)\n", path
,
1824 if (S_ISDIR(statbuf
.st_mode
))
1825 ret
= for_each_file_in_dir(path
, add_one_audio_file
,
1828 ret
= add_one_audio_file(path
, &pad
);
1830 send_va_buffer(fd
, "%s: %s\n", path
, para_strerror(-ret
));
1841 * Flags used by the touch command.
1846 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1847 TOUCH_FLAG_FNM_PATHNAME
= 1,
1848 /** Activates verbose mode. */
1849 TOUCH_FLAG_VERBOSE
= 2
1852 /** Options used by com_touch(). */
1853 struct com_touch_options
{
1854 /** New num_played value. */
1856 /** New last played count. */
1857 int64_t last_played
;
1858 /** new lyrics id. */
1860 /** new image id. */
1862 /** command line flags (see \ref touch_flags). */
1866 /** Data passed to the action handler of com_touch(). */
1867 struct touch_action_data
{
1868 /** Command line options (see \ref com_touch_options). */
1869 struct com_touch_options
*cto
;
1870 /** Message buffer. */
1871 struct para_buffer pb
;
1874 static int touch_audio_file(__a_unused
struct osl_table
*table
,
1875 struct osl_row
*row
, const char *name
, void *data
)
1877 struct touch_action_data
*tad
= data
;
1878 struct osl_object obj
;
1879 struct afs_info old_afsi
, new_afsi
;
1880 int ret
, no_options
= tad
->cto
->num_played
< 0 && tad
->cto
->last_played
< 0 &&
1881 tad
->cto
->lyrics_id
< 0 && tad
->cto
->image_id
< 0;
1882 struct afsi_change_event_data aced
;
1884 ret
= get_afsi_object_of_row(row
, &obj
);
1886 para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1889 ret
= load_afsi(&old_afsi
, &obj
);
1891 para_printf(&tad
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
1894 new_afsi
= old_afsi
;
1896 new_afsi
.num_played
++;
1897 new_afsi
.last_played
= time(NULL
);
1898 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1899 para_printf(&tad
->pb
, "%s: num_played = %u, "
1900 "last_played = now()\n", name
,
1901 new_afsi
.num_played
);
1903 if (tad
->cto
->flags
& TOUCH_FLAG_VERBOSE
)
1904 para_printf(&tad
->pb
, "touching %s\n", name
);
1905 if (tad
->cto
->lyrics_id
>= 0)
1906 new_afsi
.lyrics_id
= tad
->cto
->lyrics_id
;
1907 if (tad
->cto
->image_id
>= 0)
1908 new_afsi
.image_id
= tad
->cto
->image_id
;
1909 if (tad
->cto
->num_played
>= 0)
1910 new_afsi
.num_played
= tad
->cto
->num_played
;
1911 if (tad
->cto
->last_played
>= 0)
1912 new_afsi
.last_played
= tad
->cto
->last_played
;
1914 save_afsi(&new_afsi
, &obj
); /* in-place update */
1916 aced
.old_afsi
= &old_afsi
;
1917 afs_event(AFSI_CHANGE
, &tad
->pb
, &aced
);
1921 static int com_touch_callback(const struct osl_object
*query
,
1922 struct osl_object
*result
)
1924 struct touch_action_data tad
= {.cto
= query
->data
};
1926 struct pattern_match_data pmd
= {
1927 .table
= audio_file_table
,
1928 .loop_col_num
= AFTCOL_HASH
,
1929 .match_col_num
= AFTCOL_PATH
,
1930 .patterns
= {.data
= (char *)query
->data
+ sizeof(*tad
.cto
),
1931 .size
= query
->size
- sizeof(*tad
.cto
)},
1933 .action
= touch_audio_file
1935 if (tad
.cto
->flags
& TOUCH_FLAG_FNM_PATHNAME
)
1936 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
1937 ret
= for_each_matching_row(&pmd
);
1939 para_printf(&tad
.pb
, "%s\n", para_strerror(-ret
));
1941 result
->data
= tad
.pb
.buf
;
1942 result
->size
= tad
.pb
.size
;
1945 return ret
< 0? ret
: 0;
1948 int com_touch(int fd
, int argc
, char * const * const argv
)
1950 struct com_touch_options cto
= {
1956 struct osl_object query
= {.data
= &cto
, .size
= sizeof(cto
)},
1961 for (i
= 1; i
< argc
; i
++) {
1962 const char *arg
= argv
[i
];
1965 if (!strcmp(arg
, "--")) {
1969 if (!strncmp(arg
, "-n", 2)) {
1970 ret
= para_atoi32(arg
+ 2, &cto
.num_played
);
1975 if (!strncmp(arg
, "-l", 2)) {
1976 ret
= para_atoi64(arg
+ 2, &cto
.last_played
);
1981 if (!strncmp(arg
, "-y", 2)) {
1982 ret
= para_atoi32(arg
+ 2, &cto
.lyrics_id
);
1987 if (!strncmp(arg
, "-i", 2)) {
1988 ret
= para_atoi32(arg
+ 2, &cto
.image_id
);
1993 if (!strcmp(arg
, "-p")) {
1994 cto
.flags
|= TOUCH_FLAG_FNM_PATHNAME
;
1997 if (!strcmp(arg
, "-v")) {
1998 cto
.flags
|= TOUCH_FLAG_VERBOSE
;
2001 break; /* non-option starting with dash */
2004 return -E_AFT_SYNTAX
;
2005 ret
= send_option_arg_callback_request(&query
, argc
- i
,
2006 argv
+ i
, com_touch_callback
, &result
);
2008 send_buffer(fd
, (char *)result
.data
);
2011 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2015 /** Flags for com_rm(). */
2018 RM_FLAG_VERBOSE
= 1,
2022 RM_FLAG_FNM_PATHNAME
= 4
2025 /** Data passed to the action handler of com_rm(). */
2026 struct com_rm_action_data
{
2027 /** Command line flags ((see \ref rm_flags). */
2029 /** Message buffer. */
2030 struct para_buffer pb
;
2031 /** Number of audio files removed. */
2032 unsigned num_removed
;
2035 static int remove_audio_file(__a_unused
struct osl_table
*table
,
2036 struct osl_row
*row
, const char *name
, void *data
)
2038 struct com_rm_action_data
*crd
= data
;
2041 if (crd
->flags
& RM_FLAG_VERBOSE
)
2042 para_printf(&crd
->pb
, "removing %s\n", name
);
2043 afs_event(AUDIO_FILE_REMOVE
, &crd
->pb
, row
);
2044 ret
= osl_del_row(audio_file_table
, row
);
2046 para_printf(&crd
->pb
, "%s: %s\n", name
, para_strerror(-ret
));
2052 static int com_rm_callback(const struct osl_object
*query
,
2053 __a_unused
struct osl_object
*result
)
2055 struct com_rm_action_data crd
= {.flags
= *(uint32_t *)query
->data
};
2057 struct pattern_match_data pmd
= {
2058 .table
= audio_file_table
,
2059 .loop_col_num
= AFTCOL_HASH
,
2060 .match_col_num
= AFTCOL_PATH
,
2061 .patterns
= {.data
= (char *)query
->data
+ sizeof(uint32_t),
2062 .size
= query
->size
- sizeof(uint32_t)},
2064 .action
= remove_audio_file
2066 if (crd
.flags
& RM_FLAG_FNM_PATHNAME
)
2067 pmd
.fnmatch_flags
|= FNM_PATHNAME
;
2068 ret
= for_each_matching_row(&pmd
);
2070 para_printf(&crd
.pb
, "%s\n", para_strerror(-ret
));
2071 if (!crd
.num_removed
&& !(crd
.flags
& RM_FLAG_FORCE
))
2072 para_printf(&crd
.pb
, "no matches -- nothing removed\n");
2074 if (crd
.flags
& RM_FLAG_VERBOSE
)
2075 para_printf(&crd
.pb
, "removed %u files\n", crd
.num_removed
);
2078 result
->data
= crd
.pb
.buf
;
2079 result
->size
= crd
.pb
.size
;
2082 return ret
< 0? ret
: 0;
2085 /* TODO options: -r (recursive) */
2086 int com_rm(int fd
, int argc
, char * const * const argv
)
2089 struct osl_object query
= {.data
= &flags
, .size
= sizeof(flags
)},
2093 for (i
= 1; i
< argc
; i
++) {
2094 const char *arg
= argv
[i
];
2097 if (!strcmp(arg
, "--")) {
2101 if (!strcmp(arg
, "-f")) {
2102 flags
|= RM_FLAG_FORCE
;
2105 if (!strcmp(arg
, "-p")) {
2106 flags
|= RM_FLAG_FNM_PATHNAME
;
2109 if (!strcmp(arg
, "-v")) {
2110 flags
|= RM_FLAG_VERBOSE
;
2116 return -E_AFT_SYNTAX
;
2117 ret
= send_option_arg_callback_request(&query
, argc
- i
, argv
+ i
,
2118 com_rm_callback
, &result
);
2120 send_buffer(fd
, (char *)result
.data
);
2123 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2128 * Flags used by the cpsi command.
2133 /** Whether the lyrics id should be copied. */
2134 CPSI_FLAG_COPY_LYRICS_ID
= 1,
2135 /** Whether the image id should be copied. */
2136 CPSI_FLAG_COPY_IMAGE_ID
= 2,
2137 /** Whether the lastplayed time should be copied. */
2138 CPSI_FLAG_COPY_LASTPLAYED
= 4,
2139 /** Whether the numplayed count should be copied. */
2140 CPSI_FLAG_COPY_NUMPLAYED
= 8,
2141 /** Whether the attributes should be copied. */
2142 CPSI_FLAG_COPY_ATTRIBUTES
= 16,
2143 /** Activates verbose mode. */
2144 CPSI_FLAG_VERBOSE
= 32,
2147 /** Data passed to the action handler of com_cpsi(). */
2148 struct cpsi_action_data
{
2149 /** command line flags (see \ref cpsi_flags). */
2151 /** Number of audio files changed. */
2152 unsigned num_copied
;
2153 /** Message buffer. */
2154 struct para_buffer pb
;
2155 /** Values are copied from here. */
2156 struct afs_info source_afsi
;
2159 static int copy_selector_info(__a_unused
struct osl_table
*table
,
2160 struct osl_row
*row
, const char *name
, void *data
)
2162 struct cpsi_action_data
*cad
= data
;
2163 struct osl_object target_afsi_obj
;
2165 struct afs_info old_afsi
, target_afsi
;
2166 struct afsi_change_event_data aced
;
2168 ret
= get_afsi_object_of_row(row
, &target_afsi_obj
);
2171 load_afsi(&target_afsi
, &target_afsi_obj
);
2172 old_afsi
= target_afsi
;
2173 if (cad
->flags
& CPSI_FLAG_COPY_LYRICS_ID
)
2174 target_afsi
.lyrics_id
= cad
->source_afsi
.lyrics_id
;
2175 if (cad
->flags
& CPSI_FLAG_COPY_IMAGE_ID
)
2176 target_afsi
.image_id
= cad
->source_afsi
.image_id
;
2177 if (cad
->flags
& CPSI_FLAG_COPY_LASTPLAYED
)
2178 target_afsi
.last_played
= cad
->source_afsi
.last_played
;
2179 if (cad
->flags
& CPSI_FLAG_COPY_NUMPLAYED
)
2180 target_afsi
.num_played
= cad
->source_afsi
.num_played
;
2181 if (cad
->flags
& CPSI_FLAG_COPY_ATTRIBUTES
)
2182 target_afsi
.attributes
= cad
->source_afsi
.attributes
;
2183 save_afsi(&target_afsi
, &target_afsi_obj
); /* in-place update */
2185 if (cad
->flags
& CPSI_FLAG_VERBOSE
)
2186 para_printf(&cad
->pb
, "copied afsi to %s\n", name
);
2188 aced
.old_afsi
= &old_afsi
;
2189 afs_event(AFSI_CHANGE
, &cad
->pb
, &aced
);
2193 static int com_cpsi_callback(const struct osl_object
*query
,
2194 struct osl_object
*result
)
2196 struct cpsi_action_data cad
= {.flags
= *(unsigned *)query
->data
};
2198 char *source_path
= (char *)query
->data
+ sizeof(cad
.flags
);
2200 ret
= get_afsi_of_path(source_path
, &cad
.source_afsi
);
2203 struct pattern_match_data pmd
= {
2204 .table
= audio_file_table
,
2205 .loop_col_num
= AFTCOL_HASH
,
2206 .match_col_num
= AFTCOL_PATH
,
2207 .patterns
= {.data
= source_path
+ strlen(source_path
) + 1,
2208 .size
= query
->size
- sizeof(cad
.flags
)
2209 - strlen(source_path
) - 1},
2211 .action
= copy_selector_info
2213 ret
= for_each_matching_row(&pmd
);
2216 para_printf(&cad
.pb
, "%s\n", para_strerror(-ret
));
2217 if (cad
.flags
& CPSI_FLAG_VERBOSE
) {
2219 para_printf(&cad
.pb
, "copied requested afsi from %s "
2221 source_path
, cad
.num_copied
);
2223 para_printf(&cad
.pb
, "nothing copied\n");
2226 result
->data
= cad
.pb
.buf
;
2227 result
->size
= cad
.pb
.size
;
2230 return ret
< 0? ret
: 0;
2233 int com_cpsi(int fd
, int argc
, char * const * const argv
)
2237 struct osl_object options
= {.data
= &flags
, .size
= sizeof(flags
)},
2240 for (i
= 1; i
< argc
; i
++) {
2241 const char *arg
= argv
[i
];
2244 if (!strcmp(arg
, "--")) {
2248 if (!strcmp(arg
, "-y")) {
2249 flags
|= CPSI_FLAG_COPY_LYRICS_ID
;
2252 if (!strcmp(arg
, "-i")) {
2253 flags
|= CPSI_FLAG_COPY_IMAGE_ID
;
2256 if (!strcmp(arg
, "-l")) {
2257 flags
|= CPSI_FLAG_COPY_LASTPLAYED
;
2260 if (!strcmp(arg
, "-n")) {
2261 flags
|= CPSI_FLAG_COPY_NUMPLAYED
;
2264 if (!strcmp(arg
, "-a")) {
2265 flags
|= CPSI_FLAG_COPY_ATTRIBUTES
;
2268 if (!strcmp(arg
, "-v")) {
2269 flags
|= CPSI_FLAG_VERBOSE
;
2274 if (i
+ 1 >= argc
) /* need at least souce file and pattern */
2275 return -E_AFT_SYNTAX
;
2276 if (!(flags
& ~CPSI_FLAG_VERBOSE
)) /* no copy flags given */
2277 flags
= ~(unsigned)CPSI_FLAG_VERBOSE
| flags
;
2278 ret
= send_option_arg_callback_request(&options
, argc
- i
, argv
+ i
,
2279 com_cpsi_callback
, &result
);
2281 send_buffer(fd
, (char *)result
.data
);
2284 send_va_buffer(fd
, "%s\n", para_strerror(-ret
));
2288 /* TODO: optionally fix problems by removing offending rows */
2289 static int check_audio_file(struct osl_row
*row
, void *data
)
2292 struct para_buffer
*pb
= data
;
2293 struct stat statbuf
;
2294 int ret
= get_audio_file_path_of_row(row
, &path
);
2295 struct afs_info afsi
;
2299 para_printf(pb
, "%s\n", para_strerror(-ret
));
2302 if (stat(path
, &statbuf
) < 0)
2303 para_printf(pb
, "%s: stat error (%s)\n", path
, strerror(errno
));
2305 if (!S_ISREG(statbuf
.st_mode
))
2306 para_printf(pb
, "%s: not a regular file\n", path
);
2308 ret
= get_afsi_of_row(row
, &afsi
);
2310 para_printf(pb
, "%s: %s\n", path
, para_strerror(-ret
));
2313 ret
= lyr_get_name_by_id(afsi
.lyrics_id
, &blob_name
);
2315 para_printf(pb
, "%s lyrics id %u: %s\n", path
, afsi
.lyrics_id
,
2316 para_strerror(-ret
));
2317 ret
= img_get_name_by_id(afsi
.image_id
, &blob_name
);
2319 para_printf(pb
, "%s image id %u: %s\n", path
, afsi
.image_id
,
2320 para_strerror(-ret
));
2325 * Check the audio file table for inconsistencies.
2327 * \param query Unused.
2328 * \param result Contains message string upon return.
2330 * This function always succeeds.
2334 int aft_check_callback(__a_unused
const struct osl_object
*query
, struct osl_object
*result
)
2336 struct para_buffer pb
= {.buf
= NULL
};
2338 para_printf(&pb
, "checking audio file table...\n");
2339 audio_file_loop(&pb
, check_audio_file
);
2340 result
->data
= pb
.buf
;
2341 result
->size
= pb
.size
;
2347 * Close the audio file table.
2349 * \param flags Usual flags that are passed to osl_close_table().
2351 * \sa osl_close_table().
2353 static void aft_close(void)
2355 osl_close_table(audio_file_table
, OSL_MARK_CLEAN
);
2356 audio_file_table
= NULL
;
2360 * Open the audio file table.
2362 * \param dir The database directory.
2366 * \sa osl_open_table().
2368 static int aft_open(const char *dir
)
2372 audio_file_table_desc
.dir
= dir
;
2373 ret
= osl_open_table(&audio_file_table_desc
, &audio_file_table
);
2376 osl_get_num_rows(audio_file_table
, &num
);
2377 PARA_INFO_LOG("audio file table contains %d files\n", num
);
2380 PARA_INFO_LOG("failed to open audio file table\n");
2381 audio_file_table
= NULL
;
2382 if (ret
>= 0 || is_errno(-ret
, ENOENT
))
2387 static int aft_create(const char *dir
)
2389 audio_file_table_desc
.dir
= dir
;
2390 return osl_create_table(&audio_file_table_desc
);
2393 static int clear_attribute(struct osl_row
*row
, void *data
)
2395 struct rmatt_event_data
*red
= data
;
2396 struct afs_info afsi
;
2397 struct osl_object obj
;
2398 int ret
= get_afsi_object_of_row(row
, &obj
);
2399 uint64_t mask
= ~(1ULL << red
->bitnum
);
2403 ret
= load_afsi(&afsi
, &obj
);
2406 afsi
.attributes
&= mask
;
2407 save_afsi(&afsi
, &obj
);
2411 static int aft_event_handler(enum afs_events event
, struct para_buffer
*pb
,
2415 case ATTRIBUTE_REMOVE
: {
2416 const struct rmatt_event_data
*red
= data
;
2417 para_printf(pb
, "clearing attribute %s (bit %u) from all "
2418 "entries in the audio file table\n", red
->name
,
2420 return audio_file_loop(data
, clear_attribute
);
2427 void aft_init(struct afs_table
*t
)
2429 t
->name
= audio_file_table_desc
.name
;
2431 t
->close
= aft_close
;
2432 t
->create
= aft_create
;
2433 t
->event_handler
= aft_event_handler
;