bseek.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. int64_t
  13. Bseek(Biobufhdr *bp, int64_t offset, int base)
  14. {
  15. int64_t n, d;
  16. switch(bp->state) {
  17. default:
  18. fprint(2, "Bseek: unknown state %d\n", bp->state);
  19. return Beof;
  20. case Bracteof:
  21. bp->state = Bractive;
  22. bp->icount = 0;
  23. bp->gbuf = bp->ebuf;
  24. case Bractive:
  25. n = offset;
  26. if(base == 1) {
  27. n += Boffset(bp);
  28. base = 0;
  29. }
  30. /*
  31. * try to seek within buffer
  32. */
  33. if(base == 0) {
  34. /*
  35. * if d is too large for an int, icount may wrap,
  36. * so we need to ensure that icount hasn't wrapped
  37. * and points within the buffer's valid data.
  38. */
  39. d = n - Boffset(bp);
  40. bp->icount += d;
  41. if(d <= bp->bsize && bp->icount <= 0 &&
  42. bp->ebuf - bp->gbuf >= -bp->icount)
  43. return n;
  44. }
  45. /*
  46. * reset the buffer
  47. */
  48. n = seek(bp->fid, n, base);
  49. bp->icount = 0;
  50. bp->gbuf = bp->ebuf;
  51. break;
  52. case Bwactive:
  53. Bflush(bp);
  54. n = seek(bp->fid, offset, base);
  55. break;
  56. }
  57. bp->offset = n;
  58. return n;
  59. }