fs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.USE_HARD_LINKS = exports.DO_NOT_USE_HARD_LINKS = exports.dirSize = exports.copyDir = exports.FileCopier = exports.copyOrLinkFile = exports.copyFile = exports.walk = exports.exists = exports.statOrNull = exports.unlinkIfExists = exports.CopyFileTransformer = exports.CONCURRENCY = exports.MAX_FILE_REQUESTS = void 0;
  4. const bluebird_lst_1 = require("bluebird-lst");
  5. const fs_extra_1 = require("fs-extra");
  6. const os_1 = require("os");
  7. const promises_1 = require("fs/promises");
  8. const path = require("path");
  9. const stat_mode_1 = require("stat-mode");
  10. const log_1 = require("./log");
  11. const promise_1 = require("./promise");
  12. const isCI = require("is-ci");
  13. exports.MAX_FILE_REQUESTS = 8;
  14. exports.CONCURRENCY = { concurrency: exports.MAX_FILE_REQUESTS };
  15. class CopyFileTransformer {
  16. constructor(afterCopyTransformer) {
  17. this.afterCopyTransformer = afterCopyTransformer;
  18. }
  19. }
  20. exports.CopyFileTransformer = CopyFileTransformer;
  21. function unlinkIfExists(file) {
  22. return (0, promises_1.unlink)(file).catch(() => {
  23. /* ignore */
  24. });
  25. }
  26. exports.unlinkIfExists = unlinkIfExists;
  27. async function statOrNull(file) {
  28. return (0, promise_1.orNullIfFileNotExist)((0, promises_1.stat)(file));
  29. }
  30. exports.statOrNull = statOrNull;
  31. async function exists(file) {
  32. try {
  33. await (0, promises_1.access)(file);
  34. return true;
  35. }
  36. catch (e) {
  37. return false;
  38. }
  39. }
  40. exports.exists = exists;
  41. /**
  42. * Returns list of file paths (system-dependent file separator)
  43. */
  44. async function walk(initialDirPath, filter, consumer) {
  45. let result = [];
  46. const queue = [initialDirPath];
  47. let addDirToResult = false;
  48. const isIncludeDir = consumer == null ? false : consumer.isIncludeDir === true;
  49. while (queue.length > 0) {
  50. const dirPath = queue.pop();
  51. if (isIncludeDir) {
  52. if (addDirToResult) {
  53. result.push(dirPath);
  54. }
  55. else {
  56. addDirToResult = true;
  57. }
  58. }
  59. const childNames = await (0, promise_1.orIfFileNotExist)((0, promises_1.readdir)(dirPath), []);
  60. childNames.sort();
  61. let nodeModuleContent = null;
  62. const dirs = [];
  63. // our handler is async, but we should add sorted files, so, we add file to result not in the mapper, but after map
  64. const sortedFilePaths = await bluebird_lst_1.default.map(childNames, name => {
  65. if (name === ".DS_Store" || name === ".gitkeep") {
  66. return null;
  67. }
  68. const filePath = dirPath + path.sep + name;
  69. return (0, promises_1.lstat)(filePath).then(stat => {
  70. if (filter != null && !filter(filePath, stat)) {
  71. return null;
  72. }
  73. const consumerResult = consumer == null ? null : consumer.consume(filePath, stat, dirPath, childNames);
  74. if (consumerResult === false) {
  75. return null;
  76. }
  77. else if (consumerResult == null || !("then" in consumerResult)) {
  78. if (stat.isDirectory()) {
  79. dirs.push(name);
  80. return null;
  81. }
  82. else {
  83. return filePath;
  84. }
  85. }
  86. else {
  87. return consumerResult.then((it) => {
  88. if (it != null && Array.isArray(it)) {
  89. nodeModuleContent = it;
  90. return null;
  91. }
  92. // asarUtil can return modified stat (symlink handling)
  93. if ((it != null && "isDirectory" in it ? it : stat).isDirectory()) {
  94. dirs.push(name);
  95. return null;
  96. }
  97. else {
  98. return filePath;
  99. }
  100. });
  101. }
  102. });
  103. }, exports.CONCURRENCY);
  104. for (const child of sortedFilePaths) {
  105. if (child != null) {
  106. result.push(child);
  107. }
  108. }
  109. dirs.sort();
  110. for (const child of dirs) {
  111. queue.push(dirPath + path.sep + child);
  112. }
  113. if (nodeModuleContent != null) {
  114. result = result.concat(nodeModuleContent);
  115. }
  116. }
  117. return result;
  118. }
  119. exports.walk = walk;
  120. const _isUseHardLink = process.platform !== "win32" && process.env.USE_HARD_LINKS !== "false" && (isCI || process.env.USE_HARD_LINKS === "true");
  121. function copyFile(src, dest, isEnsureDir = true) {
  122. return (isEnsureDir ? (0, promises_1.mkdir)(path.dirname(dest), { recursive: true }) : Promise.resolve()).then(() => copyOrLinkFile(src, dest, null, false));
  123. }
  124. exports.copyFile = copyFile;
  125. /**
  126. * Hard links is used if supported and allowed.
  127. * File permission is fixed — allow execute for all if owner can, allow read for all if owner can.
  128. *
  129. * ensureDir is not called, dest parent dir must exists
  130. */
  131. function copyOrLinkFile(src, dest, stats, isUseHardLink, exDevErrorHandler) {
  132. if (isUseHardLink === undefined) {
  133. isUseHardLink = _isUseHardLink;
  134. }
  135. if (stats != null) {
  136. const originalModeNumber = stats.mode;
  137. const mode = new stat_mode_1.Mode(stats);
  138. if (mode.owner.execute) {
  139. mode.group.execute = true;
  140. mode.others.execute = true;
  141. }
  142. mode.group.read = true;
  143. mode.others.read = true;
  144. mode.setuid = false;
  145. mode.setgid = false;
  146. if (originalModeNumber !== stats.mode) {
  147. if (log_1.log.isDebugEnabled) {
  148. const oldMode = new stat_mode_1.Mode({ mode: originalModeNumber });
  149. log_1.log.debug({ file: dest, oldMode, mode }, "permissions fixed from");
  150. }
  151. // https://helgeklein.com/blog/2009/05/hard-links-and-permissions-acls/
  152. // Permissions on all hard links to the same data on disk are always identical. The same applies to attributes.
  153. // That means if you change the permissions/owner/attributes on one hard link, you will immediately see the changes on all other hard links.
  154. if (isUseHardLink) {
  155. isUseHardLink = false;
  156. log_1.log.debug({ dest }, "copied, but not linked, because file permissions need to be fixed");
  157. }
  158. }
  159. }
  160. if (isUseHardLink) {
  161. return (0, promises_1.link)(src, dest).catch((e) => {
  162. if (e.code === "EXDEV") {
  163. const isLog = exDevErrorHandler == null ? true : exDevErrorHandler();
  164. if (isLog && log_1.log.isDebugEnabled) {
  165. log_1.log.debug({ error: e.message }, "cannot copy using hard link");
  166. }
  167. return doCopyFile(src, dest, stats);
  168. }
  169. else {
  170. throw e;
  171. }
  172. });
  173. }
  174. return doCopyFile(src, dest, stats);
  175. }
  176. exports.copyOrLinkFile = copyOrLinkFile;
  177. function doCopyFile(src, dest, stats) {
  178. const promise = (0, fs_extra_1.copyFile)(src, dest);
  179. if (stats == null) {
  180. return promise;
  181. }
  182. return promise.then(() => (0, promises_1.chmod)(dest, stats.mode));
  183. }
  184. class FileCopier {
  185. constructor(isUseHardLinkFunction, transformer) {
  186. this.isUseHardLinkFunction = isUseHardLinkFunction;
  187. this.transformer = transformer;
  188. if (isUseHardLinkFunction === exports.USE_HARD_LINKS) {
  189. this.isUseHardLink = true;
  190. }
  191. else {
  192. this.isUseHardLink = _isUseHardLink && isUseHardLinkFunction !== exports.DO_NOT_USE_HARD_LINKS;
  193. }
  194. }
  195. async copy(src, dest, stat) {
  196. let afterCopyTransformer = null;
  197. if (this.transformer != null && stat != null && stat.isFile()) {
  198. let data = this.transformer(src);
  199. if (data != null) {
  200. if (typeof data === "object" && "then" in data) {
  201. data = await data;
  202. }
  203. if (data != null) {
  204. if (data instanceof CopyFileTransformer) {
  205. afterCopyTransformer = data.afterCopyTransformer;
  206. }
  207. else {
  208. await (0, promises_1.writeFile)(dest, data);
  209. return;
  210. }
  211. }
  212. }
  213. }
  214. const isUseHardLink = afterCopyTransformer == null && (!this.isUseHardLink || this.isUseHardLinkFunction == null ? this.isUseHardLink : this.isUseHardLinkFunction(dest));
  215. await copyOrLinkFile(src, dest, stat, isUseHardLink, isUseHardLink
  216. ? () => {
  217. // files are copied concurrently, so, we must not check here currentIsUseHardLink — our code can be executed after that other handler will set currentIsUseHardLink to false
  218. if (this.isUseHardLink) {
  219. this.isUseHardLink = false;
  220. return true;
  221. }
  222. else {
  223. return false;
  224. }
  225. }
  226. : null);
  227. if (afterCopyTransformer != null) {
  228. await afterCopyTransformer(dest);
  229. }
  230. }
  231. }
  232. exports.FileCopier = FileCopier;
  233. /**
  234. * Empty directories is never created.
  235. * Hard links is used if supported and allowed.
  236. */
  237. function copyDir(src, destination, options = {}) {
  238. const fileCopier = new FileCopier(options.isUseHardLink, options.transformer);
  239. if (log_1.log.isDebugEnabled) {
  240. log_1.log.debug({ src, destination }, `copying${fileCopier.isUseHardLink ? " using hard links" : ""}`);
  241. }
  242. const createdSourceDirs = new Set();
  243. const links = [];
  244. const symlinkType = (0, os_1.platform)() === "win32" ? "junction" : "file";
  245. return walk(src, options.filter, {
  246. consume: async (file, stat, parent) => {
  247. if (!stat.isFile() && !stat.isSymbolicLink()) {
  248. return;
  249. }
  250. if (!createdSourceDirs.has(parent)) {
  251. await (0, promises_1.mkdir)(parent.replace(src, destination), { recursive: true });
  252. createdSourceDirs.add(parent);
  253. }
  254. const destFile = file.replace(src, destination);
  255. if (stat.isFile()) {
  256. await fileCopier.copy(file, destFile, stat);
  257. }
  258. else {
  259. links.push({ file: destFile, link: await (0, promises_1.readlink)(file) });
  260. }
  261. },
  262. }).then(() => bluebird_lst_1.default.map(links, it => (0, promises_1.symlink)(it.link, it.file, symlinkType), exports.CONCURRENCY));
  263. }
  264. exports.copyDir = copyDir;
  265. async function dirSize(dirPath) {
  266. const entries = await (0, promises_1.readdir)(dirPath, { withFileTypes: true });
  267. const entrySizes = entries.map(async (entry) => {
  268. const entryPath = path.join(dirPath, entry.name);
  269. if (entry.isDirectory()) {
  270. return await dirSize(entryPath);
  271. }
  272. if (entry.isFile()) {
  273. const { size } = await (0, promises_1.stat)(entryPath);
  274. return size;
  275. }
  276. return 0;
  277. });
  278. return (await Promise.all(entrySizes)).reduce((entrySize, totalSize) => entrySize + totalSize, 0);
  279. }
  280. exports.dirSize = dirSize;
  281. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  282. const DO_NOT_USE_HARD_LINKS = (file) => false;
  283. exports.DO_NOT_USE_HARD_LINKS = DO_NOT_USE_HARD_LINKS;
  284. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  285. const USE_HARD_LINKS = (file) => true;
  286. exports.USE_HARD_LINKS = USE_HARD_LINKS;
  287. //# sourceMappingURL=fs.js.map