From 999dd7ae34c28634b7e35c55da450025d196ff9a Mon Sep 17 00:00:00 2001 From: kruskall <99559985+kruskall@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:52:49 +0200 Subject: [PATCH] feat: add support for all upstream dist to file package (#226) use specific function for unix goos and fallback for non-unix systems --- file/fileinfo.go | 9 ++++ ...{fileinfo_windows.go => fileinfo_other.go} | 11 +---- file/fileinfo_plan9.go | 48 ------------------- file/fileinfo_unix.go | 9 ---- 4 files changed, 11 insertions(+), 66 deletions(-) rename file/{fileinfo_windows.go => fileinfo_other.go} (83%) delete mode 100644 file/fileinfo_plan9.go diff --git a/file/fileinfo.go b/file/fileinfo.go index c1a8ce60..50332f7a 100644 --- a/file/fileinfo.go +++ b/file/fileinfo.go @@ -43,6 +43,15 @@ func Lstat(name string) (FileInfo, error) { return stat(name, os.Lstat) } +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) +} + // Wrap wraps the given os.FileInfo and returns a FileInfo in order to expose // the UID and GID in a uniform manner across operating systems. func Wrap(info os.FileInfo) (FileInfo, error) { diff --git a/file/fileinfo_windows.go b/file/fileinfo_other.go similarity index 83% rename from file/fileinfo_windows.go rename to file/fileinfo_other.go index 855eb704..8e3771d9 100644 --- a/file/fileinfo_windows.go +++ b/file/fileinfo_other.go @@ -15,21 +15,14 @@ // specific language governing permissions and limitations // under the License. +//go:build !unix + package file import ( "os" ) -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) { return fileInfo{FileInfo: info}, nil } diff --git a/file/fileinfo_plan9.go b/file/fileinfo_plan9.go deleted file mode 100644 index 59aa7591..00000000 --- a/file/fileinfo_plan9.go +++ /dev/null @@ -1,48 +0,0 @@ -// 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 355bed91..2311ff27 100644 --- a/file/fileinfo_unix.go +++ b/file/fileinfo_unix.go @@ -25,15 +25,6 @@ import ( "syscall" ) -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().(*syscall.Stat_t) if !ok {