github.com/theclapp/sh@v2.6.4+incompatible/interp/perm_unix.go (about)

     1  // Copyright (c) 2017, Andrey Nering <andrey.nering@gmail.com>
     2  // See LICENSE for licensing information
     3  
     4  // +build !windows
     5  
     6  package interp
     7  
     8  import (
     9  	"os"
    10  	"os/user"
    11  	"strconv"
    12  	"syscall"
    13  )
    14  
    15  // hasPermissionToDir returns if the OS current user has execute permission
    16  // to the given directory
    17  func hasPermissionToDir(info os.FileInfo) bool {
    18  	user, err := user.Current()
    19  	if err != nil {
    20  		return true
    21  	}
    22  	uid, _ := strconv.Atoi(user.Uid)
    23  	// super-user
    24  	if uid == 0 {
    25  		return true
    26  	}
    27  
    28  	st, _ := info.Sys().(*syscall.Stat_t)
    29  	if st == nil {
    30  		return true
    31  	}
    32  	perm := info.Mode().Perm()
    33  	// user (u)
    34  	if perm&0100 != 0 && st.Uid == uint32(uid) {
    35  		return true
    36  	}
    37  
    38  	gid, _ := strconv.Atoi(user.Gid)
    39  	// other users in group (g)
    40  	if perm&0010 != 0 && st.Uid != uint32(uid) && st.Gid == uint32(gid) {
    41  		return true
    42  	}
    43  	// remaining users (o)
    44  	if perm&0001 != 0 && st.Uid != uint32(uid) && st.Gid != uint32(gid) {
    45  		return true
    46  	}
    47  
    48  	return false
    49  }