Initial commit
[yaffs-website] / node_modules / fstream / lib / link-writer.js
1 module.exports = LinkWriter
2
3 var fs = require('graceful-fs')
4 var Writer = require('./writer.js')
5 var inherits = require('inherits')
6 var path = require('path')
7 var rimraf = require('rimraf')
8
9 inherits(LinkWriter, Writer)
10
11 function LinkWriter (props) {
12   var self = this
13   if (!(self instanceof LinkWriter)) {
14     throw new Error('LinkWriter must be called as constructor.')
15   }
16
17   // should already be established as a Link type
18   if (!((props.type === 'Link' && props.Link) ||
19     (props.type === 'SymbolicLink' && props.SymbolicLink))) {
20     throw new Error('Non-link type ' + props.type)
21   }
22
23   if (props.linkpath === '') props.linkpath = '.'
24   if (!props.linkpath) {
25     self.error('Need linkpath property to create ' + props.type)
26   }
27
28   Writer.call(this, props)
29 }
30
31 LinkWriter.prototype._create = function () {
32   // console.error(" LW _create")
33   var self = this
34   var hard = self.type === 'Link' || process.platform === 'win32'
35   var link = hard ? 'link' : 'symlink'
36   var lp = hard ? path.resolve(self.dirname, self.linkpath) : self.linkpath
37
38   // can only change the link path by clobbering
39   // For hard links, let's just assume that's always the case, since
40   // there's no good way to read them if we don't already know.
41   if (hard) return clobber(self, lp, link)
42
43   fs.readlink(self._path, function (er, p) {
44     // only skip creation if it's exactly the same link
45     if (p && p === lp) return finish(self)
46     clobber(self, lp, link)
47   })
48 }
49
50 function clobber (self, lp, link) {
51   rimraf(self._path, function (er) {
52     if (er) return self.error(er)
53     create(self, lp, link)
54   })
55 }
56
57 function create (self, lp, link) {
58   fs[link](lp, self._path, function (er) {
59     // if this is a hard link, and we're in the process of writing out a
60     // directory, it's very possible that the thing we're linking to
61     // doesn't exist yet (especially if it was intended as a symlink),
62     // so swallow ENOENT errors here and just soldier in.
63     // Additionally, an EPERM or EACCES can happen on win32 if it's trying
64     // to make a link to a directory.  Again, just skip it.
65     // A better solution would be to have fs.symlink be supported on
66     // windows in some nice fashion.
67     if (er) {
68       if ((er.code === 'ENOENT' ||
69         er.code === 'EACCES' ||
70         er.code === 'EPERM') && process.platform === 'win32') {
71         self.ready = true
72         self.emit('ready')
73         self.emit('end')
74         self.emit('close')
75         self.end = self._finish = function () {}
76       } else return self.error(er)
77     }
78     finish(self)
79   })
80 }
81
82 function finish (self) {
83   self.ready = true
84   self.emit('ready')
85   if (self._ended && !self._finished) self._finish()
86 }
87
88 LinkWriter.prototype.end = function () {
89   // console.error("LW finish in end")
90   this._ended = true
91   if (this.ready) {
92     this._finished = true
93     this._finish()
94   }
95 }