polyfill.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // this file is a modified version of the code in node 17.2.0
  2. // which is, in turn, a modified version of the fs-extra module on npm
  3. // node core changes:
  4. // - Use of the assert module has been replaced with core's error system.
  5. // - All code related to the glob dependency has been removed.
  6. // - Bring your own custom fs module is not currently supported.
  7. // - Some basic code cleanup.
  8. // changes here:
  9. // - remove all callback related code
  10. // - drop sync support
  11. // - change assertions back to non-internal methods (see options.js)
  12. // - throws ENOTDIR when rmdir gets an ENOENT for a path that exists in Windows
  13. 'use strict'
  14. const {
  15. ERR_FS_CP_DIR_TO_NON_DIR,
  16. ERR_FS_CP_EEXIST,
  17. ERR_FS_CP_EINVAL,
  18. ERR_FS_CP_FIFO_PIPE,
  19. ERR_FS_CP_NON_DIR_TO_DIR,
  20. ERR_FS_CP_SOCKET,
  21. ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY,
  22. ERR_FS_CP_UNKNOWN,
  23. ERR_FS_EISDIR,
  24. ERR_INVALID_ARG_TYPE,
  25. } = require('../errors.js')
  26. const {
  27. constants: {
  28. errno: {
  29. EEXIST,
  30. EISDIR,
  31. EINVAL,
  32. ENOTDIR,
  33. },
  34. },
  35. } = require('os')
  36. const {
  37. chmod,
  38. copyFile,
  39. lstat,
  40. mkdir,
  41. readdir,
  42. readlink,
  43. stat,
  44. symlink,
  45. unlink,
  46. utimes,
  47. } = require('../fs.js')
  48. const {
  49. dirname,
  50. isAbsolute,
  51. join,
  52. parse,
  53. resolve,
  54. sep,
  55. toNamespacedPath,
  56. } = require('path')
  57. const { fileURLToPath } = require('url')
  58. const defaultOptions = {
  59. dereference: false,
  60. errorOnExist: false,
  61. filter: undefined,
  62. force: true,
  63. preserveTimestamps: false,
  64. recursive: false,
  65. }
  66. async function cp (src, dest, opts) {
  67. if (opts != null && typeof opts !== 'object') {
  68. throw new ERR_INVALID_ARG_TYPE('options', ['Object'], opts)
  69. }
  70. return cpFn(
  71. toNamespacedPath(getValidatedPath(src)),
  72. toNamespacedPath(getValidatedPath(dest)),
  73. { ...defaultOptions, ...opts })
  74. }
  75. function getValidatedPath (fileURLOrPath) {
  76. const path = fileURLOrPath != null && fileURLOrPath.href
  77. && fileURLOrPath.origin
  78. ? fileURLToPath(fileURLOrPath)
  79. : fileURLOrPath
  80. return path
  81. }
  82. async function cpFn (src, dest, opts) {
  83. // Warn about using preserveTimestamps on 32-bit node
  84. // istanbul ignore next
  85. if (opts.preserveTimestamps && process.arch === 'ia32') {
  86. const warning = 'Using the preserveTimestamps option in 32-bit ' +
  87. 'node is not recommended'
  88. process.emitWarning(warning, 'TimestampPrecisionWarning')
  89. }
  90. const stats = await checkPaths(src, dest, opts)
  91. const { srcStat, destStat } = stats
  92. await checkParentPaths(src, srcStat, dest)
  93. if (opts.filter) {
  94. return handleFilter(checkParentDir, destStat, src, dest, opts)
  95. }
  96. return checkParentDir(destStat, src, dest, opts)
  97. }
  98. async function checkPaths (src, dest, opts) {
  99. const { 0: srcStat, 1: destStat } = await getStats(src, dest, opts)
  100. if (destStat) {
  101. if (areIdentical(srcStat, destStat)) {
  102. throw new ERR_FS_CP_EINVAL({
  103. message: 'src and dest cannot be the same',
  104. path: dest,
  105. syscall: 'cp',
  106. errno: EINVAL,
  107. })
  108. }
  109. if (srcStat.isDirectory() && !destStat.isDirectory()) {
  110. throw new ERR_FS_CP_DIR_TO_NON_DIR({
  111. message: `cannot overwrite directory ${src} ` +
  112. `with non-directory ${dest}`,
  113. path: dest,
  114. syscall: 'cp',
  115. errno: EISDIR,
  116. })
  117. }
  118. if (!srcStat.isDirectory() && destStat.isDirectory()) {
  119. throw new ERR_FS_CP_NON_DIR_TO_DIR({
  120. message: `cannot overwrite non-directory ${src} ` +
  121. `with directory ${dest}`,
  122. path: dest,
  123. syscall: 'cp',
  124. errno: ENOTDIR,
  125. })
  126. }
  127. }
  128. if (srcStat.isDirectory() && isSrcSubdir(src, dest)) {
  129. throw new ERR_FS_CP_EINVAL({
  130. message: `cannot copy ${src} to a subdirectory of self ${dest}`,
  131. path: dest,
  132. syscall: 'cp',
  133. errno: EINVAL,
  134. })
  135. }
  136. return { srcStat, destStat }
  137. }
  138. function areIdentical (srcStat, destStat) {
  139. return destStat.ino && destStat.dev && destStat.ino === srcStat.ino &&
  140. destStat.dev === srcStat.dev
  141. }
  142. function getStats (src, dest, opts) {
  143. const statFunc = opts.dereference ?
  144. (file) => stat(file, { bigint: true }) :
  145. (file) => lstat(file, { bigint: true })
  146. return Promise.all([
  147. statFunc(src),
  148. statFunc(dest).catch((err) => {
  149. // istanbul ignore next: unsure how to cover.
  150. if (err.code === 'ENOENT') {
  151. return null
  152. }
  153. // istanbul ignore next: unsure how to cover.
  154. throw err
  155. }),
  156. ])
  157. }
  158. async function checkParentDir (destStat, src, dest, opts) {
  159. const destParent = dirname(dest)
  160. const dirExists = await pathExists(destParent)
  161. if (dirExists) {
  162. return getStatsForCopy(destStat, src, dest, opts)
  163. }
  164. await mkdir(destParent, { recursive: true })
  165. return getStatsForCopy(destStat, src, dest, opts)
  166. }
  167. function pathExists (dest) {
  168. return stat(dest).then(
  169. () => true,
  170. // istanbul ignore next: not sure when this would occur
  171. (err) => (err.code === 'ENOENT' ? false : Promise.reject(err)))
  172. }
  173. // Recursively check if dest parent is a subdirectory of src.
  174. // It works for all file types including symlinks since it
  175. // checks the src and dest inodes. It starts from the deepest
  176. // parent and stops once it reaches the src parent or the root path.
  177. async function checkParentPaths (src, srcStat, dest) {
  178. const srcParent = resolve(dirname(src))
  179. const destParent = resolve(dirname(dest))
  180. if (destParent === srcParent || destParent === parse(destParent).root) {
  181. return
  182. }
  183. let destStat
  184. try {
  185. destStat = await stat(destParent, { bigint: true })
  186. } catch (err) {
  187. // istanbul ignore else: not sure when this would occur
  188. if (err.code === 'ENOENT') {
  189. return
  190. }
  191. // istanbul ignore next: not sure when this would occur
  192. throw err
  193. }
  194. if (areIdentical(srcStat, destStat)) {
  195. throw new ERR_FS_CP_EINVAL({
  196. message: `cannot copy ${src} to a subdirectory of self ${dest}`,
  197. path: dest,
  198. syscall: 'cp',
  199. errno: EINVAL,
  200. })
  201. }
  202. return checkParentPaths(src, srcStat, destParent)
  203. }
  204. const normalizePathToArray = (path) =>
  205. resolve(path).split(sep).filter(Boolean)
  206. // Return true if dest is a subdir of src, otherwise false.
  207. // It only checks the path strings.
  208. function isSrcSubdir (src, dest) {
  209. const srcArr = normalizePathToArray(src)
  210. const destArr = normalizePathToArray(dest)
  211. return srcArr.every((cur, i) => destArr[i] === cur)
  212. }
  213. async function handleFilter (onInclude, destStat, src, dest, opts, cb) {
  214. const include = await opts.filter(src, dest)
  215. if (include) {
  216. return onInclude(destStat, src, dest, opts, cb)
  217. }
  218. }
  219. function startCopy (destStat, src, dest, opts) {
  220. if (opts.filter) {
  221. return handleFilter(getStatsForCopy, destStat, src, dest, opts)
  222. }
  223. return getStatsForCopy(destStat, src, dest, opts)
  224. }
  225. async function getStatsForCopy (destStat, src, dest, opts) {
  226. const statFn = opts.dereference ? stat : lstat
  227. const srcStat = await statFn(src)
  228. // istanbul ignore else: can't portably test FIFO
  229. if (srcStat.isDirectory() && opts.recursive) {
  230. return onDir(srcStat, destStat, src, dest, opts)
  231. } else if (srcStat.isDirectory()) {
  232. throw new ERR_FS_EISDIR({
  233. message: `${src} is a directory (not copied)`,
  234. path: src,
  235. syscall: 'cp',
  236. errno: EINVAL,
  237. })
  238. } else if (srcStat.isFile() ||
  239. srcStat.isCharacterDevice() ||
  240. srcStat.isBlockDevice()) {
  241. return onFile(srcStat, destStat, src, dest, opts)
  242. } else if (srcStat.isSymbolicLink()) {
  243. return onLink(destStat, src, dest)
  244. } else if (srcStat.isSocket()) {
  245. throw new ERR_FS_CP_SOCKET({
  246. message: `cannot copy a socket file: ${dest}`,
  247. path: dest,
  248. syscall: 'cp',
  249. errno: EINVAL,
  250. })
  251. } else if (srcStat.isFIFO()) {
  252. throw new ERR_FS_CP_FIFO_PIPE({
  253. message: `cannot copy a FIFO pipe: ${dest}`,
  254. path: dest,
  255. syscall: 'cp',
  256. errno: EINVAL,
  257. })
  258. }
  259. // istanbul ignore next: should be unreachable
  260. throw new ERR_FS_CP_UNKNOWN({
  261. message: `cannot copy an unknown file type: ${dest}`,
  262. path: dest,
  263. syscall: 'cp',
  264. errno: EINVAL,
  265. })
  266. }
  267. function onFile (srcStat, destStat, src, dest, opts) {
  268. if (!destStat) {
  269. return _copyFile(srcStat, src, dest, opts)
  270. }
  271. return mayCopyFile(srcStat, src, dest, opts)
  272. }
  273. async function mayCopyFile (srcStat, src, dest, opts) {
  274. if (opts.force) {
  275. await unlink(dest)
  276. return _copyFile(srcStat, src, dest, opts)
  277. } else if (opts.errorOnExist) {
  278. throw new ERR_FS_CP_EEXIST({
  279. message: `${dest} already exists`,
  280. path: dest,
  281. syscall: 'cp',
  282. errno: EEXIST,
  283. })
  284. }
  285. }
  286. async function _copyFile (srcStat, src, dest, opts) {
  287. await copyFile(src, dest)
  288. if (opts.preserveTimestamps) {
  289. return handleTimestampsAndMode(srcStat.mode, src, dest)
  290. }
  291. return setDestMode(dest, srcStat.mode)
  292. }
  293. async function handleTimestampsAndMode (srcMode, src, dest) {
  294. // Make sure the file is writable before setting the timestamp
  295. // otherwise open fails with EPERM when invoked with 'r+'
  296. // (through utimes call)
  297. if (fileIsNotWritable(srcMode)) {
  298. await makeFileWritable(dest, srcMode)
  299. return setDestTimestampsAndMode(srcMode, src, dest)
  300. }
  301. return setDestTimestampsAndMode(srcMode, src, dest)
  302. }
  303. function fileIsNotWritable (srcMode) {
  304. return (srcMode & 0o200) === 0
  305. }
  306. function makeFileWritable (dest, srcMode) {
  307. return setDestMode(dest, srcMode | 0o200)
  308. }
  309. async function setDestTimestampsAndMode (srcMode, src, dest) {
  310. await setDestTimestamps(src, dest)
  311. return setDestMode(dest, srcMode)
  312. }
  313. function setDestMode (dest, srcMode) {
  314. return chmod(dest, srcMode)
  315. }
  316. async function setDestTimestamps (src, dest) {
  317. // The initial srcStat.atime cannot be trusted
  318. // because it is modified by the read(2) system call
  319. // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
  320. const updatedSrcStat = await stat(src)
  321. return utimes(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
  322. }
  323. function onDir (srcStat, destStat, src, dest, opts) {
  324. if (!destStat) {
  325. return mkDirAndCopy(srcStat.mode, src, dest, opts)
  326. }
  327. return copyDir(src, dest, opts)
  328. }
  329. async function mkDirAndCopy (srcMode, src, dest, opts) {
  330. await mkdir(dest)
  331. await copyDir(src, dest, opts)
  332. return setDestMode(dest, srcMode)
  333. }
  334. async function copyDir (src, dest, opts) {
  335. const dir = await readdir(src)
  336. for (let i = 0; i < dir.length; i++) {
  337. const item = dir[i]
  338. const srcItem = join(src, item)
  339. const destItem = join(dest, item)
  340. const { destStat } = await checkPaths(srcItem, destItem, opts)
  341. await startCopy(destStat, srcItem, destItem, opts)
  342. }
  343. }
  344. async function onLink (destStat, src, dest) {
  345. let resolvedSrc = await readlink(src)
  346. if (!isAbsolute(resolvedSrc)) {
  347. resolvedSrc = resolve(dirname(src), resolvedSrc)
  348. }
  349. if (!destStat) {
  350. return symlink(resolvedSrc, dest)
  351. }
  352. let resolvedDest
  353. try {
  354. resolvedDest = await readlink(dest)
  355. } catch (err) {
  356. // Dest exists and is a regular file or directory,
  357. // Windows may throw UNKNOWN error. If dest already exists,
  358. // fs throws error anyway, so no need to guard against it here.
  359. // istanbul ignore next: can only test on windows
  360. if (err.code === 'EINVAL' || err.code === 'UNKNOWN') {
  361. return symlink(resolvedSrc, dest)
  362. }
  363. // istanbul ignore next: should not be possible
  364. throw err
  365. }
  366. if (!isAbsolute(resolvedDest)) {
  367. resolvedDest = resolve(dirname(dest), resolvedDest)
  368. }
  369. if (isSrcSubdir(resolvedSrc, resolvedDest)) {
  370. throw new ERR_FS_CP_EINVAL({
  371. message: `cannot copy ${resolvedSrc} to a subdirectory of self ` +
  372. `${resolvedDest}`,
  373. path: dest,
  374. syscall: 'cp',
  375. errno: EINVAL,
  376. })
  377. }
  378. // Do not copy if src is a subdir of dest since unlinking
  379. // dest in this case would result in removing src contents
  380. // and therefore a broken symlink would be created.
  381. const srcStat = await stat(src)
  382. if (srcStat.isDirectory() && isSrcSubdir(resolvedDest, resolvedSrc)) {
  383. throw new ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY({
  384. message: `cannot overwrite ${resolvedDest} with ${resolvedSrc}`,
  385. path: dest,
  386. syscall: 'cp',
  387. errno: EINVAL,
  388. })
  389. }
  390. return copyLink(resolvedSrc, dest)
  391. }
  392. async function copyLink (resolvedSrc, dest) {
  393. await unlink(dest)
  394. return symlink(resolvedSrc, dest)
  395. }
  396. module.exports = cp