github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/libpod/diff.go (about)

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