mirror of
https://github.com/containers/podman.git
synced 2025-07-18 01:57:24 +08:00
update c/common to latest
includes the slirp4netns package Signed-off-by: Paul Holzinger <pholzing@redhat.com>
This commit is contained in:
56
vendor/github.com/containers/common/pkg/util/util.go
generated
vendored
56
vendor/github.com/containers/common/pkg/util/util.go
generated
vendored
@ -3,11 +3,16 @@ package util
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/containers/common/libnetwork/types"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -131,3 +136,54 @@ func FilterID(id string, filters []string) bool {
|
||||
}
|
||||
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 := os.Stat(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 := os.Stat(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user