URI:
       tdiskftp.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tdiskftp.c (2199B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <thread.h>
            4 #include <sunrpc.h>
            5 #include <nfs3.h>
            6 #include <diskfs.h>
            7 
            8 int debug;
            9 
           10 void
           11 usage(void)
           12 {
           13         fprint(2, "usage: fsview fspartition cmd\n");
           14         fprint(2, "cmd is:\n");
           15         fprint(2, "\tcat file\n");
           16         fprint(2, "\tls dir\n");
           17         fprint(2, "\tstat file\n");
           18         threadexitsall("usage");
           19 }
           20 
           21 void
           22 printattr(Nfs3Attr *attr)
           23 {
           24         Fmt fmt;
           25         char buf[256];
           26 
           27         fmtfdinit(&fmt, 2, buf, sizeof buf);
           28         nfs3attrprint(&fmt, attr);
           29         fmtfdflush(&fmt);
           30         fprint(2, "\n");
           31 }
           32 
           33 char buf[8192];
           34 
           35 void
           36 x(int ok)
           37 {
           38         if(ok != Nfs3Ok){
           39                 nfs3errstr(ok);
           40                 sysfatal("%r");
           41         }
           42 }
           43 
           44 void
           45 threadmain(int argc, char **argv)
           46 {
           47         char *p, *q;
           48         u32int n;
           49         Disk *disk;
           50         Fsys *fsys;
           51         Nfs3Handle h;
           52         SunAuthUnix au;
           53         Nfs3Attr attr;
           54         u64int offset;
           55         u1int eof;
           56         uchar *data;
           57         char *link;
           58 
           59         ARGBEGIN{
           60         case 'd':
           61                 debug = 1;
           62                 break;
           63         default:
           64                 usage();
           65         }ARGEND
           66 
           67         if(argc != 3)
           68                 usage();
           69 
           70         if((disk = diskopenfile(argv[0])) == nil)
           71                 sysfatal("diskopen: %r");
           72         if((disk = diskcache(disk, 16384, 16)) == nil)
           73                 sysfatal("diskcache: %r");
           74         if((fsys = fsysopen(disk)) == nil)
           75                 sysfatal("fsysopen: %r");
           76 
           77         allowall = 1;
           78         memset(&au, 0, sizeof au);
           79 
           80         /* walk */
           81         if(debug) fprint(2, "get root...");
           82         x(fsysroot(fsys, &h));
           83         p = argv[2];
           84         while(*p){
           85                 while(*p == '/')
           86                         p++;
           87                 if(*p == 0)
           88                         break;
           89                 q = strchr(p, '/');
           90                 if(q){
           91                         *q = 0;
           92                         q++;
           93                 }else
           94                         q = "";
           95                 if(debug) fprint(2, "walk %s...", p);
           96                 x(fsyslookup(fsys, &au, &h, p, &h));
           97                 p = q;
           98         }
           99 
          100         if(debug) fprint(2, "getattr...");
          101         x(fsysgetattr(fsys, &au, &h, &attr));
          102         printattr(&attr);
          103 
          104         /* do the op */
          105         if(strcmp(argv[1], "cat") == 0){
          106                 switch(attr.type){
          107                 case Nfs3FileReg:
          108                 case Nfs3FileDir:
          109                         offset = 0;
          110                         for(;;){
          111                                 x(fsysreadfile(fsys, &au, &h, sizeof buf, offset, &data, &n, &eof));
          112                                 if(n){
          113                                         write(1, data, n);
          114                                         free(data);
          115                                         offset += n;
          116                                 }
          117                                 if(eof)
          118                                         break;
          119                         }
          120                         break;
          121                 case Nfs3FileSymlink:
          122                         x(fsysreadlink(fsys, &au, &h, &link));
          123                         print("%s\n", link);
          124                         break;
          125                 default:
          126                         print("cannot cat: not file, not link\n");
          127                         break;
          128                 }
          129         }else if(strcmp(argv[1], "ls") == 0){
          130                 /* not implemented */
          131         }else if(strcmp(argv[1], "stat") == 0){
          132                 /* already done */
          133         }
          134         threadexitsall(nil);
          135 }