aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/diskfs.h1
-rw-r--r--src/libdiskfs/part.c60
2 files changed, 61 insertions, 0 deletions
diff --git a/include/diskfs.h b/include/diskfs.h
index df28e606..50366ef2 100644
--- a/include/diskfs.h
+++ b/include/diskfs.h
@@ -68,6 +68,7 @@ void blockput(Block *b);
Disk* diskcache(Disk*, uint, uint);
Disk* diskopenventi(struct VtCache*, uchar*);
Disk* diskopenfile(char *file);
+Disk* diskpart(Disk*, u64int offset, u64int count);
Disk* diskopen(char *file);
void diskclose(Disk *disk);
diff --git a/src/libdiskfs/part.c b/src/libdiskfs/part.c
new file mode 100644
index 00000000..d156457d
--- /dev/null
+++ b/src/libdiskfs/part.c
@@ -0,0 +1,60 @@
+#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 region %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;
+
+ return disksync(d->subdisk);
+}
+
+static void
+diskpartclose(Disk *dd)
+{
+ DiskPart *d = (DiskPart*)dd;
+
+ diskclose(d->subdisk);
+ free(d);
+}
+
+Disk*
+diskpart(Disk *subdisk, u64int size, u64int offset)
+{
+ 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;
+}