As Ben and Bert say, if you checking for your own process and need to be
cross-platform, then the easiest and safest is to just try.

function canWrite(path, callback) {
  fs.open(path, function (err, fd) {
    if (err) {
      if (err.code === "EISDIR") {
        // Create a file in the directory or something...
      }
      if (err.code === "EACCESS") {
        return callback(null, false);
      }
      return callback(err);
    }
    fs.close(fd, function (err) {
      if (err) return callback(err);
      callback(null, true);
    });
  });
});

But the question is why do you need to know if you can write to it?  Best
to just try to do whatever you're trying to do and watch for possible
errors.

On Tue, Jun 5, 2012 at 9:49 AM, Bert Belder <bertbel...@gmail.com> wrote:

> On Jun 5, 4:31 pm, Tim Caswell <t...@creationix.com> wrote:
> > 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
> likehttps://github.com/c9/vfs/blob/master/local/localfs.js#L104-124
>
> I can think of a couple of other exceptions :-)
> - The directory resides on a read-only mount
> - You are using an operating system that uses ACLs like Windows or
> Solaris
>
> I don't think there's a generic solution to this problem. You should
> probably just try :-)
>
> --
> 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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> 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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to