github.com/singularityware/singularity@v3.1.1+incompatible/internal/pkg/client/cache/library.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package cache
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/sylabs/singularity/internal/pkg/sylog"
    13  	client "github.com/sylabs/singularity/pkg/client/library"
    14  )
    15  
    16  const (
    17  	// LibraryDir is the directory inside the cache.Dir where library images are cached
    18  	LibraryDir = "library"
    19  )
    20  
    21  // Library returns the directory inside the cache.Dir() where library images are cached
    22  func Library() string {
    23  	return updateCacheSubdir(LibraryDir)
    24  }
    25  
    26  // LibraryImage creates a directory inside cache.Dir() with the name of the SHA sum of the image
    27  func LibraryImage(sum, name string) string {
    28  	updateCacheSubdir(filepath.Join(LibraryDir, sum))
    29  
    30  	return filepath.Join(Library(), sum, name)
    31  }
    32  
    33  // LibraryImageExists returns whether the image with the SHA sum exists in the LibraryImage cache
    34  func LibraryImageExists(sum, name string) (bool, error) {
    35  	imagePath := LibraryImage(sum, name)
    36  	_, err := os.Stat(imagePath)
    37  	if os.IsNotExist(err) {
    38  		return false, nil
    39  	} else if err != nil {
    40  		return false, err
    41  	}
    42  
    43  	cacheSum, err := client.ImageHash(imagePath)
    44  	if err != nil {
    45  		return false, err
    46  	}
    47  	if cacheSum != sum {
    48  		sylog.Debugf("Cached File Sum(%s) and Expected Sum(%s) does not match", cacheSum, sum)
    49  		return false, nil
    50  	}
    51  
    52  	return true, nil
    53  }