Debian Codegraph @

plumbing/format/packfile/fsobject.go

raw
1 package packfile
2
3 import (
4 "io"
5
6 billy "github.com/go-git/go-billy/v5"
7 "github.com/go-git/go-git/v5/plumbing"
8 "github.com/go-git/go-git/v5/plumbing/cache"
9 "github.com/go-git/go-git/v5/plumbing/format/idxfile"
10 "github.com/go-git/go-git/v5/utils/ioutil"
11 )
12
13 // FSObject is an object from the packfile on the filesystem.
14 type FSObject struct {
15 hash plumbing.Hash
16 offset int64
17 size int64
18 typ plumbing.ObjectType
19 index idxfile.Index
20 fs billy.Filesystem
21 path string
22 cache cache.Object
23 largeObjectThreshold int64
24 }
25
26 // NewFSObject creates a new filesystem object.
27 func NewFSObject(
28 hash plumbing.Hash,
29 finalType plumbing.ObjectType,
30 offset int64,
31 contentSize int64,
32 index idxfile.Index,
33 fs billy.Filesystem,
34 path string,
35 cache cache.Object,
36 largeObjectThreshold int64,
37 ) *FSObject {
38 return &FSObject{
39 hash: hash,
40 offset: offset,
41 size: contentSize,
42 typ: finalType,
43 index: index,
44 fs: fs,
45 path: path,
46 cache: cache,
47 largeObjectThreshold: largeObjectThreshold,
48 }
49 }
50
51 // Reader implements the plumbing.EncodedObject interface.
52 func (o *FSObject) Reader() (io.ReadCloser, error) {
53 obj, ok := o.cache.Get(o.hash)
54 if ok && obj != o {
55 reader, err := obj.Reader()
56 if err != nil {
57 return nil, err
58 }
59
60 return reader, nil
61 }
62
63 f, err := o.fs.Open(o.path)
64 if err != nil {
65 return nil, err
66 }
67
68 p := NewPackfileWithCache(o.index, nil, f, o.cache, o.largeObjectThreshold)
69 if o.largeObjectThreshold > 0 && o.size > o.largeObjectThreshold {
70 // We have a big object
71 h, err := p.objectHeaderAtOffset(o.offset)
72 if err != nil {
73 return nil, err
74 }
75
76 r, err := p.getReaderDirect(h)
77 if err != nil {
78 _ = f.Close()
79 return nil, err
80 }
81 // Cap the lazy stream at the resolved object size: well-formed
82 // content reaches EOF inside the bound, an inflated stream that
83 // runs past surfaces ErrInflatedSizeMismatch on the byte just
84 // past the limit. For delta-resolved objects o.size is the
85 // expanded size, which is what the caller is reading here.
86 bounded := newBoundedReadCloser(r, o.size)
87 return ioutil.NewReadCloserWithCloser(bounded, f.Close), nil
88 }
89 r, err := p.getObjectContent(o.offset)
90 if err != nil {
91 _ = f.Close()
92 return nil, err
93 }
94
95 if err := f.Close(); err != nil {
96 return nil, err
97 }
98
99 return r, nil
100 }
101
102 // SetSize implements the plumbing.EncodedObject interface. This method
103 // is a noop.
104 func (o *FSObject) SetSize(int64) {}
105
106 // SetType implements the plumbing.EncodedObject interface. This method is
107 // a noop.
108 func (o *FSObject) SetType(plumbing.ObjectType) {}
109
110 // Hash implements the plumbing.EncodedObject interface.
111 func (o *FSObject) Hash() plumbing.Hash { return o.hash }
112
113 // Size implements the plumbing.EncodedObject interface.
114 func (o *FSObject) Size() int64 { return o.size }
115
116 // Type implements the plumbing.EncodedObject interface.
117 func (o *FSObject) Type() plumbing.ObjectType {
118 return o.typ
119 }
120
121 // Writer implements the plumbing.EncodedObject interface. This method always
122 // returns a nil writer.
123 func (o *FSObject) Writer() (io.WriteCloser, error) {
124 return nil, nil
125 }