process - How to find whether a file is an executable in node.js -
i looking platform independent (win, mac, linux) way of finding whether file executable or not. following doesn't work me (not on linux) -
var spawn = require("child_process").spawn; var proc = spawn("whatever file"); proc.stderr.on('data', function (data) { if (/^execvp\(\)/.test(data)) { console.log('failed start child process.'); } });
i advise against executing file way test whether it's executable. if don't know file is, don't run it! security , general damage nightmare. analogous testing whether vial of clear liquid poison drinking it.
step 1: read it's metadata via stat
on posix (linux, mac, bsd), use fs.stat examine mode field check various permutations executable.
step 2: read beginning of file data , guess type
you can run file utility on it, use database of magic numbers , other heuristics guess file type.
windows
in terms of platform independent, afaik functionality isn't similar enough on windows there reliable windows equivalent of above posix-oriented techniques. windows filesystems , filetypes different, think if want work, you'll need separate implementation handle windows (and other me suggest how implement on windows).
Comments
Post a Comment