convD2M.c 1.4 KB

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