11package util
22
33import (
4- "bytes"
5- "errors"
64 "fmt"
75 "os"
8- "os/exec"
96 "path/filepath"
107 "regexp"
11- "strings"
128 "time"
139
14- "github.com/containers/common/libnetwork/types"
1510 "github.com/fsnotify/fsnotify"
1611 "github.com/sirupsen/logrus"
1712)
1813
19- const (
20- UnknownPackage = "Unknown"
21- )
22-
23- var ErrInterrupt = errors .New ("interrupted" )
24-
25- // Note: This function is copied from containers/podman libpod/util.go
26- // Please see https://github.com/containers/common/pull/1460
27- func queryPackageVersion (cmdArg ... string ) string {
28- output := UnknownPackage
29- if 1 < len (cmdArg ) {
30- cmd := exec .Command (cmdArg [0 ], cmdArg [1 :]... )
31- if outp , err := cmd .Output (); err == nil {
32- output = string (outp )
33- deb := false
34- if cmdArg [0 ] == "/usr/bin/dlocate" {
35- // can return multiple matches
36- l := strings .Split (output , "\n " )
37- output = l [0 ]
38- deb = true
39- } else if cmdArg [0 ] == "/usr/bin/dpkg" {
40- deb = true
41- }
42- if deb {
43- r := strings .Split (output , ": " )
44- queryFormat := `${Package}_${Version}_${Architecture}`
45- cmd = exec .Command ("/usr/bin/dpkg-query" , "-f" , queryFormat , "-W" , r [0 ])
46- if outp , err := cmd .Output (); err == nil {
47- output = string (outp )
48- }
49- }
50- }
51- if cmdArg [0 ] == "/sbin/apk" {
52- prefix := cmdArg [len (cmdArg )- 1 ] + " is owned by "
53- output = strings .Replace (output , prefix , "" , 1 )
54- }
55- }
56- return strings .Trim (output , "\n " )
57- }
58-
59- // Note: This function is copied from containers/podman libpod/util.go
60- // Please see https://github.com/containers/common/pull/1460
61- func PackageVersion (program string ) string { // program is full path
62- _ , err := os .Stat (program )
63- if err != nil {
64- return UnknownPackage
65- }
66- packagers := [][]string {
67- {"/usr/bin/rpm" , "-q" , "-f" },
68- {"/usr/bin/dlocate" , "-F" }, // Debian, Ubuntu (quick)
69- {"/usr/bin/dpkg" , "-S" }, // Debian, Ubuntu (slow)
70- {"/usr/bin/pacman" , "-Qo" }, // Arch
71- {"/usr/bin/qfile" , "-qv" }, // Gentoo (quick)
72- {"/usr/bin/equery" , "b" }, // Gentoo (slow)
73- {"/sbin/apk" , "info" , "-W" }, // Alpine
74- {"/usr/local/sbin/pkg" , "which" , "-q" }, // FreeBSD
75- }
76-
77- for _ , cmd := range packagers {
78- cmd = append (cmd , program )
79- if out := queryPackageVersion (cmd ... ); out != UnknownPackage {
80- return out
81- }
82- }
83- return UnknownPackage
84- }
85-
86- // Note: This function is copied from containers/podman libpod/util.go
87- // Please see https://github.com/containers/common/pull/1460
88- func ProgramVersion (program string ) (string , error ) {
89- return programVersion (program , false )
90- }
91-
92- func ProgramVersionDnsname (program string ) (string , error ) {
93- return programVersion (program , true )
94- }
95-
96- func programVersion (program string , dnsname bool ) (string , error ) {
97- cmd := exec .Command (program , "--version" )
98- var stdout bytes.Buffer
99- var stderr bytes.Buffer
100- cmd .Stdout = & stdout
101- cmd .Stderr = & stderr
102-
103- err := cmd .Run ()
104- if err != nil {
105- return "" , fmt .Errorf ("`%v --version` failed: %v %v (%v)" , program , stderr .String (), stdout .String (), err )
106- }
107-
108- output := strings .TrimSuffix (stdout .String (), "\n " )
109- // dnsname --version returns the information to stderr
110- if dnsname {
111- output = strings .TrimSuffix (stderr .String (), "\n " )
112- }
113-
114- return output , nil
115- }
116-
11714// StringInSlice determines if a string is in a string slice, returns bool
11815func StringInSlice (s string , sl []string ) bool {
11916 for _ , i := range sl {
@@ -135,25 +32,6 @@ func StringMatchRegexSlice(s string, re []string) bool {
13532 return false
13633}
13734
138- // FilterID is a function used to compare an id against a set of ids, if the
139- // input is hex we check if the prefix matches. Otherwise we assume it is a
140- // regex and try to match that.
141- // see https://github.com/containers/podman/issues/18471 for why we do this
142- func FilterID (id string , filters []string ) bool {
143- for _ , want := range filters {
144- isRegex := types .NotHexRegex .MatchString (want )
145- if isRegex {
146- match , err := regexp .MatchString (want , id )
147- if err == nil && match {
148- return true
149- }
150- } else if strings .HasPrefix (id , strings .ToLower (want )) {
151- return true
152- }
153- }
154- return false
155- }
156-
15735// WaitForFile waits until a file has been created or the given timeout has occurred
15836func WaitForFile (path string , chWait chan error , timeout time.Duration ) (bool , error ) {
15937 var inotifyEvents chan fsnotify.Event
0 commit comments