pump.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /* pump - copy through circular buffer */
  10. #include <u.h>
  11. #include <libc.h>
  12. uint8_t* buf;
  13. Lock arithlock; /* protect 64-bit accesses: unlikely to be atomic */
  14. uint64_t nin;
  15. uint64_t nout;
  16. uint32_t kilo;
  17. uint32_t max;
  18. int32_t ssize;
  19. int64_t tsize;
  20. int dsize;
  21. int done;
  22. int ibsize;
  23. int obsize;
  24. int verb;
  25. int64_t off;
  26. void doinput(int);
  27. void dooutput(int);
  28. static void
  29. usage(void)
  30. {
  31. fprint(2, "usage: pump [-b iando] [-d sleeptime] [-f ofile] "
  32. "[-i ireadsize]\n\t[-k KB-buffer] [-o owritesize] "
  33. "[-s start-KB] [-S seek-offset]\n\t[-t mins] [files]\n");
  34. exits("usage");
  35. }
  36. void
  37. main(int argc, char *argv[])
  38. {
  39. int i, f, fo;
  40. char *file;
  41. kilo = 5000;
  42. obsize = ibsize = 8*1024;
  43. dsize = 0;
  44. fo = 1;
  45. off = 0;
  46. ARGBEGIN {
  47. default:
  48. usage();
  49. case 'b':
  50. obsize = ibsize = atoi(EARGF(usage()));
  51. break;
  52. case 'd':
  53. dsize = atoi(EARGF(usage()));
  54. break;
  55. case 'f':
  56. file = EARGF(usage());
  57. fo = create(file, 1, 0666);
  58. if(fo < 0)
  59. sysfatal("can't create %s: %r", file);
  60. break;
  61. case 'i':
  62. ibsize = atoi(EARGF(usage()));
  63. break;
  64. case 'k':
  65. kilo = atoi(EARGF(usage()));
  66. break;
  67. case 'o':
  68. obsize = atoi(EARGF(usage()));
  69. break;
  70. case 's':
  71. ssize = atoi(EARGF(usage()));
  72. if(ssize <= 0)
  73. ssize = 800;
  74. ssize <<= 10;
  75. break;
  76. case 'S':
  77. off = atoll(EARGF(usage()));
  78. if(off < 0)
  79. sysfatal("seek offset %lld must be non-negative", off);
  80. break;
  81. case 't':
  82. tsize = atoll(EARGF(usage()));
  83. tsize *= 10584000; /* minutes */
  84. break;
  85. } ARGEND
  86. kilo <<= 10;
  87. buf = malloc(kilo);
  88. if(buf == nil)
  89. sysfatal("no memory: %r");
  90. nin = 0;
  91. nout = 0;
  92. done = 0;
  93. max = 0;
  94. switch(rfork(RFPROC|RFNOWAIT|RFNAMEG|RFMEM)) {
  95. default:
  96. dooutput(fo);
  97. break;
  98. case 0:
  99. for(i=0; i<argc; i++) {
  100. f = open(argv[i], OREAD);
  101. if(f < 0) {
  102. fprint(2, "%s: can't open %s: %r\n",
  103. argv0, argv[i]);
  104. break;
  105. }
  106. doinput(f);
  107. close(f);
  108. }
  109. if(argc == 0)
  110. doinput(0);
  111. break;
  112. case -1:
  113. fprint(2, "%s: fork failed: %r\n", argv0);
  114. break;
  115. }
  116. done = 1;
  117. exits(0);
  118. }
  119. /* call with arithlock held */
  120. static int
  121. sleepunlocked(int32_t ms)
  122. {
  123. int r;
  124. unlock(&arithlock);
  125. r = sleep(ms);
  126. lock(&arithlock);
  127. return r;
  128. }
  129. void
  130. dooutput(int f)
  131. {
  132. int32_t n, l, c;
  133. seek(f, off, 0);
  134. lock(&arithlock);
  135. for (;;) {
  136. n = nin - nout;
  137. if(n == 0) {
  138. if(done)
  139. break;
  140. sleepunlocked(dsize);
  141. continue;
  142. }
  143. if(verb && n > max) {
  144. fprint(2, "n = %ld\n", n);
  145. max = n;
  146. }
  147. l = nout % kilo;
  148. unlock(&arithlock);
  149. if(kilo-l < n)
  150. n = kilo-l;
  151. if(n > obsize)
  152. n = obsize;
  153. c = write(f, buf+l, n);
  154. lock(&arithlock);
  155. if(c != n) {
  156. fprint(2, "%s: write error at offset %,lld: %r\n",
  157. argv0, seek(f, 0, 1));
  158. break;
  159. }
  160. nout += c;
  161. if(tsize && nout > tsize) {
  162. fprint(2, "%s: time limit exceeded\n", argv0);
  163. break;
  164. }
  165. }
  166. unlock(&arithlock);
  167. }
  168. void
  169. doinput(int f)
  170. {
  171. int32_t n, l, c, xnin;
  172. seek(f, off, 0);
  173. lock(&arithlock);
  174. if(ssize > 0) {
  175. for (xnin = 0; xnin < ssize && !done; xnin += c) {
  176. n = kilo - (xnin - nout);
  177. if(n == 0)
  178. break;
  179. unlock(&arithlock);
  180. l = xnin % kilo;
  181. if(kilo-l < n)
  182. n = kilo-l;
  183. if(n > ibsize)
  184. n = ibsize;
  185. c = read(f, buf+l, n);
  186. lock(&arithlock);
  187. if(c <= 0) {
  188. if(c < 0)
  189. fprint(2, "%s: read error: %r\n", argv0);
  190. break;
  191. }
  192. }
  193. nin = xnin;
  194. }
  195. while(!done) {
  196. n = kilo - (nin - nout);
  197. if(n == 0) {
  198. sleepunlocked(0);
  199. continue;
  200. }
  201. l = nin % kilo;
  202. unlock(&arithlock);
  203. if(kilo-l < n)
  204. n = kilo-l;
  205. if(n > ibsize)
  206. n = ibsize;
  207. c = read(f, buf+l, n);
  208. lock(&arithlock);
  209. if(c <= 0) {
  210. if(c < 0)
  211. fprint(2, "%s: read error: %r\n", argv0);
  212. break;
  213. }
  214. nin += c;
  215. }
  216. unlock(&arithlock);
  217. }