walk.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const fs = require('fs');
  20. const path = require('path');
  21. const debug = require('debug')('joystream:util:fs:walk');
  22. class Walker
  23. {
  24. constructor(archive, base, cb)
  25. {
  26. this.archive = archive;
  27. this.base = base;
  28. this.slice_offset = this.base.length;
  29. if (this.base[this.slice_offset - 1] != '/') {
  30. this.slice_offset += 1;
  31. }
  32. this.cb = cb;
  33. this.pending = 0;
  34. }
  35. /*
  36. * Check pending
  37. */
  38. check_pending(name)
  39. {
  40. // Decrease pending count again.
  41. this.pending -= 1;
  42. debug('Finishing', name, 'decreases pending to', this.pending);
  43. if (!this.pending) {
  44. debug('No more pending.');
  45. this.cb(null);
  46. }
  47. }
  48. /*
  49. * Helper function for walk; split out because it's used in two places.
  50. */
  51. report_and_recurse(relname, fname, lstat, linktarget)
  52. {
  53. // First report the value
  54. this.cb(null, relname, lstat, linktarget);
  55. // Recurse
  56. if (lstat.isDirectory()) {
  57. this.walk(fname);
  58. }
  59. this.check_pending(fname);
  60. }
  61. walk(dir)
  62. {
  63. // This is a little hacky - since readdir() may take a while, and we don't
  64. // want the pending count to drop to zero before it's finished, we bump
  65. // it up and down while readdir() does it's job.
  66. // What this achieves is that when processing a parent directory finishes
  67. // before walk() on a subdirectory could finish its readdir() call, the
  68. // pending count still has a value.
  69. // Note that in order not to hang on empty directories, we need to
  70. // explicitly check the pending count in cases when there are no files.
  71. this.pending += 1;
  72. this.archive.readdir(dir, (err, files) => {
  73. if (err) {
  74. this.cb(err);
  75. return;
  76. }
  77. // More pending data.
  78. this.pending += files.length;
  79. debug('Reading', dir, 'bumps pending to', this.pending);
  80. files.forEach((name) => {
  81. const fname = path.resolve(dir, name);
  82. this.archive.lstat(fname, (err2, lstat) => {
  83. if (err2) {
  84. this.cb(err2);
  85. return;
  86. }
  87. // The base is always prefixed, so a simple string slice should do.
  88. const relname = fname.slice(this.slice_offset);
  89. // We have a symbolic link? Resolve it.
  90. if (lstat.isSymbolicLink()) {
  91. this.archive.readlink(fname, (err3, linktarget) => {
  92. if (err3) {
  93. this.cb(err3);
  94. return;
  95. }
  96. this.report_and_recurse(relname, fname, lstat, linktarget);
  97. });
  98. }
  99. else {
  100. this.report_and_recurse(relname, fname, lstat);
  101. }
  102. });
  103. });
  104. this.check_pending(dir);
  105. });
  106. }
  107. }
  108. /*
  109. * Recursively walk a file system hierarchy (in undefined order), returning all
  110. * entries via the callback(err, relname, lstat, [linktarget]). The name relative
  111. * to the base is returned.
  112. *
  113. * You can optionally pass an 'archive', i.e. a class or module that responds to
  114. * file system like functions. If you don't, then the 'fs' module is assumed as
  115. * default.
  116. *
  117. * The callback is invoked one last time without data to signal the end of data.
  118. */
  119. module.exports = function(base, archive, cb)
  120. {
  121. // Archive is optional and defaults to fs, but cb is not.
  122. if (!cb) {
  123. cb = archive;
  124. archive = fs;
  125. }
  126. const resolved = path.resolve(base);
  127. const w = new Walker(archive, resolved, cb);
  128. w.walk(resolved);
  129. };