Skip to content

Commit

Permalink
refactor: update unix build tags and add plan9
Browse files Browse the repository at this point in the history
resolve obscure compile errors when trying to compile on
different goos.
Use unix build tags and add plan9.

We can't implement uid/gid logic without breaks as plan9 uses
strings while the api requires int.
Use plan9.Dup to redirect stderr.
  • Loading branch information
kruskall committed Aug 28, 2024
1 parent 8c7c5ca commit b4f6f1b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
48 changes: 48 additions & 0 deletions file/fileinfo_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//go:build plan9

package file

import (
"errors"
"os"

"golang.org/x/sys/plan9"
)

func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) {
info, err := statFunc(name)
if err != nil {
return nil, err
}

return wrap(info)
}

func wrap(info os.FileInfo) (FileInfo, error) {
stat, ok := info.Sys().(*plan9.Dir)
if !ok {
return nil, errors.New("failed to get uid/gid")
}

// on plan9 uid/gid is a string so we can't cast to int
_ = stat.Uid
_ = stat.Gid
return fileInfo{FileInfo: info, uid: nil, gid: nil}, nil
}
7 changes: 4 additions & 3 deletions file/fileinfo_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
// specific language governing permissions and limitations
// under the License.

//go:build !windows
//go:build unix

package file

import (
"errors"
"os"
"syscall"

"golang.org/x/sys/unix"
)

func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) {
Expand All @@ -35,7 +36,7 @@ func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInf
}

func wrap(info os.FileInfo) (FileInfo, error) {
stat, ok := info.Sys().(*syscall.Stat_t)
stat, ok := info.Sys().(*unix.Stat_t)
if !ok {
return nil, errors.New("failed to get uid/gid")
}
Expand Down
33 changes: 33 additions & 0 deletions file/stderr_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

//go:build plan9

package file

import (
"os"

"golang.org/x/sys/plan9"
)

// RedirectStandardError causes all standard error output to be directed to the
// given file.
func RedirectStandardError(toFile *os.File) error {
_, err := plan9.Dup(int(toFile.Fd()), 2)
return err
}
2 changes: 1 addition & 1 deletion file/stderr_other.go → file/stderr_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

//go:build !windows
//go:build unix

package file

Expand Down

0 comments on commit b4f6f1b

Please sign in to comment.