github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/fs2objectcache/convert.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/sha512"
     5  	"io"
     6  	"os"
     7  	"path"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/fsutil"
    10  	"github.com/Cloud-Foundations/Dominator/lib/hash"
    11  	"github.com/Cloud-Foundations/Dominator/lib/objectcache"
    12  )
    13  
    14  func convertToObject(pathname, objectsDir string) error {
    15  	file, err := os.Open(pathname)
    16  	if err != nil {
    17  		return err
    18  	}
    19  	defer file.Close()
    20  	hasher := sha512.New()
    21  	_, err = io.Copy(hasher, file)
    22  	if err != nil {
    23  		return err
    24  	}
    25  	var hashVal hash.Hash
    26  	copy(hashVal[:], hasher.Sum(nil))
    27  	objPathname := path.Join(objectsDir, objectcache.HashToFilename(hashVal))
    28  	if err = os.MkdirAll(path.Dir(objPathname), 0755); err != nil {
    29  		return err
    30  	}
    31  	return fsutil.ForceRename(pathname, objPathname)
    32  }