Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file is executable in go?

Tags:

unix

go

How would I write a function to check whether a file is executable in Go? Given an os.FileInfo, I can get os.FileInfo.Mode(), but I stall out trying to parse the permission bits.

Test case:

#!/usr/bin/env bash
function setup() {
  mkdir -p test/foo/bar
  touch test/foo/bar/{baz.txt,quux.sh}
  chmod +x test/foo/bar/quux.sh
}

function teardown() { rm -r ./test }

setup
import (
    "os"
    "path/filepath"
    "fmt"
)

func IsExectuable(mode os.FileMode) bool {
    // ???
}

func main() {
    filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
        if err != nil || info.IsDir() {
            return err
        }
        fmt.Printf("%v %v", path, IsExectuable(info.Mode().Perm()))
    }
}
// should print "test/foo/bar/baz.txt false"
//              "test/foo/bar/quux.txt true"

I only care about Unix files, but extra points if the solution works for Windows as well.

like image 951
Steven Kalt Avatar asked Feb 08 '20 15:02

Steven Kalt


People also ask

How do you check if a file is executable or not?

[ -w file ] tests if a file is writeable. [ -x file ] tests if a file is executable. The recommended tool to determine a file type is file .

How do you check whether a file is executable or not in Linux?

In Linux systems, we should look up the x bit of the file. We found, reading the x bits, that the file a. out may be executed by its owner, joe, the members of the joe group, and others. So, the test's x flag proves if the file exists and is executable.

How do I create an executable file in Golang?

Go The Go Command Go BuildPrintln("Hello, World!") } build creates an executable program, in this case: main or main.exe . You can then run this file to see the output Hello, World! . You can also copy it to a similar system that doesn't have Go installed, make it executable, and run it there.

How to check if a file exists in Golang?

How to check if a file exists in Golang? In order to check if a particular file exists inside a given directory in Golang, we can use the Stat () and the isNotExists () function that the os package of Go's standard library provides us with. The Stat () function is used to return the file info structure describing the file.

How do you check if a file is an executable message?

For instance, to execute ‘x /path/to/command’ is used if you have a command file path that names it that way.If this particular command executes permissions (x ), then it is an executable message. How Do You Check If A File Is An Executable File? Executable = os. access (filename, ename, os. X_OK) executable = os. access (filename, os. X_OK) )

How do I check if a file is executable in Linux?

How To Check If A File Is Executable In Linux? Use if -x /path/to/command to find the command files on the machine you know a particular path to. The executable is the one that has execute permission set ( x ). how do you check if a file is an executable file?

How to check if a file is a directory in go?

If you want to check this information without opening the file, you can use os.Stat () and pass the path to the file. If you have to check for this often, consider wrapping up the code in function such as the one shown below: As you can see, determining if a file is a directory or not is pretty straightforward in Go.


1 Answers

Whether the file is executable is stored in the Unix permission bits (returned by FileMode.Perm() which are basically the lowest 9 bits (0777 octal bitmask). Note that since we're using bitmasks in below solutions that mask out other bits anyway, calling Perm() is not necessary.

Their meaning is:

rwxrwxrwx

Where the first 3 bits are for the owner, the next 3 are for the group and the last 3 bits are for other.

To tell if the file is executable by its owner, use bitmask 0100:

func IsExecOwner(mode os.FileMode) bool {
    return mode&0100 != 0
}

Similarly for telling if executable by the group, use bitmask 0010:

func IsExecGroup(mode os.FileMode) bool {
    return mode&0010 != 0
}

And by others, use bitmask 0001:

func IsExecOther(mode os.FileMode) bool {
    return mode&0001 != 0
}

To tell if the file is executable by any of the above, use bitmask 0111:

func IsExecAny(mode os.FileMode) bool {
    return mode&0111 != 0
}

To tell if the file is executable by all of the above, again use bitmask 0111 but check if the result equals to 0111:

func IsExecAll(mode os.FileMode) bool {
    return mode&0111 == 0111
}
like image 170
icza Avatar answered Oct 18 '22 23:10

icza