Skip to content

Instantly share code, notes, and snippets.

@randwa1k
Created February 25, 2014 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save randwa1k/9210931 to your computer and use it in GitHub Desktop.
Save randwa1k/9210931 to your computer and use it in GitHub Desktop.
/home/ubuntu/local/lib/node_modules/npm/node_modules/fstream/lib/writer.js
245 // XXX This function is beastly. Break it up!
246 Writer.prototype._finish = function () {
247 var me = this
248
249 // console.error(" W Finish", me._path, me.size)
250
251 // set up all the things.
252 // At this point, we're already done writing whatever we've gotta write,
253 // adding files to the dir, etc.
254 var todo = 0
255 var errState = null
256 var done = false
257
258 if (me._old) {
259 // the times will almost *certainly* have changed.
260 // adds the utimes syscall, but remove another stat.
261 me._old.atime = new Date(0)
262 me._old.mtime = new Date(0)
263 // console.error(" W Finish Stale Stat", me._path, me.size)
264 setProps(me._old)
265 } else {
266 var stat = me.props.follow ? "stat" : "lstat"
267 // console.error(" W Finish Stating", me._path, me.size)
268 fs[stat](me._path, function (er, current) {
269 // console.error(" W Finish Stated", me._path, me.size, current)
270 if (er) {
271 // if we're in the process of writing out a
272 // directory, it's very possible that the thing we're linking to
273 // doesn't exist yet (especially if it was intended as a symlink),
274 // so swallow ENOENT errors here and just soldier on.
275 if (er.code === "ENOENT" &&
276 (me.type === "Link" || me.type === "SymbolicLink") &&
277 process.platform === "win32") {
278 me.ready = true
279 me.emit("ready")
280 me.emit("end")
281 me.emit("close")
282 me.end = me._finish = function () {}
283 return
284 } else return me.error(er)
285 }
286 setProps(me._old = current)
287 })
288 }
289
290 return
291
292 function setProps (current) {
293 todo += 3
294 endChmod(me, me.props, current, me._path, next("chmod"))
295 endChown(me, me.props, current, me._path, next("chown"))
296 endUtimes(me, me.props, current, me._path, next("utimes"))
297 }
298
299 function next (what) {
300 return function (er) {
301 // console.error(" W Finish", what, todo)
302 if (errState) return
303 if (er) {
304 er.fstream_finish_call = what
305 return me.error(errState = er)
306 }
307 if (--todo > 0) return
308 if (done) return
309 done = true
310
311 // we may still need to set the mode/etc. on some parent dirs
312 // that were created previously. delay end/close until then.
313 if (!me._madeDir) return end()
314 else endMadeDir(me, me._path, end)
315
316 function end (er) {
317 if (er) {
318 er.fstream_finish_call = "setupMadeDir"
319 return me.error(er)
320 }
321 // all the props have been set, so we're completely done.
322 me.emit("end")
323 me.emit("close")
324 }
325 }
326 }
327 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment