-
Notifications
You must be signed in to change notification settings - Fork 10
/
backend.go
167 lines (146 loc) · 3.93 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package gitobj
import (
"bufio"
"hash"
"io"
"os"
"path"
"regexp"
"strconv"
"strings"
"github.com/git-lfs/gitobj/v2/pack"
"github.com/git-lfs/gitobj/v2/storage"
)
// NewFilesystemBackend initializes a new filesystem-based backend,
// optionally with additional alternates as specified in the
// `alternates` variable. The syntax is that of the Git environment variable
// GIT_ALTERNATE_OBJECT_DIRECTORIES. The hash algorithm used is specified by
// the algo parameter.
func NewFilesystemBackend(root, tmp, alternates string, algo hash.Hash) (storage.Backend, error) {
fsobj := newFileStorer(root, tmp)
packs, err := pack.NewStorage(root, algo)
if err != nil {
return nil, err
}
storage, err := findAllBackends(fsobj, packs, root, algo)
if err != nil {
return nil, err
}
storage, err = addAlternatesFromEnvironment(storage, alternates, algo)
if err != nil {
return nil, err
}
return &filesystemBackend{
fs: fsobj,
backends: storage,
}, nil
}
func findAllBackends(mainLoose *fileStorer, mainPacked *pack.Storage, root string, algo hash.Hash) ([]storage.Storage, error) {
storage := make([]storage.Storage, 2)
storage[0] = mainLoose
storage[1] = mainPacked
f, err := os.Open(path.Join(root, "info", "alternates"))
if err != nil {
// No alternates file, no problem.
if err != os.ErrNotExist {
return storage, nil
}
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
storage, err = addAlternateDirectory(storage, scanner.Text(), algo)
if err != nil {
return nil, err
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return storage, nil
}
func addAlternateDirectory(s []storage.Storage, dir string, algo hash.Hash) ([]storage.Storage, error) {
s = append(s, newFileStorer(dir, ""))
pack, err := pack.NewStorage(dir, algo)
if err != nil {
return s, err
}
s = append(s, pack)
return s, nil
}
func addAlternatesFromEnvironment(s []storage.Storage, env string, algo hash.Hash) ([]storage.Storage, error) {
if len(env) == 0 {
return s, nil
}
for _, dir := range splitAlternateString(env, alternatesSeparator) {
var err error
s, err = addAlternateDirectory(s, dir, algo)
if err != nil {
return nil, err
}
}
return s, nil
}
var (
octalEscape = regexp.MustCompile("\\\\[0-7]{1,3}")
hexEscape = regexp.MustCompile("\\\\x[0-9a-fA-F]{2}")
replacements = []struct {
olds string
news string
}{
{`\a`, "\a"},
{`\b`, "\b"},
{`\t`, "\t"},
{`\n`, "\n"},
{`\v`, "\v"},
{`\f`, "\f"},
{`\r`, "\r"},
{`\\`, "\\"},
{`\"`, "\""},
{`\'`, "'"},
}
)
func splitAlternateString(env string, separator string) []string {
dirs := strings.Split(env, separator)
for i, s := range dirs {
if !strings.HasPrefix(s, `"`) || !strings.HasSuffix(s, `"`) {
continue
}
// Strip leading and trailing quotation marks
s = s[1 : len(s)-1]
for _, repl := range replacements {
s = strings.Replace(s, repl.olds, repl.news, -1)
}
s = octalEscape.ReplaceAllStringFunc(s, func(inp string) string {
val, _ := strconv.ParseUint(inp[1:], 8, 64)
return string([]byte{byte(val)})
})
s = hexEscape.ReplaceAllStringFunc(s, func(inp string) string {
val, _ := strconv.ParseUint(inp[2:], 16, 64)
return string([]byte{byte(val)})
})
dirs[i] = s
}
return dirs
}
// NewMemoryBackend initializes a new memory-based backend.
//
// A value of "nil" is acceptable and indicates that no entries should be added
// to the memory backend at construction time.
func NewMemoryBackend(m map[string]io.ReadWriter) (storage.Backend, error) {
return &memoryBackend{ms: newMemoryStorer(m)}, nil
}
type filesystemBackend struct {
fs *fileStorer
backends []storage.Storage
}
func (b *filesystemBackend) Storage() (storage.Storage, storage.WritableStorage) {
return storage.MultiStorage(b.backends...), b.fs
}
type memoryBackend struct {
ms *memoryStorer
}
func (b *memoryBackend) Storage() (storage.Storage, storage.WritableStorage) {
return b.ms, b.ms
}