source: trip-planner-front/node_modules/fs-extra/lib/copy-sync/copy-sync.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[6a3a178]1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5const mkdirpSync = require('../mkdirs').mkdirsSync
6const utimesSync = require('../util/utimes.js').utimesMillisSync
7const stat = require('../util/stat')
8
9function 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
29function 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
36function startCopy (destStat, src, dest, opts) {
37 if (opts.filter && !opts.filter(src, dest)) return
38 return getStats(destStat, src, dest, opts)
39}
40
41function 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
52function onFile (srcStat, destStat, src, dest, opts) {
53 if (!destStat) return copyFile(srcStat, src, dest, opts)
54 return mayCopyFile(srcStat, src, dest, opts)
55}
56
57function 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
66function 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
78function 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
98function 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
106function mkDirAndCopy (srcStat, src, dest, opts) {
107 fs.mkdirSync(dest)
108 copyDir(src, dest, opts)
109 return fs.chmodSync(dest, srcStat.mode)
110}
111
112function copyDir (src, dest, opts) {
113 fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
114}
115
116function 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
123function 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
159function copyLink (resolvedSrc, dest) {
160 fs.unlinkSync(dest)
161 return fs.symlinkSync(resolvedSrc, dest)
162}
163
164module.exports = copySync
Note: See TracBrowser for help on using the repository browser.