1 | 'use strict'
|
---|
2 |
|
---|
3 | const fs = require('graceful-fs')
|
---|
4 | const path = require('path')
|
---|
5 | const mkdirpSync = require('../mkdirs').mkdirsSync
|
---|
6 | const utimesSync = require('../util/utimes.js').utimesMillisSync
|
---|
7 | const stat = require('../util/stat')
|
---|
8 |
|
---|
9 | function copySync (src, dest, opts) {
|
---|
10 | if (typeof opts === 'function') {
|
---|
11 | opts = { filter: opts }
|
---|
12 | }
|
---|
13 |
|
---|
14 | opts = opts || {}
|
---|
15 | opts.clobber = 'clobber' in opts ? !!opts.clobber : true // default to true for now
|
---|
16 | opts.overwrite = 'overwrite' in opts ? !!opts.overwrite : opts.clobber // overwrite falls back to clobber
|
---|
17 |
|
---|
18 | // Warn about using preserveTimestamps on 32-bit node
|
---|
19 | if (opts.preserveTimestamps && process.arch === 'ia32') {
|
---|
20 | console.warn(`fs-extra: Using the preserveTimestamps option in 32-bit node is not recommended;\n
|
---|
21 | see https://github.com/jprichardson/node-fs-extra/issues/269`)
|
---|
22 | }
|
---|
23 |
|
---|
24 | const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy')
|
---|
25 | stat.checkParentPathsSync(src, srcStat, dest, 'copy')
|
---|
26 | return handleFilterAndCopy(destStat, src, dest, opts)
|
---|
27 | }
|
---|
28 |
|
---|
29 | function handleFilterAndCopy (destStat, src, dest, opts) {
|
---|
30 | if (opts.filter && !opts.filter(src, dest)) return
|
---|
31 | const destParent = path.dirname(dest)
|
---|
32 | if (!fs.existsSync(destParent)) mkdirpSync(destParent)
|
---|
33 | return startCopy(destStat, src, dest, opts)
|
---|
34 | }
|
---|
35 |
|
---|
36 | function startCopy (destStat, src, dest, opts) {
|
---|
37 | if (opts.filter && !opts.filter(src, dest)) return
|
---|
38 | return getStats(destStat, src, dest, opts)
|
---|
39 | }
|
---|
40 |
|
---|
41 | function getStats (destStat, src, dest, opts) {
|
---|
42 | const statSync = opts.dereference ? fs.statSync : fs.lstatSync
|
---|
43 | const srcStat = statSync(src)
|
---|
44 |
|
---|
45 | if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
|
---|
46 | else if (srcStat.isFile() ||
|
---|
47 | srcStat.isCharacterDevice() ||
|
---|
48 | srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
|
---|
49 | else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
|
---|
50 | }
|
---|
51 |
|
---|
52 | function onFile (srcStat, destStat, src, dest, opts) {
|
---|
53 | if (!destStat) return copyFile(srcStat, src, dest, opts)
|
---|
54 | return mayCopyFile(srcStat, src, dest, opts)
|
---|
55 | }
|
---|
56 |
|
---|
57 | function mayCopyFile (srcStat, src, dest, opts) {
|
---|
58 | if (opts.overwrite) {
|
---|
59 | fs.unlinkSync(dest)
|
---|
60 | return copyFile(srcStat, src, dest, opts)
|
---|
61 | } else if (opts.errorOnExist) {
|
---|
62 | throw new Error(`'${dest}' already exists`)
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | function copyFile (srcStat, src, dest, opts) {
|
---|
67 | if (typeof fs.copyFileSync === 'function') {
|
---|
68 | fs.copyFileSync(src, dest)
|
---|
69 | fs.chmodSync(dest, srcStat.mode)
|
---|
70 | if (opts.preserveTimestamps) {
|
---|
71 | return utimesSync(dest, srcStat.atime, srcStat.mtime)
|
---|
72 | }
|
---|
73 | return
|
---|
74 | }
|
---|
75 | return copyFileFallback(srcStat, src, dest, opts)
|
---|
76 | }
|
---|
77 |
|
---|
78 | function copyFileFallback (srcStat, src, dest, opts) {
|
---|
79 | const BUF_LENGTH = 64 * 1024
|
---|
80 | const _buff = require('../util/buffer')(BUF_LENGTH)
|
---|
81 |
|
---|
82 | const fdr = fs.openSync(src, 'r')
|
---|
83 | const fdw = fs.openSync(dest, 'w', srcStat.mode)
|
---|
84 | let pos = 0
|
---|
85 |
|
---|
86 | while (pos < srcStat.size) {
|
---|
87 | const bytesRead = fs.readSync(fdr, _buff, 0, BUF_LENGTH, pos)
|
---|
88 | fs.writeSync(fdw, _buff, 0, bytesRead)
|
---|
89 | pos += bytesRead
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (opts.preserveTimestamps) fs.futimesSync(fdw, srcStat.atime, srcStat.mtime)
|
---|
93 |
|
---|
94 | fs.closeSync(fdr)
|
---|
95 | fs.closeSync(fdw)
|
---|
96 | }
|
---|
97 |
|
---|
98 | function onDir (srcStat, destStat, src, dest, opts) {
|
---|
99 | if (!destStat) return mkDirAndCopy(srcStat, src, dest, opts)
|
---|
100 | if (destStat && !destStat.isDirectory()) {
|
---|
101 | throw new Error(`Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
|
---|
102 | }
|
---|
103 | return copyDir(src, dest, opts)
|
---|
104 | }
|
---|
105 |
|
---|
106 | function mkDirAndCopy (srcStat, src, dest, opts) {
|
---|
107 | fs.mkdirSync(dest)
|
---|
108 | copyDir(src, dest, opts)
|
---|
109 | return fs.chmodSync(dest, srcStat.mode)
|
---|
110 | }
|
---|
111 |
|
---|
112 | function copyDir (src, dest, opts) {
|
---|
113 | fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
|
---|
114 | }
|
---|
115 |
|
---|
116 | function copyDirItem (item, src, dest, opts) {
|
---|
117 | const srcItem = path.join(src, item)
|
---|
118 | const destItem = path.join(dest, item)
|
---|
119 | const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy')
|
---|
120 | return startCopy(destStat, srcItem, destItem, opts)
|
---|
121 | }
|
---|
122 |
|
---|
123 | function onLink (destStat, src, dest, opts) {
|
---|
124 | let resolvedSrc = fs.readlinkSync(src)
|
---|
125 | if (opts.dereference) {
|
---|
126 | resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (!destStat) {
|
---|
130 | return fs.symlinkSync(resolvedSrc, dest)
|
---|
131 | } else {
|
---|
132 | let resolvedDest
|
---|
133 | try {
|
---|
134 | resolvedDest = fs.readlinkSync(dest)
|
---|
135 | } catch (err) {
|
---|
136 | // dest exists and is a regular file or directory,
|
---|
137 | // Windows may throw UNKNOWN error. If dest already exists,
|
---|
138 | // fs throws error anyway, so no need to guard against it here.
|
---|
139 | if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
|
---|
140 | throw err
|
---|
141 | }
|
---|
142 | if (opts.dereference) {
|
---|
143 | resolvedDest = path.resolve(process.cwd(), resolvedDest)
|
---|
144 | }
|
---|
145 | if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
|
---|
146 | throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
|
---|
147 | }
|
---|
148 |
|
---|
149 | // prevent copy if src is a subdir of dest since unlinking
|
---|
150 | // dest in this case would result in removing src contents
|
---|
151 | // and therefore a broken symlink would be created.
|
---|
152 | if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
|
---|
153 | throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
|
---|
154 | }
|
---|
155 | return copyLink(resolvedSrc, dest)
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | function copyLink (resolvedSrc, dest) {
|
---|
160 | fs.unlinkSync(dest)
|
---|
161 | return fs.symlinkSync(resolvedSrc, dest)
|
---|
162 | }
|
---|
163 |
|
---|
164 | module.exports = copySync
|
---|