2 parted_io.c -- parted I/O code interface for libext2resize
3 Copyright (C) 1998-2000, 2007, 2009-2010 Free Software Foundation,
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <parted/parted.h>
29 /* pseudo-header.... */
31 loff_t llseek(unsigned int fd, loff_t offset, unsigned int whence);
39 /* ...then this must be pseudo-code :-) */
41 static int do_close (void *cookie);
42 static int do_sync (void *cookie);
43 static blk_t do_get_size (void *cookie);
44 static int do_read (void *cookie, void *ptr, blk_t block, blk_t numblocks);
45 static int do_set_blocksize(void *cookie, int logsize);
46 static int do_write (void *cookie, void *ptr, blk_t block, blk_t numblocks);
48 struct ext2_dev_ops ops =
51 get_size: do_get_size,
53 set_blocksize: do_set_blocksize,
60 static int do_close(void *cookie)
62 struct my_cookie *monster = cookie;
64 return ped_geometry_sync(monster->geom);
67 static int do_sync(void *cookie)
69 struct my_cookie *monster = cookie;
71 return ped_geometry_sync(monster->geom);
74 static blk_t do_get_size(void *cookie)
76 struct my_cookie *monster = cookie;
78 return monster->geom->length >> (monster->logsize - 9);
81 static int do_read(void *cookie, void *ptr, blk_t block, blk_t num)
83 struct my_cookie *monster = cookie;
85 return ped_geometry_read(monster->geom, ptr,
86 (PedSector) block << (monster->logsize - 9),
87 (PedSector) num << (monster->logsize - 9));
90 static int do_set_blocksize(void *cookie, int logsize)
92 struct my_cookie *monster = cookie;
94 monster->logsize = logsize;
98 static int do_write(void *cookie, void *ptr, blk_t block, blk_t num)
100 struct my_cookie *monster = cookie;
102 return ped_geometry_write(monster->geom, ptr,
103 (PedSector) block << (monster->logsize - 9),
104 (PedSector) num << (monster->logsize - 9));
108 struct ext2_dev_handle *ext2_make_dev_handle_from_parted_geometry(PedGeometry* geom)
110 struct ext2_dev_handle *dh;
111 struct my_cookie *monster;
113 if ((dh = ped_malloc(sizeof(struct ext2_dev_handle))) == NULL)
116 if ((monster = ped_malloc(sizeof(struct my_cookie))) == NULL)
120 dh->cookie = monster;
121 monster->logsize = 9;
122 monster->geom = geom;
132 void ext2_destroy_dev_handle(struct ext2_dev_handle *handle)
134 ped_geometry_destroy(((struct my_cookie *)handle->cookie)->geom);
135 free(handle->cookie);
138 #endif /* !DISCOVER_ONLY */