From b4f6f1b1da5bd94fdbf3af333cf388d36edd0e5b Mon Sep 17 00:00:00 2001 From: kruskal <99559985+kruskall@users.noreply.github.com> Date: Wed, 28 Aug 2024 03:32:13 +0200 Subject: [PATCH 1/2] refactor: update unix build tags and add plan9 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. --- file/fileinfo_plan9.go | 48 ++++++++++++++++++++++++ file/fileinfo_unix.go | 7 ++-- file/stderr_plan9.go | 33 ++++++++++++++++ file/{stderr_other.go => stderr_unix.go} | 2 +- 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 file/fileinfo_plan9.go create mode 100644 file/stderr_plan9.go rename file/{stderr_other.go => stderr_unix.go} (98%) diff --git a/file/fileinfo_plan9.go b/file/fileinfo_plan9.go new file mode 100644 index 00000000..59aa7591 --- /dev/null +++ b/file/fileinfo_plan9.go @@ -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 +} diff --git a/file/fileinfo_unix.go b/file/fileinfo_unix.go index b1c98b8d..15c52db0 100644 --- a/file/fileinfo_unix.go +++ b/file/fileinfo_unix.go @@ -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) { @@ -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") } diff --git a/file/stderr_plan9.go b/file/stderr_plan9.go new file mode 100644 index 00000000..f3a97ccf --- /dev/null +++ b/file/stderr_plan9.go @@ -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 +} diff --git a/file/stderr_other.go b/file/stderr_unix.go similarity index 98% rename from file/stderr_other.go rename to file/stderr_unix.go index 5af8611c..463b31ac 100644 --- a/file/stderr_other.go +++ b/file/stderr_unix.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -//go:build !windows +//go:build unix package file From 86c1607bb442e4d56fd8ff7295f58bd868e5e089 Mon Sep 17 00:00:00 2001 From: kruskal <99559985+kruskall@users.noreply.github.com> Date: Wed, 28 Aug 2024 03:47:52 +0200 Subject: [PATCH 2/2] refactor: use syscall type --- file/fileinfo_unix.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/file/fileinfo_unix.go b/file/fileinfo_unix.go index 15c52db0..355bed91 100644 --- a/file/fileinfo_unix.go +++ b/file/fileinfo_unix.go @@ -22,8 +22,7 @@ package file import ( "errors" "os" - - "golang.org/x/sys/unix" + "syscall" ) func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInfo, error) { @@ -36,7 +35,7 @@ func stat(name string, statFunc func(name string) (os.FileInfo, error)) (FileInf } func wrap(info os.FileInfo) (FileInfo, error) { - stat, ok := info.Sys().(*unix.Stat_t) + stat, ok := info.Sys().(*syscall.Stat_t) if !ok { return nil, errors.New("failed to get uid/gid") }