Skip to content

Commit

Permalink
add option for selinux
Browse files Browse the repository at this point in the history
closes acarl005#6
  • Loading branch information
mmckinst committed Feb 3, 2020
1 parent e5b3146 commit 215b19d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Flags:
-i, --icons show folder icon before dirs
-n, --nerd-font show nerd font glyphs before file names
-r, --recurse traverse all dirs recursively
-Z, --selinux include security context
-F, --find=FIND filter items with a regexp
Args:
Expand Down
2 changes: 2 additions & 0 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type arguments struct {
icons *bool
nerdfont *bool
recurse *bool
selinux *bool
find *string
}

Expand All @@ -52,6 +53,7 @@ var args = arguments{
kingpin.Flag("icons", "show folder icon before dirs").Short('i').Bool(),
kingpin.Flag("nerd-font", "show nerd font glyphs before file names").Short('n').Bool(),
kingpin.Flag("recurse", "traverse all dirs recursively").Short('r').Bool(),
kingpin.Flag("selinux", "include security context").Short('Z').Bool(),
kingpin.Flag("find", "filter items with a regexp").Short('F').String(),
}

Expand Down
21 changes: 21 additions & 0 deletions ls-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/opencontainers/selinux/go-selinux"
"github.com/acarl005/textcol"
colorable "github.com/mattn/go-colorable"
"github.com/willf/pad"
Expand Down Expand Up @@ -150,6 +151,14 @@ func listFiles(parentDir string, items *[]os.FileInfo, forceDotfiles bool) {
}
}

longestSELinuxLabel := 0
if *args.selinux && selinux.GetEnabled() {
for _, fileInfo := range *items {
selinuxlabel := getSELinuxLabel(path.Join(absPath, fileInfo.Name()))
longestSELinuxLabel = max(longestSELinuxLabel, len(selinuxlabel))
}
}

for _, fileInfo := range *items {
// if this is a dotfile (hidden file)
if fileInfo.Name()[0] == '.' {
Expand Down Expand Up @@ -204,6 +213,12 @@ func listFiles(parentDir string, items *[]os.FileInfo, forceDotfiles bool) {
displayItem.display += strings.Join(ownerInfo, " ")
}

if *args.selinux && selinux.GetEnabled() {
selinuxLabel := getSELinuxLabel(path.Join(absPath, fileInfo.Name()))
paddedSELinuxLabel := pad.Right(selinuxLabel, longestSELinuxLabel, " ")
displayItem.display += paddedSELinuxLabel
}

if *args.bytes {
if fileInfo.Mode()&os.ModeDevice != 0 {
displayItem.display += deviceNumbers(path.Join(absPath, fileInfo.Name()))
Expand Down Expand Up @@ -594,6 +609,12 @@ func splitExt(filename string) (basename, ext string) {
return
}

func getSELinuxLabel(absPath string) (string) {
selinuxLabel, err := selinux.FileLabel(absPath)
check(err)
return selinuxLabel
}

// Go doesn't provide a `Max` function for ints like it does for floats (wtf?)
func max(a int, b int) int {
if a > b {
Expand Down

0 comments on commit 215b19d

Please sign in to comment.