github.com/containers/podman/v4@v4.9.4/libpod/diff.go (about)

     1  //go:build !remote
     2  // +build !remote
     3  
     4  package libpod
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/containers/podman/v4/libpod/define"
    10  	"github.com/containers/podman/v4/libpod/layers"
    11  	"github.com/containers/storage/pkg/archive"
    12  )
    13  
    14  var initInodes = map[string]bool{
    15  	"/dev":                   true,
    16  	"/etc/hostname":          true,
    17  	"/etc/hosts":             true,
    18  	"/etc/resolv.conf":       true,
    19  	"/proc":                  true,
    20  	"/run":                   true,
    21  	"/run/notify":            true,
    22  	"/run/.containerenv":     true,
    23  	"/run/secrets":           true,
    24  	define.ContainerInitPath: true,
    25  	"/sys":                   true,
    26  	"/etc/mtab":              true,
    27  }
    28  
    29  // GetDiff returns the differences between the two images, layers, or containers
    30  func (r *Runtime) GetDiff(from, to string, diffType define.DiffType) ([]archive.Change, error) {
    31  	toLayer, err := r.getLayerID(to, diffType)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	fromLayer := ""
    36  	if from != "" {
    37  		fromLayer, err = r.getLayerID(from, diffType)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  	}
    42  	var rchanges []archive.Change
    43  	changes, err := r.store.Changes(fromLayer, toLayer)
    44  	if err == nil {
    45  		for _, c := range changes {
    46  			if initInodes[c.Path] {
    47  				continue
    48  			}
    49  			rchanges = append(rchanges, c)
    50  		}
    51  	}
    52  	return rchanges, err
    53  }
    54  
    55  // GetLayerID gets a full layer id given a full or partial id
    56  // If the id matches a container or image, the id of the top layer is returned
    57  // If the id matches a layer, the top layer id is returned
    58  func (r *Runtime) getLayerID(id string, diffType define.DiffType) (string, error) {
    59  	var lastErr error
    60  	if diffType&define.DiffImage == define.DiffImage {
    61  		toImage, _, err := r.libimageRuntime.LookupImage(id, nil)
    62  		if err == nil {
    63  			return toImage.TopLayer(), nil
    64  		}
    65  		lastErr = err
    66  	}
    67  
    68  	if diffType&define.DiffContainer == define.DiffContainer {
    69  		toCtr, err := r.store.Container(id)
    70  		if err == nil {
    71  			return toCtr.LayerID, nil
    72  		}
    73  		lastErr = err
    74  	}
    75  
    76  	if diffType == define.DiffAll {
    77  		toLayer, err := layers.FullID(r.store, id)
    78  		if err == nil {
    79  			return toLayer, nil
    80  		}
    81  		lastErr = err
    82  	}
    83  	return "", fmt.Errorf("%s not found: %w", id, lastErr)
    84  }