1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|
/* GVFS cdrom audio file system driver
*
* Copyright (C) 2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
/* NOTE: since we link the libcdio libs (GPLv2) into our process space
* the combined work is GPLv2. This source file, however, is LGPLv2+.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gudev/gudev.h>
#include "gvfsbackendcdda.h"
#include "gvfsjobopenforread.h"
#include "gvfsjobread.h"
#include "gvfsjobseekread.h"
#include "gvfsjobqueryinfo.h"
#include "gvfsjobenumerate.h"
#define DO_NOT_WANT_PARANOIA_COMPATIBILITY
#ifdef HAVE_PARANOIA_NEW_INCLUDES
#include <cdio/paranoia/paranoia.h>
#else
#include <cdio/paranoia.h>
#endif
#include <cdio/cdio.h>
/* TODO:
*
* - GVFS integration
* - g_vfs_backend_set_display_name() needs to work post mount
*
* - Metadata
* - Use Musicbrainz to read metadata from the net
* - libmusicbrainz appear to be a dead-end: http://musicbrainz.org/doc/libmusicbrainz
* - Need to provide some UI for configuring musicbrainz; think proxy, local server,
* lockdown (secure facilities don't want us to randomly connect to the Interwebs)
* - Ideally use libjuicer for all this
* - however it is currently private to sound-juicer and brings in GTK+, gnome-vfs, gconf...
* - Use metadata for file names and display_name of our Mount (using g_vfs_backend_set_display_name())
* - Also encode metadata in the WAV header so transcoding to Vorbis or MP3 Just Works(tm)
* - This is already done; see create_header() in this file
* - see thread on gtk-devel-list for a plan
*
* - Scratched discs / error conditions from paranoia
* - Need to handle this better... ideally caller passes a flag when opening the file to
* specify whether he wants us to try hard to get the hard result (ripping) or whether
* he's fine with some noise (playback)
*
* - Sector cache? Might be useful to maintain a cache of previously read sectors
*/
/*--------------------------------------------------------------------------------------------------------------*/
typedef struct {
char *artist;
char *title;
int duration; /* Duration in seconds */
} GVfsBackendCddaTrack;
struct _GVfsBackendCdda
{
GVfsBackend parent_instance;
GUdevClient *gudev_client;
guint64 size;
char *device_path;
cdrom_drive_t *drive;
int num_open_files;
/* Metadata from CD-Text */
char *album_title;
char *album_artist;
char *genre;
GList *tracks; /* a GList of GVfsBackendCddaTrack */
};
G_DEFINE_TYPE (GVfsBackendCdda, g_vfs_backend_cdda, G_VFS_TYPE_BACKEND)
static void
release_device (GVfsBackendCdda *cdda_backend)
{
g_free (cdda_backend->device_path);
cdda_backend->device_path = NULL;
if (cdda_backend->drive != NULL)
{
cdio_cddap_close (cdda_backend->drive);
cdda_backend->drive = NULL;
}
}
/* Metadata related functions */
static void
track_free (GVfsBackendCddaTrack *track)
{
if (track == NULL)
return;
g_free (track->artist);
g_free (track->title);
}
static void
release_metadata (GVfsBackendCdda *cdda_backend)
{
g_free (cdda_backend->album_title);
cdda_backend->album_title = NULL;
g_free (cdda_backend->album_artist);
cdda_backend->album_artist = NULL;
g_free (cdda_backend->genre);
cdda_backend->genre = NULL;
g_list_free_full (cdda_backend->tracks, (GDestroyNotify) track_free);
cdda_backend->tracks = NULL;
}
#if LIBCDIO_VERSION_NUM < 84
static char *
cdtext_string_to_utf8 (const char *string)
{
if (string == NULL)
return NULL;
/* CD-text doesn't specify encoding. In case outside ascii, assume latin-1. */
return g_convert (string, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
}
#endif
static void
fetch_metadata (GVfsBackendCdda *cdda_backend)
{
CdIo *cdio;
track_t cdtrack, last_cdtrack;
const cdtext_t *cdtext;
cdio = cdio_open (cdda_backend->device_path, DRIVER_UNKNOWN);
if (!cdio)
return;
#if LIBCDIO_VERSION_NUM >= 84
cdtext = cdio_get_cdtext (cdio);
#else
cdtext = cdio_get_cdtext (cdio, 0);
#endif /* LIBCDIO_VERSION_NUM >= 84 */
if (cdtext) {
#if LIBCDIO_VERSION_NUM >= 84
cdda_backend->album_title = g_strdup (cdtext_get_const (cdtext, CDTEXT_FIELD_TITLE, 0));
cdda_backend->album_artist = g_strdup (cdtext_get_const (cdtext, CDTEXT_FIELD_PERFORMER, 0));
cdda_backend->genre = g_strdup (cdtext_get_const (cdtext, CDTEXT_FIELD_GENRE, 0));
#else
cdda_backend->album_title = cdtext_string_to_utf8 (cdtext_get_const (CDTEXT_TITLE, cdtext));
cdda_backend->album_artist = cdtext_string_to_utf8 (cdtext_get_const (CDTEXT_PERFORMER, cdtext));
cdda_backend->genre = cdtext_string_to_utf8 (cdtext_get_const (CDTEXT_GENRE, cdtext));
#endif /* LIBCDIO_VERSION_NUM >= 84 */
}
cdtrack = cdio_get_first_track_num(cdio);
last_cdtrack = cdtrack + cdio_get_num_tracks(cdio);
for ( ; cdtrack < last_cdtrack; cdtrack++ ) {
GVfsBackendCddaTrack *track;
track = g_new0 (GVfsBackendCddaTrack, 1);
#if LIBCDIO_VERSION_NUM < 84
cdtext = cdio_get_cdtext (cdio, cdtrack);
#endif /* LIBCDIO_VERSION_NUM < 84 */
if (cdtext) {
#if LIBCDIO_VERSION_NUM >= 84
track->title = g_strdup (cdtext_get_const (cdtext, CDTEXT_FIELD_TITLE, cdtrack));
track->artist = g_strdup (cdtext_get_const (cdtext, CDTEXT_FIELD_PERFORMER, cdtrack));
#else
track->title = cdtext_string_to_utf8 (cdtext_get_const (CDTEXT_TITLE, cdtext));
track->artist = cdtext_string_to_utf8 (cdtext_get_const (CDTEXT_PERFORMER, cdtext));
#endif /* LIBCDIO_VERSION_NUM >= 84 */
}
track->duration = cdio_get_track_sec_count (cdio, cdtrack) / CDIO_CD_FRAMES_PER_SEC;
cdda_backend->tracks = g_list_append (cdda_backend->tracks, track);
}
cdio_destroy (cdio);
}
static void
on_uevent (GUdevClient *client, gchar *action, GUdevDevice *device, gpointer user_data)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (user_data);
const gchar *u_dev = g_udev_device_get_device_file (device);
/* we unmount ourselves if the changed device is "our's" and it either gets
* removed or changed to "no media" */
if (cdda_backend->device_path == NULL || g_strcmp0 (cdda_backend->device_path, u_dev) != 0)
return;
if (strcmp (action, "remove") == 0 || (strcmp (action, "change") == 0 &&
g_udev_device_get_property_as_int (device, "ID_CDROM_MEDIA") != 1))
{
g_vfs_backend_force_unmount (G_VFS_BACKEND (cdda_backend));
g_signal_handlers_disconnect_by_func (cdda_backend->gudev_client, on_uevent, cdda_backend);
}
}
static void
g_vfs_backend_cdda_finalize (GObject *object)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (object);
//g_warning ("finalizing %p", object);
release_device (cdda_backend);
release_metadata (cdda_backend);
if (G_OBJECT_CLASS (g_vfs_backend_cdda_parent_class)->finalize)
(*G_OBJECT_CLASS (g_vfs_backend_cdda_parent_class)->finalize) (object);
}
static void
g_vfs_backend_cdda_init (GVfsBackendCdda *cdda_backend)
{
GVfsBackend *backend = G_VFS_BACKEND (cdda_backend);
GMountSpec *mount_spec;
char *x_content_types[] = {"x-content/audio-cdda", NULL};
//g_warning ("initing %p", cdda_backend);
g_vfs_backend_set_display_name (backend, "cdda");
g_vfs_backend_set_x_content_types (backend, x_content_types);
// TODO: HMM: g_vfs_backend_set_user_visible (backend, FALSE);
mount_spec = g_mount_spec_new ("cdda");
g_vfs_backend_set_mount_spec (backend, mount_spec);
g_mount_spec_unref (mount_spec);
}
static void
do_mount (GVfsBackend *backend,
GVfsJobMount *job,
GMountSpec *mount_spec,
GMountSource *mount_source,
gboolean is_automount)
{
char *fuse_name;
char *display_name;
const char *host;
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
GError *error = NULL;
GMountSpec *cdda_mount_spec;
//g_warning ("do_mount %p", cdda_backend);
/* setup gudev */
const char *subsystems[] = {"block", NULL};
GUdevDevice *gudev_device;
cdda_backend->gudev_client = g_udev_client_new (subsystems);
if (cdda_backend->gudev_client == NULL)
{
release_device (cdda_backend);
release_metadata (cdda_backend);
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Cannot create gudev client"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
return;
}
g_signal_connect (cdda_backend->gudev_client, "uevent", G_CALLBACK (on_uevent), cdda_backend);
/* setup libcdio */
host = g_mount_spec_get (mount_spec, "host");
//g_warning ("host=%s", host);
if (host == NULL)
{
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED, _("No drive specified"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
release_device (cdda_backend);
release_metadata (cdda_backend);
return;
}
cdda_backend->device_path = g_strdup_printf ("/dev/%s", host);
gudev_device = g_udev_client_query_by_device_file (cdda_backend->gudev_client, cdda_backend->device_path);
if (gudev_device != NULL)
cdda_backend->size = g_udev_device_get_sysfs_attr_as_uint64 (gudev_device, "size") * 512;
g_object_unref (gudev_device);
cdda_backend->drive = cdio_cddap_identify (cdda_backend->device_path, 0, NULL);
if (cdda_backend->drive == NULL)
{
g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Cannot find drive %s"), cdda_backend->device_path);
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
release_device (cdda_backend);
release_metadata (cdda_backend);
return;
}
fetch_metadata (cdda_backend);
if (cdio_cddap_open (cdda_backend->drive) != 0)
{
g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
_("Drive %s does not contain audio files"), cdda_backend->device_path);
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
release_device (cdda_backend);
release_metadata (cdda_backend);
return;
}
/* Translator: %s is the device the disc is inserted into. 'cdda' is the name
name of the backend and shouldn't be translated. */
fuse_name = g_strdup_printf (_("cdda mount on %s"), host);
display_name = g_strdup_printf (_("Audio Disc"));
g_vfs_backend_set_stable_name (backend, fuse_name);
g_vfs_backend_set_display_name (backend, display_name);
g_free (display_name);
g_free (fuse_name);
g_vfs_backend_set_icon_name (backend, "media-optical-audio");
g_vfs_backend_set_symbolic_icon_name (backend, "media-optical-symbolic");
g_vfs_job_succeeded (G_VFS_JOB (job));
cdda_mount_spec = g_mount_spec_new ("cdda");
g_mount_spec_set (cdda_mount_spec, "host", host);
g_vfs_backend_set_mount_spec (backend, cdda_mount_spec);
g_mount_spec_unref (cdda_mount_spec);
//g_warning ("mounted %p", cdda_backend);
}
static gboolean
try_mount (GVfsBackend *backend,
GVfsJobMount *job,
GMountSpec *mount_spec,
GMountSource *mount_source,
gboolean is_automount)
{
const char *host;
GError *error = NULL;
GMountSpec *cdda_mount_spec;
//g_warning ("try_mount %p", backend);
/* TODO: Hmm.. apparently we have to set the mount spec in
* try_mount(); doing it in mount() won't work..
*/
host = g_mount_spec_get (mount_spec, "host");
//g_warning ("tm host=%s", host);
if (host == NULL)
{
g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_FAILED, _("No drive specified"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
g_error_free (error);
return TRUE;
}
cdda_mount_spec = g_mount_spec_new ("cdda");
g_mount_spec_set (cdda_mount_spec, "host", host);
g_vfs_backend_set_mount_spec (backend, cdda_mount_spec);
g_mount_spec_unref (cdda_mount_spec);
return FALSE;
}
static void
do_unmount (GVfsBackend *backend,
GVfsJobUnmount *job,
GMountUnmountFlags flags,
GMountSource *mount_source)
{
GError *error;
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
if (cdda_backend->num_open_files > 0)
{
error = g_error_new (G_IO_ERROR, G_IO_ERROR_BUSY,
ngettext ("File system is busy: %d open file",
"File system is busy: %d open files",
cdda_backend->num_open_files),
cdda_backend->num_open_files);
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
return;
}
release_device (cdda_backend);
release_metadata (cdda_backend);
g_signal_handlers_disconnect_by_func (cdda_backend->gudev_client, on_uevent, cdda_backend);
g_vfs_job_succeeded (G_VFS_JOB (job));
//g_warning ("unmounted %p", backend);
}
/* returns -1 if we couldn't map */
static int
get_track_num_from_name (const char *filename)
{
int n;
char *basename;
basename = g_path_get_basename (filename);
if (sscanf (basename, "Track %d.wav", &n) == 1 &&
g_str_has_suffix (basename, ".wav"))
{
g_free (basename);
return n;
}
return -1;
}
typedef struct {
cdrom_paranoia_t *paranoia;
long size; /* size of file being read */
long header_size; /* size of the header */
long content_size; /* size of content after the header */
long cursor; /* cursor into the file being read */
long first_sector; /* first sector of raw PCM audio data */
long last_sector; /* last sector of raw PCM audio data */
long sector_cursor; /* sector we're currently at */
char *header; /* header payload */
/* These following two fields are used for caching the last read sector. This
* is to avoid seeking back if fewer bytes than whole sector is requested.
*/
long buf_at_sector_num; /* the sector that is cached */
char buf_at_sector[CDIO_CD_FRAMESIZE_RAW]; /* the data of the sector */
} ReadHandle;
static void
free_read_handle (ReadHandle *read_handle)
{
if (read_handle->paranoia != NULL)
cdio_paranoia_free (read_handle->paranoia);
g_free (read_handle->header);
g_free (read_handle);
}
static char *
create_header (GVfsBackendCdda *cdda_backend, long *header_size, long content_size)
{
char *artist;
char *title;
const char *software;
size_t artist_len;
size_t title_len;
size_t software_len;
char *header;
char *ptr;
int var;
/* See http://www.saettler.com/RIFFMCI/riffmci.html for the spec.
*
* artist -> IART
* title -> INAM
* track_number -> ?? (TODO: work with GStreamer people on coordinate with the wavparse plugin)
*
* software -> ISFT
*/
//artist = g_strdup ("Homer Simpson");
//title = g_strdup ("Simpsons Jail House Rock");
/* TODO: fill in from metadata */
artist = NULL;
title = NULL;
software = "gvfs-cdda using libcdio " CDIO_VERSION;
artist_len = 0;
title_len = 0;
/* ensure even length and include room for the chunk */
if (artist != NULL)
artist_len = 2 * ((strlen (artist) + 2) / 2) + 8;
if (title != NULL)
title_len = 2 * ((strlen (title) + 2) / 2) + 8;
software_len = 2 * ((strlen (software) + 2) / 2) + 8;
*header_size = 44;
*header_size += 12; /* for LIST INFO */
*header_size += artist_len;
*header_size += title_len;
*header_size += software_len;
header = g_new0 (char, *header_size);
ptr = header;
memcpy (ptr, "RIFF", 4); ptr += 4;
var = content_size + *header_size - 8;
memcpy (ptr, &var, 4); ptr += 4;
memcpy (ptr, "WAVE", 4); ptr += 4;
memcpy (ptr, "fmt ", 4); ptr += 4;
var = 16;
memcpy (ptr, &var, 4); ptr += 4;
var = 1;
memcpy (ptr, &var, 2); ptr += 2;
var = 2;
memcpy (ptr, &var, 2); ptr += 2;
var = 44100;
memcpy (ptr, &var, 4); ptr += 4;
var = 44100 * 2 * 2;
memcpy (ptr, &var, 4); ptr += 4;
var = 4;
memcpy (ptr, &var, 2); ptr += 2;
var = 16;
memcpy (ptr, &var, 2); ptr += 2;
memcpy (ptr, "LIST", 4); ptr += 4;
var = 4 + artist_len + title_len + software_len;
memcpy (ptr, &var, 4); ptr += 4;
memcpy (ptr, "INFO", 4); ptr += 4;
#if 0
if (artist != NULL)
{
memcpy (ptr, "IART", 4);
var = artist_len - 8;
memcpy (ptr + 4, &var, 4);
strncpy (ptr + 8, artist, artist_len);
ptr += artist_len;
}
if (title != NULL)
{
memcpy (ptr, "INAM", 4);
var = title_len - 8;
memcpy (ptr + 4, &var, 4);
strncpy (ptr + 8, title, title_len);
ptr += title_len;
}
#endif
memcpy (ptr, "ISFT", 4);
var = software_len - 8;
memcpy (ptr + 4, &var, 4);
strncpy (ptr + 8, software, software_len);
ptr += software_len;
memcpy (ptr, "data", 4); ptr += 4;
memcpy (ptr, &content_size, 4); ptr += 4;
g_free (artist);
g_free (title);
return header;
}
static void
do_open_for_read (GVfsBackend *backend,
GVfsJobOpenForRead *job,
const char *filename)
{
int track_num;
GError *error;
ReadHandle *read_handle;
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
//g_warning ("open_for_read (%s)", filename);
read_handle = g_new0 (ReadHandle, 1);
track_num = get_track_num_from_name (job->filename);
if (track_num == -1)
{
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
_("No such file %s on drive %s"), job->filename, cdda_backend->device_path);
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
free_read_handle (read_handle);
return;
}
read_handle->first_sector = cdio_cddap_track_firstsector (cdda_backend->drive, track_num);
read_handle->last_sector = cdio_cddap_track_lastsector (cdda_backend->drive, track_num);
read_handle->sector_cursor = -1;
read_handle->cursor = 0;
read_handle->buf_at_sector_num = -1;
read_handle->content_size = ((read_handle->last_sector - read_handle->first_sector) + 1) * CDIO_CD_FRAMESIZE_RAW;
read_handle->header = create_header (cdda_backend, &(read_handle->header_size), read_handle->content_size);
read_handle->size = read_handle->header_size + read_handle->content_size;
read_handle->paranoia = cdio_paranoia_init (cdda_backend->drive);
cdio_paranoia_modeset (read_handle->paranoia, PARANOIA_MODE_DISABLE);
cdda_backend->num_open_files++;
g_vfs_job_open_for_read_set_can_seek (job, TRUE);
g_vfs_job_open_for_read_set_handle (job, GINT_TO_POINTER (read_handle));
g_vfs_job_succeeded (G_VFS_JOB (job));
}
/* We have to pass in a callback to paranoia_read, even though we don't use it */
static void
paranoia_callback (long int inpos, paranoia_cb_mode_t function)
{
}
static void
do_read (GVfsBackend *backend,
GVfsJobRead *job,
GVfsBackendHandle handle,
char *buffer,
gsize bytes_requested)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
ReadHandle *read_handle = (ReadHandle *) handle;
int bytes_read;
long skip_bytes;
char *readbuf;
long desired_sector;
int bytes_to_copy;
long cursor_in_stream;
//g_warning ("read (%"G_GSSIZE_FORMAT") (@ %ld)", bytes_requested, read_handle->cursor);
/* header */
if (read_handle->cursor < read_handle->header_size)
{
skip_bytes = read_handle->cursor;
bytes_read = read_handle->header_size - read_handle->cursor;
readbuf = read_handle->header + skip_bytes;
goto read_data_done;
}
/* EOF */
if (read_handle->cursor >= read_handle->size)
{
skip_bytes = 0;
bytes_read = 0;
readbuf = NULL;
goto read_data_done;
}
cursor_in_stream = read_handle->cursor - read_handle->header_size;
desired_sector = cursor_in_stream / CDIO_CD_FRAMESIZE_RAW + read_handle->first_sector;
if (desired_sector == read_handle->buf_at_sector_num)
{
/* got it cached */
/* skip some bytes */
skip_bytes = cursor_in_stream - (desired_sector - read_handle->first_sector) * CDIO_CD_FRAMESIZE_RAW;
readbuf = read_handle->buf_at_sector + skip_bytes;
bytes_read = CDIO_CD_FRAMESIZE_RAW - skip_bytes;
//g_warning ("read from cache for cursor @ %ld", read_handle->buf_at_sector_num);
}
else
{
/* first check that we're at the right sector */
if (desired_sector != read_handle->sector_cursor)
{
cdio_paranoia_seek (read_handle->paranoia, desired_sector, SEEK_SET);
read_handle->sector_cursor = desired_sector;
//g_warning ("seeking cursor to %ld", read_handle->sector_cursor);
}
/* skip some bytes */
skip_bytes = cursor_in_stream - (read_handle->sector_cursor - read_handle->first_sector) * CDIO_CD_FRAMESIZE_RAW;
//g_warning ("advanced cursor to %ld", read_handle->sector_cursor);
readbuf = (char *) cdio_paranoia_read (read_handle->paranoia, paranoia_callback);
if (readbuf == NULL)
{
int errsv = errno;
g_vfs_job_failed (G_VFS_JOB (job), G_IO_ERROR,
g_io_error_from_errno (errsv),
/* Translators: paranoia is the name of the cd audio reading library */
_("Error from “paranoia” on drive %s"), cdda_backend->device_path);
return;
}
read_handle->buf_at_sector_num = read_handle->sector_cursor;
memcpy (read_handle->buf_at_sector, readbuf, CDIO_CD_FRAMESIZE_RAW);
read_handle->sector_cursor++;
readbuf += skip_bytes;
bytes_read = CDIO_CD_FRAMESIZE_RAW - skip_bytes;
}
read_data_done:
bytes_to_copy = bytes_read;
if (bytes_requested < bytes_read)
bytes_to_copy = bytes_requested;
read_handle->cursor += bytes_to_copy;
cursor_in_stream = read_handle->cursor - read_handle->header_size;
if (bytes_to_copy > 0 && readbuf != NULL)
memcpy (buffer, readbuf, bytes_to_copy);
g_vfs_job_read_set_size (job, bytes_to_copy);
g_vfs_job_succeeded (G_VFS_JOB (job));
}
static void
do_seek_on_read (GVfsBackend *backend,
GVfsJobSeekRead *job,
GVfsBackendHandle handle,
goffset offset,
GSeekType type)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
ReadHandle *read_handle = (ReadHandle *) handle;
long new_offset;
//g_warning ("seek_on_read (%d, %d)", (int)offset, type);
switch (type)
{
default:
case G_SEEK_SET:
new_offset = offset;
break;
case G_SEEK_CUR:
new_offset = read_handle->cursor + offset;
break;
case G_SEEK_END:
new_offset = read_handle->size + offset;
break;
}
if (new_offset < 0)
{
g_vfs_job_failed (G_VFS_JOB (job), G_IO_ERROR,
G_IO_ERROR_FAILED,
_("Error seeking in stream on drive %s"), cdda_backend->device_path);
}
else
{
read_handle->cursor = new_offset;
g_vfs_job_seek_read_set_offset (job, offset);
g_vfs_job_succeeded (G_VFS_JOB (job));
}
}
static void
do_close_read (GVfsBackend *backend,
GVfsJobCloseRead *job,
GVfsBackendHandle handle)
{
ReadHandle *read_handle = (ReadHandle *) handle;
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
//g_warning ("close ()");
free_read_handle (read_handle);
cdda_backend->num_open_files--;
g_vfs_job_succeeded (G_VFS_JOB (job));
}
static void
set_info_for_track (GVfsBackendCdda *cdda_backend, GFileInfo *info, int track_num)
{
char *header;
long first;
long last;
long header_size;
long content_size;
GIcon *icon;
GVfsBackendCddaTrack *track;
first = cdio_cddap_track_firstsector (cdda_backend->drive, track_num);
last = cdio_cddap_track_lastsector (cdda_backend->drive, track_num);
content_size = (last - first + 1) * CDIO_CD_FRAMESIZE_RAW;
header = create_header (cdda_backend, &header_size, content_size);
g_free (header);
//g_warning ("size=%ld for track %d", size, track_num);
g_file_info_set_file_type (info, G_FILE_TYPE_REGULAR);
g_file_info_set_content_type (info, "audio/x-wav");
g_file_info_set_size (info, header_size + content_size);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ, TRUE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_DELETE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_TRASH, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_RENAME, FALSE);
track = g_list_nth_data (cdda_backend->tracks, track_num - 1);
if (track) {
if (track->title)
g_file_info_set_attribute_string (info, "xattr::org.gnome.audio.title", track->title);
if (track->artist)
g_file_info_set_attribute_string (info, "xattr::org.gnome.audio.artist", track->artist);
g_file_info_set_attribute_uint64 (info, "xattr::org.gnome.audio.duration", track->duration);
}
icon = g_themed_icon_new ("audio-x-generic");
g_file_info_set_icon (info, icon);
g_object_unref (icon);
}
#define SET_INFO(attr, value) if (value) g_file_info_set_attribute_string (info, attr, value);
static void
do_query_info (GVfsBackend *backend,
GVfsJobQueryInfo *job,
const char *filename,
GFileQueryInfoFlags flags,
GFileInfo *info,
GFileAttributeMatcher *matcher)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
int track_num;
GError *error;
//g_warning ("get_file_info (%s)", filename);
if (strcmp (filename, "/") == 0)
{
GIcon *icon;
g_file_info_set_display_name (info, _("Audio Disc")); /* TODO: fill in from metadata */
g_file_info_set_file_type (info, G_FILE_TYPE_DIRECTORY);
g_file_info_set_content_type (info, "inode/directory");
if (cdda_backend->album_title)
SET_INFO ("xattr::org.gnome.audio.title", cdda_backend->album_title);
if (cdda_backend->album_artist)
SET_INFO ("xattr::org.gnome.audio.artist", cdda_backend->album_artist);
SET_INFO ("xattr::org.gnome.audio.genre", cdda_backend->genre);
g_file_info_set_size (info, 0);
icon = g_themed_icon_new ("folder");
g_file_info_set_icon (info, icon);
g_object_unref (icon);
}
else
{
if (*filename == '/')
{
g_file_info_set_name (info, filename + 1);
g_file_info_set_display_name (info, filename + 1);
}
else
{
g_file_info_set_name (info, filename);
g_file_info_set_display_name (info, filename);
}
track_num = get_track_num_from_name (filename);
if (track_num == -1)
{
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("No such file"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
return;
}
if (track_num > cdda_backend->drive->tracks)
{
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("File doesn’t exist"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
return;
}
if (! cdio_cddap_track_audiop (cdda_backend->drive, track_num))
{
error = g_error_new (G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("The file does not exist or isn’t an audio track"));
g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
return;
}
set_info_for_track (cdda_backend, info, track_num);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
}
static void
do_enumerate (GVfsBackend *backend,
GVfsJobEnumerate *job,
const char *filename,
GFileAttributeMatcher *matcher,
GFileQueryInfoFlags flags)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
GFileInfo *info;
GList *l;
int n;
//g_warning ("enumerate (%s)", filename);
l = NULL;
for (n = 1; n <= cdda_backend->drive->tracks; n++)
{
char *name;
/* not audio track */
if (! cdio_cddap_track_audiop (cdda_backend->drive, n))
continue;
info = g_file_info_new ();
name = g_strdup_printf ("Track %d.wav", n);
g_file_info_set_name (info, name);
g_file_info_set_display_name (info, name);
g_free (name);
set_info_for_track (cdda_backend, info, n);
l = g_list_append (l, info);
}
g_vfs_job_succeeded (G_VFS_JOB (job));
g_vfs_job_enumerate_add_infos (job, l);
g_list_free_full (l, g_object_unref);
g_vfs_job_enumerate_done (job);
}
static void
do_query_fs_info (GVfsBackend *backend,
GVfsJobQueryFsInfo *job,
const char *filename,
GFileInfo *info,
GFileAttributeMatcher *attribute_matcher)
{
GVfsBackendCdda *cdda_backend = G_VFS_BACKEND_CDDA (backend);
g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, "cdda");
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE, FALSE);
g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USE_PREVIEW, G_FILESYSTEM_PREVIEW_TYPE_IF_LOCAL);
if (cdda_backend->size > 0)
{
g_file_info_set_attribute_uint64 (info,
G_FILE_ATTRIBUTE_FILESYSTEM_SIZE,
cdda_backend->size);
}
g_file_info_set_attribute_uint64 (info,
G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
0);
g_vfs_job_succeeded (G_VFS_JOB (job));
}
static void
g_vfs_backend_cdda_class_init (GVfsBackendCddaClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GVfsBackendClass *backend_class = G_VFS_BACKEND_CLASS (klass);
gobject_class->finalize = g_vfs_backend_cdda_finalize;
backend_class->try_mount = try_mount;
backend_class->mount = do_mount;
backend_class->unmount = do_unmount;
backend_class->open_for_read = do_open_for_read;
backend_class->read = do_read;
backend_class->seek_on_read = do_seek_on_read;
backend_class->close_read = do_close_read;
backend_class->query_info = do_query_info;
backend_class->query_fs_info = do_query_fs_info;
backend_class->enumerate = do_enumerate;
}
void
g_vfs_cdda_daemon_init (void)
{
g_set_application_name (_("Audio CD File System Service"));
}
|