source: frontend/node_modules/fs-extra/lib/copy/copy-sync.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.6 KB
Line 
1'use strict'
2
3const fs = require('graceful-fs')
4const path = require('path')
5const mkdirsSync = require('../mkdirs').mkdirsSync
6const utimesMillisSync = require('../util/utimes').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 process.emitWarning(
21 'Using the preserveTimestamps option in 32-bit node is not recommended;\n\n' +
22 '\tsee https://github.com/jprichardson/node-fs-extra/issues/269',
23 'Warning', 'fs-extra-WARN0002'
24 )
25 }
26
27 const { srcStat, destStat } = stat.checkPathsSync(src, dest, 'copy', opts)
28 stat.checkParentPathsSync(src, srcStat, dest, 'copy')
29 return handleFilterAndCopy(destStat, src, dest, opts)
30}
31
32function handleFilterAndCopy (destStat, src, dest, opts) {
33 if (opts.filter && !opts.filter(src, dest)) return
34 const destParent = path.dirname(dest)
35 if (!fs.existsSync(destParent)) mkdirsSync(destParent)
36 return getStats(destStat, src, dest, opts)
37}
38
39function startCopy (destStat, src, dest, opts) {
40 if (opts.filter && !opts.filter(src, dest)) return
41 return getStats(destStat, src, dest, opts)
42}
43
44function getStats (destStat, src, dest, opts) {
45 const statSync = opts.dereference ? fs.statSync : fs.lstatSync
46 const srcStat = statSync(src)
47
48 if (srcStat.isDirectory()) return onDir(srcStat, destStat, src, dest, opts)
49 else if (srcStat.isFile() ||
50 srcStat.isCharacterDevice() ||
51 srcStat.isBlockDevice()) return onFile(srcStat, destStat, src, dest, opts)
52 else if (srcStat.isSymbolicLink()) return onLink(destStat, src, dest, opts)
53 else if (srcStat.isSocket()) throw new Error(`Cannot copy a socket file: ${src}`)
54 else if (srcStat.isFIFO()) throw new Error(`Cannot copy a FIFO pipe: ${src}`)
55 throw new Error(`Unknown file: ${src}`)
56}
57
58function onFile (srcStat, destStat, src, dest, opts) {
59 if (!destStat) return copyFile(srcStat, src, dest, opts)
60 return mayCopyFile(srcStat, src, dest, opts)
61}
62
63function mayCopyFile (srcStat, src, dest, opts) {
64 if (opts.overwrite) {
65 fs.unlinkSync(dest)
66 return copyFile(srcStat, src, dest, opts)
67 } else if (opts.errorOnExist) {
68 throw new Error(`'${dest}' already exists`)
69 }
70}
71
72function copyFile (srcStat, src, dest, opts) {
73 fs.copyFileSync(src, dest)
74 if (opts.preserveTimestamps) handleTimestamps(srcStat.mode, src, dest)
75 return setDestMode(dest, srcStat.mode)
76}
77
78function handleTimestamps (srcMode, src, dest) {
79 // Make sure the file is writable before setting the timestamp
80 // otherwise open fails with EPERM when invoked with 'r+'
81 // (through utimes call)
82 if (fileIsNotWritable(srcMode)) makeFileWritable(dest, srcMode)
83 return setDestTimestamps(src, dest)
84}
85
86function fileIsNotWritable (srcMode) {
87 return (srcMode & 0o200) === 0
88}
89
90function makeFileWritable (dest, srcMode) {
91 return setDestMode(dest, srcMode | 0o200)
92}
93
94function setDestMode (dest, srcMode) {
95 return fs.chmodSync(dest, srcMode)
96}
97
98function setDestTimestamps (src, dest) {
99 // The initial srcStat.atime cannot be trusted
100 // because it is modified by the read(2) system call
101 // (See https://nodejs.org/api/fs.html#fs_stat_time_values)
102 const updatedSrcStat = fs.statSync(src)
103 return utimesMillisSync(dest, updatedSrcStat.atime, updatedSrcStat.mtime)
104}
105
106function onDir (srcStat, destStat, src, dest, opts) {
107 if (!destStat) return mkDirAndCopy(srcStat.mode, src, dest, opts)
108 return copyDir(src, dest, opts)
109}
110
111function mkDirAndCopy (srcMode, src, dest, opts) {
112 fs.mkdirSync(dest)
113 copyDir(src, dest, opts)
114 return setDestMode(dest, srcMode)
115}
116
117function copyDir (src, dest, opts) {
118 fs.readdirSync(src).forEach(item => copyDirItem(item, src, dest, opts))
119}
120
121function copyDirItem (item, src, dest, opts) {
122 const srcItem = path.join(src, item)
123 const destItem = path.join(dest, item)
124 const { destStat } = stat.checkPathsSync(srcItem, destItem, 'copy', opts)
125 return startCopy(destStat, srcItem, destItem, opts)
126}
127
128function onLink (destStat, src, dest, opts) {
129 let resolvedSrc = fs.readlinkSync(src)
130 if (opts.dereference) {
131 resolvedSrc = path.resolve(process.cwd(), resolvedSrc)
132 }
133
134 if (!destStat) {
135 return fs.symlinkSync(resolvedSrc, dest)
136 } else {
137 let resolvedDest
138 try {
139 resolvedDest = fs.readlinkSync(dest)
140 } catch (err) {
141 // dest exists and is a regular file or directory,
142 // Windows may throw UNKNOWN error. If dest already exists,
143 // fs throws error anyway, so no need to guard against it here.
144 if (err.code === 'EINVAL' || err.code === 'UNKNOWN') return fs.symlinkSync(resolvedSrc, dest)
145 throw err
146 }
147 if (opts.dereference) {
148 resolvedDest = path.resolve(process.cwd(), resolvedDest)
149 }
150 if (stat.isSrcSubdir(resolvedSrc, resolvedDest)) {
151 throw new Error(`Cannot copy '${resolvedSrc}' to a subdirectory of itself, '${resolvedDest}'.`)
152 }
153
154 // prevent copy if src is a subdir of dest since unlinking
155 // dest in this case would result in removing src contents
156 // and therefore a broken symlink would be created.
157 if (fs.statSync(dest).isDirectory() && stat.isSrcSubdir(resolvedDest, resolvedSrc)) {
158 throw new Error(`Cannot overwrite '${resolvedDest}' with '${resolvedSrc}'.`)
159 }
160 return copyLink(resolvedSrc, dest)
161 }
162}
163
164function copyLink (resolvedSrc, dest) {
165 fs.unlinkSync(dest)
166 return fs.symlinkSync(resolvedSrc, dest)
167}
168
169module.exports = copySync
Note: See TracBrowser for help on using the repository browser.