File: udfdump.c

package info (click to toggle)
udfclient 0.8.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,140 kB
  • sloc: ansic: 16,639; sh: 3,050; makefile: 51
file content (261 lines) | stat: -rw-r--r-- 6,870 bytes parent folder | download | duplicates (4)
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
/* $NetBSD$ */

/*
 * File "udfdump.c" is part of the UDFclient toolkit.
 * File $Id: udfdump.c,v 1.75 2016/04/25 21:01:41 reinoud Exp $ $Name:  $
 *
 * Copyright (c) 2003, 2004, 2005, 2006, 2011
 * 	Reinoud Zandijk <reinoud@netbsd.org>
 * All rights reserved.
 *
 * The UDFclient toolkit is distributed under the Clarified Artistic Licence.
 * A copy of the licence is included in the distribution as
 * `LICENCE.clearified.artistic' and a copy of the licence can also be
 * requested at the GNU foundantion's website.
 *
 * Visit the UDFclient toolkit homepage http://www.13thmonkey.org/udftoolkit/
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "udf.h"
#include "udf_bswap.h"


/* switches */

/* #define DEBUG(a) (a) */
#define DEBUG(a) if (0) { a; }
int dump_sequential	 = 0;
int dump_directory_trees = 0;

#ifndef MAX
#	define MAX(a,b) ((a)>(b)?(a):(b))
#	define MIN(a,b) ((a)<(b)?(a):(b))
#endif

/* include the dump parts ... in order to get a more sane splitting */
extern void udf_dump_descriptor(union dscrptr *dscrpt);
extern void udf_dump_alive_sets(void);
extern void udf_dump_root_dir(struct udf_mountpoint *mountpoint);


/* globals */
extern int udf_verbose;
extern int uscsilib_verbose;


/* main discdump routine */

void udf_dump_sequential(int alt_sector_size, int flags, char *dev_name) {
	struct udf_discinfo *disc;
	union dscrptr	*dscr;
	uint64_t	 sec_num, rsec_num;
	uint32_t	 sector_size, size, chunk, dscr_len;
	uint8_t		*new_sector, *sector, *pos;
	int		 error;

	udf_verbose     = UDF_VERBLEV_ACTIONS;

	printf("Opening disc:\n");
	error = udf_open_disc(dev_name, flags, &disc);
	if (error) return;

	if (alt_sector_size)
		disc->sector_size = alt_sector_size;

	sector = calloc(1, 65*1024);
	assert(sector);

	printf("Sequential dump of device/file (sector size %d)\n", disc->sector_size);
	sec_num = 0;
	sector_size = disc->sector_size;
	do {
		size  = 1;
		error = udf_read_physical_sectors(disc, sec_num, size, "sector", sector);

		if (!error) {
			dscr = (union dscrptr *) sector;
			if (!udf_check_tag(dscr)) {
				if (udf_rw16(dscr->tag.id) <= TAGID_MAX) {
					dscr_len = udf_calc_tag_malloc_size(dscr, sector_size);
					dscr_len = MAX(sector_size, dscr_len);
					if (dscr_len > sector_size) {
						size = (dscr_len + sector_size -1) / sector_size;
						new_sector = realloc(sector, size * sector_size);
						if (!new_sector)
							goto next_sector;
						if (new_sector)
							sector = new_sector;

						dscr   = (union dscrptr *) sector;
						chunk  = MIN(size, 0xffff);
						pos = sector;
						rsec_num = sec_num;
						while (size) {
							error = udf_read_physical_sectors(
									disc, rsec_num, chunk, "sector", pos);
							if (error)
								break;
							pos      += chunk * sector_size; 
							rsec_num += chunk;
							size     -= chunk;
							chunk     = MIN(size, 0xffff);
						}
						if (error)
							break;
					}
					size = (dscr_len + sector_size -1) / sector_size;

					if (!udf_check_tag_payload(dscr)) {
						printf("%8d ", (int) sec_num); udf_dump_descriptor(dscr);
					}
				}
			}
		} else {
			if (error == ENOENT)
				break;
			printf("\nRead error : %s\n", strerror(error));
		}
next_sector:
		sec_num++;
		printf("\r%08d ", (int) sec_num);
		fflush(stdout);
	} while (1);
	printf("\n");
}


void udf_dump_mountable_dirtrees(void) {
	struct udf_mountpoint *mountpoint;

	printf("Dumping directory tree for each non obsolete logical volume's fileset\n");
	SLIST_FOREACH(mountpoint, &udf_mountables, all_next) {
		printf("\n");
		printf("Directory tree of mount point %s\n", mountpoint->mount_name);
		udf_dump_root_dir(mountpoint);
	}
}


int usage(char *program) {
	fprintf(stderr, "Usage %s [options] devicename [devicename]*)\n", program);
	fprintf(stderr, "-S		dump device sequential\n"
			"-s		byteswap read sectors (for PVRs)\n"
			"-t		dump complete directory contents\n"
			"-b blocksize	use alternative sectorsize; use only on files/discs\n"
			"-u level	UDF system verbose level\n"
			"-D		verbose SCSI command errors\n"
			"-r range	only use UDF sessions range like -3,4-5 or 6-\n"
	);
	return 1;
}


int main(int argc, char *argv[]) {
	struct udf_discinfo *disc;
	char *progname, *range;
	uint32_t alt_sector_size;
	int flag, mntflags, bswap, error;

	progname = argv[0];
	if (argc == 1) return usage(progname);

	dump_sequential      = 0;
	dump_directory_trees = 0;
	range                = NULL;
	alt_sector_size      = 0;
	bswap                = 0;
	while ((flag = getopt(argc, argv, "u:r:b:DSst")) != -1) {
		switch (flag) {
			case 'u' :
				udf_verbose = atoi(optarg);
				break;
			case 'r' :
				range = strdup(optarg);
				if (udf_check_session_range(range)) {
					fprintf(stderr, "Invalid range %s\n", range);
					return usage(progname);
				}
				break;
			case 'b' :
				alt_sector_size = atoi(optarg);
				break;
			case 'D' :
				uscsilib_verbose = 1;
				break;
			case 'S' :
				dump_sequential = 1;
				break;
			case 's' :
				bswap = 1;
				break;
			case 't' :
				dump_directory_trees = 1;
				break;
			default  :
				return usage(progname);
		}
	}
	argv += optind;
	argc -= optind;

	if (dump_sequential) {
		if (argc != 1) return usage(progname);
		udf_dump_sequential(alt_sector_size, bswap, *argv);
		return 0;
	}

	if (argc == 0) return usage(progname);

	/* all other arguments are devices */
	udf_init();
	while (argc) {
		printf("Opening device %s\n\n", *argv);
		mntflags  = UDF_MNT_RDONLY;
		mntflags |= bswap ? UDF_MNT_BSWAP : 0;
		error = udf_mount_disc(*argv, range, alt_sector_size, mntflags, &disc);
		if (error) {
			fprintf(stderr, "Can't open my device; bailing out : %s\n", strerror(error));
			exit(1);
		}

		argv++; argc--;
		if (udf_verbose) printf("\n\n");
	}

	printf("Resulting list of alive sets :\n\n");
	udf_dump_alive_sets();

	if (dump_directory_trees) {
		printf("Dump tree of all mountables\n");
		udf_dump_mountable_dirtrees();
		printf("\n");
	}

	printf("Closing discs\n");
	SLIST_FOREACH(disc, &udf_discs_list, next_disc) {
		udf_close_disc(disc);
	}

	return 0;
}