github.com/OpenFlowLabs/moby@v17.12.1-ce-rc2+incompatible/daemon/image.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/docker/distribution/reference"
     8  	"github.com/docker/docker/image"
     9  )
    10  
    11  // errImageDoesNotExist is error returned when no image can be found for a reference.
    12  type errImageDoesNotExist struct {
    13  	ref reference.Reference
    14  }
    15  
    16  func (e errImageDoesNotExist) Error() string {
    17  	ref := e.ref
    18  	if named, ok := ref.(reference.Named); ok {
    19  		ref = reference.TagNameOnly(named)
    20  	}
    21  	return fmt.Sprintf("No such image: %s", reference.FamiliarString(ref))
    22  }
    23  
    24  func (e errImageDoesNotExist) NotFound() {}
    25  
    26  // GetImageIDAndOS returns an image ID and operating system corresponding to the image referred to by
    27  // refOrID.
    28  func (daemon *Daemon) GetImageIDAndOS(refOrID string) (image.ID, string, error) {
    29  	ref, err := reference.ParseAnyReference(refOrID)
    30  	if err != nil {
    31  		return "", "", validationError{err}
    32  	}
    33  	namedRef, ok := ref.(reference.Named)
    34  	if !ok {
    35  		digested, ok := ref.(reference.Digested)
    36  		if !ok {
    37  			return "", "", errImageDoesNotExist{ref}
    38  		}
    39  		id := image.IDFromDigest(digested.Digest())
    40  		for platform := range daemon.stores {
    41  			if _, err = daemon.stores[platform].imageStore.Get(id); err == nil {
    42  				return id, platform, nil
    43  			}
    44  		}
    45  		return "", "", errImageDoesNotExist{ref}
    46  	}
    47  
    48  	if digest, err := daemon.referenceStore.Get(namedRef); err == nil {
    49  		// Search the image stores to get the operating system, defaulting to host OS.
    50  		imageOS := runtime.GOOS
    51  		id := image.IDFromDigest(digest)
    52  		for os := range daemon.stores {
    53  			if img, err := daemon.stores[os].imageStore.Get(id); err == nil {
    54  				imageOS = img.OperatingSystem()
    55  				break
    56  			}
    57  		}
    58  		return id, imageOS, nil
    59  	}
    60  
    61  	// Search based on ID
    62  	for os := range daemon.stores {
    63  		if id, err := daemon.stores[os].imageStore.Search(refOrID); err == nil {
    64  			return id, os, nil
    65  		}
    66  	}
    67  
    68  	return "", "", errImageDoesNotExist{ref}
    69  }
    70  
    71  // GetImage returns an image corresponding to the image referred to by refOrID.
    72  func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
    73  	imgID, os, err := daemon.GetImageIDAndOS(refOrID)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return daemon.stores[os].imageStore.Get(imgID)
    78  }