File: store.go

package info (click to toggle)
golang-pault-go-blobstore 0.0~git20180314.d6d187c-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 68 kB
  • sloc: makefile: 2
file content (292 lines) | stat: -rw-r--r-- 4,972 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package blobstore

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"

	"crypto/sha256"
)

// Load {{{

func Load(path string) (*Store, error) {
	absPath, err := filepath.Abs(path)
	if err != nil {
		return nil, err
	}

	return &Store{
		root:           absPath,
		blobRoot:       ".blobs/store",
		tempRoot:       ".blobs/new",
		stageRoot:      "",
		objectIDHasher: sha256.New,
	}, nil
}

// }}}

// Store {{{

type Store struct {
	root      string
	blobRoot  string
	stageRoot string
	tempRoot  string

	objectIDHasher hashFunc
}

// Exists {{{

func (s Store) Exists(o Object) bool {
	_, err := os.Stat(s.objToPath(o))
	return !os.IsNotExist(err)
}

// }}}

// Open {{{

func (s Store) Open(o Object) (io.ReadCloser, error) {
	fd, err := os.Open(s.objToPath(o))
	if err != nil {
		return nil, err
	}
	return fd, nil
}

// }}}

// Copy {{{

func (s Store) Copy(o Object, w io.Writer) (int64, error) {
	fd, err := os.Open(s.objToPath(o))
	if err != nil {
		return 0, err
	}
	return io.Copy(w, fd)
}

// }}}

// OpenPath {{{

func (s Store) OpenPath(p string) (io.ReadCloser, error) {
	fd, err := os.Open(s.qualifyStagePath(p))
	if err != nil {
		return nil, err
	}
	return fd, nil
}

// }}}

// Link {{{

func (s Store) Link(o Object, targetPath string) error {
	if !s.Exists(o) {
		return fmt.Errorf("No commited blob: '%s'", o.Id())
	}
	storePath := s.objToPath(o)
	stagePath := s.qualifyStagePath(targetPath)

	if err := os.MkdirAll(path.Dir(stagePath), 0755); err != nil {
		return err
	}

	_, err := os.Stat(stagePath)
	if err != nil && !os.IsNotExist(err) {
		return err
	}
	if err == nil {
		if err := os.Remove(stagePath); err != nil {
			return err
		}
	}

	return os.Symlink(storePath, stagePath)
}

// }}}

// Load {{{

func (s Store) Load(hash string) (*Object, error) {
	o := Object{id: hash}
	if s.Exists(o) {
		return &o, nil
	}
	return nil, fmt.Errorf("No such object: '%s'", hash)
}

// }}}

// Visitor {{{

func (s Store) LinkedVisitor(progn func(Object, string, os.FileInfo) error) error {
	blobRoot := path.Clean(path.Join(s.root, s.blobRoot))
	return filepath.Walk(
		path.Join(s.root, s.stageRoot),
		func(p string, f os.FileInfo, err error) error {
			p = path.Clean(p)

			/* For each file in the stage (but anything that's not in the
			 * blob root), let's read the link. If it's a symlink, call the
			 * visitor, and move on */
			if f.IsDir() || strings.HasPrefix(path.Clean(p), blobRoot) {
				return nil
			}
			link, err := os.Readlink(p)
			if err != nil {
				/* The only error is of type PathError */
				return nil
			}

			if !strings.HasPrefix(path.Clean(link), blobRoot) {
				/* If the link is pointing outside the blobRoot, we don't
				 * care to visit it */
				return nil
			}
			_, hash := path.Split(link)
			obj := Object{id: hash}
			return progn(obj, p, f)
		},
	)
}

// }}}

// Linked {{{

func (s Store) Linked() (map[Object][]string, error) {
	seen := map[Object][]string{}
	err := s.LinkedVisitor(func(obj Object, p string, info os.FileInfo) error {
		seen[obj] = append(seen[obj], p)
		return nil
	})
	return seen, err
}

// }}}

// Paths {{{

func (s Store) Paths() (map[string]Object, error) {
	seen := map[string]Object{}
	err := s.LinkedVisitor(func(obj Object, p string, info os.FileInfo) error {
		seen[p] = obj
		return nil
	})
	return seen, err
}

// }}}

// List {{{

func (s Store) List() ([]Object, error) {
	objectList := []Object{}

	err := filepath.Walk(
		path.Join(s.root, s.blobRoot),
		func(p string, f os.FileInfo, err error) error {
			if f.IsDir() {
				return nil
			}
			_, hash := path.Split(p)
			objectList = append(objectList, Object{id: hash})
			return nil
		},
	)

	if err != nil {
		return nil, err
	}

	return objectList, nil
}

// }}}

// GC {{{

func (s Store) GC(gc GarbageCollector) error {
	nodes, err := gc.Find(s)
	if err != nil {
		return err
	}

	for _, node := range nodes {
		if err := s.Remove(node); err != nil {
			return err
		}
	}
	return nil
}

// }}}

// Remove {{{

func (s Store) Remove(o Object) error {
	if !s.Exists(o) {
		return fmt.Errorf("No such object: '%s'", o.Id())
	}

	path := s.objToPath(o)
	return os.Remove(path)
}

// }}}

// Create {{{

func (s Store) Create() (*Writer, error) {
	dir := path.Join(s.root, s.tempRoot)

	if err := os.MkdirAll(dir, 0755); err != nil {
		return nil, err
	}

	fd, err := ioutil.TempFile(dir, "blob")
	if err != nil {
		return nil, err
	}
	hashWriter := s.objectIDHasher()

	return &Writer{
		path:   fd.Name(),
		writer: fd,
		target: io.MultiWriter(fd, hashWriter),
		hash:   hashWriter,
	}, nil
}

// }}}

// path helpers {{{

func (s Store) qualifyBlobPath(p string) string {
	return path.Join(s.root, s.blobRoot, p)
}

func (s Store) qualifyStagePath(p string) string {
	return path.Join(s.root, s.stageRoot, p)
}

func (s Store) objToPath(o Object) string {
	id := o.Id()
	return s.qualifyBlobPath(path.Join(id[0:1], id[1:2], id[2:6], id))
}

// }}}

// }}}

// vim: foldmethod=marker