summaryrefslogtreecommitdiff
path: root/libparted/fs/ext2/parted_io.c
blob: 8f132403ea9e0020d4453b2a6c3ac02b4952b001 (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
/*
    parted_io.c -- parted I/O code interface for libext2resize
    Copyright (C) 1998-2000, 2007 Free Software Foundation, Inc.

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#ifndef DISCOVER_ONLY

#include <parted/parted.h>
#include <stdio.h>
#include <stdlib.h>
#include "ext2.h"

/* pseudo-header.... */

loff_t llseek(unsigned int fd, loff_t offset, unsigned int whence);

struct my_cookie
{
	int logsize;
	PedGeometry* geom;
};

/* ...then this must be pseudo-code  :-) */

static int   do_close        (void *cookie);
static int   do_sync         (void *cookie);
static blk_t do_get_size     (void *cookie);
static int   do_read         (void *cookie, void *ptr, blk_t block, blk_t numblocks);
static int   do_set_blocksize(void *cookie, int logsize);
static int   do_write        (void *cookie, void *ptr, blk_t block, blk_t numblocks);

struct ext2_dev_ops ops =
{
	close:		do_close,
	get_size:	do_get_size,
	read:		do_read,
	set_blocksize:	do_set_blocksize,
	sync:		do_sync,
	write:		do_write
};



static int do_close(void *cookie)
{
	struct my_cookie *monster = cookie;

	return ped_geometry_sync(monster->geom);
}

static int do_sync(void *cookie)
{
	struct my_cookie *monster = cookie;

	return ped_geometry_sync(monster->geom);
}

static blk_t do_get_size(void *cookie)
{
	struct my_cookie *monster = cookie;

	return monster->geom->length >> (monster->logsize - 9);
}

static int do_read(void *cookie, void *ptr, blk_t block, blk_t num)
{
	struct my_cookie *monster = cookie;

	return ped_geometry_read(monster->geom, ptr, block << (monster->logsize - 9), num << (monster->logsize - 9));
}

static int do_set_blocksize(void *cookie, int logsize)
{
	struct my_cookie *monster = cookie;

	monster->logsize = logsize;
	return 1;
} 

static int do_write(void *cookie, void *ptr, blk_t block, blk_t num)
{
	struct my_cookie *monster = cookie;

	return ped_geometry_write(monster->geom, ptr,
				  block << (monster->logsize - 9),
				  num << (monster->logsize - 9));
}


struct ext2_dev_handle *ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom)
{
	struct ext2_dev_handle *dh;
	struct my_cookie *monster;

	if ((dh = ped_malloc(sizeof(struct ext2_dev_handle))) == NULL)
		goto error;

	if ((monster = ped_malloc(sizeof(struct my_cookie))) == NULL)
		goto error_free_dh;

	dh->ops = &ops;
	dh->cookie = monster;
	monster->logsize = 9;
	monster->geom = geom;

	return dh;

error_free_dh:
	free(dh);
error:
	return NULL;
}

void ext2_destroy_dev_handle(struct ext2_dev_handle *handle)
{
	ped_geometry_destroy(((struct my_cookie *)handle->cookie)->geom);
	free(handle->cookie);
	free(handle);
}
#endif /* !DISCOVER_ONLY */