NfsFsDir.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NfsFsDir = void 0;
  4. const NfsFsDirent_1 = require("./NfsFsDirent");
  5. const builder_1 = require("../builder");
  6. const Reader_1 = require("@jsonjoy.com/buffers/lib/Reader");
  7. const XdrDecoder_1 = require("../../../xdr/XdrDecoder");
  8. class NfsFsDir {
  9. constructor(path, nfs, operations) {
  10. this.path = path;
  11. this.nfs = nfs;
  12. this.operations = operations;
  13. this.entries = [];
  14. this.position = 0;
  15. this.closed = false;
  16. }
  17. async ensureLoaded() {
  18. if (this.entries.length > 0 || this.closed)
  19. return;
  20. const attrNums = [1];
  21. const attrMask = [];
  22. for (const attrNum of attrNums) {
  23. const wordIndex = Math.floor(attrNum / 32);
  24. const bitIndex = attrNum % 32;
  25. while (attrMask.length <= wordIndex)
  26. attrMask.push(0);
  27. attrMask[wordIndex] |= 1 << bitIndex;
  28. }
  29. const operations = [...this.operations];
  30. operations.push(builder_1.nfs.READDIR(attrMask));
  31. const response = await this.nfs.compound(operations);
  32. if (response.status !== 0)
  33. throw new Error(`Failed to read directory: ${response.status}`);
  34. const readdirRes = response.resarray[response.resarray.length - 1];
  35. if (readdirRes.status !== 0 || !readdirRes.resok)
  36. throw new Error(`Failed to read directory: ${readdirRes.status}`);
  37. const entryList = readdirRes.resok.entries;
  38. for (let i = 0; i < entryList.length; i++) {
  39. const entry = entryList[i];
  40. const name = entry.name;
  41. const fattr = entry.attrs;
  42. const reader = new Reader_1.Reader();
  43. reader.reset(fattr.attrVals);
  44. const xdr = new XdrDecoder_1.XdrDecoder(reader);
  45. let fileType = 1;
  46. const returnedMask = fattr.attrmask.mask;
  47. for (let i = 0; i < returnedMask.length; i++) {
  48. const word = returnedMask[i];
  49. if (!word)
  50. continue;
  51. for (let bit = 0; bit < 32; bit++) {
  52. if (!(word & (1 << bit)))
  53. continue;
  54. const attrNum = i * 32 + bit;
  55. if (attrNum === 1) {
  56. fileType = xdr.readUnsignedInt();
  57. }
  58. }
  59. }
  60. this.entries.push(new NfsFsDirent_1.NfsFsDirent(name, fileType));
  61. }
  62. }
  63. async close(callback) {
  64. this.closed = true;
  65. this.entries = [];
  66. this.position = 0;
  67. if (callback) {
  68. try {
  69. callback();
  70. }
  71. catch (err) {
  72. callback(err);
  73. }
  74. }
  75. }
  76. closeSync() {
  77. this.closed = true;
  78. this.entries = [];
  79. this.position = 0;
  80. }
  81. async read(callback) {
  82. try {
  83. if (this.closed) {
  84. const err = new Error('Directory is closed');
  85. if (callback) {
  86. callback(err, null);
  87. return null;
  88. }
  89. throw err;
  90. }
  91. await this.ensureLoaded();
  92. if (this.position >= this.entries.length) {
  93. if (callback) {
  94. callback(null, null);
  95. }
  96. return null;
  97. }
  98. const entry = this.entries[this.position++];
  99. if (callback) {
  100. callback(null, entry);
  101. }
  102. return entry;
  103. }
  104. catch (err) {
  105. if (callback) {
  106. callback(err, null);
  107. return null;
  108. }
  109. throw err;
  110. }
  111. }
  112. readSync() {
  113. if (this.closed) {
  114. throw new Error('Directory is closed');
  115. }
  116. if (this.position >= this.entries.length) {
  117. return null;
  118. }
  119. return this.entries[this.position++];
  120. }
  121. async *[Symbol.asyncIterator]() {
  122. await this.ensureLoaded();
  123. for (const entry of this.entries) {
  124. yield entry;
  125. }
  126. }
  127. }
  128. exports.NfsFsDir = NfsFsDir;
  129. //# sourceMappingURL=NfsFsDir.js.map