9dir.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "stdinc.h"
  10. #include "9.h"
  11. /* one entry buffer for reading directories */
  12. struct DirBuf {
  13. DirEntryEnum* dee;
  14. int valid;
  15. DirEntry de;
  16. };
  17. static DirBuf*
  18. dirBufAlloc(File* file)
  19. {
  20. DirBuf *db;
  21. db = vtMemAllocZ(sizeof(DirBuf));
  22. db->dee = deeOpen(file);
  23. if(db->dee == nil){
  24. /* can happen if dir is removed from under us */
  25. vtMemFree(db);
  26. return nil;
  27. }
  28. return db;
  29. }
  30. void
  31. dirBufFree(DirBuf* db)
  32. {
  33. if(db == nil)
  34. return;
  35. if(db->valid)
  36. deCleanup(&db->de);
  37. deeClose(db->dee);
  38. vtMemFree(db);
  39. }
  40. int
  41. dirDe2M(DirEntry* de, uint8_t* p, int np)
  42. {
  43. int n;
  44. Dir dir;
  45. memset(&dir, 0, sizeof(Dir));
  46. dir.qid.path = de->qid;
  47. dir.qid.vers = de->mcount;
  48. dir.mode = de->mode & 0777;
  49. if(de->mode & ModeAppend){
  50. dir.qid.type |= QTAPPEND;
  51. dir.mode |= DMAPPEND;
  52. }
  53. if(de->mode & ModeExclusive){
  54. dir.qid.type |= QTEXCL;
  55. dir.mode |= DMEXCL;
  56. }
  57. if(de->mode & ModeDir){
  58. dir.qid.type |= QTDIR;
  59. dir.mode |= DMDIR;
  60. }
  61. if(de->mode & ModeSnapshot){
  62. dir.qid.type |= QTMOUNT; /* just for debugging */
  63. dir.mode |= DMMOUNT;
  64. }
  65. if(de->mode & ModeTemporary){
  66. dir.qid.type |= QTTMP;
  67. dir.mode |= DMTMP;
  68. }
  69. dir.atime = de->atime;
  70. dir.mtime = de->mtime;
  71. dir.length = de->size;
  72. dir.name = de->elem;
  73. if((dir.uid = unameByUid(de->uid)) == nil)
  74. dir.uid = smprint("(%s)", de->uid);
  75. if((dir.gid = unameByUid(de->gid)) == nil)
  76. dir.gid = smprint("(%s)", de->gid);
  77. if((dir.muid = unameByUid(de->mid)) == nil)
  78. dir.muid = smprint("(%s)", de->mid);
  79. n = convD2M(&dir, p, np);
  80. vtMemFree(dir.muid);
  81. vtMemFree(dir.gid);
  82. vtMemFree(dir.uid);
  83. return n;
  84. }
  85. int
  86. dirRead(Fid* fid, uint8_t* p, int count, int64_t offset)
  87. {
  88. int n, nb;
  89. DirBuf *db;
  90. /*
  91. * special case of rewinding a directory
  92. * otherwise ignore the offset
  93. */
  94. if(offset == 0 && fid->db){
  95. dirBufFree(fid->db);
  96. fid->db = nil;
  97. }
  98. if(fid->db == nil){
  99. fid->db = dirBufAlloc(fid->file);
  100. if(fid->db == nil)
  101. return -1;
  102. }
  103. db = fid->db;
  104. for(nb = 0; nb < count; nb += n){
  105. if(!db->valid){
  106. n = deeRead(db->dee, &db->de);
  107. if(n < 0)
  108. return -1;
  109. if(n == 0)
  110. break;
  111. db->valid = 1;
  112. }
  113. n = dirDe2M(&db->de, p+nb, count-nb);
  114. if(n <= BIT16SZ)
  115. break;
  116. db->valid = 0;
  117. deCleanup(&db->de);
  118. }
  119. return nb;
  120. }