File: dvd_reader.c

package info (click to toggle)
cdrkit 9%3A1.1.11-3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 7,464 kB
  • sloc: ansic: 107,182; perl: 968; sh: 481; makefile: 229; sed: 4
file content (295 lines) | stat: -rw-r--r-- 6,749 bytes parent folder | download | duplicates (8)
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
/*
 * This file has been modified for the cdrkit suite.
 *
 * The behaviour and appearence of the program code below can differ to a major
 * extent from the version distributed by the original author(s).
 *
 * For details, see Changelog file distributed with the cdrkit package. If you
 * received this file from another source then ask the distributing person for
 * a log of modifications.
 *
 */

/* @(#)dvd_reader.c	1.3 04/03/04 joerg */
/*
 * Copyright (C) 2001, 2002 Billy Biggs <vektor@dumbterm.net>,
 *                          Hkan Hjort <d95hjort@dtek.chalmers.se>,
 *                          Olaf Beck <olaf_sc@yahoo.com>
 *			    (I only did the cut down no other contribs)
 *			    Jrg Schilling <schilling@fokus.gmd.de>
 *			    (making the code portable)
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * NOTE: This is a cut down version of libdvdread for genisoimage, due
 * to portability issues with the current libdvdread according to
 * the maintainer of genisoimage.
 * This cut down version only reads from a harddisk file structure
 * and it only implements the functions necessary inorder to make
 * genisoimage produce valid DVD-Video images.
 * DON'T USE THIS LIBRARY IN ANY OTHER PROGRAM GET THE REAL
 * LIBDVDREAD INSTEAD
 */
#ifdef DVD_VIDEO

#include "genisoimage.h"
#include <fctldefs.h>
#include <schily.h>

#include "dvd_reader.h"

struct dvd_file_s {
	/* Basic information. */
	dvd_reader_t	*dvd;

	/* Calculated at open-time, size in blocks. */
	ssize_t		filesize;
};


void		DVDCloseFile(dvd_file_t *dvd_file);
static	dvd_file_t *DVDOpenFilePath(dvd_reader_t *dvd, char *filename);
static	dvd_file_t *DVDOpenVOBPath(dvd_reader_t *dvd, int title, int menu);
dvd_file_t *DVDOpenFile(dvd_reader_t *dvd, int titlenum, 
								dvd_read_domain_t domain);
static	dvd_reader_t *DVDOpenPath(const char *path_root);
dvd_reader_t *DVDOpen(const char *path);
void		DVDClose(dvd_reader_t *dvd);
ssize_t		DVDFileSize(dvd_file_t *dvd_file);


/*
 * Free a DVD file
 */
void
DVDCloseFile(dvd_file_t *dvd_file)
{
	free(dvd_file);
	dvd_file = 0;
}


/*
 * Stat a IFO or BUP file from a DVD directory tree.
 */
static dvd_file_t *
DVDOpenFilePath(dvd_reader_t *dvd, char *filename)
{

	char		full_path[PATH_MAX + 1];
	dvd_file_t	*dvd_file;
	struct stat	fileinfo;

	/* Get the full path of the file. */

	snprintf(full_path, sizeof (full_path),
				"%s/%s", dvd->path_root, filename);


	dvd_file = (dvd_file_t *) e_malloc(sizeof (dvd_file_t));
	if (!dvd_file)
		return (0);
	dvd_file->dvd = dvd;
	dvd_file->filesize = 0;

	if (stat(full_path, &fileinfo) < 0) {
		free(dvd_file);
		return (0);
	}
	dvd_file->filesize = fileinfo.st_size / DVD_VIDEO_LB_LEN;

	return (dvd_file);
}


/*
 * Stat a VOB file from a DVD directory tree.
 */
static dvd_file_t *
DVDOpenVOBPath(dvd_reader_t *dvd, int title, int menu)
{

	char		filename[PATH_MAX + 1];
	struct stat	fileinfo;
	dvd_file_t	*dvd_file;
	int		i;

	dvd_file = (dvd_file_t *) e_malloc(sizeof (dvd_file_t));
	if (!dvd_file)
		return (0);
	dvd_file->dvd = dvd;
	dvd_file->filesize = 0;

	if (menu) {
		if (title == 0) {
			snprintf(filename, sizeof (filename),
				"%s/VIDEO_TS/VIDEO_TS.VOB", dvd->path_root);
		} else {
			snprintf(filename, sizeof (filename),
				"%s/VIDEO_TS/VTS_%02i_0.VOB", dvd->path_root, title);
		}
		if (stat(filename, &fileinfo) < 0) {
			free(dvd_file);
			return (0);
		}
		dvd_file->filesize = fileinfo.st_size / DVD_VIDEO_LB_LEN;
	} else {
		for (i = 0; i < 9; ++i) {

			snprintf(filename, sizeof (filename),
				"%s/VIDEO_TS/VTS_%02i_%i.VOB", dvd->path_root, title, i + 1);
			if (stat(filename, &fileinfo) < 0) {
					break;
			}

			dvd_file->filesize += fileinfo.st_size / DVD_VIDEO_LB_LEN;
		}
	}

	return (dvd_file);
}

/*
 * Stat a DVD file from a DVD directory tree
 */
EXPORT dvd_file_t *
DVDOpenFile(dvd_reader_t *dvd, int titlenum, dvd_read_domain_t domain)
{
	char		filename[MAX_UDF_FILE_NAME_LEN];

	switch (domain) {

	case DVD_READ_INFO_FILE:
		if (titlenum == 0) {
			snprintf(filename, sizeof (filename),
					"/VIDEO_TS/VIDEO_TS.IFO");
		} else {
			snprintf(filename, sizeof (filename),
					"/VIDEO_TS/VTS_%02i_0.IFO", titlenum);
		}
		break;

	case DVD_READ_INFO_BACKUP_FILE:
		if (titlenum == 0) {
			snprintf(filename, sizeof (filename),
					"/VIDEO_TS/VIDEO_TS.BUP");
		} else {
			snprintf(filename, sizeof (filename),
					"/VIDEO_TS/VTS_%02i_0.BUP", titlenum);
		}
		break;

	case DVD_READ_MENU_VOBS:
		return (DVDOpenVOBPath(dvd, titlenum, 1));

	case DVD_READ_TITLE_VOBS:
		if (titlenum == 0)
			return (0);
		return (DVDOpenVOBPath(dvd, titlenum, 0));

	default:
#ifdef	USE_LIBSCHILY
		errmsgno(EX_BAD, "Invalid domain for file open.\n");
#else
		fprintf(stderr, "Invalid domain for file open.\n");
#endif
		return (0);
	}
	return (DVDOpenFilePath(dvd, filename));
}



/*
 * Stat a DVD directory structure
 */
static dvd_reader_t *
DVDOpenPath(const char *path_root)
{
	dvd_reader_t	*dvd;

	dvd = (dvd_reader_t *) e_malloc(sizeof (dvd_reader_t));
	if (!dvd)
		return (0);
	dvd->path_root = strdup(path_root);

	return (dvd);
}


/*
 * Stat a DVD structure - this one only works with directory structures
 */
dvd_reader_t *
DVDOpen(const char *path)
{
	struct stat	fileinfo;
	int		ret;

	if (!path)
		return (0);

	ret = stat(path, &fileinfo);
	if (ret < 0) {
	/* If we can't stat the file, give up */
#ifdef	USE_LIBSCHILY
		errmsg("Can't stat %s\n", path);
#else
		fprintf(stderr, "Can't stat %s\n", path);
		perror("");
#endif
		return (0);
	}


	if (S_ISDIR(fileinfo.st_mode)) {
		return (DVDOpenPath(path));
	}

	/* If it's none of the above, screw it. */
#ifdef	USE_LIBSCHILY
	errmsgno(EX_BAD, "Could not open %s\n", path);
#else
	fprintf(stderr, "Could not open %s\n", path);
#endif
	return (0);
}

/*
 * Free a DVD structure - this one will only close a directory tree
 */
void
DVDClose(dvd_reader_t *dvd)
{
	if (dvd) {
		if (dvd->path_root) free(dvd->path_root);
		free(dvd);
		dvd = 0;
	}
}



/*
 * Return the size of a DVD file
 */
ssize_t
DVDFileSize(dvd_file_t *dvd_file)
{
	return (dvd_file->filesize);
}

#endif /* DVD_VIDEO */