Add symlinks package with Resolve function

This change adds a symlinks.Resolve function for resolving symlinks and
updates usages across the code to make use of it.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-05-11 14:04:16 +02:00
parent e7d2a9c212
commit 927ec78b6e
4 changed files with 45 additions and 27 deletions

View File

@ -24,6 +24,7 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -125,21 +126,18 @@ func (m command) run(c *cli.Context, cfg *config) error {
created := make(map[string]bool) created := make(map[string]bool)
// candidates is a list of absolute paths to symlinks in a chain, or the final target of the chain. // candidates is a list of absolute paths to symlinks in a chain, or the final target of the chain.
for _, candidate := range candidates { for _, candidate := range candidates {
targets, err := m.Locate(candidate) target, err := symlinks.Resolve(candidate)
if err != nil { if err != nil {
m.logger.Debugf("Skipping invalid link: %v", err) m.logger.Debugf("Skipping invalid link: %v", err)
continue continue
} else if len(targets) != 1 { } else if target == candidate {
m.logger.Debugf("Unexepected number of targets: %v", targets)
continue
} else if targets[0] == candidate {
m.logger.Debugf("%v is not a symlink", candidate) m.logger.Debugf("%v is not a symlink", candidate)
continue continue
} }
err = m.createLink(created, cfg.hostRoot, containerRoot, targets[0], candidate) err = m.createLink(created, cfg.hostRoot, containerRoot, target, candidate)
if err != nil { if err != nil {
m.logger.Warnf("Failed to create link %v: %v", []string{targets[0], candidate}, err) m.logger.Warnf("Failed to create link %v: %v", []string{target, candidate}, err)
} }
} }

View File

@ -29,6 +29,7 @@ import (
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -288,16 +289,7 @@ func (c *ldcache) resolve(target string) (string, error) {
c.logger.Debugf("checking %v", string(name)) c.logger.Debugf("checking %v", string(name))
info, err := os.Lstat(name) link, err := symlinks.Resolve(name)
if err != nil {
return "", fmt.Errorf("failed to get file info: %v", info)
}
if info.Mode()&os.ModeSymlink == 0 {
c.logger.Debugf("Resolved regular file: %v", name)
return name, nil
}
link, err := os.Readlink(name)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to resolve symlink: %v", err) return "", fmt.Errorf("failed to resolve symlink: %v", err)
} }

View File

@ -18,9 +18,9 @@ package lookup
import ( import (
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -74,16 +74,9 @@ func (p symlinkChain) Locate(pattern string) ([]string, error) {
} }
found[candidate] = true found[candidate] = true
info, err := os.Lstat(candidate) target, err := symlinks.Resolve(candidate)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get file info: %v", info) return nil, fmt.Errorf("error resolving symlink: %v", err)
}
if info.Mode()&os.ModeSymlink == 0 {
continue
}
target, err := os.Readlink(candidate)
if err != nil {
return nil, fmt.Errorf("error checking symlink: %v", err)
} }
if !filepath.IsAbs(target) { if !filepath.IsAbs(target) {

View File

@ -0,0 +1,35 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/
package symlinks
import (
"fmt"
"os"
)
// Resolve returns the link target of the specified filename or the filename if it is not a link.
func Resolve(filename string) (string, error) {
info, err := os.Lstat(filename)
if err != nil {
return filename, fmt.Errorf("failed to get file info: %v", info)
}
if info.Mode()&os.ModeSymlink == 0 {
return filename, nil
}
return os.Readlink(filename)
}