Obtain afs status items directly from afs.
[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 char *make_attribute_lines(const char *att_bitmap, struct afs_info *afsi)
727 {
728 char *att_text, *att_lines;
729
730 get_attribute_text(&afsi->attributes, " ", &att_text);
731 if (!att_text)
732 return para_strdup(att_bitmap);
733 att_lines = make_message("%s: %s\n%s: %s",
734 status_item_list[SI_ATTRIBUTES_BITMAP], att_bitmap,
735 status_item_list[SI_ATTRIBUTES_TXT], att_text);
736 free(att_text);
737 return att_lines;
738 }
739
740 static char *make_lyrics_lines(struct afs_info *afsi)
741 {
742 char *lyrics_name;
743
744 lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
745 return make_message("%s: %u\n%s: %s\n",
746 status_item_list[SI_LYRICS_ID], afsi->lyrics_id,
747 status_item_list[SI_LYRICS_NAME], lyrics_name?
748 lyrics_name : "(none)");
749 }
750
751 static char *make_image_lines(struct afs_info *afsi)
752 {
753 char *image_name;
754 img_get_name_by_id(afsi->image_id, &image_name);
755 return make_message("%s: %u\n%s: %s\n",
756 status_item_list[SI_IMAGE_ID], afsi->image_id,
757 status_item_list[SI_IMAGE_NAME], image_name?
758 image_name : "(none)");
759 }
760
761 static char *make_filename_lines(const char *path, unsigned flags)
762 {
763 char *dirname, *ret;
764 const char *basename;
765
766 if (!(flags & LS_FLAG_FULL_PATH))
767 return make_message("%s: %s\n",
768 status_item_list[SI_BASENAME], path);
769 basename = para_basename(path),
770 dirname = para_dirname(path);
771 ret = make_message("%s: %s\n%s: %s\n%s: %s\n",
772 status_item_list[SI_PATH], path,
773 status_item_list[SI_DIRECTORY], dirname? dirname : "?",
774 status_item_list[SI_BASENAME], basename? basename : "?");
775 free(dirname);
776 return ret;
777 }
778
779 static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
780 {
781 struct osl_object chunk_table_obj;
782 struct osl_row *aft_row;
783 int ret, i;
784 char *buf;
785
786 ret = aft_get_row_of_hash(d->hash, &aft_row);
787 if (ret < 0)
788 return ret;
789 ret = osl_open_disk_object(audio_file_table, aft_row,
790 AFTCOL_CHUNKS, &chunk_table_obj);
791 if (ret < 0)
792 return ret;
793 ret = para_printf(b, "%s\n"
794 "chunk_time: %lu:%lu\nchunk_offsets: ",
795 d->path,
796 (long unsigned) d->afhi.chunk_tv.tv_sec,
797 (long unsigned) d->afhi.chunk_tv.tv_usec
798 );
799 if (ret < 0)
800 goto out;
801 buf = chunk_table_obj.data;
802 for (i = 0; i <= d->afhi.chunks_total; i++) {
803 ret = para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
804 if (ret < 0)
805 goto out;
806 }
807 ret = para_printf(b, "\n");
808 out:
809 osl_close_disk_object(&chunk_table_obj);
810 return ret;
811 }
812
813 static int print_list_item(struct ls_data *d, struct ls_options *opts,
814 struct para_buffer *b, time_t current_time)
815 {
816 int ret;
817 char att_buf[65];
818 char last_played_time[30];
819 char duration_buf[30]; /* nobody has an audio file long enough to overflow this */
820 char score_buf[30] = "";
821 struct afs_info *afsi = &d->afsi;
822 struct afh_info *afhi = &d->afhi;
823 struct ls_widths *w = &opts->widths;
824 int have_score = opts->flags & LS_FLAG_ADMISSIBLE_ONLY;
825 char asc_hash[2 * HASH_SIZE + 1];
826 char *att_lines, *lyrics_lines, *image_lines, *filename_lines;
827
828 if (opts->mode == LS_MODE_SHORT) {
829 ret = para_printf(b, "%s\n", d->path);
830 goto out;
831 }
832 if (opts->mode == LS_MODE_CHUNKS) {
833 ret = print_chunk_table(d, b);
834 goto out;
835 }
836 get_attribute_bitmap(&afsi->attributes, att_buf);
837 ret = get_local_time(&afsi->last_played, last_played_time,
838 sizeof(last_played_time), current_time, opts->mode);
839 if (ret < 0)
840 goto out;
841 get_duration_buf(afhi->seconds_total, duration_buf, opts);
842 if (have_score) {
843 if (opts->mode == LS_MODE_LONG)
844 sprintf(score_buf, "%*li ", w->score_width, d->score);
845 else
846 sprintf(score_buf, "%li ", d->score);
847 }
848
849 if (opts->mode == LS_MODE_LONG) {
850 ret = para_printf(b,
851 "%s" /* score */
852 "%s " /* attributes */
853 "%*u " /* amp */
854 "%*d " /* image_id */
855 "%*d " /* lyrics_id */
856 "%*d " /* bitrate */
857 "%s " /* audio format */
858 "%*d " /* frequency */
859 "%d " /* channels */
860 "%s " /* duration */
861 "%*d " /* num_played */
862 "%s " /* last_played */
863 "%s\n", /* path */
864 score_buf,
865 att_buf,
866 w->amp_width, afsi->amp,
867 w->image_id_width, afsi->image_id,
868 w->lyrics_id_width, afsi->lyrics_id,
869 w->bitrate_width, afhi->bitrate,
870 audio_format_name(afsi->audio_format_id),
871 w->frequency_width, afhi->frequency,
872 afhi->channels,
873 duration_buf,
874 w->num_played_width, afsi->num_played,
875 last_played_time,
876 d->path
877 );
878 goto out;
879 }
880 hash_to_asc(d->hash, asc_hash);
881 att_lines = make_attribute_lines(att_buf, afsi);
882 lyrics_lines = make_lyrics_lines(afsi);
883 image_lines = make_image_lines(afsi);
884 filename_lines = make_filename_lines(d->path, opts->flags);
885 if (opts->mode == LS_MODE_MBOX) {
886 const char *bn = para_basename(d->path);
887 ret = para_printf(b,
888 "From foo@localhost %s\n"
889 "Received: from\nTo: bar\nFrom: a\n"
890 "Subject: %s\n\n",
891 last_played_time,
892 bn? bn : "?");
893 if (ret < 0)
894 goto out;
895 }
896 ret = para_printf(b,
897 "%s" /* filename stuff */
898 "%s%s%s%s" /* score */
899 "%s\n" /* attributes */
900 "%s: %s\n" /* hash */
901 "%s" /* image id, image name */
902 "%s" /* lyrics */
903 "%s: %dkbit/s\n" /* bitrate */
904 "%s: %s\n" /* format */
905 "%s: %dHz\n" /* frequency */
906 "%s: %d\n" /* channels */
907 "%s: %s\n" /* duration */
908 "%s: %lu\n" /* seconds total */
909 "%s: %s\n" /* last played time */
910 "%s: %d\n" /* num_played */
911 "%s: %u\n" /* amplification */
912 "%s: %lu\n" /* chunk time */
913 "%s: %lu\n" /* num chunks */
914 "%s: %s\n" /* techinfo */
915 "%s: %s\n" /* artist */
916 "%s: %s\n" /* title */
917 "%s: %s\n" /* year */
918 "%s: %s\n" /* album */
919 "%s: %s\n", /* comment */
920 filename_lines,
921 have_score? status_item_list[SI_SCORE] : "",
922 have_score? ": " : "",
923 score_buf,
924 have_score? "\n" : "",
925 att_lines,
926 status_item_list[SI_HASH], asc_hash,
927 image_lines,
928 lyrics_lines,
929 status_item_list[SI_BITRATE], afhi->bitrate,
930 status_item_list[SI_FORMAT], audio_format_name(afsi->audio_format_id),
931 status_item_list[SI_FREQUENCY], afhi->frequency,
932 status_item_list[SI_CHANNELS], afhi->channels,
933 status_item_list[SI_DURATION], duration_buf,
934 status_item_list[SI_SECONDS_TOTAL], afhi->seconds_total,
935 status_item_list[SI_LAST_PLAYED], last_played_time,
936 status_item_list[SI_NUM_PLAYED], afsi->num_played,
937 status_item_list[SI_AMPLIFICATION], afsi->amp,
938 status_item_list[SI_CHUNK_TIME], tv2ms(&afhi->chunk_tv),
939 status_item_list[SI_NUM_CHUNKS], afhi->chunks_total,
940 status_item_list[SI_TECHINFO], afhi->techinfo,
941 status_item_list[SI_ARTIST], afhi->tags.artist,
942 status_item_list[SI_TITLE], afhi->tags.title,
943 status_item_list[SI_YEAR], afhi->tags.year,
944 status_item_list[SI_ALBUM], afhi->tags.album,
945 status_item_list[SI_COMMENT], afhi->tags.comment
946 );
947 if (ret < 0)
948 goto out;
949 if (opts->mode == LS_MODE_MBOX) {
950 struct osl_object lyrics_def;
951 lyr_get_def_by_id(afsi->lyrics_id, &lyrics_def);
952 if (lyrics_def.data) {
953 ret = para_printf(b, "Lyrics:\n~~~~~~~\n%s",
954 (char *)lyrics_def.data);
955 osl_close_disk_object(&lyrics_def);
956 }
957 }
958 free(att_lines);
959 free(lyrics_lines);
960 free(image_lines);
961 free(filename_lines);
962 out:
963 return ret;
964 }
965
966 static int make_status_items(struct audio_file_data *afd,
967 struct afs_info *afsi, char *path, long score,
968 HASH_TYPE *hash)
969 {
970 struct ls_data d = {
971 .afhi = afd->afhi,
972 .afsi = *afsi,
973 .path = path,
974 .score = score,
975 .hash = hash
976 };
977 struct ls_options opts = {
978 .flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
979 .mode = LS_MODE_VERBOSE,
980 };
981 struct para_buffer pb = {.max_size = SHMMAX - 1};
982 time_t current_time;
983 int ret;
984
985 time(&current_time);
986 ret = print_list_item(&d, &opts, &pb, current_time);
987 if (ret < 0)
988 return ret;
989 free(current_status_items);
990 current_status_items = pb.buf;
991 return 1;
992 }
993
994 /**
995 * Mmap the given audio file and update statistics.
996 *
997 * \param aft_row Determines the audio file to be opened and updated.
998 * \param score The score of the audio file.
999 * \param afd Result pointer.
1000 *
1001 * On success, the numplayed field of the audio file selector info is increased
1002 * and the lastplayed time is set to the current time. Finally, the score of
1003 * the audio file is updated.
1004 *
1005 * \return Positive shmid on success, negative on errors.
1006 */
1007 int open_and_update_audio_file(struct osl_row *aft_row, long score,
1008 struct audio_file_data *afd)
1009 {
1010 HASH_TYPE *aft_hash, file_hash[HASH_SIZE];
1011 struct osl_object afsi_obj;
1012 struct afs_info old_afsi, new_afsi;
1013 int ret = get_hash_of_row(aft_row, &aft_hash);
1014 struct afsi_change_event_data aced;
1015 struct osl_object map, chunk_table_obj;
1016 char *path;
1017
1018 if (ret < 0)
1019 return ret;
1020 ret = get_audio_file_path_of_row(aft_row, &path);
1021 if (ret < 0)
1022 return ret;
1023 ret = get_afsi_object_of_row(aft_row, &afsi_obj);
1024 if (ret < 0)
1025 return ret;
1026 ret = load_afsi(&old_afsi, &afsi_obj);
1027 if (ret < 0)
1028 return ret;
1029 ret = get_afhi_of_row(aft_row, &afd->afhi);
1030 if (ret < 0)
1031 return ret;
1032 afd->afhi.chunk_table = NULL;
1033 ret = osl_open_disk_object(audio_file_table, aft_row,
1034 AFTCOL_CHUNKS, &chunk_table_obj);
1035 if (ret < 0)
1036 goto err;
1037 ret = mmap_full_file(path, O_RDONLY, &map.data,
1038 &map.size, &afd->fd);
1039 if (ret < 0)
1040 goto err;
1041 hash_function(map.data, map.size, file_hash);
1042 ret = hash_compare(file_hash, aft_hash);
1043 para_munmap(map.data, map.size);
1044 if (ret) {
1045 ret = -E_HASH_MISMATCH;
1046 goto err;
1047 }
1048 new_afsi = old_afsi;
1049 new_afsi.num_played++;
1050 new_afsi.last_played = time(NULL);
1051 save_afsi(&new_afsi, &afsi_obj); /* in-place update */
1052
1053 load_chunk_table(&afd->afhi, chunk_table_obj.data);
1054 ret = make_status_items(afd, &old_afsi, path, score, file_hash);
1055 if (ret < 0)
1056 goto err;
1057 aced.aft_row = aft_row;
1058 aced.old_afsi = &old_afsi;
1059 afs_event(AFSI_CHANGE, NULL, &aced);
1060 ret = save_afd(afd);
1061 err:
1062 free(afd->afhi.chunk_table);
1063 osl_close_disk_object(&chunk_table_obj);
1064 return ret;
1065 }
1066
1067 static int ls_audio_format_compare(const void *a, const void *b)
1068 {
1069 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1070 return NUM_COMPARE(d1->afsi.audio_format_id, d2->afsi.audio_format_id);
1071 }
1072
1073 static int ls_duration_compare(const void *a, const void *b)
1074 {
1075 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1076 return NUM_COMPARE(d1->afhi.seconds_total, d2->afhi.seconds_total);
1077 }
1078
1079 static int ls_bitrate_compare(const void *a, const void *b)
1080 {
1081 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1082 return NUM_COMPARE(d1->afhi.bitrate, d2->afhi.bitrate);
1083 }
1084
1085 static int ls_lyrics_id_compare(const void *a, const void *b)
1086 {
1087 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1088 return NUM_COMPARE(d1->afsi.lyrics_id, d2->afsi.lyrics_id);
1089 }
1090
1091 static int ls_image_id_compare(const void *a, const void *b)
1092 {
1093 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1094 return NUM_COMPARE(d1->afsi.image_id, d2->afsi.image_id);
1095 }
1096
1097 static int ls_channels_compare(const void *a, const void *b)
1098 {
1099 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1100 return NUM_COMPARE(d1->afhi.channels, d2->afhi.channels);
1101 }
1102
1103 static int ls_frequency_compare(const void *a, const void *b)
1104 {
1105 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1106 return NUM_COMPARE(d1->afhi.frequency, d2->afhi.frequency);
1107 }
1108
1109 static int ls_num_played_compare(const void *a, const void *b)
1110 {
1111 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1112 return NUM_COMPARE(d1->afsi.num_played, d2->afsi.num_played);
1113 }
1114
1115 static int ls_last_played_compare(const void *a, const void *b)
1116 {
1117 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1118 return NUM_COMPARE(d1->afsi.last_played, d2->afsi.last_played);
1119 }
1120
1121 static int ls_score_compare(const void *a, const void *b)
1122 {
1123 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1124 return NUM_COMPARE(d1->score, d2->score);
1125 }
1126
1127 static int ls_path_compare(const void *a, const void *b)
1128 {
1129 struct ls_data *d1 = *(struct ls_data **)a, *d2 = *(struct ls_data **)b;
1130 return strcmp(d1->path, d2->path);
1131 }
1132
1133 static int sort_matching_paths(struct ls_options *options)
1134 {
1135 size_t nmemb = options->num_matching_paths;
1136 size_t size = sizeof(*options->data_ptr);
1137 int (*compar)(const void *, const void *);
1138 int i;
1139
1140 options->data_ptr = para_malloc(nmemb * sizeof(*options->data_ptr));
1141 for (i = 0; i < nmemb; i++)
1142 options->data_ptr[i] = options->data + i;
1143
1144 /* In these cases the array is already sorted */
1145 if (options->sorting == LS_SORT_BY_PATH
1146 && !(options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1147 && (options->flags & LS_FLAG_FULL_PATH))
1148 return 1;
1149 if (options->sorting == LS_SORT_BY_SCORE &&
1150 options->flags & LS_FLAG_ADMISSIBLE_ONLY)
1151 return 1;
1152
1153 switch (options->sorting) {
1154 case LS_SORT_BY_PATH:
1155 compar = ls_path_compare; break;
1156 case LS_SORT_BY_SCORE:
1157 compar = ls_score_compare; break;
1158 case LS_SORT_BY_LAST_PLAYED:
1159 compar = ls_last_played_compare; break;
1160 case LS_SORT_BY_NUM_PLAYED:
1161 compar = ls_num_played_compare; break;
1162 case LS_SORT_BY_FREQUENCY:
1163 compar = ls_frequency_compare; break;
1164 case LS_SORT_BY_CHANNELS:
1165 compar = ls_channels_compare; break;
1166 case LS_SORT_BY_IMAGE_ID:
1167 compar = ls_image_id_compare; break;
1168 case LS_SORT_BY_LYRICS_ID:
1169 compar = ls_lyrics_id_compare; break;
1170 case LS_SORT_BY_BITRATE:
1171 compar = ls_bitrate_compare; break;
1172 case LS_SORT_BY_DURATION:
1173 compar = ls_duration_compare; break;
1174 case LS_SORT_BY_AUDIO_FORMAT:
1175 compar = ls_audio_format_compare; break;
1176 default:
1177 return -E_BAD_SORT;
1178 }
1179 qsort(options->data_ptr, nmemb, size, compar);
1180 return 1;
1181 }
1182
1183 /* row is either an aft_row or a row of the score table */
1184 /* TODO: Only compute widths if we need them */
1185 static int prepare_ls_row(struct osl_row *row, void *ls_opts)
1186 {
1187 int ret, i;
1188 struct ls_options *options = ls_opts;
1189 struct ls_data *d;
1190 struct ls_widths *w;
1191 unsigned short num_digits;
1192 unsigned tmp;
1193 struct osl_row *aft_row;
1194 long score;
1195 char *path;
1196
1197 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1198 ret = get_score_and_aft_row(row, &score, &aft_row);
1199 if (ret < 0)
1200 return ret;
1201 } else
1202 aft_row = row;
1203 ret = get_audio_file_path_of_row(aft_row, &path);
1204 if (ret < 0)
1205 return ret;
1206 if (!(options->flags & LS_FLAG_FULL_PATH)) {
1207 char *p = strrchr(path, '/');
1208 if (p)
1209 path = p + 1;
1210 }
1211 if (options->num_patterns) {
1212 for (i = 0; i < options->num_patterns; i++) {
1213 ret = fnmatch(options->patterns[i], path, 0);
1214 if (!ret)
1215 break;
1216 if (ret == FNM_NOMATCH)
1217 continue;
1218 return -E_FNMATCH;
1219 }
1220 if (i >= options->num_patterns) /* no match */
1221 return 1;
1222 }
1223 tmp = options->num_matching_paths++;
1224 if (options->num_matching_paths > options->array_size) {
1225 options->array_size++;
1226 options->array_size *= 2;
1227 options->data = para_realloc(options->data, options->array_size
1228 * sizeof(*options->data));
1229 }
1230 d = options->data + tmp;
1231 ret = get_afsi_of_row(aft_row, &d->afsi);
1232 if (ret < 0)
1233 return ret;
1234 ret = get_afhi_of_row(aft_row, &d->afhi);
1235 if (ret < 0)
1236 return ret;
1237 d->path = path;
1238 ret = get_hash_of_row(aft_row, &d->hash);
1239 if (ret < 0)
1240 goto err;
1241 w = &options->widths;
1242 GET_NUM_DIGITS(d->afsi.image_id, &num_digits);
1243 w->image_id_width = PARA_MAX(w->image_id_width, num_digits);
1244 GET_NUM_DIGITS(d->afsi.lyrics_id, &num_digits);
1245 w->lyrics_id_width = PARA_MAX(w->lyrics_id_width, num_digits);
1246 GET_NUM_DIGITS(d->afhi.bitrate, &num_digits);
1247 w->bitrate_width = PARA_MAX(w->bitrate_width, num_digits);
1248 GET_NUM_DIGITS(d->afhi.frequency, &num_digits);
1249 w->frequency_width = PARA_MAX(w->frequency_width, num_digits);
1250 GET_NUM_DIGITS(d->afsi.num_played, &num_digits);
1251 w->num_played_width = PARA_MAX(w->num_played_width, num_digits);
1252 /* get the number of chars to print this amount of time */
1253 num_digits = get_duration_width(d->afhi.seconds_total);
1254 w->duration_width = PARA_MAX(w->duration_width, num_digits);
1255 GET_NUM_DIGITS(d->afsi.amp, &num_digits);
1256 w->amp_width = PARA_MAX(w->amp_width, num_digits);
1257 if (options->flags & LS_FLAG_ADMISSIBLE_ONLY) {
1258 GET_NUM_DIGITS(score, &num_digits);
1259 num_digits++; /* add one for the sign (space or "-") */
1260 w->score_width = PARA_MAX(w->score_width, num_digits);
1261 d->score = score;
1262 }
1263 return 1;
1264 err:
1265 return ret;
1266 }
1267
1268 static void com_ls_callback(int fd, const struct osl_object *query)
1269 {
1270 struct ls_options *opts = query->data;
1271 char *p, *pattern_start = (char *)query->data + sizeof(*opts);
1272 struct para_buffer b = {.max_size = SHMMAX,
1273 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1274 int i = 0, ret;
1275 time_t current_time;
1276
1277
1278 if (opts->num_patterns) {
1279 opts->patterns = para_malloc(opts->num_patterns * sizeof(char *));
1280 for (i = 0, p = pattern_start; i < opts->num_patterns; i++) {
1281 opts->patterns[i] = p;
1282 p += strlen(p) + 1;
1283 }
1284 } else
1285 opts->patterns = NULL;
1286 if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY)
1287 ret = admissible_file_loop(opts, prepare_ls_row);
1288 else
1289 ret = osl(osl_rbtree_loop(audio_file_table, AFTCOL_PATH, opts,
1290 prepare_ls_row));
1291 if (ret < 0)
1292 goto out;
1293 if (!opts->num_matching_paths)
1294 goto out;
1295 ret = sort_matching_paths(opts);
1296 if (ret < 0)
1297 goto out;
1298 time(&current_time);
1299 if (opts->flags & LS_FLAG_REVERSE)
1300 for (i = opts->num_matching_paths - 1; i >= 0; i--) {
1301 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1302 if (ret < 0)
1303 goto out;
1304 }
1305 else
1306 for (i = 0; i < opts->num_matching_paths; i++) {
1307 ret = print_list_item(opts->data_ptr[i], opts, &b, current_time);
1308 if (ret < 0)
1309 goto out;
1310 }
1311 out:
1312 if (b.offset)
1313 pass_buffer_as_shm(b.buf, b.offset, &fd);
1314 free(b.buf);
1315 free(opts->data);
1316 free(opts->data_ptr);
1317 free(opts->patterns);
1318 }
1319
1320 /*
1321 * TODO: flags -h (sort by hash)
1322 */
1323 int com_ls(int fd, int argc, char * const * const argv)
1324 {
1325 int i, ret;
1326 unsigned flags = 0;
1327 enum ls_sorting_method sort = LS_SORT_BY_PATH;
1328 enum ls_listing_mode mode = LS_MODE_SHORT;
1329 struct ls_options opts = {.patterns = NULL};
1330 struct osl_object query = {.data = &opts, .size = sizeof(opts)};
1331
1332 for (i = 1; i < argc; i++) {
1333 const char *arg = argv[i];
1334 if (arg[0] != '-')
1335 break;
1336 if (!strcmp(arg, "--")) {
1337 i++;
1338 break;
1339 }
1340 if (!strncmp(arg, "-l", 2)) {
1341 if (!*(arg + 2)) {
1342 mode = LS_MODE_LONG;
1343 continue;
1344 }
1345 if (*(arg + 3))
1346 return -E_AFT_SYNTAX;
1347 switch(*(arg + 2)) {
1348 case 's':
1349 mode = LS_MODE_SHORT;
1350 continue;
1351 case 'l':
1352 mode = LS_MODE_LONG;
1353 continue;
1354 case 'v':
1355 mode = LS_MODE_VERBOSE;
1356 continue;
1357 case 'm':
1358 mode = LS_MODE_MBOX;
1359 continue;
1360 case 'c':
1361 mode = LS_MODE_CHUNKS;
1362 continue;
1363 default:
1364 return -E_AFT_SYNTAX;
1365 }
1366 }
1367 if (!strcmp(arg, "-p")) {
1368 flags |= LS_FLAG_FULL_PATH;
1369 continue;
1370 }
1371 if (!strcmp(arg, "-a")) {
1372 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1373 continue;
1374 }
1375 if (!strcmp(arg, "-r")) {
1376 flags |= LS_FLAG_REVERSE;
1377 continue;
1378 }
1379 if (!strncmp(arg, "-s", 2)) {
1380 if (!*(arg + 2) || *(arg + 3))
1381 return -E_AFT_SYNTAX;
1382 switch(*(arg + 2)) {
1383 case 'p':
1384 sort = LS_SORT_BY_PATH;
1385 continue;
1386 case 's': /* -ss implies -a */
1387 sort = LS_SORT_BY_SCORE;
1388 flags |= LS_FLAG_ADMISSIBLE_ONLY;
1389 continue;
1390 case 'l':
1391 sort = LS_SORT_BY_LAST_PLAYED;
1392 continue;
1393 case 'n':
1394 sort = LS_SORT_BY_NUM_PLAYED;
1395 continue;
1396 case 'f':
1397 sort = LS_SORT_BY_FREQUENCY;
1398 continue;
1399 case 'c':
1400 sort = LS_SORT_BY_CHANNELS;
1401 continue;
1402 case 'i':
1403 sort = LS_SORT_BY_IMAGE_ID;
1404 continue;
1405 case 'y':
1406 sort = LS_SORT_BY_LYRICS_ID;
1407 continue;
1408 case 'b':
1409 sort = LS_SORT_BY_BITRATE;
1410 continue;
1411 case 'd':
1412 sort = LS_SORT_BY_DURATION;
1413 continue;
1414 case 'a':
1415 sort = LS_SORT_BY_AUDIO_FORMAT;
1416 continue;
1417 default:
1418 return -E_AFT_SYNTAX;
1419 }
1420 }
1421 return -E_AFT_SYNTAX;
1422 }
1423 opts.flags = flags;
1424 opts.sorting = sort;
1425 opts.mode = mode;
1426 opts.num_patterns = argc - i;
1427 ret = send_option_arg_callback_request(&query, opts.num_patterns,
1428 argv + i, com_ls_callback, send_result, &fd);
1429 return ret;
1430 }
1431
1432 /**
1433 * Call the given function for each file in the audio file table.
1434 *
1435 * \param private_data An arbitrary data pointer, passed to \a func.
1436 * \param func The custom function to be called.
1437 *
1438 * \return Standard.
1439 */
1440 int audio_file_loop(void *private_data, osl_rbtree_loop_func *func)
1441 {
1442 return osl(osl_rbtree_loop(audio_file_table, AFTCOL_HASH, private_data,
1443 func));
1444 }
1445
1446 static struct osl_row *find_hash_sister(HASH_TYPE *hash)
1447 {
1448 const struct osl_object obj = {.data = hash, .size = HASH_SIZE};
1449 struct osl_row *row;
1450
1451 osl_get_row(audio_file_table, AFTCOL_HASH, &obj, &row);
1452 return row;
1453 }
1454
1455 /** The format of the data stored by save_audio_file_data(). */
1456 enum com_add_buffer_offsets {
1457 /** afhi (if present) starts here. */
1458 CAB_AFHI_OFFSET_POS = 0,
1459 /** Start of the chunk table (if present). */
1460 CAB_CHUNKS_OFFSET_POS = 2,
1461 /** Audio format id. */
1462 CAB_AUDIO_FORMAT_OFFSET = 4,
1463 /** Flags given to the add command. */
1464 CAB_FLAGS_OFFSET = 5,
1465 /** The hash of the audio file being added. */
1466 CAB_HASH_OFFSET = 9,
1467 /** Start of the path of the audio file. */
1468 CAB_PATH_OFFSET = (CAB_HASH_OFFSET + HASH_SIZE),
1469 };
1470
1471 /*
1472 * Store the given data to a single buffer. Doesn't need the audio file selector
1473 * info struct as the server knows it as well.
1474 *
1475 * It's OK to call this with afhi == NULL. In this case, the audio format
1476 * handler info won't be stored in the buffer.
1477 */
1478 static void save_add_callback_buffer(HASH_TYPE *hash, const char *path,
1479 struct afh_info *afhi, uint32_t flags,
1480 uint8_t audio_format_num, struct osl_object *obj)
1481 {
1482 size_t path_len = strlen(path) + 1;
1483 size_t afhi_size = sizeof_afhi_buf(afhi);
1484 size_t size = CAB_PATH_OFFSET + path_len + afhi_size
1485 + sizeof_chunk_table(afhi);
1486 char *buf = para_malloc(size);
1487 uint16_t pos;
1488
1489 write_u8(buf + CAB_AUDIO_FORMAT_OFFSET, audio_format_num);
1490 write_u32(buf + CAB_FLAGS_OFFSET, flags);
1491
1492 memcpy(buf + CAB_HASH_OFFSET, hash, HASH_SIZE);
1493 strcpy(buf + CAB_PATH_OFFSET, path);
1494
1495 pos = CAB_PATH_OFFSET + path_len;
1496 PARA_DEBUG_LOG("size: %zu, afhi starts at %d\n", size, pos);
1497 PARA_DEBUG_LOG("last afhi byte: %p, pos %zu\n", buf + pos + afhi_size - 1,
1498 pos + afhi_size - 1);
1499 write_u16(buf + CAB_AFHI_OFFSET_POS, pos);
1500 save_afhi(afhi, buf + pos);
1501
1502 pos += afhi_size;
1503 PARA_DEBUG_LOG("size: %zu, chunks start at %d\n", size, pos);
1504 write_u16(buf + CAB_CHUNKS_OFFSET_POS, pos);
1505 if (afhi)
1506 save_chunk_table(afhi, buf + pos);
1507 PARA_DEBUG_LOG("last byte in buf: %p\n", buf + size - 1);
1508 obj->data = buf;
1509 obj->size = size;
1510 }
1511
1512 /*
1513
1514 Overview of the add command.
1515
1516 Input: What was passed to the callback by the command handler.
1517 ~~~~~~
1518 HS: Hash sister. Whether an audio file with identical hash
1519 already exists in the osl database.
1520
1521 PB: Path brother. Whether a file with the given path exists
1522 in the table.
1523
1524 F: Force flag given. Whether add was called with -f.
1525
1526 output: Action performed by the callback.
1527 ~~~~~~~
1528 AFHI: Whether afhi and chunk table are computed and sent.
1529 ACTION: Table modifications to be done by the callback.
1530
1531 +----+----+---+------+---------------------------------------------------+
1532 | HS | PB | F | AFHI | ACTION
1533 +----+----+---+------+---------------------------------------------------+
1534 | Y | Y | Y | Y | if HS != PB: remove PB. HS: force afhi update,
1535 | | update path, keep afsi
1536 +----+----+---+------+---------------------------------------------------+
1537 | Y | Y | N | N | if HS == PB: do not send callback request at all.
1538 | | otherwise: remove PB, HS: update path, keep afhi,
1539 | | afsi.
1540 +----+----+---+------+---------------------------------------------------+
1541 | Y | N | Y | Y | (rename) force afhi update of HS, update path of
1542 | | HS, keep afsi
1543 +----+----+---+------+---------------------------------------------------+
1544 | Y | N | N | N | (file rename) update path of HS, keep afsi, afhi
1545 +----+----+---+------+---------------------------------------------------+
1546 | N | Y | Y | Y | (file change) update afhi, hash, of PB, keep afsi
1547 | | (force has no effect)
1548 +----+----+---+------+---------------------------------------------------+
1549 | N | Y | N | Y | (file change) update afhi, hash of PB, keep afsi
1550 +----+----+---+------+---------------------------------------------------+
1551 | N | N | Y | Y | (new file) create new entry (force has no effect)
1552 +----+----+---+------+---------------------------------------------------+
1553 | N | N | N | Y | (new file) create new entry
1554 +----+----+---+------+---------------------------------------------------+
1555
1556 Notes:
1557
1558 afhi <=> force or no HS
1559 F => AFHI
1560
1561 */
1562
1563 /** Flags passed to the add command. */
1564 enum com_add_flags {
1565 /** Skip paths that exist already. */
1566 ADD_FLAG_LAZY = 1,
1567 /** Force adding. */
1568 ADD_FLAG_FORCE = 2,
1569 /** Print what is being done. */
1570 ADD_FLAG_VERBOSE = 4,
1571 /** Try to add files with unknown suffixes. */
1572 ADD_FLAG_ALL = 8,
1573 };
1574
1575 static void com_add_callback(int fd, const struct osl_object *query)
1576 {
1577 char *buf = query->data, *path;
1578 struct osl_row *pb, *aft_row;
1579 struct osl_row *hs;
1580 struct osl_object objs[NUM_AFT_COLUMNS];
1581 HASH_TYPE *hash;
1582 char asc[2 * HASH_SIZE + 1];
1583 int ret;
1584 char afsi_buf[AFSI_SIZE];
1585 uint32_t flags = read_u32(buf + CAB_FLAGS_OFFSET);
1586 struct afs_info default_afsi = {.last_played = 0};
1587 struct para_buffer msg = {.max_size = SHMMAX,
1588 .max_size_handler = pass_buffer_as_shm, .private_data = &fd};
1589 uint16_t afhi_offset, chunks_offset;
1590
1591 hash = (HASH_TYPE *)buf + CAB_HASH_OFFSET;
1592 hash_to_asc(hash, asc);;
1593 objs[AFTCOL_HASH].data = buf + CAB_HASH_OFFSET;
1594 objs[AFTCOL_HASH].size = HASH_SIZE;
1595
1596 path = buf + CAB_PATH_OFFSET;
1597 objs[AFTCOL_PATH].data = path;
1598 objs[AFTCOL_PATH].size = strlen(path) + 1;
1599
1600 PARA_INFO_LOG("request to add %s\n", path);
1601 hs = find_hash_sister(hash);
1602 ret = aft_get_row_of_path(path, &pb);
1603 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1604 goto out;
1605 if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
1606 if (flags & ADD_FLAG_VERBOSE)
1607 ret = para_printf(&msg, "ignoring duplicate\n");
1608 else
1609 ret = 1;
1610 goto out;
1611 }
1612 if (hs && hs != pb) {
1613 struct osl_object obj;
1614 if (pb) { /* hs trumps pb, remove pb */
1615 if (flags & ADD_FLAG_VERBOSE) {
1616 ret = para_printf(&msg, "removing path brother\n");
1617 if (ret < 0)
1618 goto out;
1619 }
1620 ret = osl(osl_del_row(audio_file_table, pb));
1621 if (ret < 0)
1622 goto out;
1623 pb = NULL;
1624 }
1625 /* file rename, update hs' path */
1626 if (flags & ADD_FLAG_VERBOSE) {
1627 ret = osl(osl_get_object(audio_file_table, hs,
1628 AFTCOL_PATH, &obj));
1629 if (ret < 0)
1630 goto out;
1631 ret = para_printf(&msg, "renamed from %s\n", (char *)obj.data);
1632 if (ret < 0)
1633 goto out;
1634 }
1635 ret = osl(osl_update_object(audio_file_table, hs, AFTCOL_PATH,
1636 &objs[AFTCOL_PATH]));
1637 if (ret < 0)
1638 goto out;
1639 afs_event(AUDIO_FILE_RENAME, &msg, hs);
1640 if (!(flags & ADD_FLAG_FORCE))
1641 goto out;
1642 }
1643 /* no hs or force mode, child must have sent afhi */
1644 afhi_offset = read_u16(buf + CAB_AFHI_OFFSET_POS);
1645 chunks_offset = read_u16(buf + CAB_CHUNKS_OFFSET_POS);
1646
1647 objs[AFTCOL_AFHI].data = buf + afhi_offset;
1648 objs[AFTCOL_AFHI].size = chunks_offset - afhi_offset;
1649 ret = -E_NO_AFHI;
1650 if (!objs[AFTCOL_AFHI].size) /* "impossible" */
1651 goto out;
1652 objs[AFTCOL_CHUNKS].data = buf + chunks_offset;
1653 objs[AFTCOL_CHUNKS].size = query->size - chunks_offset;
1654 if (pb && !hs) { /* update pb's hash */
1655 char old_asc[2 * HASH_SIZE + 1];
1656 HASH_TYPE *old_hash;
1657 ret = get_hash_of_row(pb, &old_hash);
1658 if (ret < 0)
1659 goto out;
1660 hash_to_asc(old_hash, old_asc);
1661 if (flags & ADD_FLAG_VERBOSE) {
1662 ret = para_printf(&msg, "file change: %s -> %s\n",
1663 old_asc, asc);
1664 if (ret < 0)
1665 goto out;
1666 }
1667 ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
1668 &objs[AFTCOL_HASH]);
1669 if (ret < 0)
1670 goto out;
1671 }
1672 if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
1673 struct osl_row *row = pb? pb : hs;
1674 /* update afhi and chunk_table */
1675 if (flags & ADD_FLAG_VERBOSE) {
1676 ret = para_printf(&msg, "updating afhi and chunk table\n");
1677 if (ret < 0)
1678 goto out;
1679 }
1680 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_AFHI,
1681 &objs[AFTCOL_AFHI]));
1682 if (ret < 0)
1683 goto out;
1684 ret = osl(osl_update_object(audio_file_table, row, AFTCOL_CHUNKS,
1685 &objs[AFTCOL_CHUNKS]));
1686 if (ret < 0)
1687 goto out;
1688 afs_event(AFHI_CHANGE, &msg, row);
1689 goto out;
1690 }
1691 /* new entry, use default afsi */
1692 if (flags & ADD_FLAG_VERBOSE) {
1693 ret = para_printf(&msg, "new file\n");
1694 if (ret < 0)
1695 goto out;
1696 }
1697 default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
1698 default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_OFFSET);
1699
1700 objs[AFTCOL_AFSI].data = &afsi_buf;
1701 objs[AFTCOL_AFSI].size = AFSI_SIZE;
1702 save_afsi(&default_afsi, &objs[AFTCOL_AFSI]);
1703 ret = osl(osl_add_and_get_row(audio_file_table, objs, &aft_row));
1704 afs_event(AUDIO_FILE_ADD, &msg, aft_row);
1705 out:
1706 if (ret < 0)
1707 para_printf(&msg, "%s\n", para_strerror(-ret));
1708 if (msg.offset)
1709 pass_buffer_as_shm(msg.buf, msg.offset, &fd);
1710 free(msg.buf);
1711 }
1712
1713 /** Used by com_add(). */
1714 struct private_add_data {
1715 /** The socket file descriptor. */
1716 int fd;
1717 /** The given add flags. */
1718 uint32_t flags;
1719 };
1720
1721 static void path_brother_callback(int fd, const struct osl_object *query)
1722 {
1723 char *path = query->data;
1724 struct osl_row *path_brother;
1725 int ret = aft_get_row_of_path(path, &path_brother);
1726 if (ret < 0)
1727 return;
1728 pass_buffer_as_shm((char *)&path_brother, sizeof(path_brother), &fd);
1729 }
1730
1731 static void hash_sister_callback(int fd, const struct osl_object *query)
1732 {
1733 HASH_TYPE *hash = query->data;
1734 struct osl_row *hash_sister;
1735
1736 hash_sister = find_hash_sister(hash);
1737 if (!hash_sister)
1738 return;
1739 pass_buffer_as_shm((char *)&hash_sister, sizeof(hash_sister), &fd);
1740 }
1741
1742 static int get_row_pointer_from_result(struct osl_object *result, void *private)
1743 {
1744 struct osl_row **row = private;
1745 *row = result->data;
1746 return 1;
1747 }
1748
1749 static int add_one_audio_file(const char *path, void *private_data)
1750 {
1751 int ret, send_ret = 1, fd;
1752 uint8_t format_num = -1;
1753 struct private_add_data *pad = private_data;
1754 struct afh_info afhi, *afhi_ptr = NULL;
1755 struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */
1756 struct osl_object map, obj = {.data = NULL}, query;
1757 HASH_TYPE hash[HASH_SIZE];
1758
1759 ret = guess_audio_format(path);
1760 if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
1761 goto out_free;
1762 query.data = (char *)path;
1763 query.size = strlen(path) + 1;
1764 ret = send_callback_request(path_brother_callback, &query,
1765 get_row_pointer_from_result, &pb);
1766 if (ret < 0 && ret != -OSL_ERRNO_TO_PARA_ERROR(E_OSL_RB_KEY_NOT_FOUND))
1767 goto out_free;
1768 ret = 1;
1769 if (pb && (pad->flags & ADD_FLAG_LAZY)) { /* lazy is really cheap */
1770 if (pad->flags & ADD_FLAG_VERBOSE)
1771 send_ret = send_va_buffer(pad->fd, "lazy-ignore: %s\n", path);
1772 goto out_free;
1773 }
1774 /* We still want to add this file. Compute its hash. */
1775 ret = mmap_full_file(path, O_RDONLY, &map.data, &map.size, &fd);
1776 if (ret < 0)
1777 goto out_free;
1778 hash_function(map.data, map.size, hash);
1779
1780 /* Check whether the database contains a file with the same hash. */
1781 query.data = hash;
1782 query.size = HASH_SIZE;
1783 ret = send_callback_request(hash_sister_callback, &query,
1784 get_row_pointer_from_result, &hs);
1785 if (ret < 0)
1786 goto out_unmap;
1787 /* Return success if we already know this file. */
1788 ret = 1;
1789 if (pb && hs && hs == pb && !(pad->flags & ADD_FLAG_FORCE)) {
1790 if (pad->flags & ADD_FLAG_VERBOSE)
1791 send_ret = send_va_buffer(pad->fd,
1792 "%s exists, not forcing update\n", path);
1793 goto out_unmap;
1794 }
1795 /*
1796 * We won't recalculate the audio format info and the chunk table if
1797 * there is a hash sister and FORCE was not given.
1798 */
1799 if (!hs || (pad->flags & ADD_FLAG_FORCE)) {
1800 ret = compute_afhi(path, map.data, map.size, fd, &afhi);
1801 if (ret < 0)
1802 goto out_unmap;
1803 format_num = ret;
1804 afhi_ptr = &afhi;
1805 }
1806 munmap(map.data, map.size);
1807 close(fd);
1808 if (pad->flags & ADD_FLAG_VERBOSE) {
1809 send_ret = send_va_buffer(pad->fd, "adding %s\n", path);
1810 if (send_ret < 0)
1811 goto out_free;
1812 }
1813 save_add_callback_buffer(hash, path, afhi_ptr, pad->flags, format_num, &obj);
1814 /* Ask afs to consider this entry for adding. */
1815 ret = send_callback_request(com_add_callback, &obj, send_result, &pad->fd);
1816 goto out_free;
1817
1818 out_unmap:
1819 close(fd);
1820 munmap(map.data, map.size);
1821 out_free:
1822 if (ret < 0 && send_ret >= 0)
1823 send_ret = send_va_buffer(pad->fd, "failed to add %s (%s)\n", path,
1824 para_strerror(-ret));
1825 free(obj.data);
1826 if (afhi_ptr) {
1827 free(afhi_ptr->chunk_table);
1828 free(afhi_ptr->techinfo);
1829 free(afhi_ptr->tags.artist);
1830 free(afhi_ptr->tags.title);
1831 free(afhi_ptr->tags.year);
1832 free(afhi_ptr->tags.album);
1833 free(afhi_ptr->tags.comment);
1834 }
1835 /* Stop adding files only on send errors. */
1836 return send_ret;
1837 }
1838
1839 int com_add(int fd, int argc, char * const * const argv)
1840 {
1841 int i, ret;
1842 struct private_add_data pad = {.fd = fd, .flags = 0};
1843 struct stat statbuf;
1844
1845 for (i = 1; i < argc; i++) {
1846 const char *arg = argv[i];
1847 if (arg[0] != '-')
1848 break;
1849 if (!strcmp(arg, "--")) {
1850 i++;
1851 break;
1852 }
1853 if (!strcmp(arg, "-a")) {
1854 pad.flags |= ADD_FLAG_ALL;
1855 continue;
1856 }
1857 if (!strcmp(arg, "-l")) {
1858 pad.flags |= ADD_FLAG_LAZY;
1859 continue;
1860 }
1861 if (!strcmp(arg, "-f")) {
1862 pad.flags |= ADD_FLAG_FORCE;
1863 continue;
1864 }
1865 if (!strcmp(arg, "-v")) {
1866 pad.flags |= ADD_FLAG_VERBOSE;
1867 continue;
1868 }
1869 }
1870 if (argc <= i)
1871 return -E_AFT_SYNTAX;
1872 for (; i < argc; i++) {
1873 char *path;
1874 ret = verify_path(argv[i], &path);
1875 if (ret < 0) {
1876 ret = send_va_buffer(fd, "%s: %s\n", argv[i],
1877 para_strerror(-ret));
1878 if (ret < 0)
1879 return ret;
1880 continue;
1881 }
1882 ret = stat(path, &statbuf);
1883 if (ret < 0) {
1884 ret = send_va_buffer(fd, "failed to stat %s (%s)\n", path,
1885 strerror(errno));
1886 free(path);
1887 if (ret < 0)
1888 return ret;
1889 continue;
1890 }
1891 if (S_ISDIR(statbuf.st_mode))
1892 ret = for_each_file_in_dir(path, add_one_audio_file,
1893 &pad);
1894 else
1895 ret = add_one_audio_file(path, &pad);
1896 if (ret < 0) {
1897 send_va_buffer(fd, "%s: %s\n", path, para_strerror(-ret));
1898 free(path);
1899 return ret;
1900 }
1901 free(path);
1902 }
1903 return 1;
1904
1905 }
1906
1907 /**
1908 * Flags used by the touch command.
1909 *
1910 * \sa com_touch().
1911 */
1912 enum touch_flags {
1913 /** Whether the \p FNM_PATHNAME flag should be passed to fnmatch(). */
1914 TOUCH_FLAG_FNM_PATHNAME = 1,
1915 /** Activates verbose mode. */
1916 TOUCH_FLAG_VERBOSE = 2
1917 };
1918
1919 /** Options used by com_touch(). */
1920 struct com_touch_options {
1921 /** New num_played value. */
1922 int32_t num_played;
1923 /** New last played count. */
1924 int64_t last_played;
1925 /** New lyrics id. */
1926 int32_t lyrics_id;
1927 /** New image id. */
1928 int32_t image_id;
1929 /** New amplification value. */
1930 int32_t amp;
1931 /** Command line flags (see \ref touch_flags). */
1932 unsigned flags;
1933 };
1934
1935 /** Data passed to the action handler of com_touch(). */
1936 struct touch_action_data {
1937 /** Command line options (see \ref com_touch_options). */
1938 struct com_touch_options *cto;
1939 /** Message buffer. */
1940 struct para_buffer pb;
1941 /** How many audio files matched the given pattern. */
1942 unsigned num_matches;
1943 };
1944
1945 static int touch_audio_file(__a_unused struct osl_table *table,
1946 struct osl_row *row, const char *name, void *data)
1947 {
1948 struct touch_action_data *tad = data;
1949 struct osl_object obj;
1950 struct afs_info old_afsi, new_afsi;
1951 int ret, no_options = tad->cto->num_played < 0 && tad->cto->last_played < 0 &&
1952 tad->cto->lyrics_id < 0 && tad->cto->image_id < 0 && tad->cto->amp < 0;
1953 struct afsi_change_event_data aced;
1954
1955 ret = get_afsi_object_of_row(row, &obj);
1956 if (ret < 0)
1957 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1958 ret = load_afsi(&old_afsi, &obj);
1959 if (ret < 0)
1960 return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
1961 new_afsi = old_afsi;
1962 if (no_options) {
1963 new_afsi.num_played++;
1964 new_afsi.last_played = time(NULL);
1965 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1966 ret = para_printf(&tad->pb, "%s: num_played = %u, "
1967 "last_played = now()\n", name,
1968 new_afsi.num_played);
1969 if (ret < 0)
1970 return ret;
1971 }
1972 } else {
1973 if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
1974 ret = para_printf(&tad->pb, "touching %s\n", name);
1975 if (ret < 0)
1976 return ret;
1977 }
1978 if (tad->cto->lyrics_id >= 0)
1979 new_afsi.lyrics_id = tad->cto->lyrics_id;
1980 if (tad->cto->image_id >= 0)
1981 new_afsi.image_id = tad->cto->image_id;
1982 if (tad->cto->num_played >= 0)
1983 new_afsi.num_played = tad->cto->num_played;
1984 if (tad->cto->last_played >= 0)
1985 new_afsi.last_played = tad->cto->last_played;
1986 if (tad->cto->amp >= 0)
1987 new_afsi.amp = tad->cto->amp;
1988 }
1989 tad->num_matches++;
1990 save_afsi(&new_afsi, &obj); /* in-place update */
1991 aced.aft_row = row;
1992 aced.old_afsi = &old_afsi;
1993 afs_event(AFSI_CHANGE, &tad->pb, &aced);
1994 return 1;
1995 }
1996
1997 static void com_touch_callback(int fd, const struct osl_object *query)
1998 {
1999 struct touch_action_data tad = {.cto = query->data,
2000 .pb = {
2001 .max_size = SHMMAX,
2002 .private_data = &fd,
2003 .max_size_handler = pass_buffer_as_shm
2004 }
2005 };
2006 int ret, ret2 = 0;
2007 struct pattern_match_data pmd = {
2008 .table = audio_file_table,
2009 .loop_col_num = AFTCOL_HASH,
2010 .match_col_num = AFTCOL_PATH,
2011 .patterns = {.data = (char *)query->data + sizeof(*tad.cto),
2012 .size = query->size - sizeof(*tad.cto)},
2013 .data = &tad,
2014 .action = touch_audio_file
2015 };
2016 if (tad.cto->flags & TOUCH_FLAG_FNM_PATHNAME)
2017 pmd.fnmatch_flags |= FNM_PATHNAME;
2018 ret = for_each_matching_row(&pmd);
2019 if (ret < 0)
2020 ret2 = para_printf(&tad.pb, "%s\n", para_strerror(-ret));
2021 else
2022 if (!tad.num_matches)
2023 ret2 = para_printf(&tad.pb, "no matches\n");
2024 if (ret2 >= 0 && tad.pb.offset)
2025 pass_buffer_as_shm(tad.pb.buf, tad.pb.offset, &fd);
2026 free(tad.pb.buf);
2027 }
2028
2029 int com_touch(int fd, int argc, char * const * const argv)
2030 {
2031 struct com_touch_options cto = {
2032 .num_played = -1,
2033 .last_played = -1,
2034 .lyrics_id = -1,
2035 .image_id = -1,
2036 .amp = -1,
2037 };
2038 struct osl_object query = {.data = &cto, .size = sizeof(cto)};
2039 int i, ret;
2040
2041
2042 for (i = 1; i < argc; i++) {
2043 const char *arg = argv[i];
2044 if (arg[0] != '-')
2045 break;
2046 if (!strcmp(arg, "--")) {
2047 i++;
2048 break;
2049 }
2050 if (!strncmp(arg, "-n", 2)) {
2051 ret = para_atoi32(arg + 2, &cto.num_played);
2052 if (ret < 0)
2053 return ret;
2054 continue;
2055 }
2056 if (!strncmp(arg, "-l", 2)) {
2057 ret = para_atoi64(arg + 2, &cto.last_played);
2058 if (ret < 0)
2059 return ret;
2060 continue;
2061 }
2062 if (!strncmp(arg, "-y", 2)) {
2063 ret = para_atoi32(arg + 2, &cto.lyrics_id);
2064 if (ret < 0)
2065 return ret;
2066 continue;
2067 }
2068 if (!strncmp(arg, "-i", 2)) {
2069 ret = para_atoi32(arg + 2, &cto.image_id);
2070 if (ret < 0)
2071 return ret;
2072 continue;
2073 }
2074 if (!strncmp(arg, "-a", 2)) {
2075 int32_t val;
2076 ret = para_atoi32(arg + 2, &val);
2077 if (ret < 0)
2078 return ret;
2079 if (val < 0 || val > 255)
2080 return -ERRNO_TO_PARA_ERROR(EINVAL);
2081 cto.amp = val;
2082 continue;
2083 }
2084 if (!strcmp(arg, "-p")) {
2085 cto.flags |= TOUCH_FLAG_FNM_PATHNAME;
2086 continue;
2087 }
2088 if (!strcmp(arg, "-v")) {
2089 cto.flags |= TOUCH_FLAG_VERBOSE;
2090 continue;
2091 }
2092 break; /* non-option starting with dash */
2093 }
2094 if (i >= argc)
2095 return -E_AFT_SYNTAX;
2096 ret = send_option_arg_callback_request(&query, argc - i,
2097 argv + i, com_touch_callback, send_result, &fd);
2098 if (ret < 0)
2099 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2100 return ret;
2101 }
2102
2103 /** Flags for com_rm(). */
2104 enum rm_flags {
2105 /** -v */
2106 RM_FLAG_VERBOSE = 1,
2107 /** -f */
2108 RM_FLAG_FORCE = 2,
2109 /** -p */
2110 RM_FLAG_FNM_PATHNAME = 4
2111 };
2112
2113 /** Data passed to the action handler of com_rm(). */
2114 struct com_rm_action_data {
2115 /** Command line flags ((see \ref rm_flags). */
2116 uint32_t flags;
2117 /** Message buffer. */
2118 struct para_buffer pb;
2119 /** Number of audio files removed. */
2120 unsigned num_removed;
2121 };
2122
2123 static int remove_audio_file(__a_unused struct osl_table *table,
2124 struct osl_row *row, const char *name, void *data)
2125 {
2126 struct com_rm_action_data *crd = data;
2127 int ret;
2128
2129 if (crd->flags & RM_FLAG_VERBOSE) {
2130 ret = para_printf(&crd->pb, "removing %s\n", name);
2131 if (ret < 0)
2132 return ret;
2133 }
2134 afs_event(AUDIO_FILE_REMOVE, &crd->pb, row);
2135 ret = osl(osl_del_row(audio_file_table, row));
2136 if (ret < 0)
2137 para_printf(&crd->pb, "%s: %s\n", name, para_strerror(-ret));
2138 else
2139 crd->num_removed++;
2140 return ret;
2141 }
2142
2143 static void com_rm_callback(int fd, const struct osl_object *query)
2144 {
2145 struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
2146 .pb = {
2147 .max_size = SHMMAX,
2148 .private_data = &fd,
2149 .max_size_handler = pass_buffer_as_shm
2150 }
2151 };
2152 int ret;
2153 struct pattern_match_data pmd = {
2154 .table = audio_file_table,
2155 .loop_col_num = AFTCOL_HASH,
2156 .match_col_num = AFTCOL_PATH,
2157 .patterns = {.data = (char *)query->data + sizeof(uint32_t),
2158 .size = query->size - sizeof(uint32_t)},
2159 .data = &crd,
2160 .action = remove_audio_file
2161 };
2162 if (crd.flags & RM_FLAG_FNM_PATHNAME)
2163 pmd.fnmatch_flags |= FNM_PATHNAME;
2164 ret = for_each_matching_row(&pmd);
2165 if (ret < 0) {
2166 para_printf(&crd.pb, "%s\n", para_strerror(-ret));
2167 return;
2168 }
2169 if (!crd.num_removed && !(crd.flags & RM_FLAG_FORCE))
2170 ret = para_printf(&crd.pb, "no matches -- nothing removed\n");
2171 else {
2172 if (crd.flags & RM_FLAG_VERBOSE)
2173 ret = para_printf(&crd.pb, "removed %u files\n", crd.num_removed);
2174 }
2175 if (ret >= 0 && crd.pb.offset)
2176 pass_buffer_as_shm(crd.pb.buf, crd.pb.offset, &fd);
2177 free(crd.pb.buf);
2178 }
2179
2180 /* TODO options: -r (recursive) */
2181 int com_rm(int fd, int argc, char * const * const argv)
2182 {
2183 uint32_t flags = 0;
2184 struct osl_object query = {.data = &flags, .size = sizeof(flags)};
2185 int i, ret;
2186
2187 for (i = 1; i < argc; i++) {
2188 const char *arg = argv[i];
2189 if (arg[0] != '-')
2190 break;
2191 if (!strcmp(arg, "--")) {
2192 i++;
2193 break;
2194 }
2195 if (!strcmp(arg, "-f")) {
2196 flags |= RM_FLAG_FORCE;
2197 continue;
2198 }
2199 if (!strcmp(arg, "-p")) {
2200 flags |= RM_FLAG_FNM_PATHNAME;
2201 continue;
2202 }
2203 if (!strcmp(arg, "-v")) {
2204 flags |= RM_FLAG_VERBOSE;
2205 continue;
2206 }
2207 break;
2208 }
2209 if (i >= argc)
2210 return -E_AFT_SYNTAX;
2211 ret = send_option_arg_callback_request(&query, argc - i, argv + i,
2212 com_rm_callback, send_result, &fd);
2213 if (ret < 0)
2214 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2215 return ret;
2216 }
2217
2218 /**
2219 * Flags used by the cpsi command.
2220 *
2221 * \sa com_cpsi().
2222 */
2223 enum cpsi_flags {
2224 /** Whether the lyrics id should be copied. */
2225 CPSI_FLAG_COPY_LYRICS_ID = 1,
2226 /** Whether the image id should be copied. */
2227 CPSI_FLAG_COPY_IMAGE_ID = 2,
2228 /** Whether the lastplayed time should be copied. */
2229 CPSI_FLAG_COPY_LASTPLAYED = 4,
2230 /** Whether the numplayed count should be copied. */
2231 CPSI_FLAG_COPY_NUMPLAYED = 8,
2232 /** Whether the attributes should be copied. */
2233 CPSI_FLAG_COPY_ATTRIBUTES = 16,
2234 /** Activates verbose mode. */
2235 CPSI_FLAG_VERBOSE = 32,
2236 };
2237
2238 /** Data passed to the action handler of com_cpsi(). */
2239 struct cpsi_action_data {
2240 /** command line flags (see \ref cpsi_flags). */
2241 unsigned flags;
2242 /** Number of audio files changed. */
2243 unsigned num_copied;
2244 /** Message buffer. */
2245 struct para_buffer pb;
2246 /** Values are copied from here. */
2247 struct afs_info source_afsi;
2248 };
2249
2250 static int copy_selector_info(__a_unused struct osl_table *table,
2251 struct osl_row *row, const char *name, void *data)
2252 {
2253 struct cpsi_action_data *cad = data;
2254 struct osl_object target_afsi_obj;
2255 int ret;
2256 struct afs_info old_afsi, target_afsi;
2257 struct afsi_change_event_data aced;
2258
2259 ret = get_afsi_object_of_row(row, &target_afsi_obj);
2260 if (ret < 0)
2261 return ret;
2262 load_afsi(&target_afsi, &target_afsi_obj);
2263 old_afsi = target_afsi;
2264 if (cad->flags & CPSI_FLAG_COPY_LYRICS_ID)
2265 target_afsi.lyrics_id = cad->source_afsi.lyrics_id;
2266 if (cad->flags & CPSI_FLAG_COPY_IMAGE_ID)
2267 target_afsi.image_id = cad->source_afsi.image_id;
2268 if (cad->flags & CPSI_FLAG_COPY_LASTPLAYED)
2269 target_afsi.last_played = cad->source_afsi.last_played;
2270 if (cad->flags & CPSI_FLAG_COPY_NUMPLAYED)
2271 target_afsi.num_played = cad->source_afsi.num_played;
2272 if (cad->flags & CPSI_FLAG_COPY_ATTRIBUTES)
2273 target_afsi.attributes = cad->source_afsi.attributes;
2274 save_afsi(&target_afsi, &target_afsi_obj); /* in-place update */
2275 cad->num_copied++;
2276 if (cad->flags & CPSI_FLAG_VERBOSE) {
2277 ret = para_printf(&cad->pb, "copied afsi to %s\n", name);
2278 if (ret < 0)
2279 return ret;
2280 }
2281 aced.aft_row = row;
2282 aced.old_afsi = &old_afsi;
2283 afs_event(AFSI_CHANGE, &cad->pb, &aced);
2284 return 1;
2285 }
2286
2287 static void com_cpsi_callback(int fd, const struct osl_object *query)
2288 {
2289 struct cpsi_action_data cad = {
2290 .flags = *(unsigned *)query->data,
2291 .pb = {
2292 .max_size = SHMMAX,
2293 .private_data = &fd,
2294 .max_size_handler = pass_buffer_as_shm
2295 }
2296 };
2297 int ret;
2298 char *source_path = (char *)query->data + sizeof(cad.flags);
2299 struct pattern_match_data pmd = {
2300 .table = audio_file_table,
2301 .loop_col_num = AFTCOL_HASH,
2302 .match_col_num = AFTCOL_PATH,
2303 .patterns = {.data = source_path + strlen(source_path) + 1,
2304 .size = query->size - sizeof(cad.flags)
2305 - strlen(source_path) - 1},
2306 .data = &cad,
2307 .action = copy_selector_info
2308 };
2309
2310 ret = get_afsi_of_path(source_path, &cad.source_afsi);
2311 if (ret < 0)
2312 goto out;
2313 ret = for_each_matching_row(&pmd);
2314 out:
2315 if (ret < 0)
2316 para_printf(&cad.pb, "%s\n", para_strerror(-ret));
2317 else if (cad.flags & CPSI_FLAG_VERBOSE) {
2318 if (cad.num_copied)
2319 para_printf(&cad.pb, "copied requested afsi from %s "
2320 "to %u files\n", source_path, cad.num_copied);
2321 else
2322 para_printf(&cad.pb, "nothing copied\n");
2323 }
2324 if (cad.pb.offset)
2325 pass_buffer_as_shm(cad.pb.buf, cad.pb.offset, &fd);
2326 free(cad.pb.buf);
2327 }
2328
2329 int com_cpsi(int fd, int argc, char * const * const argv)
2330 {
2331 unsigned flags = 0;
2332 int i, ret;
2333 struct osl_object options = {.data = &flags, .size = sizeof(flags)};
2334
2335 for (i = 1; i < argc; i++) {
2336 const char *arg = argv[i];
2337 if (arg[0] != '-')
2338 break;
2339 if (!strcmp(arg, "--")) {
2340 i++;
2341 break;
2342 }
2343 if (!strcmp(arg, "-y")) {
2344 flags |= CPSI_FLAG_COPY_LYRICS_ID;
2345 continue;
2346 }
2347 if (!strcmp(arg, "-i")) {
2348 flags |= CPSI_FLAG_COPY_IMAGE_ID;
2349 continue;
2350 }
2351 if (!strcmp(arg, "-l")) {
2352 flags |= CPSI_FLAG_COPY_LASTPLAYED;
2353 continue;
2354 }
2355 if (!strcmp(arg, "-n")) {
2356 flags |= CPSI_FLAG_COPY_NUMPLAYED;
2357 continue;
2358 }
2359 if (!strcmp(arg, "-a")) {
2360 flags |= CPSI_FLAG_COPY_ATTRIBUTES;
2361 continue;
2362 }
2363 if (!strcmp(arg, "-v")) {
2364 flags |= CPSI_FLAG_VERBOSE;
2365 continue;
2366 }
2367 break;
2368 }
2369 if (i + 1 >= argc) /* need at least souce file and pattern */
2370 return -E_AFT_SYNTAX;
2371 if (!(flags & ~CPSI_FLAG_VERBOSE)) /* no copy flags given */
2372 flags = ~(unsigned)CPSI_FLAG_VERBOSE | flags;
2373 ret = send_option_arg_callback_request(&options, argc - i, argv + i,
2374 com_cpsi_callback, send_result, &fd);
2375 if (ret < 0)
2376 send_va_buffer(fd, "%s\n", para_strerror(-ret));
2377 return ret;
2378 }
2379
2380 void afs_stat_callback(int fd, __a_unused const struct osl_object *query)
2381 {
2382 if (current_status_items)
2383 pass_buffer_as_shm(current_status_items,
2384 strlen(current_status_items), &fd);
2385 }
2386
2387 int send_afs_status(int fd)
2388 {
2389 return send_callback_request(afs_stat_callback, NULL, send_result, &fd);
2390 }
2391
2392 /* TODO: optionally fix problems by removing offending rows */
2393 static int check_audio_file(struct osl_row *row, void *data)
2394 {
2395 char *path;
2396 struct para_buffer *pb = data;
2397 struct stat statbuf;
2398 int ret = get_audio_file_path_of_row(row, &path);
2399 struct afs_info afsi;
2400 char *blob_name;
2401
2402 if (ret < 0)
2403 return para_printf(pb, "%s\n", para_strerror(-ret));
2404 if (stat(path, &statbuf) < 0) {
2405 ret = para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
2406 if (ret < 0)
2407 return ret;
2408 } else {
2409 if (!S_ISREG(statbuf.st_mode)) {
2410 ret = para_printf(pb, "%s: not a regular file\n", path);
2411 if (ret < 0)
2412 return ret;
2413 }
2414 }
2415 ret = get_afsi_of_row(row, &afsi);
2416 if (ret < 0)
2417 return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
2418 ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
2419 if (ret < 0) {
2420 ret = para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
2421 para_strerror(-ret));
2422 if (ret < 0)
2423 return ret;
2424 }
2425 ret = img_get_name_by_id(afsi.image_id, &blob_name);
2426 if (ret < 0)
2427 ret = para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
2428 para_strerror(-ret));
2429 return ret;
2430 }
2431
2432 /**
2433 * Check the audio file table for inconsistencies.
2434 *
2435 * \param fd The afs socket.
2436 * \param query Unused.
2437 *
2438 * This function always succeeds.
2439 *
2440 * \sa com_check().
2441 */
2442 void aft_check_callback(int fd, __a_unused const struct osl_object *query)
2443 {
2444 struct para_buffer pb = {
2445 .max_size = SHMMAX,
2446 .private_data = &fd,
2447 .max_size_handler = pass_buffer_as_shm
2448 };
2449 int ret = para_printf(&pb, "checking audio file table...\n");
2450
2451 if (ret < 0)
2452 return;
2453 audio_file_loop(&pb, check_audio_file);
2454 if (pb.offset)
2455 pass_buffer_as_shm(pb.buf, pb.offset, &fd);
2456 free(pb.buf);
2457 }
2458
2459 /**
2460 * Close the audio file table.
2461 *
2462 * \param flags Usual flags that are passed to osl_close_table().
2463 *
2464 * \sa osl_close_table().
2465 */
2466 static void aft_close(void)
2467 {
2468 osl_close_table(audio_file_table, OSL_MARK_CLEAN);
2469 audio_file_table = NULL;
2470 free(current_status_items);
2471 current_status_items = NULL;
2472 }
2473
2474 /**
2475 * Open the audio file table.
2476 *
2477 * \param dir The database directory.
2478 *
2479 * \return Standard.
2480 *
2481 * \sa osl_open_table().
2482 */
2483 static int aft_open(const char *dir)
2484 {
2485 int ret;
2486
2487 audio_file_table_desc.dir = dir;
2488 ret = osl(osl_open_table(&audio_file_table_desc, &audio_file_table));
2489 if (ret >= 0) {
2490 unsigned num;
2491 osl_get_num_rows(audio_file_table, &num);
2492 PARA_INFO_LOG("audio file table contains %d files\n", num);
2493 return ret;
2494 }
2495 PARA_INFO_LOG("failed to open audio file table\n");
2496 audio_file_table = NULL;
2497 if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
2498 return 1;
2499 return ret;
2500 }
2501
2502 static int aft_create(const char *dir)
2503 {
2504 audio_file_table_desc.dir = dir;
2505 return osl(osl_create_table(&audio_file_table_desc));
2506 }
2507
2508 static int clear_attribute(struct osl_row *row, void *data)
2509 {
2510 struct rmatt_event_data *red = data;
2511 struct afs_info afsi;
2512 struct osl_object obj;
2513 int ret = get_afsi_object_of_row(row, &obj);
2514 uint64_t mask = ~(1ULL << red->bitnum);
2515
2516 if (ret < 0)
2517 return ret;
2518 ret = load_afsi(&afsi, &obj);
2519 if (ret < 0)
2520 return ret;
2521 afsi.attributes &= mask;
2522 save_afsi(&afsi, &obj);
2523 return 1;
2524 }
2525
2526 static int aft_event_handler(enum afs_events event, struct para_buffer *pb,
2527 void *data)
2528 {
2529 int ret;
2530
2531 switch(event) {
2532 case ATTRIBUTE_REMOVE: {
2533 const struct rmatt_event_data *red = data;
2534 ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
2535 "entries in the audio file table\n", red->name,
2536 red->bitnum);
2537 if (ret < 0)
2538 return ret;
2539 return audio_file_loop(data, clear_attribute);
2540 }
2541 default:
2542 return 1;
2543 }
2544 }
2545
2546 void aft_init(struct afs_table *t)
2547 {
2548 t->name = audio_file_table_desc.name;
2549 t->open = aft_open;
2550 t->close = aft_close;
2551 t->create = aft_create;
2552 t->event_handler = aft_event_handler;
2553 }