Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created February 24, 2011 09:49
Show Gist options
  • Save isaacs/841992 to your computer and use it in GitHub Desktop.
Save isaacs/841992 to your computer and use it in GitHub Desktop.
Test whether or not a file is writable/readable/executable
exports.isReadable = isReadable
exports.isReadableSync = isReadableSync
exports.isWritable = isWritable
exports.isWritableSync = isWritableSync
exports.isExecutable = isExecutable
exports.isExecutableSync = isExecutableSync
function isReadable (path, cb) {
fs.stat(path, function (er, s) {
if (er) return cb(er)
var mode = s.mode & 0777
cb(null, ( (mode & 0004)
||(mode & 0040 && process.getgid() === s.gid)
||(mode & 0400 && process.getuid() === s.uid)))
})
}
function isReadableSync (path) {
var s = fs.statSync(path)
var mode = s.mode & 0777
return ( (mode & 0004)
||(mode & 0040 && process.getgid() === s.gid)
||(mode & 0400 && process.getuid() === s.uid)))
}
function isWritable (path, cb) {
fs.stat(path, function (er, s) {
if (er) return cb(er)
var mode = s.mode & 0777
cb(null, ( (mode & 0002)
||(mode & 0020 && process.getgid() === s.gid)
||(mode & 0200 && process.getuid() === s.uid)))
})
}
function isWritableSync (path) {
var s = fs.statSync(path)
var mode = s.mode & 0777
return ( (mode & 0002)
||(mode & 0020 && process.getgid() === s.gid)
||(mode & 0200 && process.getuid() === s.uid)))
}
function isExecutable (path, cb) {
fs.stat(path, function (er, s) {
if (er) return cb(er)
var mode = s.mode & 0777
cb(null, ( (mode & 0001)
||(mode & 0010 && process.getgid() === s.gid)
||(mode & 0100 && process.getuid() === s.uid)))
})
}
function isExecutableSync (path) {
var s = fs.statSync(path)
var mode = s.mode & 0777
return ( (mode & 0001)
||(mode & 0010 && process.getgid() === s.gid)
||(mode & 0100 && process.getuid() === s.uid)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment