1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var common = require('./common');
|
4 |
|
5 | common.register('cp', _cp, {
|
6 | cmdOptions: {
|
7 | 'f': '!no_force',
|
8 | 'n': 'no_force',
|
9 | 'u': 'update',
|
10 | 'R': 'recursive',
|
11 | 'r': 'recursive',
|
12 | 'L': 'followsymlink',
|
13 | 'P': 'noFollowsymlink',
|
14 | },
|
15 | wrapOutput: false,
|
16 | });
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function copyFileSync(srcFile, destFile, options) {
|
22 | if (!fs.existsSync(srcFile)) {
|
23 | common.error('copyFileSync: no such file or directory: ' + srcFile);
|
24 | }
|
25 |
|
26 | var isWindows = process.platform === 'win32';
|
27 |
|
28 |
|
29 | try {
|
30 | if (options.update && common.statFollowLinks(srcFile).mtime < fs.statSync(destFile).mtime) {
|
31 | return;
|
32 | }
|
33 | } catch (e) {
|
34 |
|
35 | }
|
36 |
|
37 | if (common.statNoFollowLinks(srcFile).isSymbolicLink() && !options.followsymlink) {
|
38 | try {
|
39 | common.statNoFollowLinks(destFile);
|
40 | common.unlinkSync(destFile);
|
41 | } catch (e) {
|
42 |
|
43 | }
|
44 |
|
45 | var symlinkFull = fs.readlinkSync(srcFile);
|
46 | fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null);
|
47 | } else {
|
48 | var buf = common.buffer();
|
49 | var bufLength = buf.length;
|
50 | var bytesRead = bufLength;
|
51 | var pos = 0;
|
52 | var fdr = null;
|
53 | var fdw = null;
|
54 |
|
55 | try {
|
56 | fdr = fs.openSync(srcFile, 'r');
|
57 | } catch (e) {
|
58 |
|
59 | common.error('copyFileSync: could not read src file (' + srcFile + ')');
|
60 | }
|
61 |
|
62 | try {
|
63 | fdw = fs.openSync(destFile, 'w');
|
64 | } catch (e) {
|
65 |
|
66 | common.error('copyFileSync: could not write to dest file (code=' + e.code + '):' + destFile);
|
67 | }
|
68 |
|
69 | while (bytesRead === bufLength) {
|
70 | bytesRead = fs.readSync(fdr, buf, 0, bufLength, pos);
|
71 | fs.writeSync(fdw, buf, 0, bytesRead);
|
72 | pos += bytesRead;
|
73 | }
|
74 |
|
75 | fs.closeSync(fdr);
|
76 | fs.closeSync(fdw);
|
77 |
|
78 | fs.chmodSync(destFile, common.statFollowLinks(srcFile).mode);
|
79 | }
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) {
|
91 | if (!opts) opts = {};
|
92 |
|
93 |
|
94 | if (currentDepth >= common.config.maxdepth) return;
|
95 | currentDepth++;
|
96 |
|
97 | var isWindows = process.platform === 'win32';
|
98 |
|
99 |
|
100 |
|
101 | try {
|
102 | var checkDir = common.statFollowLinks(sourceDir);
|
103 | fs.mkdirSync(destDir, checkDir.mode);
|
104 | } catch (e) {
|
105 |
|
106 | if (e.code !== 'EEXIST') throw e;
|
107 | }
|
108 |
|
109 | var files = fs.readdirSync(sourceDir);
|
110 |
|
111 | for (var i = 0; i < files.length; i++) {
|
112 | var srcFile = sourceDir + '/' + files[i];
|
113 | var destFile = destDir + '/' + files[i];
|
114 | var srcFileStat = common.statNoFollowLinks(srcFile);
|
115 |
|
116 | var symlinkFull;
|
117 | if (opts.followsymlink) {
|
118 | if (cpcheckcycle(sourceDir, srcFile)) {
|
119 |
|
120 | console.error('Cycle link found.');
|
121 | symlinkFull = fs.readlinkSync(srcFile);
|
122 | fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null);
|
123 | continue;
|
124 | }
|
125 | }
|
126 | if (srcFileStat.isDirectory()) {
|
127 |
|
128 | cpdirSyncRecursive(srcFile, destFile, currentDepth, opts);
|
129 | } else if (srcFileStat.isSymbolicLink() && !opts.followsymlink) {
|
130 | symlinkFull = fs.readlinkSync(srcFile);
|
131 | try {
|
132 | common.statNoFollowLinks(destFile);
|
133 | common.unlinkSync(destFile);
|
134 | } catch (e) {
|
135 |
|
136 | }
|
137 | fs.symlinkSync(symlinkFull, destFile, isWindows ? 'junction' : null);
|
138 | } else if (srcFileStat.isSymbolicLink() && opts.followsymlink) {
|
139 | srcFileStat = common.statFollowLinks(srcFile);
|
140 | if (srcFileStat.isDirectory()) {
|
141 | cpdirSyncRecursive(srcFile, destFile, currentDepth, opts);
|
142 | } else {
|
143 | copyFileSync(srcFile, destFile, opts);
|
144 | }
|
145 | } else {
|
146 |
|
147 | if (fs.existsSync(destFile) && opts.no_force) {
|
148 | common.log('skipping existing file: ' + files[i]);
|
149 | } else {
|
150 | copyFileSync(srcFile, destFile, opts);
|
151 | }
|
152 | }
|
153 | }
|
154 | }
|
155 |
|
156 |
|
157 | function checkRecentCreated(sources, index) {
|
158 | var lookedSource = sources[index];
|
159 | return sources.slice(0, index).some(function (src) {
|
160 | return path.basename(src) === path.basename(lookedSource);
|
161 | });
|
162 | }
|
163 |
|
164 | function cpcheckcycle(sourceDir, srcFile) {
|
165 | var srcFileStat = common.statNoFollowLinks(srcFile);
|
166 | if (srcFileStat.isSymbolicLink()) {
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | var cyclecheck = common.statFollowLinks(srcFile);
|
174 | if (cyclecheck.isDirectory()) {
|
175 | var sourcerealpath = fs.realpathSync(sourceDir);
|
176 | var symlinkrealpath = fs.realpathSync(srcFile);
|
177 | var re = new RegExp(symlinkrealpath);
|
178 | if (re.test(sourcerealpath)) {
|
179 | return true;
|
180 | }
|
181 | }
|
182 | }
|
183 | return false;
|
184 | }
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | function _cp(options, sources, dest) {
|
209 |
|
210 | if (options.followsymlink) {
|
211 | options.noFollowsymlink = false;
|
212 | }
|
213 | if (!options.recursive && !options.noFollowsymlink) {
|
214 | options.followsymlink = true;
|
215 | }
|
216 |
|
217 |
|
218 | if (arguments.length < 3) {
|
219 | common.error('missing <source> and/or <dest>');
|
220 | } else {
|
221 | sources = [].slice.call(arguments, 1, arguments.length - 1);
|
222 | dest = arguments[arguments.length - 1];
|
223 | }
|
224 |
|
225 | var destExists = fs.existsSync(dest);
|
226 | var destStat = destExists && common.statFollowLinks(dest);
|
227 |
|
228 |
|
229 | if ((!destExists || !destStat.isDirectory()) && sources.length > 1) {
|
230 | common.error('dest is not a directory (too many sources)');
|
231 | }
|
232 |
|
233 |
|
234 | if (destExists && destStat.isFile() && options.no_force) {
|
235 | return new common.ShellString('', '', 0);
|
236 | }
|
237 |
|
238 | sources.forEach(function (src, srcIndex) {
|
239 | if (!fs.existsSync(src)) {
|
240 | if (src === '') src = "''";
|
241 | common.error('no such file or directory: ' + src, { continue: true });
|
242 | return;
|
243 | }
|
244 | var srcStat = common.statFollowLinks(src);
|
245 | if (!options.noFollowsymlink && srcStat.isDirectory()) {
|
246 | if (!options.recursive) {
|
247 |
|
248 | common.error("omitting directory '" + src + "'", { continue: true });
|
249 | } else {
|
250 |
|
251 |
|
252 | var newDest = (destStat && destStat.isDirectory()) ?
|
253 | path.join(dest, path.basename(src)) :
|
254 | dest;
|
255 |
|
256 | try {
|
257 | common.statFollowLinks(path.dirname(dest));
|
258 | cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink });
|
259 | } catch (e) {
|
260 |
|
261 | common.error("cannot create directory '" + dest + "': No such file or directory");
|
262 | }
|
263 | }
|
264 | } else {
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | var thisDest = dest;
|
270 | if (destStat && destStat.isDirectory()) {
|
271 | thisDest = path.normalize(dest + '/' + path.basename(src));
|
272 | }
|
273 |
|
274 | var thisDestExists = fs.existsSync(thisDest);
|
275 | if (thisDestExists && checkRecentCreated(sources, srcIndex)) {
|
276 |
|
277 | if (!options.no_force) {
|
278 | common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true });
|
279 | }
|
280 | return;
|
281 | }
|
282 |
|
283 | if (thisDestExists && options.no_force) {
|
284 | return;
|
285 | }
|
286 |
|
287 | if (path.relative(src, thisDest) === '') {
|
288 |
|
289 | common.error("'" + thisDest + "' and '" + src + "' are the same file", { continue: true });
|
290 | return;
|
291 | }
|
292 |
|
293 | copyFileSync(src, thisDest, options);
|
294 | }
|
295 | });
|
296 |
|
297 | return new common.ShellString('', common.state.error, common.state.errorCode);
|
298 | }
|
299 | module.exports = _cp;
|