greenhouse/diskcache/cache.go
func (c *Cache) Put(key string, content io.Reader, contentSHA256 string) error {
	// make sure directory exists
	path := c.KeyToPath(key)
	dir := filepath.Dir(path)
	err := ensureDir(dir)
	if err != nil {
		logrus.WithError(err).Errorf("error ensuring directory '%s' exists", dir)
	}

	// create a temp file to get the content on disk
	temp, err := ioutil.TempFile(dir, "temp-put")
	if err != nil {
		return fmt.Errorf("failed to create cache entry: %v", err)
	}

	// fast path copying when not hashing content,s
	if contentSHA256 == "" {
		_, err = io.Copy(temp, content)
		if err != nil {
			removeTemp(temp.Name())
			return fmt.Errorf("failed to copy into cache entry: %v", err)
		}

	} else {
		hasher := sha256.New()
		_, err = io.Copy(io.MultiWriter(temp, hasher), content)
		if err != nil {
			removeTemp(temp.Name())
			return fmt.Errorf("failed to copy into cache entry: %v", err)
		}
		actualContentSHA256 := hex.EncodeToString(hasher.Sum(nil))
		if actualContentSHA256 != contentSHA256 {
			removeTemp(temp.Name())
			return fmt.Errorf(
				"hashes did not match for '%s', given: '%s' actual: '%s",
				key, contentSHA256, actualContentSHA256)
		}
	}

	// move the content to the key location
	err = temp.Sync()
	if err != nil {
		removeTemp(temp.Name())
		return fmt.Errorf("failed to sync cache entry: %v", err)
	}
	temp.Close()
	err = os.Rename(temp.Name(), path)
	if err != nil {
		removeTemp(temp.Name())
		return fmt.Errorf("failed to insert contents into cache: %v", err)
	}
	return nil
}
func (c *Cache) Put(key string, content io.Reader, contentSHA256 string) error {
	// make sure directory exists
	path := c.KeyToPath(key)
	dir := filepath.Dir(path)
	err := ensureDir(dir)
	if err != nil {
		logrus.WithError(err).Errorf("error ensuring directory '%s' exists", dir)
	}

	// create a temp file to get the content on disk
	temp, err := ioutil.TempFile(dir, "temp-put")
	if err != nil {
		return fmt.Errorf("failed to create cache entry: %v", err)
	}

	// fast path copying when not hashing content,s
	if contentSHA256 == "" {
		_, err = io.Copy(temp, content)
		if err != nil {
			removeTemp(temp.Name())
			return fmt.Errorf("failed to copy into cache entry: %v", err)
		}

	} else {
		hasher := sha256.New()
		_, err = io.Copy(io.MultiWriter(temp, hasher), content)
		if err != nil {
			removeTemp(temp.Name())
			return fmt.Errorf("failed to copy into cache entry: %v", err)
		}
		actualContentSHA256 := hex.EncodeToString(hasher.Sum(nil))
		if actualContentSHA256 != contentSHA256 {
			removeTemp(temp.Name())
			return fmt.Errorf(
				"hashes did not match for '%s', given: '%s' actual: '%s",
				key, contentSHA256, actualContentSHA256)
		}
	}

	// move the content to the key location
	err = temp.Sync()
	if err != nil {
		removeTemp(temp.Name())
		return fmt.Errorf("failed to sync cache entry: %v", err)
	}
	temp.Close()
	err = os.Rename(temp.Name(), path)
	if err != nil {
		removeTemp(temp.Name())
		return fmt.Errorf("failed to insert contents into cache: %v", err)
	}
	return nil
}
func ( c * Cache ) Put ( key string , content io . Reader , contentSHA256 string ) error { // make sure directory exists path := c . KeyToPath ( key ) dir := filepath . Dir ( path ) err := ensureDir ( dir ) if err != nil { logrus . WithError ( err ) . Errorf ( " " , dir ) } // create a temp file to get the content on disk temp , err := ioutil . TempFile ( dir , " " ) if err != nil { return fmt . Errorf ( " " , err ) } // fast path copying when not hashing content,s if contentSHA256 == " " { _ , err = io . Copy ( temp , content ) if err != nil { removeTemp ( temp . Name ( ) ) return fmt . Errorf ( " " , err ) } } else { hasher := sha256 . New ( ) _ , err = io . Copy ( io . MultiWriter ( temp , hasher ) , content ) if err != nil { removeTemp ( temp . Name ( ) ) return fmt . Errorf ( " " , err ) } actualContentSHA256 := hex . EncodeToString ( hasher . Sum ( nil ) ) if actualContentSHA256 != contentSHA256 { removeTemp ( temp . Name ( ) ) return fmt . Errorf ( " " , key , contentSHA256 , actualContentSHA256 ) } } // move the content to the key location err = temp . Sync ( ) if err != nil { removeTemp ( temp . Name ( ) ) return fmt . Errorf ( " " , err ) } temp . Close ( ) err = os . Rename ( temp . Name ( ) , path ) if err != nil { removeTemp ( temp . Name ( ) ) return fmt . Errorf ( " " , err ) } return nil }
// Put copies the content reader until the end into the cache at key
// if contentSHA256 is not "" then the contents will only be stored in the
// cache if the content's hex string SHA256 matches
Put copies the content reader until the end into the cache at key if contentSHA256 is not then the contents will only be stored in the cache if the content s hex string SHA256 matches