convD2M.c 1.4 KB

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