convD2M.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "lib.h"
  2. #include <string.h>
  3. #include "sys9.h"
  4. #include "dir.h"
  5. uint
  6. _convD2M(Dir *d, uchar *buf, uint nbuf)
  7. {
  8. uchar *p, *ebuf;
  9. char *sv[4];
  10. int i, ns, nsv[4], ss;
  11. if(nbuf < BIT16SZ)
  12. return 0;
  13. p = buf;
  14. ebuf = buf + nbuf;
  15. sv[0] = d->name;
  16. sv[1] = d->uid;
  17. sv[2] = d->gid;
  18. sv[3] = d->muid;
  19. ns = 0;
  20. for(i = 0; i < 4; i++){
  21. nsv[i] = strlen(sv[i]);
  22. ns += nsv[i];
  23. }
  24. ss = STATFIXLEN + ns;
  25. /* set size befor erroring, so user can know how much is needed */
  26. /* note that length excludes count field itself */
  27. PBIT16(p, ss-BIT16SZ);
  28. p += BIT16SZ;
  29. if(ss > nbuf)
  30. return BIT16SZ;
  31. PBIT16(p, d->type);
  32. p += BIT16SZ;
  33. PBIT32(p, d->dev);
  34. p += BIT32SZ;
  35. PBIT8(p, d->qid.type);
  36. p += BIT8SZ;
  37. PBIT32(p, d->qid.vers);
  38. p += BIT32SZ;
  39. PBIT64(p, d->qid.path);
  40. p += BIT64SZ;
  41. PBIT32(p, d->mode);
  42. p += BIT32SZ;
  43. PBIT32(p, d->atime);
  44. p += BIT32SZ;
  45. PBIT32(p, d->mtime);
  46. p += BIT32SZ;
  47. PBIT64(p, d->length);
  48. p += BIT64SZ;
  49. for(i = 0; i < 4; i++){
  50. ns = nsv[i];
  51. if(p + ns + BIT16SZ > ebuf)
  52. return 0;
  53. PBIT16(p, ns);
  54. p += BIT16SZ;
  55. memmove(p, sv[i], ns);
  56. p += ns;
  57. }
  58. if(ss != p - buf)
  59. return 0;
  60. return p - buf;
  61. }
  62. uint
  63. _sizeD2M(Dir *d)
  64. {
  65. char *sv[4];
  66. int i, ns;
  67. sv[0] = d->name;
  68. sv[1] = d->uid;
  69. sv[2] = d->gid;
  70. sv[3] = d->muid;
  71. ns = 0;
  72. for(i = 0; i < 4; i++)
  73. if(sv[i])
  74. ns += strlen(sv[i]);
  75. return STATFIXLEN + ns;
  76. }