mirror of
https://github.com/containers/podman.git
synced 2025-10-18 19:53:58 +08:00

- Update github.com/containers/common to v0.64.1-0.20250806164630-57def9601f3b - Update github.com/spf13/pflag to v1.0.7 - Update github.com/seccomp/libseccomp-golang to v0.11.1 Signed-off-by: Joshua Arrevillaga <2004jarrevillaga@gmail.com>
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"time"
|
|
|
|
"github.com/containers/storage/pkg/fileutils"
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// StringMatchRegexSlice determines if a given string matches one of the given regexes, returns bool
|
|
func StringMatchRegexSlice(s string, re []string) bool {
|
|
for _, r := range re {
|
|
m, err := regexp.MatchString(r, s)
|
|
if err == nil && m {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// WaitForFile waits until a file has been created or the given timeout has occurred
|
|
func WaitForFile(path string, chWait chan error, timeout time.Duration) (bool, error) {
|
|
var inotifyEvents chan fsnotify.Event
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err == nil {
|
|
if err := watcher.Add(filepath.Dir(path)); err == nil {
|
|
inotifyEvents = watcher.Events
|
|
}
|
|
defer func() {
|
|
if err := watcher.Close(); err != nil {
|
|
logrus.Errorf("Failed to close fsnotify watcher: %v", err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
var timeoutChan <-chan time.Time
|
|
|
|
if timeout != 0 {
|
|
timeoutChan = time.After(timeout)
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case e := <-chWait:
|
|
return true, e
|
|
case <-inotifyEvents:
|
|
err := fileutils.Exists(path)
|
|
if err == nil {
|
|
return false, nil
|
|
}
|
|
if !os.IsNotExist(err) {
|
|
return false, err
|
|
}
|
|
case <-time.After(25 * time.Millisecond):
|
|
// Check periodically for the file existence. It is needed
|
|
// if the inotify watcher could not have been created. It is
|
|
// also useful when using inotify as if for any reasons we missed
|
|
// a notification, we won't hang the process.
|
|
err := fileutils.Exists(path)
|
|
if err == nil {
|
|
return false, nil
|
|
}
|
|
if !os.IsNotExist(err) {
|
|
return false, err
|
|
}
|
|
case <-timeoutChan:
|
|
return false, fmt.Errorf("timed out waiting for file %s", path)
|
|
}
|
|
}
|
|
}
|