Debian Codegraph @

object_writer.go

raw
1 package gitobj
2
3 import (
4 "compress/zlib"
5 "fmt"
6 "hash"
7 "io"
8 "sync/atomic"
9 )
10
11 // ObjectWriter provides an implementation of io.Writer that compresses and
12 // writes data given to it, and keeps track of the SHA1 hash of the data as it
13 // is written.
14 type ObjectWriter struct {
15 // members managed via sync/atomic must be aligned at the top of this
16 // structure (see: https://github.com/git-lfs/git-lfs/pull/2880).
17
18 // wroteHeader is a uint32 managed by the sync/atomic package. It is 1
19 // if the header was written, and 0 otherwise.
20 wroteHeader uint32
21
22 // w is the underling writer that this ObjectWriter is writing to.
23 w io.Writer
24 // sum is the in-progress hash calculation.
25 sum hash.Hash
26
27 // closeFn supplies an optional function that, when called, frees an
28 // resources (open files, memory, etc) held by this instance of the
29 // *ObjectWriter.
30 //
31 // closeFn returns any error encountered when closing/freeing resources
32 // held.
33 //
34 // It is allowed to be nil.
35 closeFn func() error
36 }
37
38 // nopCloser provides a no-op implementation of the io.WriteCloser interface by
39 // taking an io.Writer and wrapping it with a Close() method that returns nil.
40 type nopCloser struct {
41 // Writer is an embedded io.Writer that receives the Write() method
42 // call.
43 io.Writer
44 }
45
46 // Close implements the io.Closer interface by returning nil.
47 func (n *nopCloser) Close() error {
48 return nil
49 }
50
51 // NewObjectWriter returns a new *ObjectWriter instance that drains incoming
52 // writes into the io.Writer given, "w". "hash" is a hash instance from the
53 // ObjectDatabase'e Hash method.
54 func NewObjectWriter(w io.Writer, hash hash.Hash) *ObjectWriter {
55 return NewObjectWriteCloser(&nopCloser{w}, hash)
56 }
57
58 // NewObjectWriter returns a new *ObjectWriter instance that drains incoming
59 // writes into the io.Writer given, "w". "sum" is a hash instance from the
60 // ObjectDatabase'e Hash method.
61 //
62 // Upon closing, it calls the given Close() function of the io.WriteCloser.
63 func NewObjectWriteCloser(w io.WriteCloser, sum hash.Hash) *ObjectWriter {
64 zw := zlib.NewWriter(w)
65 sum.Reset()
66
67 return &ObjectWriter{
68 w: io.MultiWriter(zw, sum),
69 sum: sum,
70
71 closeFn: func() error {
72 if err := zw.Close(); err != nil {
73 return err
74 }
75 if err := w.Close(); err != nil {
76 return err
77 }
78 return nil
79 },
80 }
81 }
82
83 // WriteHeader writes object header information and returns the number of
84 // uncompressed bytes written, or any error that was encountered along the way.
85 //
86 // WriteHeader MUST be called only once, or a panic() will occur.
87 func (w *ObjectWriter) WriteHeader(typ ObjectType, len int64) (n int, err error) {
88 if !atomic.CompareAndSwapUint32(&w.wroteHeader, 0, 1) {
89 panic("gitobj: cannot write headers more than once")
90 }
91 return fmt.Fprintf(w, "%s %d\x00", typ, len)
92 }
93
94 // Write writes the given buffer "p" of uncompressed bytes into the underlying
95 // data-stream, returning the number of uncompressed bytes written, along with
96 // any error encountered along the way.
97 //
98 // A call to WriteHeaders MUST occur before calling Write, or a panic() will
99 // occur.
100 func (w *ObjectWriter) Write(p []byte) (n int, err error) {
101 if atomic.LoadUint32(&w.wroteHeader) != 1 {
102 panic("gitobj: cannot write data without header")
103 }
104 return w.w.Write(p)
105 }
106
107 // Sha returns the in-progress SHA1 of the compressed object contents.
108 func (w *ObjectWriter) Sha() []byte {
109 return w.sum.Sum(nil)
110 }
111
112 // Close closes the ObjectWriter and frees any resources held by it, including
113 // flushing the zlib-compressed content to the underling writer. It must be
114 // called before discarding of the Writer instance.
115 //
116 // If any error occurred while calling close, it will be returned immediately,
117 // otherwise nil.
118 func (w *ObjectWriter) Close() error {
119 if w.closeFn == nil {
120 return nil
121 }
122 return w.closeFn()
123 }