rimraf-move-remove.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. // https://youtu.be/uhRWMGBjlO8?t=537
  3. //
  4. // 1. readdir
  5. // 2. for each entry
  6. // a. if a non-empty directory, recurse
  7. // b. if an empty directory, move to random hidden file name in $TEMP
  8. // c. unlink/rmdir $TEMP
  9. //
  10. // This works around the fact that unlink/rmdir is non-atomic and takes
  11. // a non-deterministic amount of time to complete.
  12. //
  13. // However, it is HELLA SLOW, like 2-10x slower than a naive recursive rm.
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.rimrafMoveRemoveSync = exports.rimrafMoveRemove = void 0;
  16. const path_1 = require("path");
  17. const default_tmp_js_1 = require("./default-tmp.js");
  18. const ignore_enoent_js_1 = require("./ignore-enoent.js");
  19. const fs_js_1 = require("./fs.js");
  20. const { lstat, rename, unlink, rmdir, chmod } = fs_js_1.promises;
  21. const readdir_or_error_js_1 = require("./readdir-or-error.js");
  22. // crypto.randomBytes is much slower, and Math.random() is enough here
  23. const uniqueFilename = (path) => `.${(0, path_1.basename)(path)}.${Math.random()}`;
  24. const unlinkFixEPERM = async (path) => unlink(path).catch((er) => {
  25. if (er.code === 'EPERM') {
  26. return chmod(path, 0o666).then(() => unlink(path), er2 => {
  27. if (er2.code === 'ENOENT') {
  28. return;
  29. }
  30. throw er;
  31. });
  32. }
  33. else if (er.code === 'ENOENT') {
  34. return;
  35. }
  36. throw er;
  37. });
  38. const unlinkFixEPERMSync = (path) => {
  39. try {
  40. (0, fs_js_1.unlinkSync)(path);
  41. }
  42. catch (er) {
  43. if (er?.code === 'EPERM') {
  44. try {
  45. return (0, fs_js_1.chmodSync)(path, 0o666);
  46. }
  47. catch (er2) {
  48. if (er2?.code === 'ENOENT') {
  49. return;
  50. }
  51. throw er;
  52. }
  53. }
  54. else if (er?.code === 'ENOENT') {
  55. return;
  56. }
  57. throw er;
  58. }
  59. };
  60. const rimrafMoveRemove = async (path, opt) => {
  61. if (opt?.signal?.aborted) {
  62. throw opt.signal.reason;
  63. }
  64. try {
  65. return await rimrafMoveRemoveDir(path, opt, await lstat(path));
  66. }
  67. catch (er) {
  68. if (er?.code === 'ENOENT')
  69. return true;
  70. throw er;
  71. }
  72. };
  73. exports.rimrafMoveRemove = rimrafMoveRemove;
  74. const rimrafMoveRemoveDir = async (path, opt, ent) => {
  75. if (opt?.signal?.aborted) {
  76. throw opt.signal.reason;
  77. }
  78. if (!opt.tmp) {
  79. return rimrafMoveRemoveDir(path, { ...opt, tmp: await (0, default_tmp_js_1.defaultTmp)(path) }, ent);
  80. }
  81. if (path === opt.tmp && (0, path_1.parse)(path).root !== path) {
  82. throw new Error('cannot delete temp directory used for deletion');
  83. }
  84. const entries = ent.isDirectory() ? await (0, readdir_or_error_js_1.readdirOrError)(path) : null;
  85. if (!Array.isArray(entries)) {
  86. // this can only happen if lstat/readdir lied, or if the dir was
  87. // swapped out with a file at just the right moment.
  88. /* c8 ignore start */
  89. if (entries) {
  90. if (entries.code === 'ENOENT') {
  91. return true;
  92. }
  93. if (entries.code !== 'ENOTDIR') {
  94. throw entries;
  95. }
  96. }
  97. /* c8 ignore stop */
  98. if (opt.filter && !(await opt.filter(path, ent))) {
  99. return false;
  100. }
  101. await (0, ignore_enoent_js_1.ignoreENOENT)(tmpUnlink(path, opt.tmp, unlinkFixEPERM));
  102. return true;
  103. }
  104. const removedAll = (await Promise.all(entries.map(ent => rimrafMoveRemoveDir((0, path_1.resolve)(path, ent.name), opt, ent)))).reduce((a, b) => a && b, true);
  105. if (!removedAll) {
  106. return false;
  107. }
  108. // we don't ever ACTUALLY try to unlink /, because that can never work
  109. // but when preserveRoot is false, we could be operating on it.
  110. // No need to check if preserveRoot is not false.
  111. if (opt.preserveRoot === false && path === (0, path_1.parse)(path).root) {
  112. return false;
  113. }
  114. if (opt.filter && !(await opt.filter(path, ent))) {
  115. return false;
  116. }
  117. await (0, ignore_enoent_js_1.ignoreENOENT)(tmpUnlink(path, opt.tmp, rmdir));
  118. return true;
  119. };
  120. const tmpUnlink = async (path, tmp, rm) => {
  121. const tmpFile = (0, path_1.resolve)(tmp, uniqueFilename(path));
  122. await rename(path, tmpFile);
  123. return await rm(tmpFile);
  124. };
  125. const rimrafMoveRemoveSync = (path, opt) => {
  126. if (opt?.signal?.aborted) {
  127. throw opt.signal.reason;
  128. }
  129. try {
  130. return rimrafMoveRemoveDirSync(path, opt, (0, fs_js_1.lstatSync)(path));
  131. }
  132. catch (er) {
  133. if (er?.code === 'ENOENT')
  134. return true;
  135. throw er;
  136. }
  137. };
  138. exports.rimrafMoveRemoveSync = rimrafMoveRemoveSync;
  139. const rimrafMoveRemoveDirSync = (path, opt, ent) => {
  140. if (opt?.signal?.aborted) {
  141. throw opt.signal.reason;
  142. }
  143. if (!opt.tmp) {
  144. return rimrafMoveRemoveDirSync(path, { ...opt, tmp: (0, default_tmp_js_1.defaultTmpSync)(path) }, ent);
  145. }
  146. const tmp = opt.tmp;
  147. if (path === opt.tmp && (0, path_1.parse)(path).root !== path) {
  148. throw new Error('cannot delete temp directory used for deletion');
  149. }
  150. const entries = ent.isDirectory() ? (0, readdir_or_error_js_1.readdirOrErrorSync)(path) : null;
  151. if (!Array.isArray(entries)) {
  152. // this can only happen if lstat/readdir lied, or if the dir was
  153. // swapped out with a file at just the right moment.
  154. /* c8 ignore start */
  155. if (entries) {
  156. if (entries.code === 'ENOENT') {
  157. return true;
  158. }
  159. if (entries.code !== 'ENOTDIR') {
  160. throw entries;
  161. }
  162. }
  163. /* c8 ignore stop */
  164. if (opt.filter && !opt.filter(path, ent)) {
  165. return false;
  166. }
  167. (0, ignore_enoent_js_1.ignoreENOENTSync)(() => tmpUnlinkSync(path, tmp, unlinkFixEPERMSync));
  168. return true;
  169. }
  170. let removedAll = true;
  171. for (const ent of entries) {
  172. const p = (0, path_1.resolve)(path, ent.name);
  173. removedAll = rimrafMoveRemoveDirSync(p, opt, ent) && removedAll;
  174. }
  175. if (!removedAll) {
  176. return false;
  177. }
  178. if (opt.preserveRoot === false && path === (0, path_1.parse)(path).root) {
  179. return false;
  180. }
  181. if (opt.filter && !opt.filter(path, ent)) {
  182. return false;
  183. }
  184. (0, ignore_enoent_js_1.ignoreENOENTSync)(() => tmpUnlinkSync(path, tmp, fs_js_1.rmdirSync));
  185. return true;
  186. };
  187. const tmpUnlinkSync = (path, tmp, rmSync) => {
  188. const tmpFile = (0, path_1.resolve)(tmp, uniqueFilename(path));
  189. (0, fs_js_1.renameSync)(path, tmpFile);
  190. return rmSync(tmpFile);
  191. };
  192. //# sourceMappingURL=rimraf-move-remove.js.map