make_attribute_lines(): Check return value of ().
[paraslash.git] / aft.c
1 /*
2 * Copyright (C) 2007-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file aft.c Audio file table functions. */
8
9 #include <dirent.h> /* readdir() */
10 #include <osl.h>
11 #include "para.h"
12 #include "error.h"
13 #include "string.h"
14 #include <sys/mman.h>
15 #include <fnmatch.h>
16 #include <sys/shm.h>
17
18 #include "afh.h"
19 #include "afs.h"
20 #include "net.h"
21 #include "vss.h"
22 #include "fd.h"
23 #include "ipc.h"
24 #include "portable_io.h"
25
26 static struct osl_table *audio_file_table;
27 static char *current_status_items;
28
29 /** The different sorting methods of the ls command. */
30 enum ls_sorting_method {
31 /** -sp (default) */
32 LS_SORT_BY_PATH,
33 /** -ss */
34 LS_SORT_BY_SCORE,
35 /** -sl */
36 LS_SORT_BY_LAST_PLAYED,
37 /** -sn */
38 LS_SORT_BY_NUM_PLAYED,
39 /** -sf */
40 LS_SORT_BY_FREQUENCY,
41 /** -sc */
42 LS_SORT_BY_CHANNELS,
43 /** -si */
44 LS_SORT_BY_IMAGE_ID,
45 /** -sy */
46 LS_SORT_BY_LYRICS_ID,
47 /** -sb */
48 LS_SORT_BY_BITRATE,
49 /** -sd */
50 LS_SORT_BY_DURATION,
51 /** -sa */
52 LS_SORT_BY_AUDIO_FORMAT,
53 /** -sh */
54 LS_SORT_BY_HASH,
55 };
56
57 /** The different listing modes of the ls command. */
58 enum ls_listing_mode {
59 /** Default listing mode. */
60 LS_MODE_SHORT,
61 /** -l or -ll */
62 LS_MODE_LONG,
63 /** -lv */
64 LS_MODE_VERBOSE,
65 /** -lm */
66 LS_MODE_MBOX,
67 /** -lc */
68 LS_MODE_CHUNKS
69 };
70
71 /** The flags accepted by the ls command. */
72 enum ls_flags {
73 /** -p */
74 LS_FLAG_FULL_PATH = 1,
75 /** -a */
76 LS_FLAG_ADMISSIBLE_ONLY = 2,
77 /** -r */
78 LS_FLAG_REVERSE = 4,
79 };
80
81 /**
82 * The size of the individual output fields of the ls command.
83 *
84 * These depend on the actual content being listed. If, for instance only files
85 * with duration less than an hour are being listed, then the duration with is
86 * made smaller because then the duration is listed as mm:ss rather than
87 * hh:mm:ss.
88 */
89 struct ls_widths {
90 /** size of the score field. */
91 unsigned short score_width;
92 /** size of the image id field. */
93 unsigned short image_id_width;
94 /** size of the lyrics id field. */
95 unsigned short lyrics_id_width;
96 /** size of the bitrate field. */
97 unsigned short bitrate_width;
98 /** size of the frequency field. */
99 unsigned short frequency_width;
100 /** size of the duration field. */
101 unsigned short duration_width;
102 /** size of the num played field. */
103 unsigned short num_played_width;
104 /** size of the amp field. */
105 unsigned short amp_width;
106 };
107
108 /** Data passed from the ls command handler to its callback function. */
109 struct ls_options {
110 /** The given command line flags. */
111 unsigned flags;
112 /** The sorting method given at the command line. */
113 enum ls_sorting_method sorting;
114 /** The given listing mode (short, long, verbose, mbox). */
115 enum ls_listing_mode mode;
116 /** The arguments passed to the ls command. */
117 char **patterns;
118 /** Number of non-option arguments. */
119 int num_patterns;
120 /** Used for long listing mode to align the output fields. */
121 struct ls_widths widths;
122 /** Size of the \a data array. */
123 uint32_t array_size;
124 /** Number of used entries in the data array. */
125 uint32_t num_matching_paths;
126 /** Array of matching entries. */
127 struct ls_data *data;
128 /** Used to sort the array. */
129 struct ls_data **data_ptr;
130 };
131
132 /**
133 * Describes the layout of the mmapped-afs info struct.
134 *
135 * \sa struct afs_info.
136 */
137 enum afsi_offsets {
138 /** Where .last_played is stored. */
139 AFSI_LAST_PLAYED_OFFSET = 0,
140 /** Storage position of the attributes bitmap. */
141 AFSI_ATTRIBUTES_OFFSET = 8,
142 /** Storage position of the .num_played field. */
143 AFSI_NUM_PLAYED_OFFSET = 16,
144 /** Storage position of the .image_id field. */
145 AFSI_IMAGE_ID_OFFSET = 20,
146 /** Storage position of the .lyrics_id field. */
147 AFSI_LYRICS_ID_OFFSET = 24,
148 /** Storage position of the .audio_format_id field. */
149 AFSI_AUDIO_FORMAT_ID_OFFSET = 28,
150 /** Storage position of the amplification field. */
151 AFSI_AMP_OFFSET = 29,
152 /** 2 bytes reserved space for future usage. */
153 AFSI_AUDIO_FORMAT_UNUSED_OFFSET = 30,
154 /** On-disk storage space needed. */
155 AFSI_SIZE = 32
156 };
157
158 /**
159 * Convert a struct afs_info to an osl object.
160 *
161 * \param afsi Pointer to the audio file info to be converted.
162 * \param obj Result pointer.
163 *
164 * \sa load_afsi().
165 */
166 void save_afsi(struct afs_info *afsi, struct osl_object *obj)
167 {
168 char *buf = obj->data;
169
170 write_u64(buf + AFSI_LAST_PLAYED_OFFSET, afsi->last_played);
171 write_u64(buf + AFSI_ATTRIBUTES_OFFSET, afsi->attributes);
172 write_u32(buf + AFSI_NUM_PLAYED_OFFSET, afsi->num_played);
173 write_u32(buf + AFSI_IMAGE_ID_OFFSET, afsi->image_id);
174 write_u32(buf + AFSI_LYRICS_ID_OFFSET, afsi->lyrics_id);
175 write_u8(buf + AFSI_AUDIO_FORMAT_ID_OFFSET,
176 afsi->audio_format_id);
177 write_u8(buf + AFSI_AMP_OFFSET, afsi->amp);
178 memset(buf + AFSI_AUDIO_FORMAT_UNUSED_OFFSET, 0, 2);
179 }
180
181 /**
182 * Get the audio file selector info struct stored in an osl object.
183 *
184 * \param afsi Points to the audio_file info structure to be filled in.
185 * \param obj The osl object holding the data.
186 *
187 * \return Positive on success, negative on errors. Possible errors: \p E_BAD_AFS.
188 *
189 * \sa save_afsi().
190 */
191 int load_afsi(struct afs_info *afsi, struct osl_object *obj)
192 {
193 char *buf = obj->data;
194 if (obj->size < AFSI_SIZE)
195 return -E_BAD_AFSI;
196 afsi->last_played = read_u64(buf + AFSI_LAST_PLAYED_OFFSET);
197 afsi->attributes = read_u64(buf + AFSI_ATTRIBUTES_OFFSET);
198 afsi->num_played = read_u32(buf + AFSI_NUM_PLAYED_OFFSET);
199 afsi->image_id = read_u32(buf + AFSI_IMAGE_ID_OFFSET);
200 afsi->lyrics_id = read_u32(buf + AFSI_LYRICS_ID_OFFSET);
201 afsi->audio_format_id = read_u8(buf +
202 AFSI_AUDIO_FORMAT_ID_OFFSET);
203 afsi->amp = read_u8(buf + AFSI_AMP_OFFSET);
204 return 1;
205 }
206
207 /** The columns of the audio file table. */
208 enum audio_file_table_columns {
209 /** The hash on the content of the audio file. */
210 AFTCOL_HASH,
211 /** The full path in the filesystem. */
212 AFTCOL_PATH,
213 /** The audio file selector info. */
214 AFTCOL_AFSI,
215 /** The audio format handler info. */
216 AFTCOL_AFHI,
217 /** The chunk table info and the chunk table of the audio file. */
218 AFTCOL_CHUNKS,
219 /** The number of columns of this table. */
220 NUM_AFT_COLUMNS
221 };
222
223 /**
224 * Compare two osl objects pointing to hash values.
225 *
226 * \param obj1 Pointer to the first hash object.
227 * \param obj2 Pointer to the second hash object.
228 *
229 * \return The values required for an osl compare function.
230 *
231 * \sa osl_compare_func, uint32_compare().
232 */
233 int aft_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2)
234 {
235 return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data);
236 }
237
238 static struct osl_column_description aft_cols[] = {
239 [AFTCOL_HASH] = {
240 .storage_type = OSL_MAPPED_STORAGE,
241 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
242 .name = "hash",
243 .compare_function = aft_hash_compare,
244 .data_size = HASH_SIZE
245 },
246 [AFTCOL_PATH] = {
247 .storage_type = OSL_MAPPED_STORAGE,
248 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
249 .name = "path",
250 .compare_function = string_compare,
251 },
252 [AFTCOL_AFSI] = {
253 .storage_type = OSL_MAPPED_STORAGE,
254 .storage_flags = OSL_FIXED_SIZE,
255 .name = "afs_info",
256 .data_size = AFSI_SIZE
257 },
258 [AFTCOL_AFHI] = {
259 .storage_type = OSL_MAPPED_STORAGE,
260 .name = "afh_info",
261 },
262 [AFTCOL_CHUNKS] = {
263 .storage_type = OSL_DISK_STORAGE,
264 .name = "chunks",
265 }
266 };
267
268 static struct osl_table_description audio_file_table_desc = {
269 .name = "audio_files",
270 .num_columns = NUM_AFT_COLUMNS,
271 .flags = OSL_LARGE_TABLE,
272 .column_descriptions = aft_cols
273 };
274
275 /* We don't want * dot or dot-dot anywhere. */
276 static int verify_dotfile(const char *rest)
277 {
278 /*
279 * The first character was '.', but that has already been discarded, we
280 * now test the rest.
281 */
282 switch (*rest) {
283 case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
284 return -1;
285 case '.': /* path start with /foo/.. */
286 if (rest[1] == '\0' || rest[1] == '/')
287 return -1; /* /foo/.. or /foo/../bar are not ok */
288 /* /foo/..bar is ok */
289 }
290 return 1;
291 }
292
293 /*
294 * We fundamentally don't like some paths: We don't want double slashes or
295 * slashes at the end that can make pathnames ambiguous.
296 */
297 static int verify_path(const char *orig_path, char **resolved_path)
298 {
299 char c;
300 size_t len;
301 char *path;
302
303 if (*orig_path != '/') /* we only accept absolute paths */
304 return -E_BAD_PATH;
305 len = strlen(orig_path);
306 *resolved_path = para_strdup(orig_path);
307 path = *resolved_path;
308 while (len > 1 && path[--len] == '/')
309 path[len] = '\0'; /* remove slash at the end */
310 c = *path++;
311 while (c) {
312 if (c == '/') {
313 c = *path++;
314 switch (c) {
315 case '/': /* double slash */
316 goto bad_path;
317 case '.':
318 if (verify_dotfile(path) < 0)
319 goto bad_path;
320 default:
321 continue;
322 }
323 }
324 c = *path++;
325 }
326 return 1;
327 bad_path:
328 free(*resolved_path);
329 return -E_BAD_PATH;
330 }
331
332 /** The on-disk layout of a afhi struct. */
333 enum afhi_offsets {
334 /** Where the number of seconds is stored. */
335 AFHI_SECONDS_TOTAL_OFFSET = 0,
336 /** Position of the bitrate. */
337 AFHI_BITRATE_OFFSET = 4,
338 /** Position of the frequency. */
339 AFHI_FREQUENCY_OFFSET = 8,
340 /** Location of the audio file header. */
341 AFHI_HEADER_OFFSET_OFFSET = 12,
342 /* Length of the audio file header. Zero means: No header. */
343 AFHI_HEADER_LEN_OFFSET = 16,
344 /** The total number of chunks (4 bytes). */
345 CHUNKS_TOTAL_OFFSET = 20,
346 /** The length of the audio file header (4 bytes). */
347 HEADER_LEN_OFFSET = 24,
348 /** The start of the audio file header (4 bytes). */
349 HEADER_OFFSET_OFFSET = 28,
350 /** The seconds part of the chunk time (4 bytes). */
351 CHUNK_TV_TV_SEC_OFFSET = 32,
352 /** The microseconds part of the chunk time (4 bytes). */
353 CHUNK_TV_TV_USEC_OFFSET = 36,
354 /** Number of channels is stored here. (1 byte) */
355 AFHI_CHANNELS_OFFSET = 40,
356 /** The tag info position. */
357 AFHI_INFO_STRING_OFFSET = 41,
358 /** Minimal on-disk size of a valid afhi struct. */
359 MIN_AFHI_SIZE = 47, /* at least 6 null bytes for techinfo/tags */
360 };
361
362 static unsigned sizeof_afhi_buf(const struct afh_info *afhi)
363 {
364 if (!afhi)
365 return 0;
366 return MIN_AFHI_SIZE
367 + strlen(afhi->techinfo)
368 + strlen(afhi->tags.artist)
369 + strlen(afhi->tags.title)
370 + strlen(afhi->tags.year)
371 + strlen(afhi->tags.album)
372 + strlen(afhi->tags.comment);
373 }
374
375 static void save_afhi(struct afh_info *afhi, char *buf)
376 {
377 char *p;
378
379 if (!afhi)
380 return;
381 write_u32(buf + AFHI_SECONDS_TOTAL_OFFSET, afhi->seconds_total);
382 write_u32(buf + AFHI_BITRATE_OFFSET, afhi->bitrate);
383 write_u32(buf + AFHI_FREQUENCY_OFFSET, afhi->frequency);
384 write_u32(buf + AFHI_HEADER_OFFSET_OFFSET, afhi->header_offset);
385 write_u32(buf + AFHI_HEADER_LEN_OFFSET, afhi->header_len);
386 write_u8(buf + AFHI_CHANNELS_OFFSET, afhi->channels);
387 write_u32(buf + CHUNKS_TOTAL_OFFSET, afhi->chunks_total);
388 write_u32(buf + HEADER_LEN_OFFSET, afhi->header_len);
389 write_u32(buf + HEADER_OFFSET_OFFSET, afhi->header_offset);
390 write_u32(buf + CHUNK_TV_TV_SEC_OFFSET, afhi->chunk_tv.tv_sec);
391 write_u32(buf + CHUNK_TV_TV_USEC_OFFSET, afhi->chunk_tv.tv_usec);
392 p = buf + AFHI_INFO_STRING_OFFSET;
393 /* The sprintf's below are OK as our caller made sure that buf is large enough */
394 p += sprintf(p, "%s", afhi->techinfo) + 1;
395 p += sprintf(p, "%s", afhi->tags.artist) + 1;
396 p += sprintf(p, "%s", afhi->tags.title) + 1;
397 p += sprintf(p, "%s", afhi->tags.year) + 1;
398 p += sprintf(p, "%s", afhi->tags.album) + 1;
399 sprintf(p, "%s", afhi->tags.comment);
400 }
401
402 static void load_afhi(const char *buf, struct afh_info *afhi)
403 {
404 afhi->seconds_total = read_u32(buf + AFHI_SECONDS_TOTAL_OFFSET);
405 afhi->bitrate = read_u32(buf + AFHI_BITRATE_OFFSET);
406 afhi->frequency = read_u32(buf + AFHI_FREQUENCY_OFFSET);
407 afhi->header_offset = read_u32(buf + AFHI_HEADER_OFFSET_OFFSET);
408 afhi->header_len = read_u32(buf + AFHI_HEADER_LEN_OFFSET);
409 afhi->channels = read_u8(buf + AFHI_CHANNELS_OFFSET);
410 afhi->chunks_total = read_u32(buf + CHUNKS_TOTAL_OFFSET);
411 afhi->header_len = read_u32(buf + HEADER_LEN_OFFSET);
412 afhi->header_offset = read_u32(buf + HEADER_OFFSET_OFFSET);
413 afhi->chunk_tv.tv_sec = read_u32(buf + CHUNK_TV_TV_SEC_OFFSET);
414 afhi->chunk_tv.tv_usec = read_u32(buf + CHUNK_TV_TV_USEC_OFFSET);
415 afhi->techinfo = buf + AFHI_INFO_STRING_OFFSET;
416 afhi->tags.artist = afhi->techinfo + strlen(afhi->techinfo) + 1;
417 afhi->tags.title = afhi->tags.artist + strlen(afhi->tags.artist) + 1;
418 afhi->tags.year = afhi->tags.title + strlen(afhi->tags.title) + 1;
419 afhi->tags.album = afhi->tags.year + strlen(afhi->tags.year) + 1;
420 afhi->tags.comment = afhi->tags.album + strlen(afhi->tags.album) + 1;
421 }
422
423 static unsigned sizeof_chunk_table(struct afh_info *afhi)
424 {
425 if (!afhi)
426 return 0;
427 return 4 * (afhi->chunks_total + 1);
428 }
429
430 static void save_chunk_table(struct afh_info *afhi, char *buf)
431 {
432 int i;
433
434 for (i = 0; i <= afhi->chunks_total; i++)
435 write_u32(buf + 4 * i, afhi->chunk_table[i]);
436 }
437
438 static void load_chunk_table(struct afh_info *afhi, char *buf)
439 {
440 int i;
441
442 afhi->chunk_table = para_malloc(sizeof_chunk_table(afhi));
443 for (i = 0; i <= afhi->chunks_total; i++)
444 afhi->chunk_table[i] = read_u32(buf + 4 * i);
445 }
446
447 /**
448 * Get the row of the audio file table corresponding to the given path.
449 *
450 * \param path The full path of the audio file.
451 * \param row Result pointer.
452 *
453 * \return Standard.
454 */
455 int aft_get_row_of_path(const char *path, struct osl_row **row)
456 {
457 struct osl_object obj = {.data = (char *)path, .size = strlen(path) + 1};
458
459 return osl(osl_get_row(audio_file_table, AFTCOL_PATH, &obj, row));
460 }
461
462 /**
463 * Get the row of the audio file table corresponding to the given hash value.
464 *
465 * \param hash The hash value of the desired audio file.
466 * \param row Result pointer.
467 *
468 * \return Standard.
469 */
470 int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row)
471 {
472 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
473 return osl(osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row));
474 }
475
476 /**
477 * Get the osl object holding the audio file selector info of a row.
478 *
479 * \param row Pointer to a row in the audio file table.
480 * \param obj Result pointer.
481 *
482 * \return Standard.
483 */
484 int get_afsi_object_of_row(const struct osl_row *row, struct osl_object *obj)
485 {
486 return osl(osl_get_object(audio_file_table, row, AFTCOL_AFSI, obj));
487 }
488
489 /**
490 * Get the osl object holding the audio file selector info, given a path.
491 *
492 *
493 * \param path The full path of the audio file.
494 * \param obj Result pointer.
495 *
496 * \return Positive on success, negative on errors.
497 */
498 int get_afsi_object_of_path(const char *path, struct osl_object *obj)
499 {
500 struct osl_row *row;
501 int ret = aft_get_row_of_path(path, &row);
502 if (ret < 0)
503 return ret;
504 return get_afsi_object_of_row(row, obj);
505 }
506
507 /**
508 * Get the audio file selector info, given a row of the audio file table.
509 *
510 * \param row Pointer to a row in the audio file table.
511 * \param afsi Result pointer.
512 *
513 * \return Positive on success, negative on errors.
514 */
515 int get_afsi_of_row(const struct osl_row *row, struct afs_info *afsi)
516 {
517 struct osl_object obj;
518 int ret = get_afsi_object_of_row(row, &obj);
519 if (ret < 0)
520 return ret;
521 return load_afsi(afsi, &obj);
522 }
523
524 /**
525 * Get the audio file selector info, given the path of an audio table.
526 *
527 * \param path The full path of the audio file.
528 * \param afsi Result pointer.
529 *
530 * \return Positive on success, negative on errors.
531 */
532 int get_afsi_of_path(const char *path, struct afs_info *afsi)
533 {
534 struct osl_object obj;
535 int ret = get_afsi_object_of_path(path, &obj);
536 if (ret < 0)
537 return ret;
538 return load_afsi(afsi, &obj);
539 }
540
541 /**
542 * Get the path of an audio file, given a row of the audio file table.
543 *
544 * \param row Pointer to a row in the audio file table.
545 * \param path Result pointer.
546 *
547 * The result is a pointer to mmapped data. The caller must not attempt
548 * to free it.
549 *
550 * \return Standard.
551 */
552 int get_audio_file_path_of_row(const struct osl_row *row, char **path)
553 {
554 struct osl_object path_obj;
555 int ret = osl(osl_get_object(audio_file_table, row, AFTCOL_PATH,
556 &path_obj));
557 if (ret < 0)
558 return ret;
559 *path = path_obj.data;
560 return 1;
561 }
562
563 /**
564 * Get the object containing the hash value of an audio file, given a row.
565 *
566 * \param row Pointer to a row of the audio file table.
567 * \param obj Result pointer.
568 *
569 * \return The return value of the underlying call to osl_get_object().
570 *
571 * \sa get_hash_of_row().
572 */
573 static int get_hash_object_of_aft_row(const struct osl_row *row,
574 struct osl_object *obj)
575 {
576 return osl(osl_get_object(audio_file_table, row, AFTCOL_HASH, obj));
577 }
578
579 /**
580 * Get the hash value of an audio file, given a row of the audio file table.
581 *
582 * \param row Pointer to a row of the audio file table.
583 * \param hash Result pointer.
584 *
585 * \a hash points to mapped data and must not be freed by the caller.
586 *
587 * \return The return value of the underlying call to
588 * get_hash_object_of_aft_row().
589 */
590 static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash)
591 {
592 struct osl_object obj;
593 int ret = get_hash_object_of_aft_row(row, &obj);
594
595 if (ret < 0)
596 return ret;
597 *hash = obj.data;
598 return 1;
599 }
600
601 /**
602 * Get the audio format handler info, given a row of the audio file table.
603 *
604 * \param row Pointer to a row of the audio file table.
605 * \param afhi Result pointer.
606 *
607 * \return The return value of the underlying call to osl_get_object().
608 *
609 * \sa get_chunk_table_of_row().
610 */
611 int get_afhi_of_row(const struct osl_row *row, struct afh_info *afhi)
612 {
613 struct osl_object obj;
614 int ret = osl(osl_get_object(audio_file_table, row, AFTCOL_AFHI,
615 &obj));
616 if (ret < 0)
617 return ret;
618 load_afhi(obj.data, afhi);
619 return 1;
620 }
621
622 /* returns shmid on success */
623 static int save_afd(struct audio_file_data *afd)
624 {
625 size_t size = sizeof(*afd) + sizeof_chunk_table(&afd->afhi);
626 int shmid, ret = shm_new(size);
627 void *shm_afd;
628 char *buf;
629
630 if (ret < 0)
631 return ret;
632 shmid = ret;
633 ret = shm_attach(shmid, ATTACH_RW, &shm_afd);
634 if (ret < 0)
635 goto err;
636 *(struct audio_file_data *)shm_afd = *afd;
637 buf = shm_afd;
638 buf += sizeof(*afd);
639 save_chunk_table(&afd->afhi, buf);
640 shm_detach(shm_afd);
641 return shmid;
642 err:
643 shm_destroy(shmid);
644 return ret;
645 }
646
647 int load_afd(int shmid, struct audio_file_data *afd)
648 {
649 void *shm_afd;
650 char *buf;
651 int ret;
652
653 ret = shm_attach(shmid, ATTACH_RO, &shm_afd);
654 if (ret < 0)
655 return ret;
656 *afd = *(struct audio_file_data *)shm_afd;
657 buf = shm_afd;
658 buf += sizeof(*afd);
659 load_chunk_table(&afd->afhi, buf);
660 shm_detach(shm_afd);
661 return 1;
662 }
663
664 static int get_local_time(uint64_t *seconds, char *buf, size_t size,
665 time_t current_time, enum ls_listing_mode lm)
666 {
667 struct tm t;
668
669 if (!localtime_r((time_t *)seconds, &t))
670 return -E_LOCALTIME;
671 if (lm == LS_MODE_MBOX) {
672 if (!strftime(buf, size, "%c", &t))
673 return -E_STRFTIME;
674 return 1;
675 }
676 if (*seconds + 6 * 30 * 24 * 3600 > current_time) {
677 if (!strftime(buf, size, "%b %e %k:%M", &t))
678 return -E_STRFTIME;
679 return 1;
680 }
681 if (!strftime(buf, size, "%b %e %Y", &t))
682 return -E_STRFTIME;
683 return 1;
684 }
685
686 /** Compute the number of (decimal) digits of a number. */
687 #define GET_NUM_DIGITS(x, num) { \
688 typeof((x)) _tmp = PARA_ABS(x); \
689 *num = 1; \
690 if ((_tmp)) \
691 while ((_tmp) > 9) { \
692 (_tmp) /= 10; \
693 (*num)++; \
694 } \
695 }
696
697 static short unsigned get_duration_width(int seconds)
698 {
699 short unsigned width;
700 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
701
702 if (!hours) /* less than one hour => m:ss or mm:ss => 4 or 5 digits */
703 return 4 + (mins > 9);
704 /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
705 GET_NUM_DIGITS(hours, &width);
706 return width + 6;
707 }
708
709 static void get_duration_buf(int seconds, char *buf, struct ls_options *opts)
710 {
711 unsigned hours = seconds / 3600, mins = (seconds % 3600) / 60;
712 short unsigned max_width;
713
714 if (!hours) { /* m:ss or mm:ss */
715 max_width = opts->mode == LS_MODE_LONG?
716 opts->widths.duration_width : 4;
717 sprintf(buf, "%*u:%02u", max_width - 3, mins, seconds % 60);
718 } else { /* more than one hour => h:mm:ss, hh:mm:ss, hhh:mm:ss, ... */
719 max_width = opts->mode == LS_MODE_LONG?
720 opts->widths.duration_width : 7;
721 sprintf(buf, "%*u:%02u:%02u", max_width - 6, hours, mins,
722 seconds % 60);
723 }
724 }
725
726 static int make_attribute_lines(const char *att_bitmap, struct afs_info *afsi,
727 char **result)
728 {
729 char *att_text;
730 int ret = get_attribute_text(&afsi->attributes, " ", &att_text);
731
732 if (ret < 0)
733 return ret;
734 *result = make_message("%s: %s\n%s: %s",
735 status_item_list[SI_ATTRIBUTES_BITMAP], att_bitmap,
736 status_item_list[SI_ATTRIBUTES_TXT], att_text);
737 free(att_text);
738 return 1;
739 }
740
741 static char *make_lyrics_lines(struct afs_info *afsi)
742 {
743 char *lyrics_name;
744
745 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
746 return make_message("%s: %u\n%s: %s\n",
747 status_item_list[SI_LYRICS_ID], afsi->lyrics_id,
748 status_item_list[SI_LYRICS_NAME], lyrics_name?
749 lyrics_name : "(none)");
750 }
751
752 static char *make_image_lines(struct afs_info *afsi)
753 {
754 char *image_name;
755 img_get_name_by_id(afsi->image_id, &image_name);
756 return make_message("%s: %u\n%s: %s\n",
757 status_item_list[SI_IMAGE_ID], afsi->image_id,
758 status_item_list[SI_IMAGE_NAME], image_name?
759 image_name : "(none)");
760 }
761
762 static char *make_filename_lines(const char *path, unsigned flags)
763 {
764 char *dirname, *ret;
765 const char *basename;
766
767 if (!(flags & LS_FLAG_FULL_PATH))
768 return make_message("%s: %s\n",
769 status_item_list[SI_BASENAME], path);
770 basename = para_basename(path),
771 dirname = para_dirname(path);
772 ret = make_message("%s: %s\n%s: %s\n%s: %s\n",
773 status_item_list[SI_PATH], path,
774 status_item_list[SI_DIRECTORY], dirname? dirname : "?",
775 status_item_list[SI_BASENAME], basename? basename : "?");
776 free(dirname);
777 return ret;
778 }
779
780 static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
781 {
782 struct osl_object chunk_table_obj;
783 struct osl_row *aft_row;
784 int ret, i;
785 char *buf;
786
787 ret = aft_get_row_of_hash(d->hash, &aft_row);
788 if (ret < 0)
789 return ret;
790 ret = osl_open_disk_object(audio_file_table, aft_row,
791 AFTCOL_CHUNKS, &chunk_table_obj);
792 if (ret < 0)
793 return ret;
794 ret = para_printf(b, "%s\n"
795 "chunk_time: %lu:%lu\nchunk_offsets: ",
796 d->path,
797 (long unsigned) d->afhi.chunk_tv.tv_sec,
798 (long unsigned) d->afhi.chunk_tv.tv_usec
799 );
800 if (ret < 0)
801 goto out;
802 buf = chunk_table_obj.data;
803 for (i = 0; i <= d->afhi.chunks_total; i++) {
804 ret = para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
805 if (ret < 0)
806 goto out;
807 }
808 ret = para_printf(b, "\n");
809 out:
810 osl_close_disk_object(&chunk_table_obj);
811 return ret;
812 }
813
814 static int print_list_item(struct ls_data *d, struct ls_options *opts,
815 struct para_buffer *b, time_t current_time)
816 {
817 int ret;
818 char att_buf[65];
819 char last_played_time[30];
820 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
821 char score_buf[30] = "";
822 struct afs_info *afsi = &d->afsi;
823 struct afh_info *afhi = &d->afhi;
824 struct ls_widths *w = &opts->widths;
825 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
826 char asc_hash[2 * HASH_SIZE + 1];
827 char *att_lines, *lyrics_lines, *image_lines, *filename_lines;
828
829 if (opts->mode == LS_MODE_SHORT) {
830 ret = para_printf(b, "%s\n", d->path);
831 goto out;
832 }
833 if (opts->mode == LS_MODE_CHUNKS) {
834 ret = print_chunk_table(d, b);
835 goto out;
836 }
837 get_attribute_bitmap(&afsi->attributes, att_buf);
838 ret = get_local_time(&afsi->last_played, last_played_time,
839 sizeof(last_played_time), current_time, opts->mode);
840 if (ret < 0)
841 goto out;
842 get_duration_buf(afhi->seconds_total, duration_buf, opts);
843 if (have_score) {
844 if (opts->mode == LS_MODE_LONG)
845 sprintf(score_buf, "%*li ", w->score_width, d->score);
846 else
847 sprintf(score_buf, "%li ", d->score);
848 }
849
850 if (opts->mode == LS_MODE_LONG) {
851 ret = para_printf(b,
852 "%s" /* score */
853 "%s " /* attributes */
854 "%*u " /* amp */
855 "%*d " /* image_id */
856 "%*d " /* lyrics_id */
857 "%*d " /* bitrate */
858 "%s " /* audio format */
859 "%*d " /* frequency */
860 "%d " /* channels */
861 "%s " /* duration */
862 "%*d " /* num_played */
863 "%s " /* last_played */
864 "%s\n", /* path */
865 score_buf,
866 att_buf,
867 w->amp_width, afsi->amp,
868 w->image_id_width, afsi->image_id,
869 w->lyrics_id_width, afsi->lyrics_id,
870 w->bitrate_width, afhi->bitrate,
871 audio_format_name(afsi->audio_format_id),
872 w->frequency_width, afhi->frequency,
873 afhi->channels,
874 duration_buf,
875 w->num_played_width, afsi->num_played,
876 last_played_time,
877 d->path
878 );
879 goto out;
880 }
881 hash_to_asc(d->hash, asc_hash);
882 ret = make_attribute_lines(att_buf, afsi, &att_lines);
883 if (ret < 0)
884 goto out;
885 lyrics_lines = make_lyrics_lines(afsi);
886 image_lines = make_image_lines(afsi);
887 filename_lines = make_filename_lines(d->path, opts->flags);
888 if (opts->mode == LS_MODE_MBOX) {
889 const char *bn = para_basename(d->path);
890 ret = para_printf(b,
891 "From foo@localhost %s\n"
892 "Received: from\nTo: bar\nFrom: a\n"
893 "Subject: %s\n\n",
894 last_played_time,
895 bn? bn : "?");
896 if (ret < 0)
897 goto out;
898 }
899 ret = para_printf(b,
900 "%s" /* filename stuff */
901 "%s%s%s%s" /* score */
902 "%s\n" /* attributes */
903 "%s: %s\n" /* hash */
904 "%s" /* image id, image name */
905 "%s" /* lyrics */
906 "%s: %dkbit/s\n" /* bitrate */
907 "%s: %s\n" /* format */
908 "%s: %dHz\n" /* frequency */
909 "%s: %d\n" /* channels */
910 "%s: %s\n" /* duration */
911 "%s: %lu\n" /* seconds total */
912 "%s: %s\n" /* last played time */
913 "%s: %d\n" /* num_played */
914 "%s: %u\n" /* amplification */
915 "%s: %lu\n" /* chunk time */
916 "%s: %lu\n" /* num chunks */
917 "%s: %s\n" /* techinfo */
918 "%s: %s\n" /* artist */
919 "%s: %s\n" /* title */
920 "%s: %s\n" /* year */
921 "%s: %s\n" /* album */
922 "%s: %s\n", /* comment */
923 filename_lines,
924 have_score? status_item_list[SI_SCORE] : "",
925 have_score? ": " : "",
926 score_buf,
927 have_score? "\n" : "",
928 att_lines,
929 status_item_list[SI_HASH], asc_hash,
930 image_lines,
931 lyrics_lines,
932 status_item_list[SI_BITRATE], afhi->bitrate,
933 status_item_list[SI_FORMAT], audio_format_name(afsi->audio_format_id),
934 status_item_list[SI_FREQUENCY], afhi->frequency,
935 status_item_list[SI_CHANNELS], afhi->channels,
936 status_item_list[SI_DURATION], duration_buf,
937 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
938 status_item_list[SI_LAST_PLAYED], last_played_time,
939 status_item_list[SI_NUM_PLAYED], afsi->num_played,
940 status_item_list[SI_AMPLIFICATION], afsi->amp,
941 status_item_list[SI_CHUNK_TIME], tv2ms(&afhi->chunk_tv),
942 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
943 status_item_list[SI_TECHINFO], afhi->techinfo,
944 status_item_list[SI_ARTIST], afhi->tags.artist,
945 status_item_list[SI_TITLE], afhi->tags.title,
946 status_item_list[SI_YEAR], afhi->tags.year,
947 status_item_list[SI_ALBUM], afhi->tags.album,
948 status_item_list[SI_COMMENT], afhi->tags.comment
949 );
950 if (ret < 0)
951 goto out;
952 if (opts->mode == LS_MODE_MBOX) {
953 struct osl_object lyrics_def;
954 lyr_get_def_by_id(afsi->lyrics_id, &lyrics_def);
955 if (lyrics_def.data) {
956 ret = para_printf(b, "Lyrics:\n~~~~~~~\n%s",
957 (char *)lyrics_def.data);
958 osl_close_disk_object(&lyrics_def);
959 }
960 }
961 free(att_lines);
962 free(lyrics_lines);
963 free(image_lines);
964 free(filename_lines);
965 out:
966 return ret;
967 }
968
969 static int make_status_items(struct audio_file_data *afd,
970 struct afs_info *afsi, char *path, long score,
971 HASH_TYPE *hash)
972 {
973 struct ls_data d = {
974 .afhi = afd->afhi,
975 .afsi = *afsi,
976 .path = path,
977 .score = score,
978 .hash = hash
979 };
980 struct ls_options opts = {
981 .flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
982 .mode = LS_MODE_VERBOSE,
983 };
984 struct para_buffer pb = {.max_size = SHMMAX - 1};
985 time_t current_time;
986 int ret;
987
988 time(&current_time);
989 ret = print_list_item(&d, &opts, &pb, current_time);
990 if (ret < 0)
991 return ret;
992 free(current_status_items);
993 current_status_items = pb.buf;
994 return 1;
995 }
996
997 /**
998 * Mmap the given audio file and update statistics.
999 *
1000 * \param aft_row Determines the audio file to be opened and updated.
1001 * \param score The score of the audio file.
1002 * \param afd Result pointer.
1003 *
1004 * On success, the numplayed field of the audio file selector info is increased
1005 * and the lastplayed time is set to the current time. Finally, the score of
1006 * the audio file is updated.
1007 *
1008 * \return Positive shmid on success, negative on errors.
1009 */
1010 int open_and_update_audio_file(struct osl_row *aft_row, long score,
1011 struct audio_file_data *afd)
1012 {
1013 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
1014 struct osl_object afsi_obj;
1015 struct afs_info old_afsi, new_afsi;
1016 int ret = get_hash_of_row(aft_row, &aft_hash);
1017 struct afsi_change_event_data aced;
1018 struct osl_object map, chunk_table_obj;
1019 char *path;
1020
1021 if (ret < 0)
1022 return ret;
1023 ret = get_audio_file_path_of_row(aft_row, &path);
1024 if (ret < 0)
1025 return ret;
1026 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
1027 if (ret < 0)
1028 return ret;
1029 ret = load_afsi(&old_afsi, &afsi_obj);
1030 if (ret < 0)
1031 return ret;
1032 ret = get_afhi_of_row(aft_row, &afd->afhi);
1033 if (ret < 0)
1034 return ret;
1035 afd->afhi.chunk_table = NULL;
1036 ret = osl_open_disk_object(audio_file_table, aft_row,
1037 AFTCOL_CHUNKS, &chunk_table_obj);
1038 if (ret < 0)
1039 goto err;
1040 ret = mmap_full_file(path, O_RDONLY, &map.data,
1041 &map.size, &afd->fd);
1042 if (ret < 0)
1043 goto err;
1044 hash_function(map.data, map.size, file_hash);
1045 ret = hash_compare(file_hash, aft_hash);
1046 para_munmap(map.data, map.size);
1047 if (ret) {
1048 ret = -E_HASH_MISMATCH;
1049 goto err;
1050 }
1051 new_afsi = old_afsi;
1052 new_afsi.num_played++;
1053 new_afsi.last_played = time(NULL);
1054 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
1055
1056 load_chunk_table(&afd->afhi, chunk_table_obj.data);
1057 ret = make_status_items(afd, &old_afsi, path, score, file_hash);
1058 if (ret < 0)
1059 goto err;
1060 aced.aft_row = aft_row;
1061 aced.old_afsi = &old_afsi;
1062 afs_event(AFSI_CHANGE, NULL, &aced);
1063 ret = save_afd(afd);
1064 err:
1065 free(afd->afhi.chunk_table);
1066 osl_close_disk_object(&chunk_table_obj);
1067 return ret;
1068 }
1069
1070 static int ls_audio_format_compare(const void *a, const void *b)
1071 {
1072 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1073 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
1074 }
1075
1076 static int ls_duration_compare(const void *a, const void *b)
1077 {
1078 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1079 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
1080 }
1081
1082 static int ls_bitrate_compare(const void *a, const void *b)
1083 {
1084 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1085 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
1086 }
1087
1088 static int ls_lyrics_id_compare(const void *a, const void *b)
1089 {
1090 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1091 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
1092 }
1093
1094 static int ls_image_id_compare(const void *a, const void *b)
1095 {
1096 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1097 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
1098 }
1099
1100 static int ls_channels_compare(const void *a, const void *b)
1101 {
1102 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1103 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
1104 }
1105
1106 static int ls_frequency_compare(const void *a, const void *b)
1107 {
1108 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1109 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
1110 }
1111
1112 static int ls_num_played_compare(const void *a, const void *b)
1113 {
1114 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1115 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
1116 }
1117
1118 static int ls_last_played_compare(const void *a, const void *b)
1119 {
1120 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1121 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
1122 }
1123
1124 static int ls_score_compare(const void *a, const void *b)
1125 {
1126 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1127 return NUM_COMPARE(d1->score, d2->score);
1128 }
1129
1130 static int ls_path_compare(const void *a, const void *b)
1131 {
1132 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1133 return strcmp(d1->path, d2->path);
1134 }
1135
1136 static int sort_matching_paths(struct ls_options *options)
1137 {
1138 size_t nmemb = options->num_matching_paths;
1139 size_t size = sizeof(*options->data_ptr);
1140 int (*compar)(const void *, const void *);
1141 int i;
1142
1143 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
1144 for (i = 0; i < nmemb; i++)
1145 options->data_ptr[i] = options->data + i;
1146
1147 /* In these cases the array is already sorted */
1148 if (options->sorting == LS_SORT_BY_PATH
1149 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1150 && (options->flags & LS_FLAG_FULL_PATH))
1151 return 1;
1152 if (options->sorting == LS_SORT_BY_SCORE &&
1153 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1154 return 1;
1155
1156 switch (options->sorting) {
1157 case LS_SORT_BY_PATH:
1158 compar = ls_path_compare; break;
1159 case LS_SORT_BY_SCORE:
1160 compar = ls_score_compare; break;
1161 case LS_SORT_BY_LAST_PLAYED:
1162 compar = ls_last_played_compare; break;
1163 case LS_SORT_BY_NUM_PLAYED:
1164 compar = ls_num_played_compare; break;
1165 case LS_SORT_BY_FREQUENCY:
1166 compar = ls_frequency_compare; break;
1167 case LS_SORT_BY_CHANNELS:
1168 compar = ls_channels_compare; break;
1169 case LS_SORT_BY_IMAGE_ID:
1170 compar = ls_image_id_compare; break;
1171 case LS_SORT_BY_LYRICS_ID:
1172 compar = ls_lyrics_id_compare; break;
1173 case LS_SORT_BY_BITRATE:
1174 compar = ls_bitrate_compare; break;
1175 case LS_SORT_BY_DURATION:
1176 compar = ls_duration_compare; break;
1177 case LS_SORT_BY_AUDIO_FORMAT:
1178 compar = ls_audio_format_compare; break;
1179 default:
1180 return -E_BAD_SORT;
1181 }
1182 qsort(options->data_ptr, nmemb, size, compar);
1183 return 1;
1184 }
1185
1186 /* row is either an aft_row or a row of the score table */
1187 /* TODO: Only compute widths if we need them */
1188 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
1189 {
1190 int ret, i;
1191 struct ls_options *options = ls_opts;
1192 struct ls_data *d;
1193 struct ls_widths *w;
1194 unsigned short num_digits;
1195 unsigned tmp;
1196 struct osl_row *aft_row;
1197 long score;
1198 char *path;
1199
1200 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1201 ret = get_score_and_aft_row(row, &score, &aft_row);
1202 if (ret < 0)
1203 return ret;
1204 } else
1205 aft_row = row;
1206 ret = get_audio_file_path_of_row(aft_row, &path);
1207 if (ret < 0)
1208 return ret;
1209 if (!(options->flags & LS_FLAG_FULL_PATH)) {
1210 char *p = strrchr(path, '/');
1211 if (p)
1212 path = p + 1;
1213 }
1214 if (options->num_patterns) {
1215 for (i = 0; i < options->num_patterns; i++) {
1216 ret = fnmatch(options->patterns[i], path, 0);
1217 if (!ret)
1218 break;
1219 if (ret == FNM_NOMATCH)
1220 continue;
1221 return -E_FNMATCH;
1222 }
1223 if (i >= options->num_patterns) /* no match */
1224 return 1;
1225 }
1226 tmp = options->num_matching_paths++;
1227 if (options->num_matching_paths > options->array_size) {
1228 options->array_size++;
1229 options->array_size *= 2;
1230 options->data = para_realloc(options->data, options->array_size
1231 * sizeof(*options->data));
1232 }
1233 d = options->data + tmp;
1234 ret = get_afsi_of_row(aft_row, &d->afsi);
1235 if (ret < 0)
1236 return ret;
1237 ret = get_afhi_of_row(aft_row, &d->afhi);
1238 if (ret < 0)
1239 return ret;
1240 d->path = path;
1241 ret = get_hash_of_row(aft_row, &d->hash);
1242 if (ret < 0)
1243 goto err;
1244 w = &options->widths;
1245 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
1246 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
1247 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
1248 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
1249 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
1250 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
1251 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
1252 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
1253 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
1254 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
1255 /* get the number of chars to print this amount of time */
1256 num_digits = get_duration_width(d->afhi.seconds_total);
1257 w->duration_width = PARA_MAX(w->duration_width, num_digits);
1258 GET_NUM_DIGITS(d->afsi.amp, &num_digits);
1259 w->amp_width = PARA_MAX(w->amp_width, num_digits);
1260 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1261 GET_NUM_DIGITS(score, &num_digits);
1262 num_digits++; /* add one for the sign (space or "-") */
1263 w->score_width = PARA_MAX(w->score_width, num_digits);
1264 d->score = score;
1265 }
1266 return 1;
1267 err:
1268 return ret;
1269 }
1270
1271 static void com_ls_callback(int fd, const struct osl_object *query)
1272 {
1273 struct ls_options *opts = query->data;
1274 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
1275 struct para_buffer b = {.max_size = SHMMAX,
1276 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1277 int i = 0, ret;
1278 time_t current_time;
1279
1280
1281 if (opts->num_patterns) {
1282 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
1283 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
1284 opts->patterns[i] = p;
1285 p += strlen(p) + 1;
1286 }
1287 } else
1288 opts->patterns = NULL;
1289 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
1290 ret = admissible_file_loop(opts, prepare_ls_row);
1291 else
1292 ret = osl(osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
1293 prepare_ls_row));
1294 if (ret < 0)
1295 goto out;
1296 if (!opts->num_matching_paths)
1297 goto out;
1298 ret = sort_matching_paths(opts);
1299 if (ret < 0)
1300 goto out;
1301 time(&current_time);
1302 if (opts->flags & LS_FLAG_REVERSE)
1303 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1304 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1305 if (ret < 0)
1306 goto out;
1307 }
1308 else
1309 for (i = 0; i < opts->num_matching_paths; i++) {
1310 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1311 if (ret < 0)
1312 goto out;
1313 }
1314 out:
1315 if (b.offset)
1316 pass_buffer_as_shm(b.buf, b.offset, &fd);
1317 free(b.buf);
1318 free(opts->data);
1319 free(opts->data_ptr);
1320 free(opts->patterns);
1321 }
1322
1323 /*
1324 * TODO: flags -h (sort by hash)
1325 */
1326 int com_ls(int fd, int argc, char * const * const argv)
1327 {
1328 int i, ret;
1329 unsigned flags = 0;
1330 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1331 enum ls_listing_mode mode = LS_MODE_SHORT;
1332 struct ls_options opts = {.patterns = NULL};
1333 struct osl_object query = {.data = &opts, .size = sizeof(opts)};
1334
1335 for (i = 1; i < argc; i++) {
1336 const char *arg = argv[i];
1337 if (arg[0] != '-')
1338 break;
1339 if (!strcmp(arg, "--")) {
1340 i++;
1341 break;
1342 }
1343 if (!strncmp(arg, "-l", 2)) {
1344 if (!*(arg + 2)) {
1345 mode = LS_MODE_LONG;
1346 continue;
1347 }
1348 if (*(arg + 3))
1349 return -E_AFT_SYNTAX;
1350 switch(*(arg + 2)) {
1351 case 's':
1352 mode = LS_MODE_SHORT;
1353 continue;
1354 case 'l':
1355 mode = LS_MODE_LONG;
1356 continue;
1357 case 'v':
1358 mode = LS_MODE_VERBOSE;
1359 continue;
1360 case 'm':
1361 mode = LS_MODE_MBOX;
1362 continue;
1363 case 'c':
1364 mode = LS_MODE_CHUNKS;
1365 continue;
1366 default:
1367 return -E_AFT_SYNTAX;
1368 }
1369 }
1370 if (!strcmp(arg, "-p")) {
1371 flags |= LS_FLAG_FULL_PATH;
1372 continue;
1373 }
1374 if (!strcmp(arg, "-a")) {
1375 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1376 continue;
1377 }
1378 if (!strcmp(arg, "-r")) {
1379 flags |= LS_FLAG_REVERSE;
1380 continue;
1381 }
1382 if (!strncmp(arg, "-s", 2)) {
1383 if (!*(arg + 2) || *(arg + 3))
1384 return -E_AFT_SYNTAX;
1385 switch(*(arg + 2)) {
1386 case 'p':
1387 sort = LS_SORT_BY_PATH;
1388 continue;
1389 case 's': /* -ss implies -a */
1390 sort = LS_SORT_BY_SCORE;
1391 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1392 continue;
1393 case 'l':
1394 sort = LS_SORT_BY_LAST_PLAYED;
1395 continue;
1396 case 'n':
1397 sort = LS_SORT_BY_NUM_PLAYED;
1398 continue;
1399 case 'f':
1400 sort = LS_SORT_BY_FREQUENCY;
1401 continue;
1402 case 'c':
1403 sort = LS_SORT_BY_CHANNELS;
1404 continue;
1405 case 'i':
1406 sort = LS_SORT_BY_IMAGE_ID;
1407 continue;
1408 case 'y':
1409 sort = LS_SORT_BY_LYRICS_ID;
1410 continue;
1411 case 'b':
1412 sort = LS_SORT_BY_BITRATE;
1413 continue;
1414 case 'd':
1415 sort = LS_SORT_BY_DURATION;
1416 continue;
1417 case 'a':
1418 sort = LS_SORT_BY_AUDIO_FORMAT;
1419 continue;
1420 default:
1421 return -E_AFT_SYNTAX;
1422 }
1423 }
1424 return -E_AFT_SYNTAX;
1425 }
1426 opts.flags = flags;
1427 opts.sorting = sort;
1428 opts.mode = mode;
1429 opts.num_patterns = argc - i;
1430 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1431 argv + i, com_ls_callback, send_result, &fd);
1432 return ret;
1433 }
1434
1435 /**
1436 * Call the given function for each file in the audio file table.
1437 *
1438 * \param private_data An arbitrary data pointer, passed to \a func.
1439 * \param func The custom function to be called.
1440 *
1441 * \return Standard.
1442 */
1443 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1444 {
1445 return osl(osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1446 func));
1447 }
1448
1449 static struct osl_row *find_hash_sister(HASH_TYPE *hash)
1450 {
1451 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1452 struct osl_row *row;
1453
1454 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1455 return row;
1456 }
1457
1458 /** The format of the data stored by save_audio_file_data(). */
1459 enum com_add_buffer_offsets {
1460 /** afhi (if present) starts here. */
1461 CAB_AFHI_OFFSET_POS = 0,
1462 /** Start of the chunk table (if present). */
1463 CAB_CHUNKS_OFFSET_POS = 2,
1464 /** Audio format id. */
1465 CAB_AUDIO_FORMAT_OFFSET = 4,
1466 /** Flags given to the add command. */
1467 CAB_FLAGS_OFFSET = 5,
1468 /** The hash of the audio file being added. */
1469 CAB_HASH_OFFSET = 9,
1470 /** Start of the path of the audio file. */
1471 CAB_PATH_OFFSET = (CAB_HASH_OFFSET + HASH_SIZE),
1472 };
1473
1474 /*
1475 * Store the given data to a single buffer. Doesn't need the audio file selector
1476 * info struct as the server knows it as well.
1477 *
1478 * It's OK to call this with afhi == NULL. In this case, the audio format
1479 * handler info won't be stored in the buffer.
1480 */
1481 static void save_add_callback_buffer(HASH_TYPE *hash, const char *path,
1482 struct afh_info *afhi, uint32_t flags,
1483 uint8_t audio_format_num, struct osl_object *obj)
1484 {
1485 size_t path_len = strlen(path) + 1;
1486 size_t afhi_size = sizeof_afhi_buf(afhi);
1487 size_t size = CAB_PATH_OFFSET + path_len + afhi_size
1488 + sizeof_chunk_table(afhi);
1489 char *buf = para_malloc(size);
1490 uint16_t pos;
1491
1492 write_u8(buf + CAB_AUDIO_FORMAT_OFFSET, audio_format_num);
1493 write_u32(buf + CAB_FLAGS_OFFSET, flags);
1494
1495 memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE);
1496 strcpy(buf + CAB_PATH_OFFSET, path);
1497
1498 pos = CAB_PATH_OFFSET + path_len;
1499 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1500 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1501 pos + afhi_size - 1);
1502 write_u16(buf + CAB_AFHI_OFFSET_POS, pos);
1503 save_afhi(afhi, buf + pos);
1504
1505 pos += afhi_size;
1506 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1507 write_u16(buf + CAB_CHUNKS_OFFSET_POS, pos);
1508 if (afhi)
1509 save_chunk_table(afhi, buf + pos);
1510 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1511 obj->data = buf;
1512 obj->size = size;
1513 }
1514
1515 /*
1516
1517 Overview of the add command.
1518
1519 Input: What was passed to the callback by the command handler.
1520 ~~~~~~
1521 HS: Hash sister. Whether an audio file with identical hash
1522 already exists in the osl database.
1523
1524 PB: Path brother. Whether a file with the given path exists
1525 in the table.
1526
1527 F: Force flag given. Whether add was called with -f.
1528
1529 output: Action performed by the callback.
1530 ~~~~~~~
1531 AFHI: Whether afhi and chunk table are computed and sent.
1532 ACTION: Table modifications to be done by the callback.
1533
1534 +----+----+---+------+---------------------------------------------------+
1535 | HS | PB | F | AFHI | ACTION
1536 +----+----+---+------+---------------------------------------------------+
1537 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1538 | | update path, keep afsi
1539 +----+----+---+------+---------------------------------------------------+
1540 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1541 | | otherwise: remove PB, HS: update path, keep afhi,
1542 | | afsi.
1543 +----+----+---+------+---------------------------------------------------+
1544 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1545 | | HS, keep afsi
1546 +----+----+---+------+---------------------------------------------------+
1547 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1548 +----+----+---+------+---------------------------------------------------+
1549 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1550 | | (force has no effect)
1551 +----+----+---+------+---------------------------------------------------+
1552 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1553 +----+----+---+------+---------------------------------------------------+
1554 | N | N | Y | Y | (new file) create new entry (force has no effect)
1555 +----+----+---+------+---------------------------------------------------+
1556 | N | N | N | Y | (new file) create new entry
1557 +----+----+---+------+---------------------------------------------------+
1558
1559 Notes:
1560
1561 afhi <=> force or no HS
1562 F => AFHI
1563
1564 */
1565
1566 /** Flags passed to the add command. */
1567 enum com_add_flags {
1568 /** Skip paths that exist already. */
1569 ADD_FLAG_LAZY = 1,
1570 /** Force adding. */
1571 ADD_FLAG_FORCE = 2,
1572 /** Print what is being done. */
1573 ADD_FLAG_VERBOSE = 4,
1574 /** Try to add files with unknown suffixes. */
1575 ADD_FLAG_ALL = 8,
1576 };
1577
1578 static void com_add_callback(int fd, const struct osl_object *query)
1579 {
1580 char *buf = query->data, *path;
1581 struct osl_row *pb, *aft_row;
1582 struct osl_row *hs;
1583 struct osl_object objs[NUM_AFT_COLUMNS];
1584 HASH_TYPE *hash;
1585 char asc[2 * HASH_SIZE + 1];
1586 int ret;
1587 char afsi_buf[AFSI_SIZE];
1588 uint32_t flags = read_u32(buf + CAB_FLAGS_OFFSET);
1589 struct afs_info default_afsi = {.last_played = 0};
1590 struct para_buffer msg = {.max_size = SHMMAX,
1591 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1592 uint16_t afhi_offset, chunks_offset;
1593
1594 hash = (HASH_TYPE *)buf + CAB_HASH_OFFSET;
1595 hash_to_asc(hash, asc);;
1596 objs[AFTCOL_HASH].data = buf + CAB_HASH_OFFSET;
1597 objs[AFTCOL_HASH].size = HASH_SIZE;
1598
1599 path = buf + CAB_PATH_OFFSET;
1600 objs[AFTCOL_PATH].data = path;
1601 objs[AFTCOL_PATH].size = strlen(path) + 1;
1602
1603 PARA_INFO_LOG("request to add %s\n", path);
1604 hs = find_hash_sister(hash);
1605 ret = aft_get_row_of_path(path, &pb);
1606 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1607 goto out;
1608 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1609 if (flags & ADD_FLAG_VERBOSE)
1610 ret = para_printf(&msg, "ignoring duplicate\n");
1611 else
1612 ret = 1;
1613 goto out;
1614 }
1615 if (hs && hs != pb) {
1616 struct osl_object obj;
1617 if (pb) { /* hs trumps pb, remove pb */
1618 if (flags & ADD_FLAG_VERBOSE) {
1619 ret = para_printf(&msg, "removing path brother\n");
1620 if (ret < 0)
1621 goto out;
1622 }
1623 ret = osl(osl_del_row(audio_file_table, pb));
1624 if (ret < 0)
1625 goto out;
1626 pb = NULL;
1627 }
1628 /* file rename, update hs' path */
1629 if (flags & ADD_FLAG_VERBOSE) {
1630 ret = osl(osl_get_object(audio_file_table, hs,
1631 AFTCOL_PATH, &obj));
1632 if (ret < 0)
1633 goto out;
1634 ret = para_printf(&msg, "renamed from %s\n", (char *)obj.data);
1635 if (ret < 0)
1636 goto out;
1637 }
1638 ret = osl(osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1639 &objs[AFTCOL_PATH]));
1640 if (ret < 0)
1641 goto out;
1642 afs_event(AUDIO_FILE_RENAME, &msg, hs);
1643 if (!(flags & ADD_FLAG_FORCE))
1644 goto out;
1645 }
1646 /* no hs or force mode, child must have sent afhi */
1647 afhi_offset = read_u16(buf + CAB_AFHI_OFFSET_POS);
1648 chunks_offset = read_u16(buf + CAB_CHUNKS_OFFSET_POS);
1649
1650 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1651 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1652 ret = -E_NO_AFHI;
1653 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1654 goto out;
1655 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1656 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1657 if (pb && !hs) { /* update pb's hash */
1658 char old_asc[2 * HASH_SIZE + 1];
1659 HASH_TYPE *old_hash;
1660 ret = get_hash_of_row(pb, &old_hash);
1661 if (ret < 0)
1662 goto out;
1663 hash_to_asc(old_hash, old_asc);
1664 if (flags & ADD_FLAG_VERBOSE) {
1665 ret = para_printf(&msg, "file change: %s -> %s\n",
1666 old_asc, asc);
1667 if (ret < 0)
1668 goto out;
1669 }
1670 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1671 &objs[AFTCOL_HASH]);
1672 if (ret < 0)
1673 goto out;
1674 }
1675 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1676 struct osl_row *row = pb? pb : hs;
1677 /* update afhi and chunk_table */
1678 if (flags & ADD_FLAG_VERBOSE) {
1679 ret = para_printf(&msg, "updating afhi and chunk table\n");
1680 if (ret < 0)
1681 goto out;
1682 }
1683 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1684 &objs[AFTCOL_AFHI]));
1685 if (ret < 0)
1686 goto out;
1687 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1688 &objs[AFTCOL_CHUNKS]));
1689 if (ret < 0)
1690 goto out;
1691 afs_event(AFHI_CHANGE, &msg, row);
1692 goto out;
1693 }
1694 /* new entry, use default afsi */
1695 if (flags & ADD_FLAG_VERBOSE) {
1696 ret = para_printf(&msg, "new file\n");
1697 if (ret < 0)
1698 goto out;
1699 }
1700 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1701 default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_OFFSET);
1702
1703 objs[AFTCOL_AFSI].data = &afsi_buf;
1704 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1705 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1706 ret = osl(osl_add_and_get_row(audio_file_table, objs, &aft_row));
1707 afs_event(AUDIO_FILE_ADD, &msg, aft_row);
1708 out:
1709 if (ret < 0)
1710 para_printf(&msg, "%s\n", para_strerror(-ret));
1711 if (msg.offset)
1712 pass_buffer_as_shm(msg.buf, msg.offset, &fd);
1713 free(msg.buf);
1714 }
1715
1716 /** Used by com_add(). */
1717 struct private_add_data {
1718 /** The socket file descriptor. */
1719 int fd;
1720 /** The given add flags. */
1721 uint32_t flags;
1722 };
1723
1724 static void path_brother_callback(int fd, const struct osl_object *query)
1725 {
1726 char *path = query->data;
1727 struct osl_row *path_brother;
1728 int ret = aft_get_row_of_path(path, &path_brother);
1729 if (ret < 0)
1730 return;
1731 pass_buffer_as_shm((char *)&path_brother, sizeof(path_brother), &fd);
1732 }
1733
1734 static void hash_sister_callback(int fd, const struct osl_object *query)
1735 {
1736 HASH_TYPE *hash = query->data;
1737 struct osl_row *hash_sister;
1738
1739 hash_sister = find_hash_sister(hash);
1740 if (!hash_sister)
1741 return;
1742 pass_buffer_as_shm((char *)&hash_sister, sizeof(hash_sister), &fd);
1743 }
1744
1745 static int get_row_pointer_from_result(struct osl_object *result, void *private)
1746 {
1747 struct osl_row **row = private;
1748 *row = result->data;
1749 return 1;
1750 }
1751
1752 static int add_one_audio_file(const char *path, void *private_data)
1753 {
1754 int ret, send_ret = 1, fd;
1755 uint8_t format_num = -1;
1756 struct private_add_data *pad = private_data;
1757 struct afh_info afhi, *afhi_ptr = NULL;
1758 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1759 struct osl_object map, obj = {.data = NULL}, query;
1760 HASH_TYPE hash[HASH_SIZE];
1761
1762 ret = guess_audio_format(path);
1763 if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
1764 goto out_free;
1765 query.data = (char *)path;
1766 query.size = strlen(path) + 1;
1767 ret = send_callback_request(path_brother_callback, &query,
1768 get_row_pointer_from_result, &pb);
1769 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1770 goto out_free;
1771 ret = 1;
1772 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1773 if (pad->flags & ADD_FLAG_VERBOSE)
1774 send_ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1775 goto out_free;
1776 }
1777 /* We still want to add this file. Compute its hash. */
1778 ret = mmap_full_file(path, O_RDONLY, &map.data, &map.size, &fd);
1779 if (ret < 0)
1780 goto out_free;
1781 hash_function(map.data, map.size, hash);
1782
1783 /* Check whether the database contains a file with the same hash. */
1784 query.data = hash;
1785 query.size = HASH_SIZE;
1786 ret = send_callback_request(hash_sister_callback, &query,
1787 get_row_pointer_from_result, &hs);
1788 if (ret < 0)
1789 goto out_unmap;
1790 /* Return success if we already know this file. */
1791 ret = 1;
1792 if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) {
1793 if (pad->flags & ADD_FLAG_VERBOSE)
1794 send_ret = send_va_buffer(pad->fd,
1795 "%s exists, not forcing update\n", path);
1796 goto out_unmap;
1797 }
1798 /*
1799 * We won't recalculate the audio format info and the chunk table if
1800 * there is a hash sister and FORCE was not given.
1801 */
1802 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1803 ret = compute_afhi(path, map.data, map.size, fd, &afhi);
1804 if (ret < 0)
1805 goto out_unmap;
1806 format_num = ret;
1807 afhi_ptr = &afhi;
1808 }
1809 munmap(map.data, map.size);
1810 close(fd);
1811 if (pad->flags & ADD_FLAG_VERBOSE) {
1812 send_ret = send_va_buffer(pad->fd, "adding %s\n", path);
1813 if (send_ret < 0)
1814 goto out_free;
1815 }
1816 save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1817 /* Ask afs to consider this entry for adding. */
1818 ret = send_callback_request(com_add_callback, &obj, send_result, &pad->fd);
1819 goto out_free;
1820
1821 out_unmap:
1822 close(fd);
1823 munmap(map.data, map.size);
1824 out_free:
1825 if (ret < 0 && send_ret >= 0)
1826 send_ret = send_va_buffer(pad->fd, "failed to add %s (%s)\n", path,
1827 para_strerror(-ret));
1828 free(obj.data);
1829 if (afhi_ptr) {
1830 free(afhi_ptr->chunk_table);
1831 free(afhi_ptr->techinfo);
1832 free(afhi_ptr->tags.artist);
1833 free(afhi_ptr->tags.title);
1834 free(afhi_ptr->tags.year);
1835 free(afhi_ptr->tags.album);
1836 free(afhi_ptr->tags.comment);
1837 }
1838 /* Stop adding files only on send errors. */
1839 return send_ret;
1840 }
1841
1842 int com_add(int fd, int argc, char * const * const argv)
1843 {
1844 int i, ret;
1845 struct private_add_data pad = {.fd = fd, .flags = 0};
1846 struct stat statbuf;
1847
1848 for (i = 1; i < argc; i++) {
1849 const char *arg = argv[i];
1850 if (arg[0] != '-')
1851 break;
1852 if (!strcmp(arg, "--")) {
1853 i++;
1854 break;
1855 }
1856 if (!strcmp(arg, "-a")) {
1857 pad.flags |= ADD_FLAG_ALL;
1858 continue;
1859 }
1860 if (!strcmp(arg, "-l")) {
1861 pad.flags |= ADD_FLAG_LAZY;
1862 continue;
1863 }
1864 if (!strcmp(arg, "-f")) {
1865 pad.flags |= ADD_FLAG_FORCE;
1866 continue;
1867 }
1868 if (!strcmp(arg, "-v")) {
1869 pad.flags |= ADD_FLAG_VERBOSE;
1870 continue;
1871 }
1872 }
1873 if (argc <= i)
1874 return -E_AFT_SYNTAX;
1875 for (; i < argc; i++) {
1876 char *path;
1877 ret = verify_path(argv[i], &path);
1878 if (ret < 0) {
1879 ret = send_va_buffer(fd, "%s: %s\n", argv[i],
1880 para_strerror(-ret));
1881 if (ret < 0)
1882 return ret;
1883 continue;
1884 }
1885 ret = stat(path, &statbuf);
1886 if (ret < 0) {
1887 ret = send_va_buffer(fd, "failed to stat %s (%s)\n", path,
1888 strerror(errno));
1889 free(path);
1890 if (ret < 0)
1891 return ret;
1892 continue;
1893 }
1894 if (S_ISDIR(statbuf.st_mode))
1895 ret = for_each_file_in_dir(path, add_one_audio_file,
1896 &pad);
1897 else
1898 ret = add_one_audio_file(path, &pad);
1899 if (ret < 0) {
1900 send_va_buffer(fd, "%s: %s\n", path, para_strerror(-ret));
1901 free(path);
1902 return ret;
1903 }
1904 free(path);
1905 }
1906 return 1;
1907
1908 }
1909
1910 /**
1911 * Flags used by the touch command.
1912 *
1913 * \sa com_touch().
1914 */
1915 enum touch_flags {
1916 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1917 TOUCH_FLAG_FNM_PATHNAME = 1,
1918 /** Activates verbose mode. */
1919 TOUCH_FLAG_VERBOSE = 2
1920 };
1921
1922 /** Options used by com_touch(). */
1923 struct com_touch_options {
1924 /** New num_played value. */
1925 int32_t num_played;
1926 /** New last played count. */
1927 int64_t last_played;
1928 /** New lyrics id. */
1929 int32_t lyrics_id;
1930 /** New image id. */
1931 int32_t image_id;
1932 /** New amplification value. */
1933 int32_t amp;
1934 /** Command line flags (see \ref touch_flags). */
1935 unsigned flags;
1936 };
1937
1938 /** Data passed to the action handler of com_touch(). */
1939 struct touch_action_data {
1940 /** Command line options (see \ref com_touch_options). */
1941 struct com_touch_options *cto;
1942 /** Message buffer. */
1943 struct para_buffer pb;
1944 /** How many audio files matched the given pattern. */
1945 unsigned num_matches;
1946 };
1947
1948 static int touch_audio_file(__a_unused struct osl_table *table,
1949 struct osl_row *row, const char *name, void *data)
1950 {
1951 struct touch_action_data *tad = data;
1952 struct osl_object obj;
1953 struct afs_info old_afsi, new_afsi;
1954 int ret, no_options = tad->cto->num_played < 0 && tad->cto->last_played < 0 &&
1955 tad->cto->lyrics_id < 0 && tad->cto->image_id < 0 && tad->cto->amp < 0;
1956 struct afsi_change_event_data aced;
1957
1958 ret = get_afsi_object_of_row(row, &obj);
1959 if (ret < 0)
1960 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1961 ret = load_afsi(&old_afsi, &obj);
1962 if (ret < 0)
1963 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1964 new_afsi = old_afsi;
1965 if (no_options) {
1966 new_afsi.num_played++;
1967 new_afsi.last_played = time(NULL);
1968 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1969 ret = para_printf(&tad->pb, "%s: num_played = %u, "
1970 "last_played = now()\n", name,
1971 new_afsi.num_played);
1972 if (ret < 0)
1973 return ret;
1974 }
1975 } else {
1976 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1977 ret = para_printf(&tad->pb, "touching %s\n", name);
1978 if (ret < 0)
1979 return ret;
1980 }
1981 if (tad->cto->lyrics_id >= 0)
1982 new_afsi.lyrics_id = tad->cto->lyrics_id;
1983 if (tad->cto->image_id >= 0)
1984 new_afsi.image_id = tad->cto->image_id;
1985 if (tad->cto->num_played >= 0)
1986 new_afsi.num_played = tad->cto->num_played;
1987 if (tad->cto->last_played >= 0)
1988 new_afsi.last_played = tad->cto->last_played;
1989 if (tad->cto->amp >= 0)
1990 new_afsi.amp = tad->cto->amp;
1991 }
1992 tad->num_matches++;
1993 save_afsi(&new_afsi, &obj); /* in-place update */
1994 aced.aft_row = row;
1995 aced.old_afsi = &old_afsi;
1996 afs_event(AFSI_CHANGE, &tad->pb, &aced);
1997 return 1;
1998 }
1999
2000 static void com_touch_callback(int fd, const struct osl_object *query)
2001 {
2002 struct touch_action_data tad = {.cto = query->data,
2003 .pb = {
2004 .max_size = SHMMAX,
2005 .private_data = &fd,
2006 .max_size_handler = pass_buffer_as_shm
2007 }
2008 };
2009 int ret, ret2 = 0;
2010 struct pattern_match_data pmd = {
2011 .table = audio_file_table,
2012 .loop_col_num = AFTCOL_HASH,
2013 .match_col_num = AFTCOL_PATH,
2014 .patterns = {.data = (char *)query->data + sizeof(*tad.cto),
2015 .size = query->size - sizeof(*tad.cto)},
2016 .data = &tad,
2017 .action = touch_audio_file
2018 };
2019 if (tad.cto->flags & TOUCH_FLAG_FNM_PATHNAME)
2020 pmd.fnmatch_flags |= FNM_PATHNAME;
2021 ret = for_each_matching_row(&pmd);
2022 if (ret < 0)
2023 ret2 = para_printf(&tad.pb, "%s\n", para_strerror(-ret));
2024 else
2025 if (!tad.num_matches)
2026 ret2 = para_printf(&tad.pb, "no matches\n");
2027 if (ret2 >= 0 && tad.pb.offset)
2028 pass_buffer_as_shm(tad.pb.buf, tad.pb.offset, &fd);
2029 free(tad.pb.buf);
2030 }
2031
2032 int com_touch(int fd, int argc, char * const * const argv)
2033 {
2034 struct com_touch_options cto = {
2035 .num_played = -1,
2036 .last_played = -1,
2037 .lyrics_id = -1,
2038 .image_id = -1,
2039 .amp = -1,
2040 };
2041 struct osl_object query = {.data = &cto, .size = sizeof(cto)};
2042 int i, ret;
2043
2044
2045 for (i = 1; i < argc; i++) {
2046 const char *arg = argv[i];
2047 if (arg[0] != '-')
2048 break;
2049 if (!strcmp(arg, "--")) {
2050 i++;
2051 break;
2052 }
2053 if (!strncmp(arg, "-n", 2)) {
2054 ret = para_atoi32(arg + 2, &cto.num_played);
2055 if (ret < 0)
2056 return ret;
2057 continue;
2058 }
2059 if (!strncmp(arg, "-l", 2)) {
2060 ret = para_atoi64(arg + 2, &cto.last_played);
2061 if (ret < 0)
2062 return ret;
2063 continue;
2064 }
2065 if (!strncmp(arg, "-y", 2)) {
2066 ret = para_atoi32(arg + 2, &cto.lyrics_id);
2067 if (ret < 0)
2068 return ret;
2069 continue;
2070 }
2071 if (!strncmp(arg, "-i", 2)) {
2072 ret = para_atoi32(arg + 2, &cto.image_id);
2073 if (ret < 0)
2074 return ret;
2075 continue;
2076 }
2077 if (!strncmp(arg, "-a", 2)) {
2078 int32_t val;
2079 ret = para_atoi32(arg + 2, &val);
2080 if (ret < 0)
2081 return ret;
2082 if (val < 0 || val > 255)
2083 return -ERRNO_TO_PARA_ERROR(EINVAL);
2084 cto.amp = val;
2085 continue;
2086 }
2087 if (!strcmp(arg, "-p")) {
2088 cto.flags |= TOUCH_FLAG_FNM_PATHNAME;
2089 continue;
2090 }
2091 if (!strcmp(arg, "-v")) {
2092 cto.flags |= TOUCH_FLAG_VERBOSE;
2093 continue;
2094 }
2095 break; /* non-option starting with dash */
2096 }
2097 if (i >= argc)
2098 return -E_AFT_SYNTAX;
2099 ret = send_option_arg_callback_request(&query, argc - i,
2100 argv + i, com_touch_callback, send_result, &fd);
2101 if (ret < 0)
2102 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2103 return ret;
2104 }
2105
2106 /** Flags for com_rm(). */
2107 enum rm_flags {
2108 /** -v */
2109 RM_FLAG_VERBOSE = 1,
2110 /** -f */
2111 RM_FLAG_FORCE = 2,
2112 /** -p */
2113 RM_FLAG_FNM_PATHNAME = 4
2114 };
2115
2116 /** Data passed to the action handler of com_rm(). */
2117 struct com_rm_action_data {
2118 /** Command line flags ((see \ref rm_flags). */
2119 uint32_t flags;
2120 /** Message buffer. */
2121 struct para_buffer pb;
2122 /** Number of audio files removed. */
2123 unsigned num_removed;
2124 };
2125
2126 static int remove_audio_file(__a_unused struct osl_table *table,
2127 struct osl_row *row, const char *name, void *data)
2128 {
2129 struct com_rm_action_data *crd = data;
2130 int ret;
2131
2132 if (crd->flags & RM_FLAG_VERBOSE) {
2133 ret = para_printf(&crd->pb, "removing %s\n", name);
2134 if (ret < 0)
2135 return ret;
2136 }
2137 afs_event(AUDIO_FILE_REMOVE, &crd->pb, row);
2138 ret = osl(osl_del_row(audio_file_table, row));
2139 if (ret < 0)
2140 para_printf(&crd->pb, "%s: %s\n", name, para_strerror(-ret));
2141 else
2142 crd->num_removed++;
2143 return ret;
2144 }
2145
2146 static void com_rm_callback(int fd, const struct osl_object *query)
2147 {
2148 struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
2149 .pb = {
2150 .max_size = SHMMAX,
2151 .private_data = &fd,
2152 .max_size_handler = pass_buffer_as_shm
2153 }
2154 };
2155 int ret;
2156 struct pattern_match_data pmd = {
2157 .table = audio_file_table,
2158 .loop_col_num = AFTCOL_HASH,
2159 .match_col_num = AFTCOL_PATH,
2160 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
2161 .size = query->size - sizeof(uint32_t)},
2162 .data = &crd,
2163 .action = remove_audio_file
2164 };
2165 if (crd.flags & RM_FLAG_FNM_PATHNAME)
2166 pmd.fnmatch_flags |= FNM_PATHNAME;
2167 ret = for_each_matching_row(&pmd);
2168 if (ret < 0) {
2169 para_printf(&crd.pb, "%s\n", para_strerror(-ret));
2170 return;
2171 }
2172 if (!crd.num_removed && !(crd.flags & RM_FLAG_FORCE))
2173 ret = para_printf(&crd.pb, "no matches -- nothing removed\n");
2174 else {
2175 if (crd.flags & RM_FLAG_VERBOSE)
2176 ret = para_printf(&crd.pb, "removed %u files\n", crd.num_removed);
2177 }
2178 if (ret >= 0 && crd.pb.offset)
2179 pass_buffer_as_shm(crd.pb.buf, crd.pb.offset, &fd);
2180 free(crd.pb.buf);
2181 }
2182
2183 /* TODO options: -r (recursive) */
2184 int com_rm(int fd, int argc, char * const * const argv)
2185 {
2186 uint32_t flags = 0;
2187 struct osl_object query = {.data = &flags, .size = sizeof(flags)};
2188 int i, ret;
2189
2190 for (i = 1; i < argc; i++) {
2191 const char *arg = argv[i];
2192 if (arg[0] != '-')
2193 break;
2194 if (!strcmp(arg, "--")) {
2195 i++;
2196 break;
2197 }
2198 if (!strcmp(arg, "-f")) {
2199 flags |= RM_FLAG_FORCE;
2200 continue;
2201 }
2202 if (!strcmp(arg, "-p")) {
2203 flags |= RM_FLAG_FNM_PATHNAME;
2204 continue;
2205 }
2206 if (!strcmp(arg, "-v")) {
2207 flags |= RM_FLAG_VERBOSE;
2208 continue;
2209 }
2210 break;
2211 }
2212 if (i >= argc)
2213 return -E_AFT_SYNTAX;
2214 ret = send_option_arg_callback_request(&query, argc - i, argv + i,
2215 com_rm_callback, send_result, &fd);
2216 if (ret < 0)
2217 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2218 return ret;
2219 }
2220
2221 /**
2222 * Flags used by the cpsi command.
2223 *
2224 * \sa com_cpsi().
2225 */
2226 enum cpsi_flags {
2227 /** Whether the lyrics id should be copied. */
2228 CPSI_FLAG_COPY_LYRICS_ID = 1,
2229 /** Whether the image id should be copied. */
2230 CPSI_FLAG_COPY_IMAGE_ID = 2,
2231 /** Whether the lastplayed time should be copied. */
2232 CPSI_FLAG_COPY_LASTPLAYED = 4,
2233 /** Whether the numplayed count should be copied. */
2234 CPSI_FLAG_COPY_NUMPLAYED = 8,
2235 /** Whether the attributes should be copied. */
2236 CPSI_FLAG_COPY_ATTRIBUTES = 16,
2237 /** Activates verbose mode. */
2238 CPSI_FLAG_VERBOSE = 32,
2239 };
2240
2241 /** Data passed to the action handler of com_cpsi(). */
2242 struct cpsi_action_data {
2243 /** command line flags (see \ref cpsi_flags). */
2244 unsigned flags;
2245 /** Number of audio files changed. */
2246 unsigned num_copied;
2247 /** Message buffer. */
2248 struct para_buffer pb;
2249 /** Values are copied from here. */
2250 struct afs_info source_afsi;
2251 };
2252
2253 static int copy_selector_info(__a_unused struct osl_table *table,
2254 struct osl_row *row, const char *name, void *data)
2255 {
2256 struct cpsi_action_data *cad = data;
2257 struct osl_object target_afsi_obj;
2258 int ret;
2259 struct afs_info old_afsi, target_afsi;
2260 struct afsi_change_event_data aced;
2261
2262 ret = get_afsi_object_of_row(row, &target_afsi_obj);
2263 if (ret < 0)
2264 return ret;
2265 load_afsi(&target_afsi, &target_afsi_obj);
2266 old_afsi = target_afsi;
2267 if (cad->flags & CPSI_FLAG_COPY_LYRICS_ID)
2268 target_afsi.lyrics_id = cad->source_afsi.lyrics_id;
2269 if (cad->flags & CPSI_FLAG_COPY_IMAGE_ID)
2270 target_afsi.image_id = cad->source_afsi.image_id;
2271 if (cad->flags & CPSI_FLAG_COPY_LASTPLAYED)
2272 target_afsi.last_played = cad->source_afsi.last_played;
2273 if (cad->flags & CPSI_FLAG_COPY_NUMPLAYED)
2274 target_afsi.num_played = cad->source_afsi.num_played;
2275 if (cad->flags & CPSI_FLAG_COPY_ATTRIBUTES)
2276 target_afsi.attributes = cad->source_afsi.attributes;
2277 save_afsi(&target_afsi, &target_afsi_obj); /* in-place update */
2278 cad->num_copied++;
2279 if (cad->flags & CPSI_FLAG_VERBOSE) {
2280 ret = para_printf(&cad->pb, "copied afsi to %s\n", name);
2281 if (ret < 0)
2282 return ret;
2283 }
2284 aced.aft_row = row;
2285 aced.old_afsi = &old_afsi;
2286 afs_event(AFSI_CHANGE, &cad->pb, &aced);
2287 return 1;
2288 }
2289
2290 static void com_cpsi_callback(int fd, const struct osl_object *query)
2291 {
2292 struct cpsi_action_data cad = {
2293 .flags = *(unsigned *)query->data,
2294 .pb = {
2295 .max_size = SHMMAX,
2296 .private_data = &fd,
2297 .max_size_handler = pass_buffer_as_shm
2298 }
2299 };
2300 int ret;
2301 char *source_path = (char *)query->data + sizeof(cad.flags);
2302 struct pattern_match_data pmd = {
2303 .table = audio_file_table,
2304 .loop_col_num = AFTCOL_HASH,
2305 .match_col_num = AFTCOL_PATH,
2306 .patterns = {.data = source_path + strlen(source_path) + 1,
2307 .size = query->size - sizeof(cad.flags)
2308 - strlen(source_path) - 1},
2309 .data = &cad,
2310 .action = copy_selector_info
2311 };
2312
2313 ret = get_afsi_of_path(source_path, &cad.source_afsi);
2314 if (ret < 0)
2315 goto out;
2316 ret = for_each_matching_row(&pmd);
2317 out:
2318 if (ret < 0)
2319 para_printf(&cad.pb, "%s\n", para_strerror(-ret));
2320 else if (cad.flags & CPSI_FLAG_VERBOSE) {
2321 if (cad.num_copied)
2322 para_printf(&cad.pb, "copied requested afsi from %s "
2323 "to %u files\n", source_path, cad.num_copied);
2324 else
2325 para_printf(&cad.pb, "nothing copied\n");
2326 }
2327 if (cad.pb.offset)
2328 pass_buffer_as_shm(cad.pb.buf, cad.pb.offset, &fd);
2329 free(cad.pb.buf);
2330 }
2331
2332 int com_cpsi(int fd, int argc, char * const * const argv)
2333 {
2334 unsigned flags = 0;
2335 int i, ret;
2336 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
2337
2338 for (i = 1; i < argc; i++) {
2339 const char *arg = argv[i];
2340 if (arg[0] != '-')
2341 break;
2342 if (!strcmp(arg, "--")) {
2343 i++;
2344 break;
2345 }
2346 if (!strcmp(arg, "-y")) {
2347 flags |= CPSI_FLAG_COPY_LYRICS_ID;
2348 continue;
2349 }
2350 if (!strcmp(arg, "-i")) {
2351 flags |= CPSI_FLAG_COPY_IMAGE_ID;
2352 continue;
2353 }
2354 if (!strcmp(arg, "-l")) {
2355 flags |= CPSI_FLAG_COPY_LASTPLAYED;
2356 continue;
2357 }
2358 if (!strcmp(arg, "-n")) {
2359 flags |= CPSI_FLAG_COPY_NUMPLAYED;
2360 continue;
2361 }
2362 if (!strcmp(arg, "-a")) {
2363 flags |= CPSI_FLAG_COPY_ATTRIBUTES;
2364 continue;
2365 }
2366 if (!strcmp(arg, "-v")) {
2367 flags |= CPSI_FLAG_VERBOSE;
2368 continue;
2369 }
2370 break;
2371 }
2372 if (i + 1 >= argc) /* need at least souce file and pattern */
2373 return -E_AFT_SYNTAX;
2374 if (!(flags & ~CPSI_FLAG_VERBOSE)) /* no copy flags given */
2375 flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags;
2376 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
2377 com_cpsi_callback, send_result, &fd);
2378 if (ret < 0)
2379 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2380 return ret;
2381 }
2382
2383 void afs_stat_callback(int fd, __a_unused const struct osl_object *query)
2384 {
2385 if (current_status_items)
2386 pass_buffer_as_shm(current_status_items,
2387 strlen(current_status_items), &fd);
2388 }
2389
2390 int send_afs_status(int fd)
2391 {
2392 return send_callback_request(afs_stat_callback, NULL, send_result, &fd);
2393 }
2394
2395 /* TODO: optionally fix problems by removing offending rows */
2396 static int check_audio_file(struct osl_row *row, void *data)
2397 {
2398 char *path;
2399 struct para_buffer *pb = data;
2400 struct stat statbuf;
2401 int ret = get_audio_file_path_of_row(row, &path);
2402 struct afs_info afsi;
2403 char *blob_name;
2404
2405 if (ret < 0)
2406 return para_printf(pb, "%s\n", para_strerror(-ret));
2407 if (stat(path, &statbuf) < 0) {
2408 ret = para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
2409 if (ret < 0)
2410 return ret;
2411 } else {
2412 if (!S_ISREG(statbuf.st_mode)) {
2413 ret = para_printf(pb, "%s: not a regular file\n", path);
2414 if (ret < 0)
2415 return ret;
2416 }
2417 }
2418 ret = get_afsi_of_row(row, &afsi);
2419 if (ret < 0)
2420 return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
2421 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
2422 if (ret < 0) {
2423 ret = para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
2424 para_strerror(-ret));
2425 if (ret < 0)
2426 return ret;
2427 }
2428 ret = img_get_name_by_id(afsi.image_id, &blob_name);
2429 if (ret < 0)
2430 ret = para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
2431 para_strerror(-ret));
2432 return ret;
2433 }
2434
2435 /**
2436 * Check the audio file table for inconsistencies.
2437 *
2438 * \param fd The afs socket.
2439 * \param query Unused.
2440 *
2441 * This function always succeeds.
2442 *
2443 * \sa com_check().
2444 */
2445 void aft_check_callback(int fd, __a_unused const struct osl_object *query)
2446 {
2447 struct para_buffer pb = {
2448 .max_size = SHMMAX,
2449 .private_data = &fd,
2450 .max_size_handler = pass_buffer_as_shm
2451 };
2452 int ret = para_printf(&pb, "checking audio file table...\n");
2453
2454 if (ret < 0)
2455 return;
2456 audio_file_loop(&pb, check_audio_file);
2457 if (pb.offset)
2458 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
2459 free(pb.buf);
2460 }
2461
2462 /**
2463 * Close the audio file table.
2464 *
2465 * \param flags Usual flags that are passed to osl_close_table().
2466 *
2467 * \sa osl_close_table().
2468 */
2469 static void aft_close(void)
2470 {
2471 osl_close_table(audio_file_table, OSL_MARK_CLEAN);
2472 audio_file_table = NULL;
2473 free(current_status_items);
2474 current_status_items = NULL;
2475 }
2476
2477 /**
2478 * Open the audio file table.
2479 *
2480 * \param dir The database directory.
2481 *
2482 * \return Standard.
2483 *
2484 * \sa osl_open_table().
2485 */
2486 static int aft_open(const char *dir)
2487 {
2488 int ret;
2489
2490 audio_file_table_desc.dir = dir;
2491 ret = osl(osl_open_table(&audio_file_table_desc, &audio_file_table));
2492 if (ret >= 0) {
2493 unsigned num;
2494 osl_get_num_rows(audio_file_table, &num);
2495 PARA_INFO_LOG("audio file table contains %d files\n", num);
2496 return ret;
2497 }
2498 PARA_INFO_LOG("failed to open audio file table\n");
2499 audio_file_table = NULL;
2500 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
2501 return 1;
2502 return ret;
2503 }
2504
2505 static int aft_create(const char *dir)
2506 {
2507 audio_file_table_desc.dir = dir;
2508 return osl(osl_create_table(&audio_file_table_desc));
2509 }
2510
2511 static int clear_attribute(struct osl_row *row, void *data)
2512 {
2513 struct rmatt_event_data *red = data;
2514 struct afs_info afsi;
2515 struct osl_object obj;
2516 int ret = get_afsi_object_of_row(row, &obj);
2517 uint64_t mask = ~(1ULL << red->bitnum);
2518
2519 if (ret < 0)
2520 return ret;
2521 ret = load_afsi(&afsi, &obj);
2522 if (ret < 0)
2523 return ret;
2524 afsi.attributes &= mask;
2525 save_afsi(&afsi, &obj);
2526 return 1;
2527 }
2528
2529 static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
2530 void *data)
2531 {
2532 int ret;
2533
2534 switch(event) {
2535 case ATTRIBUTE_REMOVE: {
2536 const struct rmatt_event_data *red = data;
2537 ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
2538 "entries in the audio file table\n", red->name,
2539 red->bitnum);
2540 if (ret < 0)
2541 return ret;
2542 return audio_file_loop(data, clear_attribute);
2543 }
2544 default:
2545 return 1;
2546 }
2547 }
2548
2549 void aft_init(struct afs_table *t)
2550 {
2551 t->name = audio_file_table_desc.name;
2552 t->open = aft_open;
2553 t->close = aft_close;
2554 t->create = aft_create;
2555 t->event_handler = aft_event_handler;
2556 }