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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|