aboutsummaryrefslogtreecommitdiff
path: root/src/libdiskfs/part.c
blob: dc5d171f877e0bf0692e6526c895e5a606506146 (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
#include <u.h>
#include <libc.h>
#include <diskfs.h>

typedef struct DiskPart DiskPart;
struct DiskPart
{
	Disk disk;
	Disk *subdisk;
	u64int offset;
	u64int size;
};

static Block*
diskpartread(Disk *dd, u32int len, u64int offset)
{
	DiskPart *d = (DiskPart*)dd;

	if(offset+len > d->size){
		werrstr("read past end of partition %llud + %lud > %llud", offset, len, d->size);
		return nil;
	}
	return diskread(d->subdisk, len, offset+d->offset);
}

static int
diskpartsync(Disk *dd)
{
	DiskPart *d = (DiskPart*)dd;
	
	if(d->subdisk)
		return disksync(d->subdisk);
	return 0;
}

static void
diskpartclose(Disk *dd)
{
	DiskPart *d = (DiskPart*)dd;
	
	if(d->subdisk)
		diskclose(d->subdisk);
	free(d);
}

Disk*
diskpart(Disk *subdisk, u64int offset, u64int size)
{
	DiskPart *d;
	
	d = mallocz(sizeof(DiskPart), 1);
	if(d == nil)
		return nil;
	
	d->subdisk = subdisk;
	d->offset = offset;
	d->size = size;
	d->disk._read = diskpartread;
	d->disk._sync = diskpartsync;
	d->disk._close = diskpartclose;
	
	return &d->disk;
}

void
diskpartabandon(Disk *d)
{
	if(d->_read != diskpartread)
		abort();
	((DiskPart*)d)->subdisk = nil;
}