2016-05-18 10:43:42 +00:00
|
|
|
// Package localstorage handles LFS content stored locally
|
|
|
|
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
|
2015-11-24 20:01:58 +00:00
|
|
|
package localstorage
|
|
|
|
|
|
|
|
import (
|
2015-11-24 20:03:43 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-11-24 20:01:58 +00:00
|
|
|
"regexp"
|
2017-10-18 19:42:51 +00:00
|
|
|
|
|
|
|
"github.com/git-lfs/git-lfs/config"
|
2015-11-24 20:01:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
chanBufSize = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-11-24 20:03:43 +00:00
|
|
|
oidRE = regexp.MustCompile(`\A[[:alnum:]]{64}`)
|
|
|
|
dirPerms os.FileMode = 0755
|
2015-11-24 20:01:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LocalStorage manages the locally stored LFS objects for a repository.
|
|
|
|
type LocalStorage struct {
|
2015-11-24 20:18:26 +00:00
|
|
|
RootDir string
|
|
|
|
TempDir string
|
2015-11-24 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object represents a locally stored LFS object.
|
|
|
|
type Object struct {
|
|
|
|
Oid string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2016-05-17 09:28:42 +00:00
|
|
|
func NewStorage(storageDir, tempDir string) (*LocalStorage, error) {
|
2015-11-24 20:18:26 +00:00
|
|
|
if err := os.MkdirAll(storageDir, dirPerms); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(tempDir, dirPerms); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &LocalStorage{storageDir, tempDir}, nil
|
2015-11-24 20:01:58 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-18 19:42:51 +00:00
|
|
|
// Storage configuration
|
|
|
|
type Configuration struct {
|
|
|
|
LfsStorageDir string
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:09:33 +00:00
|
|
|
func NewConfig(cfg *config.Configuration) (c Configuration) {
|
|
|
|
dir, _ := cfg.Git.Get("lfs.storage")
|
2017-10-18 19:42:51 +00:00
|
|
|
if len(dir) == 0 {
|
|
|
|
dir = "lfs"
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.IsAbs(dir) {
|
|
|
|
c.LfsStorageDir = dir
|
|
|
|
} else {
|
2017-10-19 00:09:33 +00:00
|
|
|
c.LfsStorageDir = filepath.Join(cfg.LocalGitStorageDir(), dir)
|
2017-10-18 19:42:51 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-24 20:03:43 +00:00
|
|
|
func localObjectDir(s *LocalStorage, oid string) string {
|
2015-11-24 20:18:26 +00:00
|
|
|
return filepath.Join(s.RootDir, oid[0:2], oid[2:4])
|
2015-11-24 20:03:43 +00:00
|
|
|
}
|