Using the canWrite function from vfs-local, you can do it with a simple
stat. https://github.com/c9/vfs/blob/master/local/localfs.js#L19-23

function canWrite(owner, inGroup, mode) {
  return owner && (mode & 00200) || // User is owner and owner can write.
         inGroup && (mode & 00020) || // User is in group and group can write.
         (mode & 00002); // Anyone can write.
}

Then in your code do:


fs.stat(file, function (err, stat) {
  if (err) { /* handle error */ }
  if (canWrite(process.uid === stat.uid, process.gid === stat.gid,
stat.mode)) {
    // you can write!
  }
});

I'm assuming you mean if the current owner can write.  Things get more
complicated if you are running as root and want to see if some arbitrary
uid/gid can write because then you have to check for execute/search access
in all the parent directories.  In that case you would need something like
https://github.com/c9/vfs/blob/master/local/localfs.js#L104-124

On Mon, Jun 4, 2012 at 3:36 PM, Alan Hoffmeister
<[email protected]>wrote:

> How to detect if a given path is writable? It can be a folder or a file...
>
> --
> Att,
> Alan Hoffmeister
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to