github.com/containerd/nerdctl@v1.7.7/pkg/tarutil/tarutil.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tarutil 18 19 import ( 20 "fmt" 21 "os" 22 "os/exec" 23 "strings" 24 25 "github.com/containerd/log" 26 ) 27 28 // FindTarBinary returns a path to the tar binary and whether it is GNU tar. 29 func FindTarBinary() (string, bool, error) { 30 isGNU := func(exe string) bool { 31 v, err := exec.Command(exe, "--version").Output() 32 if err != nil { 33 log.L.Warnf("Failed to detect whether %q is GNU tar or not", exe) 34 return false 35 } 36 if !strings.Contains(string(v), "GNU tar") { 37 log.L.Warnf("%q does not seem GNU tar", exe) 38 return false 39 } 40 return true 41 } 42 if v := os.Getenv("TAR"); v != "" { 43 if exe, err := exec.LookPath(v); err == nil { 44 return exe, isGNU(exe), nil 45 } 46 } 47 if exe, err := exec.LookPath("gnutar"); err == nil { 48 return exe, true, nil 49 } 50 if exe, err := exec.LookPath("gtar"); err == nil { 51 return exe, true, nil 52 } 53 if exe, err := exec.LookPath("tar"); err == nil { 54 return exe, isGNU(exe), nil 55 } 56 return "", false, fmt.Errorf("failed to find `tar` binary") 57 }