Blob
1 /* Copyright (C) 2003 Russ Cox, Massachusetts Institute of Technology */2 /* See COPYRIGHT */4 #include <u.h>5 #include <libc.h>6 #include <fcall.h>7 #include <fs.h>8 #include "fsimpl.h"10 static long11 _fspwrite(Fid *fid, void *buf, long n, vlong offset)12 {13 Fcall tx, rx;14 void *freep;16 tx.type = Twrite;17 tx.fid = fid->fid;18 if(offset == -1){19 qlock(&fid->lk);20 tx.offset = fid->offset;21 qunlock(&fid->lk);22 }else23 tx.offset = offset;24 tx.count = n;25 tx.data = buf;27 if(fsrpc(fid->fs, &tx, &rx, &freep) < 0)28 return -1;29 if(rx.type == Rerror){30 werrstr("%s", rx.ename);31 free(freep);32 return -1;33 }34 if(offset == -1 && rx.count){35 qlock(&fid->lk);36 fid->offset += rx.count;37 qunlock(&fid->lk);38 }39 free(freep);40 return rx.count;41 }43 long44 fspwrite(Fid *fid, void *buf, long n, vlong offset)45 {46 long tot, want, got;47 uint msize;49 msize = fid->fs->msize - IOHDRSZ;50 tot = 0;51 while(tot < n){52 want = n - tot;53 if(want > msize)54 want = msize;55 got = _fspwrite(fid, buf, want, offset);56 if(got < 0){57 if(tot == 0)58 return got;59 break;60 }61 tot += got;62 if(offset != -1)63 offset += got;64 }65 return tot;66 }68 long69 fswrite(Fid *fid, void *buf, long n)70 {71 return fspwrite(fid, buf, n, -1);72 }