Skip to content

Commit

Permalink
Merge pull request #60 from skx/windows-build
Browse files Browse the repository at this point in the history
Windows build
  • Loading branch information
skx authored Oct 17, 2022
2 parents f21d032 + 70c5d64 commit 1128604
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/build
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ D=$(pwd)
#
# We build on multiple platforms/archs
#
BUILD_PLATFORMS="linux darwin freebsd"
BUILD_PLATFORMS="linux darwin freebsd windows"
BUILD_ARCHS="amd64 386"

# For each platform
Expand Down
15 changes: 10 additions & 5 deletions builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"sort"
"strconv"
"strings"
"syscall"
"time"

"github.com/skx/yal/env"
Expand Down Expand Up @@ -631,10 +630,16 @@ func fileStatFn(env *env.Environment, args []primitive.Primitive) primitive.Prim
UID := os.Getuid()
GID := os.Getgid()

// But if we can get the "real" values, then use them.
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
UID = int(stat.Uid)
GID = int(stat.Gid)
// Get the UID in a non-portable way
u, e := getUID(info)
if e == nil {
UID = u
}

// Get the GID in a non-portable way
g, e := getGID(info)
if e == nil {
GID = g
}

var res primitive.List
Expand Down
4 changes: 2 additions & 2 deletions builtins/builtins_shell_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !windows && !darwin
// +build !windows,!darwin
//go:build !windows
// +build !windows

package builtins

Expand Down
31 changes: 31 additions & 0 deletions builtins/file_info_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build !windows

package builtins

import (
"os"
"syscall"
)

// getGID returns the group of the file, from the extended information
// available after a stat - that is not portable to Windows though.
//
// This is in a separate file so that we use build-tags to build code
// appropriately.
func getGID(info os.FileInfo) (int, error) {

stat, _ := info.Sys().(*syscall.Stat_t)
return int(stat.Gid), nil
}


// getUID returns the owner of the file, from the extended information
// available after a stat - that is not portable to Windows though.
//
// This is in a separate file so that we use build-tags to build code
// appropriately.
func getUID(info os.FileInfo) (int, error) {

stat, _ := info.Sys().(*syscall.Stat_t)
return int(stat.Uid), nil
}
34 changes: 34 additions & 0 deletions builtins/file_info_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build windows

package builtins

import (
"fmt"
"os"
)

// getGID should return the group of the file, from the extended information
// available after a stat, however on Windows platforms that doesn't work
// in the obvious way.
//
// Here we just return an error to make that apparent to the caller.
//
// This is in a separate file so that we use build-tags to build code
// appropriately.
func getGID(info os.FileInfo) (int, error) {

return 0, fmt.Errorf("not found")
}

// getUID should return the owner of the file, from the extended information
// available after a stat, however on Windows platforms that doesn't work
// in the obvious way.
//
// Here we just return an error to make that apparent to the caller.
//
// This is in a separate file so that we use build-tags to build code
// appropriately.
func getUID(info os.FileInfo) (int, error) {

return 0, fmt.Errorf("not found")
}

0 comments on commit 1128604

Please sign in to comment.