git-lfs/localstorage/localstorage.go

50 lines
997 B
Go
Raw Normal View History

package localstorage
import (
2015-11-24 20:03:43 +00:00
"fmt"
"os"
"path/filepath"
"regexp"
)
const (
chanBufSize = 100
)
var (
2015-11-24 20:03:43 +00:00
oidRE = regexp.MustCompile(`\A[[:alnum:]]{64}`)
dirPerms os.FileMode = 0755
)
// LocalStorage manages the locally stored LFS objects for a repository.
type LocalStorage struct {
Root string
}
// Object represents a locally stored LFS object.
type Object struct {
Oid string
Size int64
}
func New(dir string) *LocalStorage {
return &LocalStorage{dir}
}
2015-11-24 20:03:43 +00:00
func (s *LocalStorage) ObjectPath(oid string) string {
return filepath.Join(localObjectDir(s, oid), oid)
}
func (s *LocalStorage) BuildObjectPath(oid string) (string, error) {
dir := localObjectDir(s, oid)
if err := os.MkdirAll(dir, dirPerms); err != nil {
return "", fmt.Errorf("Error trying to create local storage directory in %q: %s", dir, err)
}
return filepath.Join(dir, oid), nil
}
func localObjectDir(s *LocalStorage, oid string) string {
return filepath.Join(s.Root, oid[0:2], oid[2:4])
}