Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Make goss compile on windows. (#2)
Browse files Browse the repository at this point in the history
I've extracted the unix-only part to file_unix.go and windows part to file_windows.go. I haven't found an easy way to support owners/groups on windows, so I've left it empty.

TODO: figure out how to write and run integration tests on windows, but probably not this pr.
  • Loading branch information
stefan-improbable authored Apr 10, 2019
1 parent 5453a57 commit a1eaf7a
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 53 deletions.
53 changes: 0 additions & 53 deletions system/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/aelsabbahy/goss/util"
"github.com/opencontainers/runc/libcontainer/user"
Expand Down Expand Up @@ -84,22 +83,6 @@ func (f *DefFile) Contains() (io.Reader, error) {
return fh, nil
}

func (f *DefFile) Mode() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

sys := fi.Sys()
stat := sys.(*syscall.Stat_t)
mode := fmt.Sprintf("%04o", (stat.Mode & 07777))
return mode, nil
}

func (f *DefFile) Size() (int, error) {
if err := f.setup(); err != nil {
return 0, err
Expand Down Expand Up @@ -145,42 +128,6 @@ func (f *DefFile) Filetype() (string, error) {
return "file", nil
}

func (f *DefFile) Owner() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

uidS := fmt.Sprint(fi.Sys().(*syscall.Stat_t).Uid)
uid, err := strconv.Atoi(uidS)
if err != nil {
return "", err
}
return getUserForUid(uid)
}

func (f *DefFile) Group() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

gidS := fmt.Sprint(fi.Sys().(*syscall.Stat_t).Gid)
gid, err := strconv.Atoi(gidS)
if err != nil {
return "", err
}
return getGroupForGid(gid)
}

func (f *DefFile) LinkedTo() (string, error) {
if err := f.setup(); err != nil {
return "", err
Expand Down
62 changes: 62 additions & 0 deletions system/file_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// +build !windows

package system

import (
"fmt"
"os"
"strconv"
"syscall"
)

func (f *DefFile) Mode() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

sys := fi.Sys()
stat := sys.(*syscall.Stat_t)
mode := fmt.Sprintf("%04o", (stat.Mode & 07777))
return mode, nil
}

func (f *DefFile) Owner() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

uidS := fmt.Sprint(fi.Sys().(*syscall.Stat_t).Uid)
uid, err := strconv.Atoi(uidS)
if err != nil {
return "", err
}
return getUserForUid(uid)
}

func (f *DefFile) Group() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

gidS := fmt.Sprint(fi.Sys().(*syscall.Stat_t).Gid)
gid, err := strconv.Atoi(gidS)
if err != nil {
return "", err
}
return getGroupForGid(gid)
}
31 changes: 31 additions & 0 deletions system/file_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package system

import (
"fmt"
"os"
)

// Mode returns golang file mode. This method is cross-platform, but we are keeping unix-specific implementation
// in file_unix.go for backwards compatibility.
func (f *DefFile) Mode() (string, error) {
if err := f.setup(); err != nil {
return "", err
}

fi, err := os.Lstat(f.realPath)
if err != nil {
return "", err
}

mode := fmt.Sprintf("%04o", (fi.Mode() & 07777))
return mode, nil
}

// TODO(ENG-1522): owner and group are not defined for windows in standard library, find out whether we can support it.
func (f *DefFile) Owner() (string, error) {
return "", nil
}

func (f *DefFile) Group() (string, error) {
return "", nil
}

0 comments on commit a1eaf7a

Please sign in to comment.