From 462f50910add57688b80df82ef772991f26ee761 Mon Sep 17 00:00:00 2001 From: lionkov Date: Thu, 17 Jul 2008 17:20:51 +0000 Subject: [PATCH] Fix the non-blocking read and write oprations. git-svn-id: http://npfs.svn.sourceforge.net/svnroot/npfs/spfs/trunk@7 fd0465a7-2a7b-4605-b674-a3f860cb63d9 --- include/spclient.h | 4 ++ libspclient/examples/9ls.c | 19 +++-- libspclient/examples/9read.c | 19 +++-- libspclient/examples/9read2.c | 19 +++-- libspclient/examples/{9read2.c => 9readnb.c} | 100 ++++++++++++++++++--------- libspclient/examples/9write.c | 19 +++-- libspclient/readnb.c | 36 +++++----- libspclient/writenb.c | 31 ++++----- 8 files changed, 135 insertions(+), 112 deletions(-) copy libspclient/examples/{9read2.c => 9readnb.c} (63%) diff --git a/include/spclient.h b/include/spclient.h index 0ff7b10..1567c94 100644 --- a/include/spclient.h +++ b/include/spclient.h @@ -54,6 +54,10 @@ int spc_write(Spcfid *fid, u8 *buf, u32 count, u64 offset); int spc_dirread(Spcfid *fid, Spwstat **stat); Spwstat *spc_stat(Spcfsys *fs, char *path); int spc_wstat(Spcfsys *fs, char *path, Spwstat *wst); +int spc_readnb(Spcfid *fid, u8 *buf, u32 count, u64 offset, + void (*cb)(void *, int), void *cba); +int spc_writenb(Spcfid *fid, u8 *buf, u32 count, u64 offset, + void (*cb)(void *, int), void *cba); Spcfd *spcfd_add(Spcfid *fid, void (*notify)(Spcfd *, void *), void *aux); Spcfd *spcfd_add_fd(int fd, void (*notify)(Spcfd *, void *), void *aux); void spcfd_remove(Spcfd *spcfd); diff --git a/libspclient/examples/9ls.c b/libspclient/examples/9ls.c index d80a268..a893dc1 100644 --- a/libspclient/examples/9ls.c +++ b/libspclient/examples/9ls.c @@ -50,19 +50,13 @@ main(int argc, char **argv) int i, n; int c; char *addr; - char *uname, *path; + char *path; Spuser *user; Spcfsys *fs; Spcfid *fid; Spwstat *stat; - user = sp_uid2user(geteuid()); - if (!user) { - fprintf(stderr, "cannot retrieve user %d\n", geteuid()); - exit(1); - } - - uname = user->uname; + user = sp_unix_users->uid2user(sp_unix_users, geteuid()); while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': @@ -70,7 +64,7 @@ main(int argc, char **argv) break; case 'u': - uname = optarg; + user = sp_unix_users->uname2user(sp_unix_users, optarg); break; default: @@ -78,7 +72,10 @@ main(int argc, char **argv) } } - + if (!user) { + fprintf(stderr, "cannot retrieve user\n"); + exit(1); + } if (argc - optind < 2) usage(); @@ -86,7 +83,7 @@ main(int argc, char **argv) addr = argv[optind]; path = argv[optind+1]; - fs = spc_netmount(addr, uname, 564); + fs = spc_netmount(addr, user, 564, NULL, NULL); fid = spc_open(fs, path, Oread); if (!fid) { diff --git a/libspclient/examples/9read.c b/libspclient/examples/9read.c index d32e8ef..c0a67e4 100644 --- a/libspclient/examples/9read.c +++ b/libspclient/examples/9read.c @@ -47,19 +47,13 @@ main(int argc, char **argv) int i, n, off; int c; char *addr; - char *uname, *path; + char *path; Spuser *user; Spcfsys *fs; Spcfid *fid; char buf[512]; - user = sp_uid2user(geteuid()); - if (!user) { - fprintf(stderr, "cannot retrieve user %d\n", geteuid()); - exit(1); - } - - uname = user->uname; + user = sp_unix_users->uid2user(sp_unix_users, geteuid()); while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': @@ -67,15 +61,18 @@ main(int argc, char **argv) break; case 'u': - uname = optarg; + user = sp_unix_users->uname2user(sp_unix_users, optarg); break; default: usage(); } } - + if (!user) { + fprintf(stderr, "cannot retrieve user %d\n", geteuid()); + exit(1); + } if (argc - optind < 2) usage(); @@ -83,7 +80,7 @@ main(int argc, char **argv) addr = argv[optind]; path = argv[optind+1]; - fs = spc_netmount(addr, uname, 564); + fs = spc_netmount(addr, user, 564, NULL, NULL); fid = spc_open(fs, path, Oread); if (!fid) { fprintf(stderr, "cannot open\n"); diff --git a/libspclient/examples/9read2.c b/libspclient/examples/9read2.c index c1d2b28..f73c5e1 100644 --- a/libspclient/examples/9read2.c +++ b/libspclient/examples/9read2.c @@ -73,18 +73,12 @@ main(int argc, char **argv) { int c; char *addr; - char *uname, *path; + char *path; Spuser *user; Spcfsys *fs; Spcfid *fid; - user = sp_uid2user(geteuid()); - if (!user) { - fprintf(stderr, "cannot retrieve user %d\n", geteuid()); - exit(1); - } - - uname = user->uname; + user = sp_unix_users->uid2user(sp_unix_users, geteuid()); while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': @@ -92,15 +86,18 @@ main(int argc, char **argv) break; case 'u': - uname = optarg; + user = sp_unix_users->uname2user(sp_unix_users, optarg); break; default: usage(); } } - + if (!user) { + fprintf(stderr, "cannot retrieve user %d\n", geteuid()); + exit(1); + } if (argc - optind < 2) usage(); @@ -108,7 +105,7 @@ main(int argc, char **argv) addr = argv[optind]; path = argv[optind+1]; - fs = spc_netmount(addr, uname, 564); + fs = spc_netmount(addr, user, 564, NULL, NULL); fid = spc_open(fs, path, Oread); if (!fid) { fprintf(stderr, "cannot open\n"); diff --git a/libspclient/examples/9read2.c b/libspclient/examples/9readnb.c similarity index 63% copy from libspclient/examples/9read2.c copy to libspclient/examples/9readnb.c index c1d2b28..803c9b1 100644 --- a/libspclient/examples/9read2.c +++ b/libspclient/examples/9readnb.c @@ -32,59 +32,72 @@ #include "spfs.h" #include "spclient.h" +#define BSIZE 8192 + extern int spc_chatty; -static Spcfd *ispcfd; +static int done; +static Spcfid *fid; +static u64 offset; +static u8 buf1[BSIZE]; +static u8 buf2[BSIZE]; +static u8 buf3[BSIZE]; +static u8 buf4[BSIZE]; +static int cnt; + +static int readnb(void *buf); static void usage() { - fprintf(stderr, "9read -d addr path\n"); + fprintf(stderr, "9readnb -d addr path\n"); exit(1); } static void -disconnect() +cb(void *a, int count) { - spcfd_remove(ispcfd); - spcfd_stop_loop(); + u8 *buf; + + buf = a; + fprintf(stderr, "cb %d\n", count); + if (count < 0) { + fprintf(stderr, "error\n"); + done++; + } else if (count > 0) { + write(1, buf, count); + cnt += count; + fprintf(stderr, "%d\n", cnt); + readnb(buf); + } else + done++; } -static void -in_notify(Spcfd *spcfd, void *aux) +static int +readnb(void *buf) { - int n; - char buf[256]; + u64 off; - while (spcfd_can_read(spcfd)) { - n = spcfd_read(spcfd, buf, sizeof(buf)); - if (n <= 0) - break; + off = offset; + offset += BSIZE; - write(1, buf, n); - } + if (spc_readnb(fid, buf, BSIZE, off, cb, buf) < 0) + return -1; - if (n<=0 || spcfd_has_error(spcfd)) - disconnect(); + return 0; } int main(int argc, char **argv) { + int ecode; int c; char *addr; - char *uname, *path; + char *ename, *path; Spuser *user; Spcfsys *fs; - Spcfid *fid; - - user = sp_uid2user(geteuid()); - if (!user) { - fprintf(stderr, "cannot retrieve user %d\n", geteuid()); - exit(1); - } - uname = user->uname; + user = sp_unix_users->uid2user(sp_unix_users, geteuid()); while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': @@ -92,15 +105,18 @@ main(int argc, char **argv) break; case 'u': - uname = optarg; + user = sp_unix_users->uname2user(sp_unix_users, optarg); break; default: usage(); } } - + if (!user) { + fprintf(stderr, "cannot retrieve user %d\n", geteuid()); + exit(1); + } if (argc - optind < 2) usage(); @@ -108,17 +124,37 @@ main(int argc, char **argv) addr = argv[optind]; path = argv[optind+1]; - fs = spc_netmount(addr, uname, 564); + fs = spc_netmount(addr, user, 564, NULL, NULL); fid = spc_open(fs, path, Oread); if (!fid) { fprintf(stderr, "cannot open\n"); exit(1); } - ispcfd = spcfd_add(fid, in_notify, fid); - spcfd_start_loop(); + if (readnb(buf1) < 0) + goto error; + + if (readnb(buf2) < 0) + goto error; + + if (readnb(buf3) < 0) + goto error; + + if (readnb(buf4) < 0) + goto error; + + while (done < 4) + sp_poll_once(); + spc_close(fid); spc_umount(fs); - exit(0); + return 0; + +error: + sp_rerror(&ename, &ecode); + fprintf(stderr, "Error: %s\n", ename); + return -1; + } + diff --git a/libspclient/examples/9write.c b/libspclient/examples/9write.c index 7f83c5b..33a47ef 100644 --- a/libspclient/examples/9write.c +++ b/libspclient/examples/9write.c @@ -47,19 +47,13 @@ main(int argc, char **argv) int i, n, off; int c; char *addr; - char *uname, *path; + char *path; Spuser *user; Spcfsys *fs; Spcfid *fid; char buf[512]; - user = sp_uid2user(geteuid()); - if (!user) { - fprintf(stderr, "cannot retrieve user %d\n", geteuid()); - exit(1); - } - - uname = user->uname; + user = sp_unix_users->uid2user(sp_unix_users, geteuid()); while ((c = getopt(argc, argv, "dp:")) != -1) { switch (c) { case 'd': @@ -67,15 +61,18 @@ main(int argc, char **argv) break; case 'u': - uname = optarg; + user = sp_unix_users->uname2user(sp_unix_users, optarg); break; default: usage(); } } - + if (!user) { + fprintf(stderr, "cannot retrieve user %d\n", geteuid()); + exit(1); + } if (argc - optind < 2) usage(); @@ -83,7 +80,7 @@ main(int argc, char **argv) addr = argv[optind]; path = argv[optind+1]; - fs = spc_netmount(addr, uname, 564); + fs = spc_netmount(addr, user, 564, NULL, NULL); fid = spc_open(fs, path, Owrite); if (!fid) { fid = spc_create(fs, path, 0666, Owrite); diff --git a/libspclient/readnb.c b/libspclient/readnb.c index 65fd59b..8165a28 100644 --- a/libspclient/readnb.c +++ b/libspclient/readnb.c @@ -49,7 +49,6 @@ spc_send_read_request(Spcread *r) int n; Spcfid *fid; Spcfsys *fs; - Spfcall *fc; fid = r->fid; fs = fid->fsys; @@ -58,12 +57,12 @@ spc_send_read_request(Spcread *r) if (n == 0) n = fs->msize - IOHDRSZ; - if (n > (r->count - r->offset)) - n = r->count - r->offset; + if (n > r->count) + n = r->count; r->tc = sp_create_tread(fid->fid, r->offset, n); if (spc_rpcnb(fs, r->tc, spc_read_cb, r) < 0) { - free(fc); + free(r->tc); return -1; } @@ -73,29 +72,24 @@ spc_send_read_request(Spcread *r) static void spc_read_cb(void *cba, Spfcall *rc) { - int ecode; - char *ename; + int n; Spcread *r; r = cba; free(r->tc); - sp_rerror(&ename, &ecode); - if (ename) { + if (sp_haserror()) { + free(rc); (*r->cb)(r->cba, -1); return; } - memmove(r->buf + r->offset, rc->data, rc->count); - r->offset += rc->count; - if (!rc->count || r->offset==r->count) { - (*r->cb)(r->cba, r->offset); - free(rc); - return; - } + n = rc->count; + if (n > r->count) + n = r->count; + memmove(r->buf, rc->data, n); + (*r->cb)(r->cba, n); free(rc); - if (spc_send_read_request(r) < 0) - (*r->cb)(r->cba, -1); } int @@ -109,13 +103,17 @@ spc_readnb(Spcfid *fid, u8 *buf, u32 count, u64 offset, return -1; r->fid = fid; - r->offset = 0; + r->offset = offset; r->count = count; r->buf = buf; r->cb = cb; r->cba = cba; r->tc = NULL; - spc_send_read_request(r); + if (spc_send_read_request(r) < 0) { + free(r); + return -1; + } + return 0; } diff --git a/libspclient/writenb.c b/libspclient/writenb.c index 6b4cc00..e6383ab 100644 --- a/libspclient/writenb.c +++ b/libspclient/writenb.c @@ -49,7 +49,6 @@ spc_send_write_request(Spcwrite *r) int n; Spcfid *fid; Spcfsys *fs; - Spfcall *fc; fid = r->fid; fs = fid->fsys; @@ -58,12 +57,12 @@ spc_send_write_request(Spcwrite *r) if (n == 0) n = fs->msize - IOHDRSZ; - if (n > (r->count - r->offset)) - n = r->count - r->offset; + if (n > r->count) + n = r->count; r->tc = sp_create_twrite(fid->fid, r->offset, n, r->buf + r->offset); if (spc_rpcnb(fs, r->tc, spc_write_cb, r) < 0) { - free(fc); + free(r->tc); return -1; } @@ -73,28 +72,22 @@ spc_send_write_request(Spcwrite *r) static void spc_write_cb(void *cba, Spfcall *rc) { - int ecode; - char *ename; + int n; Spcwrite *r; r = cba; free(r->tc); - sp_rerror(&ename, &ecode); - if (ename) { + if (sp_haserror()) { (*r->cb)(r->cba, -1); return; } - r->offset += rc->count; - if (!rc->count || r->offset==r->count) { - (*r->cb)(r->cba, r->offset); - free(rc); - return; - } + n = rc->count; + if (n > r->count) + n = r->count; + (*r->cb)(r->cba, n); free(rc); - if (spc_send_write_request(r) < 0) - (*r->cb)(r->cba, -1); } int @@ -115,6 +108,10 @@ spc_writenb(Spcfid *fid, u8 *buf, u32 count, u64 offset, r->cba = cba; r->tc = NULL; - spc_send_write_request(r); + if (spc_send_write_request(r) < 0) { + free(r); + return -1; + } + return 0; } -- 2.11.4.GIT