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 <9pclient.h>
8 #include "fsimpl.h"
10 long
11 fspread(CFid *fid, void *buf, long n, vlong offset)
12 {
13 Fcall tx, rx;
14 void *freep;
15 uint msize;
17 msize = fid->fs->msize - IOHDRSZ;
18 if(n > msize)
19 n = msize;
20 tx.type = Tread;
21 tx.fid = fid->fid;
22 if(offset == -1){
23 qlock(&fid->lk);
24 tx.offset = fid->offset;
25 qunlock(&fid->lk);
26 }else
27 tx.offset = offset;
28 tx.count = n;
30 if(_fsrpc(fid->fs, &tx, &rx, &freep) < 0)
31 return -1;
32 if(rx.type == Rerror){
33 werrstr("%s", rx.ename);
34 free(freep);
35 return -1;
36 }
37 if(rx.count){
38 memmove(buf, rx.data, rx.count);
39 if(offset == -1){
40 qlock(&fid->lk);
41 fid->offset += rx.count;
42 qunlock(&fid->lk);
43 }
44 }
45 free(freep);
47 return rx.count;
48 }
50 long
51 fsread(CFid *fid, void *buf, long n)
52 {
53 return fspread(fid, buf, n, -1);
54 }
56 long
57 fsreadn(CFid *fid, void *buf, long n)
58 {
59 long tot, nn;
61 for(tot=0; tot<n; tot+=nn){
62 nn = fsread(fid, (char*)buf+tot, n-tot);
63 if(nn <= 0){
64 if(tot == 0)
65 return nn;
66 break;
67 }
68 }
69 return tot;
70 }