ref: 1a4e1fc2e52ab80b55ef0d7fdccca973267fb4cc
dir: /dev.c/
#include <u.h> #include <libc.h> #include "dat.h" #include "fns.h" int devfd = -1; /* device fd */ u64 devsize(void) { Dir *dir; u64 size; if((dir = dirfstat(devfd)) == nil) panic("can't stat device\n"); size = dir->length; free(dir); return size; } s32 devread(u64 blkno, void *b, u16 len) { s32 n; if((n= pread(devfd, b, len*Blocksize, blkno*Blocksize)) != len*Blocksize) panic("devread failed: %r\n"); return n; } s32 devwrite(u64 blkno, void *b, u16 len) { s32 wn; if((wn = pwrite(devfd, b, len*Blocksize, blkno*Blocksize)) != len*Blocksize) panic("devwrite failed: %r\n"); return wn; } u64 devinit(char *devfile) { devfd = open(devfile, ORDWR); if(devfd < 0) panic("can't open %s", devfile); seek(devfd, 0, 0); return devsize(); }