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

typedef struct DiskFile DiskFile;
struct DiskFile
{
	Disk disk;
	int fd;
};

int nfilereads;

static long
preadn(int fd, void *vdata, u32int ulen, u64int offset)
{
	long n;
	uchar *data;
	long len;

	nfilereads++;
	len = ulen;
	data = vdata;
//	fprint(2, "readn 0x%llux 0x%ux\n", offset, ulen);
	while(len > 0){
		n = pread(fd, data, len, offset);
		if(n <= 0)
			break;
		data += n;
		offset += n;
		len -= n;
	}
	return data-(uchar*)vdata;
}

static void
diskfileblockput(Block *b)
{
	free(b);
}

uvlong nreadx;
static Block*
diskfileread(Disk *dd, u32int len, u64int offset)
{
	int n;
	Block *b;
	DiskFile *d = (DiskFile*)dd;

	b = mallocz(sizeof(Block)+len, 1);
	if(b == nil)
		return nil;
	b->data = (uchar*)&b[1];
nreadx += len;
	n = preadn(d->fd, b->data, len, offset);
	if(n <= 0){
		free(b);
		return nil;
	}
	b->_close = diskfileblockput;
	b->len = n;
	return b;
}

static int
diskfilesync(Disk *dd)
{
	USED(dd);
	return 0;
}

static void
diskfileclose(Disk *dd)
{
	DiskFile *d = (DiskFile*)dd;

	close(d->fd);
	free(d);
}

Disk*
diskopenfile(char *file)
{
	int fd;
	DiskFile *d;

	if((fd = open(file, OREAD)) < 0)
		return nil;
	d = mallocz(sizeof(DiskFile), 1);
	if(d == nil){
		close(fd);
		return nil;
	}
	d->disk._read = diskfileread;
	d->disk._sync = diskfilesync;
	d->disk._close = diskfileclose;
	d->fd = fd;
	return &d->disk;
}