File: file.c

package info (click to toggle)
delo 0.7-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 444 kB
  • ctags: 148
  • sloc: ansic: 931; perl: 235; makefile: 145; sh: 132; asm: 15
file content (265 lines) | stat: -rw-r--r-- 5,661 bytes parent folder | download
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

#include <sys/types.h>
typedef int FILE;
#include <linux/ext2_fs.h>
#include <ext2fs/ext2fs.h>

#include "dec_prom.h"
#include "delo.h"

#define ec ,},{

static struct {
    int errnum;
    char *desc;
} ext2errors[] = { 
{ 0, "OK"
#include "ext2_err.et"
}
};

static errcode_t linux_open (const char *name, int flags, io_channel * channel);
static errcode_t linux_close (io_channel channel);
static int partition_start;

#define _BLOCK_SIZE	512
#define _PART_START	64

extern const struct callback *callv;

static errcode_t delo_set_blksize(io_channel channel, int blksize);
static errcode_t delo_open(const char *name, int flags, io_channel *channel);
static errcode_t delo_read_blk(io_channel channel,
		unsigned long block, int count, void *data);
static errcode_t delo_close(io_channel channel);
static errcode_t delo_write_blk(io_channel channel,
		unsigned long block, int count, const void *data);
static errcode_t delo_flush(io_channel channel);

static struct struct_io_manager struct_delo_io_manager = {
	EXT2_ET_MAGIC_IO_MANAGER,
	"delo i/o manager",
	delo_open,
	delo_close,
	delo_set_blksize,
	delo_read_blk,
	delo_write_blk,
	delo_flush
};

static ext2_filsys fs = 0;
static io_manager delo_io_manager = &struct_delo_io_manager;
static void *filebuffer;

static errcode_t delo_open(const char *name, int flags, io_channel *channel) {

	io_channel	io;	
	int		retval;

#ifdef DEBUG
	puts("delo_open: called");
#endif

	retval=bootinit(0);

#ifdef DEBUG
	printf("delo_open: bootinit returned %d\n",retval);
#endif

	if (retval)
		return EXT2_ET_BAD_DEVICE_NAME;

	/* This cant fail :) */
	io = (io_channel) malloc (sizeof (struct struct_io_channel));

	memset(io, 0, sizeof(struct struct_io_channel));

	io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
	io->manager = delo_io_manager;
	io->name = (char *) malloc (strlen(name) + 1);
 	strcpy(io->name, name);
	io->block_size = _BLOCK_SIZE;
	io->read_error = 0;
	io->write_error = 0;

	*channel = io;
	return(0);
}

static errcode_t delo_close(io_channel channel) {
#ifdef DEBUG
	printf("delo_close: called\n");
#endif
	return(0);
}

static errcode_t delo_set_blksize(io_channel channel, int blksize) {
	channel->block_size = blksize;
#ifdef DEBUG
	printf("delo_set_blksize: called %d\n",blksize);
#endif
	return(0);
}

static errcode_t delo_read_blk(io_channel channel,
		unsigned long block, int count, void *data) {

	int retval;
	struct ext2_super_block *sb = (struct ext2_super_block *) data;

	/* FIXME: This is our partition start */
	block=block * (channel->block_size / _BLOCK_SIZE) +
				partition_start;

	/* If its negative it means bytes - Positive -> Blocks */
	count=( count < 0 ) ?
			-count :
			count * channel->block_size;

#ifdef DEBUG
	printf("delo_read_blk: called for %d count %d to %x\n",
			block, count, data);
#endif

	retval=bootread(block, data, count);
	
	return(0);
}

static errcode_t delo_write_blk(io_channel channel,
		unsigned long block, int count, const void *data) {
#ifdef DEBUG
	puts("delo_write_blk: called");
#endif
	return(0);
}

static errcode_t delo_flush(io_channel channel) {
#ifdef DEBUG
	puts("delo_flush: called");
#endif
	return(0);
}

void com_err (const char *a, long i, const char *fmt, ... ) {
	printf((char *) fmt);
}

static void ext2fs_error (int errcode)
{
    int i;
    
    for (i = 0; i < sizeof (ext2errors) / sizeof (ext2errors[0]); i++)
    	if (ext2errors [i].errnum == errcode) {
    	    printf("%s",ext2errors [i].desc);
    	    return;
    	}
    printf("Unknown ext2 error");
}

static int delo_dump_block (ext2_filsys fs, blk_t *blocknr, int blockcnt, 
			void *private) {

#ifdef DEBUG
	printf("delo_dump_block: blocknr %d blockcnt %d to %x\n",
				*blocknr, blockcnt, 
				filebuffer + blockcnt * fs->io->block_size);

	printf("%d/%d ", *blocknr, blockcnt);
#endif

	if (blockcnt < 0) 
		return 0;

	/* Some progress indicator */
	if (blockcnt % 120 == 0)
		printf(".");

	if (io_channel_read_blk(fs->io,
				*blocknr,
				1,
				filebuffer + blockcnt * fs->io->block_size))
		return BLOCK_ABORT;

	return(0);
}

void *readfile(char *filename) {
	int	retval;
	ino_t	root = EXT2_ROOT_INO,
		inode = 0;
	struct 	ext2_inode	inodebuffer;

	retval=ext2fs_namei_follow(fs, root, root, filename, &inode);

#ifdef DEBUG
	printf("readfile: ext2fs_namei_follow returned %d\n",retval);	
#endif

	if (retval != 0) {
		printf("extfs_open returned ");
		ext2fs_error(retval);
		printf("(%d)\n", retval);
		return 0;
	}

	retval=ext2fs_read_inode(fs, inode, &inodebuffer);

	if (retval != 0) {
		printf("extfs_read_inode returned ");
		ext2fs_error(retval);
		printf("(%d)\n", retval);
		return 0;
	}
#ifdef DEBUG
	printf("Allocating %d\n",inodebuffer.i_size + _SECTOR_SIZE);
#endif

	filebuffer=malloc(inodebuffer.i_size + _SECTOR_SIZE);

	/* Beginning of progress indicator */
	printf("Loading %s .", filename);

	retval=ext2fs_block_iterate(fs, inode, 0, 0, delo_dump_block, 0);

#ifdef DEBUG
	printf("readfile: ext2fs_block_iterate returned %d\n",retval);	
#endif

	if (retval != 0) {
		printf("ext2fs_block_iterate returned ");
		ext2fs_error(retval);
		printf("(%d)\n",retval);
		return 0;
	}	

	/* End of progress indicator */
	printf(" ok\n");

#ifdef DEBUG
	puts("readfile: ending");
#endif

	return filebuffer;
}

int open_partition(char *partition) {
	int retval;

#ifdef DEBUG
	printf("Getting partition info\n");
#endif

	if (!(partition_start  = getpartoffset(partition))) {
		printf("Couldnt find partition %s\n",partition);
		return(1);
	}

	if (retval=ext2fs_open(partition, EXT2_FLAG_RW,
				0, 0, delo_io_manager, &fs)) {
		printf("extfs_open returned ");
		ext2fs_error(retval);
		printf("(%d)\n", retval);
	}
	return retval;
}