2014-09-12 20:10:10 +00:00
|
|
|
package base
|
|
|
|
|
|
|
|
import (
|
2014-10-14 20:54:28 +00:00
|
|
|
"crypto"
|
|
|
|
"fmt"
|
2014-09-16 21:12:52 +00:00
|
|
|
"io"
|
2014-10-14 20:54:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2014-09-12 20:10:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-10-14 20:54:28 +00:00
|
|
|
|
2016-07-18 15:23:33 +00:00
|
|
|
"github.com/vbatts/dedupe-linker/cryptomap"
|
|
|
|
"github.com/vbatts/dedupe-linker/file"
|
2014-09-12 20:10:10 +00:00
|
|
|
)
|
|
|
|
|
2014-09-16 21:12:52 +00:00
|
|
|
func NewBase(path string, hashName string) (*Base, error) {
|
2014-09-17 13:21:56 +00:00
|
|
|
root := filepath.Join(path, "dedup")
|
2014-10-14 20:54:28 +00:00
|
|
|
for _, p := range []string{"blobs/" + hashName, "state", "tmp"} {
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, p), 0755); err != nil && !os.IsExist(err) {
|
2014-09-16 21:12:52 +00:00
|
|
|
return nil, err
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 15:23:33 +00:00
|
|
|
return &Base{Path: root, HashName: hashName, Hash: cryptomap.DetermineHash(hashName)}, nil
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Base struct {
|
2014-09-16 21:12:52 +00:00
|
|
|
Path string
|
|
|
|
HashName string
|
2014-10-14 20:54:28 +00:00
|
|
|
Hash crypto.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Base) Stat(sum string) (os.FileInfo, error) {
|
|
|
|
return os.Stat(b.blobPath(sum))
|
2014-09-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-17 13:21:56 +00:00
|
|
|
func (b Base) blobPath(sum string) string {
|
|
|
|
if len(sum) < 3 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return filepath.Join(b.Path, "blobs", b.HashName, sum[0:2], sum)
|
|
|
|
}
|
|
|
|
|
2014-10-14 20:54:28 +00:00
|
|
|
type ReaderSeekerCloser interface {
|
|
|
|
io.Reader
|
|
|
|
io.Seeker
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Base) SameFile(sum, path string) bool {
|
|
|
|
var (
|
|
|
|
bInode, dInode uint64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if bInode, err = file.GetInode(b.blobPath(sum)); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if dInode, err = file.GetInode(path); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bInode == dInode {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:12:52 +00:00
|
|
|
// GetBlob store the content from src, for the sum and hashType
|
2014-10-14 20:54:28 +00:00
|
|
|
func (b Base) GetBlob(sum string) (ReaderSeekerCloser, error) {
|
|
|
|
return os.Open(b.blobPath(sum))
|
2014-09-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutBlob store the content from src, for the sum and hashType
|
|
|
|
//
|
|
|
|
// we take the sum up front to avoid recalculation and tempfiles
|
2014-10-14 20:54:28 +00:00
|
|
|
func (b Base) PutBlob(src io.Reader, mode os.FileMode) (string, error) {
|
|
|
|
fh, err := b.tmpFile()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
fh.Close()
|
|
|
|
os.Remove(fh.Name())
|
|
|
|
}()
|
|
|
|
|
|
|
|
h := b.Hash.New()
|
|
|
|
t := io.TeeReader(src, h)
|
|
|
|
|
|
|
|
if _, err = io.Copy(fh, t); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
fi, err := b.Stat(sum)
|
|
|
|
if err == nil && fi.Mode().IsRegular() {
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(b.blobPath(sum)), 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return sum, err
|
|
|
|
}
|
|
|
|
destFh, err := os.Create(b.blobPath(sum))
|
|
|
|
if err != nil {
|
|
|
|
return sum, err
|
|
|
|
}
|
|
|
|
defer destFh.Close()
|
|
|
|
_, err = fh.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return sum, err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(destFh, fh); err != nil {
|
|
|
|
return sum, err
|
|
|
|
}
|
|
|
|
return sum, destFh.Chmod(mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b Base) tmpFile() (*os.File, error) {
|
|
|
|
return ioutil.TempFile(filepath.Join(b.Path, "tmp"), "put")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hard link the file from src to the blob for sum
|
|
|
|
func (b Base) LinkFrom(src, sum string) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(b.blobPath(sum)), 0756); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Link(src, b.blobPath(sum))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hard link the file for sum to the path at dest
|
|
|
|
func (b Base) LinkTo(dest, sum string) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := os.Link(b.blobPath(sum), dest)
|
|
|
|
if err != nil && os.IsExist(err) {
|
|
|
|
if !b.SameFile(sum, dest) {
|
|
|
|
// XXX
|
|
|
|
log.Printf("Would clobber %q with %q", dest, b.blobPath(sum))
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-16 21:12:52 +00:00
|
|
|
return nil
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 21:12:52 +00:00
|
|
|
// HasBlob tests whether a blob with this sum exists
|
|
|
|
func (b Base) HasBlob(sum string) bool {
|
2014-10-14 20:54:28 +00:00
|
|
|
fi, err := b.Stat(sum)
|
|
|
|
log.Println("SUCH FARTS", fi)
|
|
|
|
return fi != nil && err == nil
|
2014-09-12 20:10:10 +00:00
|
|
|
}
|