(spec *MapSpec, opts MapOptions, c *btf.Cache)
| 392 | } |
| 393 | |
| 394 | func newMapWithOptions(spec *MapSpec, opts MapOptions, c *btf.Cache) (_ *Map, err error) { |
| 395 | closeOnError := func(c io.Closer) { |
| 396 | if err != nil { |
| 397 | c.Close() |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | switch spec.Pinning { |
| 402 | case PinByName: |
| 403 | if spec.Name == "" { |
| 404 | return nil, fmt.Errorf("pin by name: missing Name") |
| 405 | } |
| 406 | |
| 407 | if opts.PinPath == "" { |
| 408 | return nil, fmt.Errorf("pin by name: missing MapOptions.PinPath") |
| 409 | } |
| 410 | |
| 411 | path := filepath.Join(opts.PinPath, spec.Name) |
| 412 | m, err := LoadPinnedMap(path, &opts.LoadPinOptions) |
| 413 | if errors.Is(err, unix.ENOENT) { |
| 414 | break |
| 415 | } |
| 416 | if err != nil { |
| 417 | return nil, fmt.Errorf("load pinned map: %w", err) |
| 418 | } |
| 419 | defer closeOnError(m) |
| 420 | |
| 421 | if err := spec.Compatible(m); err != nil { |
| 422 | return nil, fmt.Errorf("use pinned map %s: %w", spec.Name, err) |
| 423 | } |
| 424 | |
| 425 | return m, nil |
| 426 | |
| 427 | case PinNone: |
| 428 | // Nothing to do here |
| 429 | |
| 430 | default: |
| 431 | return nil, fmt.Errorf("pin type %d: %w", int(spec.Pinning), ErrNotSupported) |
| 432 | } |
| 433 | |
| 434 | var innerFd *sys.FD |
| 435 | if spec.Type.canStoreMap() { |
| 436 | if spec.InnerMap == nil { |
| 437 | return nil, fmt.Errorf("%s requires InnerMap", spec.Type) |
| 438 | } |
| 439 | |
| 440 | if spec.InnerMap.Pinning != PinNone { |
| 441 | return nil, errors.New("inner maps cannot be pinned") |
| 442 | } |
| 443 | |
| 444 | template, err := spec.InnerMap.createMap(nil, c) |
| 445 | if err != nil { |
| 446 | return nil, fmt.Errorf("inner map: %w", err) |
| 447 | } |
| 448 | defer template.Close() |
| 449 | |
| 450 | // Intentionally skip populating and freezing (finalizing) |
| 451 | // the inner map template since it will be removed shortly. |
no test coverage detected
searching dependent graphs…