ref: b3e875d7db45fca0cde9f4efa12872f61afc54d7
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) error("can't stat device: %r\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) error("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) error("devwrite failed: %r\n"); return wn; } u64 devinit(char *devfile) { devfd = open(devfile, ORDWR); if(devfd < 0) error("can't open %s", devfile); seek(devfd, 0, 0); return devsize(); }