]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/graceful-fs/polyfills.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / graceful-fs / polyfills.js
1 var fs = require('./fs.js')
2 var constants = require('constants')
3
4 var origCwd = process.cwd
5 var cwd = null
6 process.cwd = function() {
7   if (!cwd)
8     cwd = origCwd.call(process)
9   return cwd
10 }
11 try {
12   process.cwd()
13 } catch (er) {}
14
15 var chdir = process.chdir
16 process.chdir = function(d) {
17   cwd = null
18   chdir.call(process, d)
19 }
20
21 module.exports = patch
22
23 function patch (fs) {
24   // (re-)implement some things that are known busted or missing.
25
26   // lchmod, broken prior to 0.6.2
27   // back-port the fix here.
28   if (constants.hasOwnProperty('O_SYMLINK') &&
29       process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
30     patchLchmod(fs)
31   }
32
33   // lutimes implementation, or no-op
34   if (!fs.lutimes) {
35     patchLutimes(fs)
36   }
37
38   // https://github.com/isaacs/node-graceful-fs/issues/4
39   // Chown should not fail on einval or eperm if non-root.
40   // It should not fail on enosys ever, as this just indicates
41   // that a fs doesn't support the intended operation.
42
43   fs.chown = chownFix(fs.chown)
44   fs.fchown = chownFix(fs.fchown)
45   fs.lchown = chownFix(fs.lchown)
46
47   fs.chmod = chmodFix(fs.chmod)
48   fs.fchmod = chmodFix(fs.fchmod)
49   fs.lchmod = chmodFix(fs.lchmod)
50
51   fs.chownSync = chownFixSync(fs.chownSync)
52   fs.fchownSync = chownFixSync(fs.fchownSync)
53   fs.lchownSync = chownFixSync(fs.lchownSync)
54
55   fs.chmodSync = chmodFixSync(fs.chmodSync)
56   fs.fchmodSync = chmodFixSync(fs.fchmodSync)
57   fs.lchmodSync = chmodFixSync(fs.lchmodSync)
58
59   // if lchmod/lchown do not exist, then make them no-ops
60   if (!fs.lchmod) {
61     fs.lchmod = function (path, mode, cb) {
62       if (cb) process.nextTick(cb)
63     }
64     fs.lchmodSync = function () {}
65   }
66   if (!fs.lchown) {
67     fs.lchown = function (path, uid, gid, cb) {
68       if (cb) process.nextTick(cb)
69     }
70     fs.lchownSync = function () {}
71   }
72
73   // on Windows, A/V software can lock the directory, causing this
74   // to fail with an EACCES or EPERM if the directory contains newly
75   // created files.  Try again on failure, for up to 1 second.
76   if (process.platform === "win32") {
77     fs.rename = (function (fs$rename) { return function (from, to, cb) {
78       var start = Date.now()
79       fs$rename(from, to, function CB (er) {
80         if (er
81             && (er.code === "EACCES" || er.code === "EPERM")
82             && Date.now() - start < 1000) {
83           return fs$rename(from, to, CB)
84         }
85         if (cb) cb(er)
86       })
87     }})(fs.rename)
88   }
89
90   // if read() returns EAGAIN, then just try it again.
91   fs.read = (function (fs$read) { return function (fd, buffer, offset, length, position, callback_) {
92     var callback
93     if (callback_ && typeof callback_ === 'function') {
94       var eagCounter = 0
95       callback = function (er, _, __) {
96         if (er && er.code === 'EAGAIN' && eagCounter < 10) {
97           eagCounter ++
98           return fs$read.call(fs, fd, buffer, offset, length, position, callback)
99         }
100         callback_.apply(this, arguments)
101       }
102     }
103     return fs$read.call(fs, fd, buffer, offset, length, position, callback)
104   }})(fs.read)
105
106   fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
107     var eagCounter = 0
108     while (true) {
109       try {
110         return fs$readSync.call(fs, fd, buffer, offset, length, position)
111       } catch (er) {
112         if (er.code === 'EAGAIN' && eagCounter < 10) {
113           eagCounter ++
114           continue
115         }
116         throw er
117       }
118     }
119   }})(fs.readSync)
120 }
121
122 function patchLchmod (fs) {
123   fs.lchmod = function (path, mode, callback) {
124     fs.open( path
125            , constants.O_WRONLY | constants.O_SYMLINK
126            , mode
127            , function (err, fd) {
128       if (err) {
129         if (callback) callback(err)
130         return
131       }
132       // prefer to return the chmod error, if one occurs,
133       // but still try to close, and report closing errors if they occur.
134       fs.fchmod(fd, mode, function (err) {
135         fs.close(fd, function(err2) {
136           if (callback) callback(err || err2)
137         })
138       })
139     })
140   }
141
142   fs.lchmodSync = function (path, mode) {
143     var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
144
145     // prefer to return the chmod error, if one occurs,
146     // but still try to close, and report closing errors if they occur.
147     var threw = true
148     var ret
149     try {
150       ret = fs.fchmodSync(fd, mode)
151       threw = false
152     } finally {
153       if (threw) {
154         try {
155           fs.closeSync(fd)
156         } catch (er) {}
157       } else {
158         fs.closeSync(fd)
159       }
160     }
161     return ret
162   }
163 }
164
165 function patchLutimes (fs) {
166   if (constants.hasOwnProperty("O_SYMLINK")) {
167     fs.lutimes = function (path, at, mt, cb) {
168       fs.open(path, constants.O_SYMLINK, function (er, fd) {
169         if (er) {
170           if (cb) cb(er)
171           return
172         }
173         fs.futimes(fd, at, mt, function (er) {
174           fs.close(fd, function (er2) {
175             if (cb) cb(er || er2)
176           })
177         })
178       })
179     }
180
181     fs.lutimesSync = function (path, at, mt) {
182       var fd = fs.openSync(path, constants.O_SYMLINK)
183       var ret
184       var threw = true
185       try {
186         ret = fs.futimesSync(fd, at, mt)
187         threw = false
188       } finally {
189         if (threw) {
190           try {
191             fs.closeSync(fd)
192           } catch (er) {}
193         } else {
194           fs.closeSync(fd)
195         }
196       }
197       return ret
198     }
199
200   } else {
201     fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
202     fs.lutimesSync = function () {}
203   }
204 }
205
206 function chmodFix (orig) {
207   if (!orig) return orig
208   return function (target, mode, cb) {
209     return orig.call(fs, target, mode, function (er) {
210       if (chownErOk(er)) er = null
211       if (cb) cb.apply(this, arguments)
212     })
213   }
214 }
215
216 function chmodFixSync (orig) {
217   if (!orig) return orig
218   return function (target, mode) {
219     try {
220       return orig.call(fs, target, mode)
221     } catch (er) {
222       if (!chownErOk(er)) throw er
223     }
224   }
225 }
226
227
228 function chownFix (orig) {
229   if (!orig) return orig
230   return function (target, uid, gid, cb) {
231     return orig.call(fs, target, uid, gid, function (er) {
232       if (chownErOk(er)) er = null
233       if (cb) cb.apply(this, arguments)
234     })
235   }
236 }
237
238 function chownFixSync (orig) {
239   if (!orig) return orig
240   return function (target, uid, gid) {
241     try {
242       return orig.call(fs, target, uid, gid)
243     } catch (er) {
244       if (!chownErOk(er)) throw er
245     }
246   }
247 }
248
249 // ENOSYS means that the fs doesn't support the op. Just ignore
250 // that, because it doesn't matter.
251 //
252 // if there's no getuid, or if getuid() is something other
253 // than 0, and the error is EINVAL or EPERM, then just ignore
254 // it.
255 //
256 // This specific case is a silent failure in cp, install, tar,
257 // and most other unix tools that manage permissions.
258 //
259 // When running as root, or if other types of errors are
260 // encountered, then it's strict.
261 function chownErOk (er) {
262   if (!er)
263     return true
264
265   if (er.code === "ENOSYS")
266     return true
267
268   var nonroot = !process.getuid || process.getuid() !== 0
269   if (nonroot) {
270     if (er.code === "EINVAL" || er.code === "EPERM")
271       return true
272   }
273
274   return false
275 }