Link.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Link = void 0;
  4. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  5. const fanout_1 = require("thingies/lib/fanout");
  6. const { S_IFREG } = fs_node_utils_1.constants;
  7. /**
  8. * Represents a hard link that points to an i-node `node`.
  9. */
  10. class Link {
  11. get steps() {
  12. return this._steps;
  13. }
  14. // Recursively sync children steps, e.g. in case of dir rename
  15. set steps(val) {
  16. this._steps = val;
  17. for (const [child, link] of this.children.entries()) {
  18. if (child === '.' || child === '..') {
  19. continue;
  20. }
  21. link?.syncSteps();
  22. }
  23. }
  24. constructor(vol, parent, name) {
  25. /**
  26. * @deprecated Subscribe to the volume-level `Superblock.changes` bus or use
  27. * `CoreWatcher` instead.
  28. */
  29. this.changes = new fanout_1.FanOut();
  30. this.children = new Map();
  31. // Path to this node as Array: ['usr', 'bin', 'node'].
  32. this._steps = [];
  33. // "i-node" number of the node.
  34. this.ino = 0;
  35. // Number of children.
  36. this.length = 0;
  37. this.vol = vol;
  38. this.parent = parent;
  39. this.name = name;
  40. this.syncSteps();
  41. }
  42. setNode(node) {
  43. this.node = node;
  44. this.ino = node.ino;
  45. }
  46. getNode() {
  47. return this.node;
  48. }
  49. createChild(name, node = this.vol.createNode(S_IFREG | 0o666)) {
  50. const link = new Link(this.vol, this, name);
  51. link.setNode(node);
  52. if (node.isDirectory()) {
  53. link.children.set('.', link);
  54. link.getNode().nlink++;
  55. }
  56. this.setChild(name, link);
  57. return link;
  58. }
  59. setChild(name, link = new Link(this.vol, this, name)) {
  60. this.children.set(name, link);
  61. link.parent = this;
  62. this.length++;
  63. const node = link.getNode();
  64. if (node.isDirectory()) {
  65. link.children.set('..', this);
  66. this.getNode().nlink++;
  67. }
  68. this.getNode().mtime = new Date();
  69. this.changes.emit(['child:add', link, this]);
  70. return link;
  71. }
  72. deleteChild(link) {
  73. const node = link.getNode();
  74. if (node.isDirectory()) {
  75. link.children.delete('..');
  76. this.getNode().nlink--;
  77. }
  78. this.children.delete(link.getName());
  79. this.length--;
  80. this.getNode().mtime = new Date();
  81. this.changes.emit(['child:del', link, this]);
  82. }
  83. getChild(name) {
  84. this.getNode().atime = new Date();
  85. return this.children.get(name);
  86. }
  87. getPath() {
  88. return this.steps.join("/" /* PATH.SEP */);
  89. }
  90. getParentPath() {
  91. const parent = this.steps.slice(0, -1).join("/" /* PATH.SEP */);
  92. return parent ? parent : "/" /* PATH.SEP */;
  93. }
  94. getName() {
  95. return this.steps[this.steps.length - 1];
  96. }
  97. toJSON() {
  98. return {
  99. steps: this.steps,
  100. ino: this.ino,
  101. children: Array.from(this.children.keys()),
  102. };
  103. }
  104. syncSteps() {
  105. this.steps = this.parent ? this.parent.steps.concat([this.name]) : [this.name];
  106. }
  107. }
  108. exports.Link = Link;
  109. //# sourceMappingURL=Link.js.map