-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
248 lines (224 loc) · 6.46 KB
/
index.js
File metadata and controls
248 lines (224 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var Fs = require('fs')
var Path = require('path')
/*
* mimick mkdir -p
*/
function mkdir_p(path, perm, callback) {
path = Path.resolve(process.cwd(), path)
Fs.mkdir(path, perm, function(err) {
if (!err) { callback() ; return }
if (err.code === 'ENOENT') {
mkdir_p(Path.dirname(path), perm, function(err) {
if (err) {
callback(err)
} else {
mkdir_p(path, perm, callback)
}
})
} else if (err.code === 'EEXIST') {
Fs.stat(path, function(sterr, stat) {
if (sterr || !stat.isDirectory()) {
callback(sterr)
} else if (stat.mode != perm) {
Fs.chmod(path, perm, callback)
} else {
callback()
}
})
} else {
callback(err)
}
})
}
/*
* mimick find
*/
function match_stub_fn(path, stat, depth, cb) { cb() }
function find(path, options, callback) {
// defaults
options = options || {}
var match_fn = options.match_fn || match_stub_fn
var dir_fn = options.dir_fn || match_stub_fn
var serial = !!options.serial
// cache highly used functions
var normalize = Path.normalize
var join = Path.join
var stat = options.follow ? Fs.stat : Fs.lstat
var readdir = Fs.readdir
// base path
var base = Path.resolve(process.cwd(), path)
// collect seen inodes
var inos = {}
// recursive walk helper
function walk(path, depth, cb) {
// stat, resolving symlinks
stat(path, function (err, st) {
// stat failed? step out.
if (err) { cb(err) ; return }
// prevent endless loops in follow mode
// N.B. this is to cope with symlinks pointing to '.'
if (options.follow) {
// inode seen? step out
var inode = st.ino
if (inos[inode]) { cb() ; return }
// mark inode as seen
inos[inode] = true
}
// call matcher
match_fn.call(options, path, st, depth, function (err) {
// `true` error means stop going deeper
if (err && err !== true) { cb(err) ; return }
// path is not directory? we re done.
if (!st.isDirectory()) { cb() ; return }
// path is directory. read files
readdir(path, function (err, files) {
if (err) { cb(err) ; return }
// recursively iterate over files
// cache `files` length
var len = files.length
// set starting index
// N.B. for serial execution iterations start by calling
// `collect`, hence it's called one time more,
// hence lesser starting index
var collected = serial ? 0 : 1
function collect() {
if (collected >= len) {
// notify of directory is processed
dir_fn.call(options, path, st, depth, cb)
// if we iterate sequentially, start new iteration
} else if (serial) {
walk(join(path, files[collected]), depth + 1, collect)
}
collected++
}
// parallell execution and no files? fire callback
// sequential execution? start the first iteration
if (len === 0 || serial) {
collect()
// parallel execution? spawn concurrent walkers
} else {
for (var i = 0; i < len; i++) {
walk(join(path, files[i]), depth + 1, collect)
}
}
})
})
})
}
// walk the tree
walk(base, 0, callback)
}
/*
* mimick rm -fr
*/
function remove(path, callback) {
// cache highly used functions
var unlink = Fs.unlink
var rmdir = Fs.rmdir
path = Path.resolve(process.cwd(), path)
find(path, {
//follow: false,
match_fn: function(path, stat, depth, cb) {
if (!stat.isDirectory()) {
unlink(path, cb)
} else {
cb()
}
},
dir_fn: function (path, stat, depth, cb) {
rmdir(path, cb)
},
}, function (err) {
if (err && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) { err = null }
callback(err)
})
}
/*
* mimick cp -a
*/
function copy(src, dst, callback) {
// cache highly used functions
var join = Path.join
var dirname = Path.dirname
var basename = Path.basename
var read = Fs.readFile
var write = Fs.writeFile
var readlink = Fs.readlink
var symlink = Fs.symlink
var chmod = Fs.chmod
var chown = Fs.chown
// expand paths
var src_orig = Path.normalize(src)
src = Path.resolve(process.cwd(), src)
dst = Path.resolve(process.cwd(), dst)
// dots are special cases. E.g. copy . /foo should copy content of current directory
// while copy ../foo /bar should copy file/directory ../foo as whole
if (src_orig == '.') {
skip += '/'
}
//var skip_len = dirname(src).length + 1
var skip_len = src.length + 1
// walk over the source
find(src, {
// for each source file
match_fn: function (path, stat, depth, cb) {
// compose target path
var new_path = join(dst, path.substring(skip_len))
//console.log('?' + path)
//console.log('!' + new_path)
//cb() ; return
//p(path, stat)
// helper to set target owner and mode to source's ones
function set_perms(err) {
if (err) { cb(err) ; return }
chmod(new_path, stat.mode, function (err) {
if (err) { cb(err) ; return }
chown(new_path, stat.uid, stat.gid, function (err) {
// FIXME: err is unknown is there were no rights to chown
//if (err) { cb(err) ; return }
cb()
})
})
}
// create target
// directory
if (stat.isDirectory()) {
mkdir_p(new_path, stat.mode, set_perms)
// file
} else if (stat.isFile()) {
// TODO: stream it
read(path, function (err, data) {
write(new_path, data, set_perms)
})
// symlink
} else if (stat.isSymbolicLink()) {
readlink(path, function (err, realpath) {
if (err) { cb(err) ; return }
symlink(realpath, new_path, set_perms)
})
// special nodes not supported
// Fs.mknod() is missing
// FIXME: ^^^
} else {
cb({path: path, code: 'ENOTSUPP'})
}
},
}, callback)
}
/*
* mimick ln -s
*/
function link(target, path, callback) {
path = Path.resolve(process.cwd(), path)
mkdir_p(Path.dirname(path), '0755', function (err) {
if (err) { callback(err) ; return }
Fs.symlink(target, path, callback)
})
}
// export augmented Fs
Fs.mkdir_p = mkdir_p
Fs.find = find
Fs.remove = remove
Fs.copy = copy
Fs.link = link
module.exports = Fs