mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-01-22 18:47:32 +00:00
Allow globs in filenames for locators
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
e8843c38f2
commit
7f47a61986
@ -49,18 +49,19 @@ func NewExecutableLocator(logger *log.Logger, root string) Locator {
|
||||
|
||||
var _ Locator = (*executable)(nil)
|
||||
|
||||
// Locate finds executable files in the path. If a relative or absolute path is specified, the prefix paths are not considered.
|
||||
func (p executable) Locate(filename string) ([]string, error) {
|
||||
// Locate finds executable files with the specified pattern in the path.
|
||||
// If a relative or absolute path is specified, the prefix paths are not considered.
|
||||
func (p executable) Locate(pattern string) ([]string, error) {
|
||||
// For absolute paths we ensure that it is executable
|
||||
if strings.Contains(filename, "/") {
|
||||
err := assertExecutable(filename)
|
||||
if strings.Contains(pattern, "/") {
|
||||
err := assertExecutable(pattern)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("absolute path %v is not an executable file: %v", filename, err)
|
||||
return nil, fmt.Errorf("absolute path %v is not an executable file: %v", pattern, err)
|
||||
}
|
||||
return []string{filename}, nil
|
||||
return []string{pattern}, nil
|
||||
}
|
||||
|
||||
return p.file.Locate(filename)
|
||||
return p.file.Locate(pattern)
|
||||
}
|
||||
|
||||
// assertExecutable checks whether the specified path is an execuable file.
|
||||
|
@ -50,22 +50,29 @@ func newFileLocator(logger *log.Logger, root string) file {
|
||||
|
||||
var _ Locator = (*file)(nil)
|
||||
|
||||
// Locate attempts to find the specified file. All prefixes are searched and any matching
|
||||
// candidates are returned. If no matches are found, an error is returned.
|
||||
func (p file) Locate(filename string) ([]string, error) {
|
||||
// Locate attempts to find files with names matching the specified pattern.
|
||||
// All prefixes are searched and any matching candidates are returned. If no matches are found, an error is returned.
|
||||
func (p file) Locate(pattern string) ([]string, error) {
|
||||
var filenames []string
|
||||
for _, prefix := range p.prefixes {
|
||||
candidate := filepath.Join(prefix, filename)
|
||||
p.logger.Debugf("Checking candidate '%v'", candidate)
|
||||
err := p.filter(candidate)
|
||||
pathPattern := filepath.Join(prefix, pattern)
|
||||
candidates, err := filepath.Glob(pathPattern)
|
||||
if err != nil {
|
||||
p.logger.Debugf("Candidate '%v' does not meet requirements: %v", candidate, err)
|
||||
continue
|
||||
p.logger.Debugf("Checking pattern '%v' failed: %v", pathPattern, err)
|
||||
}
|
||||
|
||||
for _, candidate := range candidates {
|
||||
p.logger.Debugf("Checking candidate '%v'", candidate)
|
||||
err := p.filter(candidate)
|
||||
if err != nil {
|
||||
p.logger.Debugf("Candidate '%v' does not meet requirements: %v", candidate, err)
|
||||
continue
|
||||
}
|
||||
filenames = append(filenames, candidate)
|
||||
}
|
||||
filenames = append(filenames, candidate)
|
||||
}
|
||||
if len(filename) == 0 {
|
||||
return nil, fmt.Errorf("file %v not found", filename)
|
||||
if len(filenames) == 0 {
|
||||
return nil, fmt.Errorf("pattern %v not found", pattern)
|
||||
}
|
||||
return filenames, nil
|
||||
}
|
||||
|
@ -52,10 +52,10 @@ func NewSymlinkLocator(logger *logrus.Logger, root string) Locator {
|
||||
return &l
|
||||
}
|
||||
|
||||
// Locate finds the specified file at the specified root. If the file is a symlink, the link is followed and all candidates
|
||||
// to the final target are returned.
|
||||
func (p symlinkChain) Locate(filename string) ([]string, error) {
|
||||
candidates, err := p.file.Locate(filename)
|
||||
// Locate finds the specified pattern at the specified root.
|
||||
// If the file is a symlink, the link is followed and all candidates to the final target are returned.
|
||||
func (p symlinkChain) Locate(pattern string) ([]string, error) {
|
||||
candidates, err := p.file.Locate(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -104,14 +104,15 @@ func (p symlinkChain) Locate(filename string) ([]string, error) {
|
||||
return filenames, nil
|
||||
}
|
||||
|
||||
// Locate finds the specified file at the specified root. If the file is a symlink, the link is resolved and the target returned.
|
||||
func (p symlink) Locate(filename string) ([]string, error) {
|
||||
candidates, err := p.file.Locate(filename)
|
||||
// Locate finds the specified pattern at the specified root.
|
||||
// If the file is a symlink, the link is resolved and the target returned.
|
||||
func (p symlink) Locate(pattern string) ([]string, error) {
|
||||
candidates, err := p.file.Locate(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(candidates) != 1 {
|
||||
return nil, fmt.Errorf("failed to uniquely resolve symlink %v: %v", filename, candidates)
|
||||
return nil, fmt.Errorf("failed to uniquely resolve symlink %v: %v", pattern, candidates)
|
||||
}
|
||||
|
||||
target, err := filepath.EvalSymlinks(candidates[0])
|
||||
|
Loading…
Reference in New Issue
Block a user