最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - create directory with permission in node js - Stack Overflow

programmeradmin5浏览0评论

I am trying to create a folder using mkdirp node module. but it is creating with permission 0775 but i am in need to create with 0777 permission. official documentation says it is default to 0777 but in my case it is 0755. can anyone help me? code:

var new_location = 'public/images/u/7/';
mkdirp(new_location, function(err) {
  if (err) {
  } else {
  }
});

I am trying to create a folder using mkdirp node module. but it is creating with permission 0775 but i am in need to create with 0777 permission. official documentation says it is default to 0777 but in my case it is 0755. can anyone help me? code:

var new_location = 'public/images/u/7/';
mkdirp(new_location, function(err) {
  if (err) {
  } else {
  }
});
Share Improve this question edited Jan 11, 2016 at 12:00 thefourtheye 239k53 gold badges465 silver badges500 bronze badges asked Jan 11, 2016 at 11:47 iamiam 1,0134 gold badges13 silver badges22 bronze badges 1
  • not working , tried mkdirp(new_location, {mode: "777"} , function (err) { }); – iam Commented Jan 11, 2016 at 11:57
Add a comment  | 

1 Answer 1

Reset to default 20

The documentation states that the default is 0777 & (~process.umask()), which means that your umask value is "subtracted" from the 0777. Since the umask commonly is 002 or 022, you end up with 0775 or 0755.

However, even if you supply a 0777 permission to mkdirp(), the underlying system call will still apply the umask value. To prevent that, you need to clear the umask, create the directory using the permission you want, and (optionally) restore the umask to its previous value:

var oldmask = process.umask(0);
mkdirp(new_location, '0777', function(err) {
  process.umask(oldmask);
  if (err) ...
  ...
});

Alternatively, you can use fs.chmod() to set the correct permissions after the directory was created.

发布评论

评论列表(0)

  1. 暂无评论