aboutsummaryrefslogtreecommitdiffstats
path: root/debugfs/dump.c
blob: 06c588def8ab0fdb4257ab1d1338cc6f043bb460 (plain)
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
/*
 * dump.c --- dump the contents of an inode out to a file
 * 
 * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
 * under the terms of the GNU Public License.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "debugfs.h"

struct dump_block_struct {
	int		fd;
	char		*buf;
	errcode_t	errcode;
};

int dump_block(ext2_filsys fs, blk_t *blocknr, int blockcnt, void
	       *private)
{
	ssize_t nbytes;
	
	struct dump_block_struct *rec = (struct dump_block_struct *) private;
	
	if (blockcnt < 0)
		return 0;

	if (*blocknr) {
		rec->errcode = io_channel_read_blk(fs->io, *blocknr,
						   1, rec->buf);
		if (rec->errcode)
			return BLOCK_ABORT;
	} else
		memset(rec->buf, 0, fs->blocksize);

retry_write:
	nbytes = write(rec->fd, rec->buf, fs->blocksize);
	if (nbytes == -1) {
		if (errno == EINTR)
			goto retry_write;
		rec->errcode = errno;
		return BLOCK_ABORT;
	}
	if (nbytes != fs->blocksize) {
		/* XXX not quite right, but good enough */
		rec->errcode = EXT2_ET_SHORT_WRITE;
		return BLOCK_ABORT;
	}
	return 0;
}

void dump_file(char *cmdname, ino_t inode, int fd, char *outname)
{
	errcode_t retval;
	struct dump_block_struct rec;

	rec.fd = fd;
	rec.errcode = 0;
	rec.buf = malloc(fs->blocksize);

	if (rec.buf == 0) {
		com_err(cmdname, ENOMEM, "while allocating block buffer for dump_inode");
		return;
	}
	
	retval = ext2fs_block_iterate(fs, inode, 0, NULL,
				      dump_block, &rec);
	if (retval) {
		com_err(cmdname, retval, "while iterating over blocks in %s",
			outname);
		goto cleanup;
	}
	if (rec.errcode) {
		com_err(cmdname, retval, "in dump_block while dumping %s",
			outname);
		goto cleanup;
	}
	
cleanup:
	free(rec.buf);
	return;
}

void do_dump(int argc, char **argv)
{
	ino_t	inode;
	int	fd;

	if (argc != 3) {
		com_err(argv[0], 0, "Usage: dump_inode <file> <output_file>");
		return;
	}

	if (check_fs_open(argv[0]))
		return;

	inode = string_to_inode(argv[1]);
	if (!inode) 
		return;

	fd = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0666);
	if (fd < 0) {
		com_err(argv[0], errno, "while opening %s for dump_inode",
			argv[2]);
		return;
	}

	dump_file(argv[0], inode, fd, argv[2]);

	close(fd);
	return;
}

void do_cat(int argc, char **argv)
{
	ino_t	inode;

	if (argc != 2) {
		com_err(argv[0], 0, "Usage: cat <file>");
		return;
	}

	if (check_fs_open(argv[0]))
		return;

	inode = string_to_inode(argv[1]);
	if (!inode) 
		return;

	dump_file(argv[0], inode, 0, argv[2]);

	return;
}