entry.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <venti.h>
  12. #include "cvt.h"
  13. static int
  14. checksize(int n)
  15. {
  16. if(n < 256 || n > VtMaxLumpSize) {
  17. werrstr("bad block size %#x", n);
  18. return -1;
  19. }
  20. return 0;
  21. }
  22. void
  23. vtentrypack(VtEntry *e, uint8_t *p, int index)
  24. {
  25. uint32_t t32;
  26. int flags;
  27. uint8_t *op;
  28. int depth;
  29. p += index * VtEntrySize;
  30. op = p;
  31. U32PUT(p, e->gen);
  32. p += 4;
  33. U16PUT(p, e->psize);
  34. p += 2;
  35. U16PUT(p, e->dsize);
  36. p += 2;
  37. depth = e->type&VtTypeDepthMask;
  38. flags = (e->flags&~(_VtEntryDir|_VtEntryDepthMask));
  39. flags |= depth << _VtEntryDepthShift;
  40. if(e->type - depth == VtDirType)
  41. flags |= _VtEntryDir;
  42. U8PUT(p, flags);
  43. p++;
  44. memset(p, 0, 5);
  45. p += 5;
  46. U48PUT(p, e->size, t32);
  47. p += 6;
  48. memmove(p, e->score, VtScoreSize);
  49. p += VtScoreSize;
  50. assert(p-op == VtEntrySize);
  51. }
  52. int
  53. vtentryunpack(VtEntry *e, uint8_t *p, int index)
  54. {
  55. uint8_t *op;
  56. p += index * VtEntrySize;
  57. op = p;
  58. e->gen = U32GET(p);
  59. p += 4;
  60. e->psize = U16GET(p);
  61. p += 2;
  62. e->dsize = U16GET(p);
  63. p += 2;
  64. e->flags = U8GET(p);
  65. e->type = (e->flags&_VtEntryDir) ? VtDirType : VtDataType;
  66. e->type += (e->flags & _VtEntryDepthMask) >> _VtEntryDepthShift;
  67. e->flags &= ~(_VtEntryDir|_VtEntryDepthMask);
  68. p++;
  69. p += 5;
  70. e->size = U48GET(p);
  71. p += 6;
  72. memmove(e->score, p, VtScoreSize);
  73. p += VtScoreSize;
  74. assert(p-op == VtEntrySize);
  75. if(!(e->flags & VtEntryActive))
  76. return 0;
  77. /*
  78. * Some old vac files use psize==0 and dsize==0 when the
  79. * file itself has size 0 or is zeros. Just to make programs not
  80. * have to figure out what block sizes of 0 means, rewrite them.
  81. */
  82. if(e->psize == 0 && e->dsize == 0
  83. && memcmp(e->score, vtzeroscore, VtScoreSize) == 0){
  84. e->psize = 4096;
  85. e->dsize = 4096;
  86. }
  87. if(checksize(e->psize) < 0 || checksize(e->dsize) < 0)
  88. return -1;
  89. return 0;
  90. }