@@ -22,15 +22,12 @@ import (
2222 "bytes"
2323 "encoding/binary"
2424 "errors"
25- "fmt"
2625 "os"
2726 "path/filepath"
28- "strings"
2927 "syscall"
3028 "unsafe"
3129
3230 "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
33- "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
3431)
3532
3633const ldcachePath = "/etc/ld.so.cache"
@@ -82,10 +79,9 @@ type entry2 struct {
8279
8380// LDCache represents the interface for performing lookups into the LDCache
8481//
85- //go:generate moq -out ldcache_mock.go . LDCache
82+ //go:generate moq -rm - out ldcache_mock.go . LDCache
8683type LDCache interface {
8784 List () ([]string , []string )
88- Lookup (... string ) ([]string , []string )
8985}
9086
9187type ldcache struct {
@@ -105,14 +101,7 @@ func New(logger logger.Interface, root string) (LDCache, error) {
105101
106102 logger .Debugf ("Opening ld.conf at %v" , path )
107103 f , err := os .Open (path )
108- if os .IsNotExist (err ) {
109- logger .Warningf ("Could not find ld.so.cache at %v; creating empty cache" , path )
110- e := & empty {
111- logger : logger ,
112- path : path ,
113- }
114- return e , nil
115- } else if err != nil {
104+ if err != nil {
116105 return nil , err
117106 }
118107 defer f .Close ()
@@ -196,7 +185,7 @@ type entry struct {
196185}
197186
198187// getEntries returns the entires of the ldcache in a go-friendly struct.
199- func (c * ldcache ) getEntries (selected func ( string ) bool ) []entry {
188+ func (c * ldcache ) getEntries () []entry {
200189 var entries []entry
201190 for _ , e := range c .entries {
202191 bits := 0
@@ -223,9 +212,6 @@ func (c *ldcache) getEntries(selected func(string) bool) []entry {
223212 c .logger .Debugf ("Skipping invalid lib" )
224213 continue
225214 }
226- if ! selected (lib ) {
227- continue
228- }
229215 value := bytesToString (c .libs [e .Value :])
230216 if value == "" {
231217 c .logger .Debugf ("Skipping invalid value for lib %v" , lib )
@@ -236,51 +222,19 @@ func (c *ldcache) getEntries(selected func(string) bool) []entry {
236222 bits : bits ,
237223 value : value ,
238224 }
239-
240225 entries = append (entries , e )
241226 }
242-
243227 return entries
244228}
245229
246230// List creates a list of libraries in the ldcache.
247231// The 32-bit and 64-bit libraries are returned separately.
248232func (c * ldcache ) List () ([]string , []string ) {
249- all := func (s string ) bool { return true }
250-
251- return c .resolveSelected (all )
252- }
253-
254- // Lookup searches the ldcache for the specified prefixes.
255- // The 32-bit and 64-bit libraries matching the prefixes are returned.
256- func (c * ldcache ) Lookup (libPrefixes ... string ) ([]string , []string ) {
257- c .logger .Debugf ("Looking up %v in cache" , libPrefixes )
258-
259- // We define a functor to check whether a given library name matches any of the prefixes
260- matchesAnyPrefix := func (s string ) bool {
261- for _ , p := range libPrefixes {
262- if strings .HasPrefix (s , p ) {
263- return true
264- }
265- }
266- return false
267- }
268-
269- return c .resolveSelected (matchesAnyPrefix )
270- }
271-
272- // resolveSelected process the entries in the LDCach based on the supplied filter and returns the resolved paths.
273- // The paths are separated by bittage.
274- func (c * ldcache ) resolveSelected (selected func (string ) bool ) ([]string , []string ) {
275233 paths := make (map [int ][]string )
276234 processed := make (map [string ]bool )
277235
278- for _ , e := range c .getEntries (selected ) {
279- path , err := c .resolve (e .value )
280- if err != nil {
281- c .logger .Debugf ("Could not resolve entry: %v" , err )
282- continue
283- }
236+ for _ , e := range c .getEntries () {
237+ path := filepath .Join (c .root , e .value )
284238 if processed [path ] {
285239 continue
286240 }
@@ -291,29 +245,6 @@ func (c *ldcache) resolveSelected(selected func(string) bool) ([]string, []strin
291245 return paths [32 ], paths [64 ]
292246}
293247
294- // resolve resolves the specified ldcache entry based on the value being processed.
295- // The input is the name of the entry in the cache.
296- func (c * ldcache ) resolve (target string ) (string , error ) {
297- name := filepath .Join (c .root , target )
298-
299- c .logger .Debugf ("checking %v" , name )
300-
301- link , err := symlinks .Resolve (name )
302- if err != nil {
303- return "" , fmt .Errorf ("failed to resolve symlink: %v" , err )
304- }
305- if link == name {
306- return name , nil
307- }
308-
309- // We return absolute paths for all targets
310- if ! filepath .IsAbs (link ) || strings .HasPrefix (link , "." ) {
311- link = filepath .Join (filepath .Dir (target ), link )
312- }
313-
314- return c .resolve (link )
315- }
316-
317248// bytesToString converts a byte slice to a string.
318249// This assumes that the byte slice is null-terminated
319250func bytesToString (value []byte ) string {
0 commit comments