From 267c9615032c478c978b69496da9a2d95b7792e8 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 13 Sep 2016 10:27:38 -0400 Subject: [PATCH] base: golint Signed-off-by: Vincent Batts --- base/base.go | 18 +++++++++++++----- base/findbase.go | 12 +++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/base/base.go b/base/base.go index 88d92c3..3841e68 100644 --- a/base/base.go +++ b/base/base.go @@ -14,6 +14,7 @@ import ( "github.com/vbatts/dedupe-linker/file" ) +// NewBase populates the directories needed in a dedupe-base directory func NewBase(path string, hashName string) (*Base, error) { for _, p := range []string{"blobs/" + hashName, "state", "tmp"} { if err := os.MkdirAll(filepath.Join(path, p), 0755); err != nil && !os.IsExist(err) { @@ -23,12 +24,14 @@ func NewBase(path string, hashName string) (*Base, error) { return &Base{Path: path, HashName: hashName, Hash: cryptomap.DetermineHash(hashName)}, nil } +// Base is the destination for all hardlinks. Where stored objects are in a content addressible tree. type Base struct { Path string HashName string Hash crypto.Hash } +// Stat provides the os.FileInfo for the object of `sum` address func (b Base) Stat(sum string) (os.FileInfo, error) { return os.Stat(b.blobPath(sum)) } @@ -40,12 +43,15 @@ func (b Base) blobPath(sum string) string { return filepath.Join(b.Path, "blobs", b.HashName, sum[0:2], sum) } -type ReaderSeekerCloser interface { +// ReadSeekCloser is like an io.ReadCloser, but can Seek too +type ReadSeekCloser interface { io.Reader io.Seeker io.Closer } +// SameFile checks whether the object of `sum` address, and `path` file path are the same file. +// This checks by inode and device. func (b Base) SameFile(sum, path string) bool { var ( bInode, dInode uint64 @@ -65,7 +71,7 @@ func (b Base) SameFile(sum, path string) bool { } // GetBlob store the content from src, for the sum and hashType -func (b Base) GetBlob(sum string) (ReaderSeekerCloser, error) { +func (b Base) GetBlob(sum string) (ReadSeekCloser, error) { return os.Open(b.blobPath(sum)) } @@ -117,7 +123,8 @@ 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 +// LinkFrom make a hard link the file from src to the blob of address `sum`. +// TODO this function is going away, because it makes no assessment of the checksum of `src` 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 @@ -134,7 +141,8 @@ func randomString() (string, error) { return fmt.Sprintf("%x", buf), nil } -// SafeLink overrides newname if it already exists. If there is an error in creating the link, the transaction is rolled back +// SafeLink overrides newname if it already exists. If there is an error in +// creating the link, the transaction is rolled back func SafeLink(oldname, newname string) error { var backupName string // check if newname exists @@ -168,7 +176,7 @@ func SafeLink(oldname, newname string) error { return nil } -// Hard link the file for sum to the path at dest +// LinkTo makes a hard link the file of address `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 diff --git a/base/findbase.go b/base/findbase.go index d8bf3fe..414318d 100644 --- a/base/findbase.go +++ b/base/findbase.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "syscall" + + "github.com/vbatts/dedupe-linker/file" ) // FindBase steps up the directory tree to find the top-level that is still on @@ -55,13 +57,5 @@ func hasPermission(path string) bool { func sameDevice(file1, file2 os.FileInfo) bool { sys1 := file1.Sys().(*syscall.Stat_t) sys2 := file2.Sys().(*syscall.Stat_t) - return ((major(sys1.Dev) == major(sys2.Dev)) && (minor(sys1.Dev) == minor(sys2.Dev))) -} - -func major(n uint64) uint64 { - return uint64(n / 256) -} - -func minor(n uint64) uint64 { - return uint64(n % 256) + return ((file.MajorDev(sys1.Dev) == file.MajorDev(sys2.Dev)) && (file.MinorDev(sys1.Dev) == file.MinorDev(sys2.Dev))) }