webfsget.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* Example of how to use webfs */
  10. #include <u.h>
  11. #include <libc.h>
  12. void
  13. xfer(int from, int to)
  14. {
  15. char buf[12*1024];
  16. int n;
  17. while((n = read(from, buf, sizeof buf)) > 0)
  18. if(write(to, buf, n) < 0)
  19. sysfatal("write failed: %r");
  20. if(n < 0)
  21. sysfatal("read failed: %r");
  22. }
  23. void
  24. usage(void)
  25. {
  26. fprint(2, "usage: webfsget [-b baseurl] [-m mtpt] [-p postbody] url\n");
  27. exits("usage");
  28. }
  29. void
  30. main(int argc, char **argv)
  31. {
  32. int conn, ctlfd, fd, n;
  33. char buf[128], *base, *mtpt, *post, *url;
  34. mtpt = "/mnt/web";
  35. post = nil;
  36. base = nil;
  37. ARGBEGIN{
  38. default:
  39. usage();
  40. case 'b':
  41. base = EARGF(usage());
  42. break;
  43. case 'm':
  44. mtpt = EARGF(usage());
  45. break;
  46. case 'p':
  47. post = EARGF(usage());
  48. break;
  49. }ARGEND;
  50. if (argc != 1)
  51. usage();
  52. url = argv[0];
  53. snprint(buf, sizeof buf, "%s/clone", mtpt);
  54. if((ctlfd = open(buf, ORDWR)) < 0)
  55. sysfatal("couldn't open %s: %r", buf);
  56. if((n = read(ctlfd, buf, sizeof buf-1)) < 0)
  57. sysfatal("reading clone: %r");
  58. if(n == 0)
  59. sysfatal("short read on clone");
  60. buf[n] = '\0';
  61. conn = atoi(buf);
  62. if(base)
  63. if(fprint(ctlfd, "baseurl %s", base) < 0)
  64. sysfatal("baseurl ctl write: %r");
  65. if(fprint(ctlfd, "url %s", url) <= 0)
  66. sysfatal("get ctl write: %r");
  67. if(post){
  68. snprint(buf, sizeof buf, "%s/%d/postbody", mtpt, conn);
  69. if((fd = open(buf, OWRITE)) < 0)
  70. sysfatal("open %s: %r", buf);
  71. if(write(fd, post, strlen(post)) < 0)
  72. sysfatal("post write failed: %r");
  73. close(fd);
  74. }
  75. snprint(buf, sizeof buf, "%s/%d/body", mtpt, conn);
  76. if((fd = open(buf, OREAD)) < 0)
  77. sysfatal("open %s: %r", buf);
  78. xfer(fd, 1);
  79. exits(nil);
  80. }