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
|
/* --------------------------------------------------------------------------
MusicBrainz -- The Internet music metadatabase
Copyright (C) 2006 Matthias Friedrich
Copyright (C) 2000 Robert Kaye
Copyright (C) 1999 Marc E E van Woerkom
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.1 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
--------------------------------------------------------------------------- */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include "discid/discid_private.h"
#include "unix.h"
int mb_disc_unix_exists(const char *device) {
int fd;
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
/* we only check for existance, access should fail later on */
if (errno == ENOENT) {
return 0;
} else {
return 1;
}
} else {
close(fd);
return 1;
}
}
char *mb_disc_unix_find_device(char *candidates[], int num_candidates) {
int i;
for (i = 0; i < num_candidates; i++) {
if (mb_disc_unix_exists(candidates[i])) {
return candidates[i];
}
}
/* use the first name for the error message later on */
return candidates[0];
}
int mb_disc_unix_open(mb_disc_private *disc, const char *device) {
int fd;
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
"cannot open device `%s'", device);
}
/* fd < 0 check needs to be made by caller */
return fd;
}
int mb_disc_unix_read_toc(int fd, mb_disc_private *disc, mb_disc_toc *toc) {
int i;
/* Find the numbers of the first track (usually 1) and the last track. */
if ( !mb_disc_unix_read_toc_header(fd, toc) ) {
snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
"cannot read table of contents");
return 0;
}
/* basic error checking */
if ( toc->last_track_num == 0 ) {
snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
"this disc has no tracks");
return 0;
}
/*
* Read the TOC entry for every track.
*/
for (i = toc->first_track_num; i <= toc->last_track_num; i++) {
if ( !mb_disc_unix_read_toc_entry(fd, i, &toc->tracks[i]) ) {
snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
"cannot read TOC entry for track %d", i);
return 0;
}
}
if ( !mb_disc_unix_read_toc_entry(fd, 0xAA, &toc->tracks[0]) ) {
snprintf(disc->error_msg, MB_ERROR_MSG_LENGTH,
"cannot read TOC entry for lead-out");
return 0;
}
return 1;
}
int mb_disc_unix_read(mb_disc_private *disc, const char *device,
unsigned int features) {
mb_disc_toc toc;
int fd;
int i;
fd = mb_disc_unix_open(disc, device);
if (fd < 0)
return 0;
if ( !mb_disc_unix_read_toc(fd, disc, &toc) ) {
close(fd);
return 0;
}
if ( !mb_disc_load_toc(disc, &toc) ) {
close(fd);
return 0;
}
/* Read in the media catalog number */
if (features & DISCID_FEATURE_MCN
&& mb_disc_has_feature_unportable(DISCID_FEATURE_MCN)) {
mb_disc_unix_read_mcn(fd, disc);
}
/* Read the ISRC for the track */
if (features & DISCID_FEATURE_ISRC
&& mb_disc_has_feature_unportable(DISCID_FEATURE_ISRC)) {
for (i = disc->first_track_num; i <= disc->last_track_num; i++) {
mb_disc_unix_read_isrc(fd, disc, i);
}
}
close(fd);
return 1;
}
/* EOF */
|