0

I am looking for a platform independent (win, mac, linux) way of finding whether a file is executable or not. Following doesn't work for me (not even 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 to start child process.');
  }
});

2 Answers 2

2

I would advise strongly against executing a file as a way to test whether it's executable. If you don't know what a file is, don't just run it! This is a security and general damage nightmare. This is analogous to testing whether a vial of clear liquid is poison by drinking it.

Step 1: read it's metadata via stat

On posix (linux, mac, bsd), use fs.stat then examine the mode field to check for the various permutations executable.

Step 2: read the beginning of the file data and guess the type

You can run the file utility on it, which will use a database of magic numbers and other heuristics to guess the file type.

Windows

In terms of platform independent, AFAIK this functionality just isn't similar enough on Windows for there to be a reliable Windows equivalent of the above posix-oriented techniques. Windows filesystems and filetypes are different, so I think if you want this to really work, you'll need a separate implementation to handle windows properly (and someone other than me to suggest how to implement it on windows).

Sign up to request clarification or add additional context in comments.

Comments

-1

I used the mime-magic nodejs module to find out about the mime types of the files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.