-
Notifications
You must be signed in to change notification settings - Fork 18
/
context.go
53 lines (42 loc) · 1.19 KB
/
context.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
package dirtree
import (
"crypto/sha1" //nolint: gosec // required
"fmt"
"hash"
"io"
"github.com/mandelsoft/logging"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/pkg/utils"
)
type Context interface {
logging.Context
Hasher() hash.Hash
FileMode(vfs.FileMode) Mode
DirMode(vfs.FileMode) Mode
LinkMode(vfs.FileMode) Mode
WriteHeader(w io.Writer, typ string, size int64) error
}
// DefaultContext provides a default directory tree hashing context.
// It is based on the Git tree hash mechanism.
func DefaultContext(ctx ...logging.Context) Context {
return &defaultContext{utils.OptionalDefaulted(LogContext, ctx...)}
}
type defaultContext struct {
logging.Context
}
func (d defaultContext) Hasher() hash.Hash {
return sha1.New() //nolint: gosec // required
}
func (d defaultContext) FileMode(mode vfs.FileMode) Mode {
return FileMode(mode) | ModeBlob
}
func (d defaultContext) DirMode(mode vfs.FileMode) Mode {
return ModeDir
}
func (d defaultContext) LinkMode(mode vfs.FileMode) Mode {
return ModeSym
}
func (d defaultContext) WriteHeader(w io.Writer, typ string, size int64) error {
_, err := w.Write([]byte(fmt.Sprintf("%s %d\000", typ, size)))
return err
}