Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / fs-extra / lib / mkdirs / mkdirs.js
1 var fs = require('graceful-fs')
2 var path = require('path')
3
4 var o777 = parseInt('0777', 8)
5
6 function mkdirs (p, opts, callback, made) {
7   if (typeof opts === 'function') {
8     callback = opts
9     opts = {}
10   } else if (!opts || typeof opts !== 'object') {
11     opts = { mode: opts }
12   }
13
14   var mode = opts.mode
15   var xfs = opts.fs || fs
16
17   if (mode === undefined) {
18     mode = o777 & (~process.umask())
19   }
20   if (!made) made = null
21
22   callback = callback || function () {}
23   p = path.resolve(p)
24
25   xfs.mkdir(p, mode, function (er) {
26     if (!er) {
27       made = made || p
28       return callback(null, made)
29     }
30     switch (er.code) {
31       case 'ENOENT':
32         if (path.dirname(p) === p) return callback(er)
33         mkdirs(path.dirname(p), opts, function (er, made) {
34           if (er) callback(er, made)
35           else mkdirs(p, opts, callback, made)
36         })
37         break
38
39       // In the case of any other error, just see if there's a dir
40       // there already.  If so, then hooray!  If not, then something
41       // is borked.
42       default:
43         xfs.stat(p, function (er2, stat) {
44           // if the stat fails, then that's super weird.
45           // let the original error be the failure reason.
46           if (er2 || !stat.isDirectory()) callback(er, made)
47           else callback(null, made)
48         })
49         break
50     }
51   })
52 }
53
54 module.exports = mkdirs