write.c (1267B)
1 /* Copyright (C) 2003 Russ Cox, Massachusetts Institute of Technology */ 2 /* See COPYRIGHT */ 3 4 #include <u.h> 5 #include <libc.h> 6 #include <fcall.h> 7 #include <9pclient.h> 8 #include "fsimpl.h" 9 10 static long 11 _fspwrite(CFid *fid, void *buf, long n, vlong offset) 12 { 13 Fcall tx, rx; 14 void *freep; 15 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 }else 23 tx.offset = offset; 24 tx.count = n; 25 tx.data = buf; 26 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 } 42 43 long 44 fspwrite(CFid *fid, void *buf, long n, vlong offset) 45 { 46 long tot, want, got, first; 47 uint msize; 48 49 msize = fid->fs->msize - IOHDRSZ; 50 tot = 0; 51 first = 1; 52 while(tot < n || first){ 53 want = n - tot; 54 if(want > msize) 55 want = msize; 56 got = _fspwrite(fid, (char*)buf+tot, want, offset); 57 first = 0; 58 if(got < 0){ 59 if(tot == 0) 60 return got; 61 break; 62 } 63 tot += got; 64 if(offset != -1) 65 offset += got; 66 } 67 return tot; 68 } 69 70 long 71 fswrite(CFid *fid, void *buf, long n) 72 { 73 return fspwrite(fid, buf, n, -1); 74 }