1
0
Fork 0
mirror of https://github.com/vbatts/dedupe-linker.git synced 2024-12-27 00:56:31 +00:00

base: golint

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-09-13 10:27:38 -04:00
parent 7e2999aa4e
commit 267c961503
2 changed files with 16 additions and 14 deletions

View file

@ -14,6 +14,7 @@ import (
"github.com/vbatts/dedupe-linker/file" "github.com/vbatts/dedupe-linker/file"
) )
// NewBase populates the directories needed in a dedupe-base directory
func NewBase(path string, hashName string) (*Base, error) { func NewBase(path string, hashName string) (*Base, error) {
for _, p := range []string{"blobs/" + hashName, "state", "tmp"} { for _, p := range []string{"blobs/" + hashName, "state", "tmp"} {
if err := os.MkdirAll(filepath.Join(path, p), 0755); err != nil && !os.IsExist(err) { 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 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 { type Base struct {
Path string Path string
HashName string HashName string
Hash crypto.Hash Hash crypto.Hash
} }
// Stat provides the os.FileInfo for the object of `sum` address
func (b Base) Stat(sum string) (os.FileInfo, error) { func (b Base) Stat(sum string) (os.FileInfo, error) {
return os.Stat(b.blobPath(sum)) 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) 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.Reader
io.Seeker io.Seeker
io.Closer 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 { func (b Base) SameFile(sum, path string) bool {
var ( var (
bInode, dInode uint64 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 // 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)) 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") 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 { func (b Base) LinkFrom(src, sum string) error {
if err := os.MkdirAll(filepath.Dir(b.blobPath(sum)), 0756); err != nil && !os.IsExist(err) { if err := os.MkdirAll(filepath.Dir(b.blobPath(sum)), 0756); err != nil && !os.IsExist(err) {
return err return err
@ -134,7 +141,8 @@ func randomString() (string, error) {
return fmt.Sprintf("%x", buf), nil 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 { func SafeLink(oldname, newname string) error {
var backupName string var backupName string
// check if newname exists // check if newname exists
@ -168,7 +176,7 @@ func SafeLink(oldname, newname string) error {
return nil 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 { func (b Base) LinkTo(dest, sum string) error {
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil && !os.IsExist(err) { if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil && !os.IsExist(err) {
return err return err

View file

@ -5,6 +5,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
"github.com/vbatts/dedupe-linker/file"
) )
// FindBase steps up the directory tree to find the top-level that is still on // 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 { func sameDevice(file1, file2 os.FileInfo) bool {
sys1 := file1.Sys().(*syscall.Stat_t) sys1 := file1.Sys().(*syscall.Stat_t)
sys2 := file2.Sys().(*syscall.Stat_t) sys2 := file2.Sys().(*syscall.Stat_t)
return ((major(sys1.Dev) == major(sys2.Dev)) && (minor(sys1.Dev) == minor(sys2.Dev))) return ((file.MajorDev(sys1.Dev) == file.MajorDev(sys2.Dev)) && (file.MinorDev(sys1.Dev) == file.MinorDev(sys2.Dev)))
}
func major(n uint64) uint64 {
return uint64(n / 256)
}
func minor(n uint64) uint64 {
return uint64(n % 256)
} }