...

Source file src/github.com/Microsoft/hcsshim/internal/guest/storage/utilities.go

Documentation: github.com/Microsoft/hcsshim/internal/guest/storage

     1  //go:build linux
     2  // +build linux
     3  
     4  package storage
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"path/filepath"
    10  	"time"
    11  
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // export this variable so it can be mocked to aid in testing for consuming packages
    16  var filepathglob = filepath.Glob
    17  
    18  // WaitForFileMatchingPattern waits for a single file that matches the given path pattern and returns the full path
    19  // to the resulting file
    20  func WaitForFileMatchingPattern(ctx context.Context, pattern string) (string, error) {
    21  	for {
    22  		files, err := filepathglob(pattern)
    23  		if err != nil {
    24  			return "", err
    25  		}
    26  		if len(files) == 0 {
    27  			select {
    28  			case <-ctx.Done():
    29  				return "", errors.Wrapf(ctx.Err(), "timed out waiting for file matching pattern %s to exist", pattern)
    30  			default:
    31  				time.Sleep(time.Millisecond * 10)
    32  				continue
    33  			}
    34  		} else if len(files) > 1 {
    35  			return "", fmt.Errorf("more than one file could exist for pattern \"%s\"", pattern)
    36  		}
    37  		return files[0], nil
    38  	}
    39  }
    40  

View as plain text