aboutsummaryrefslogtreecommitdiff
path: root/object/store/loose/writer.go
blob: b02fe2d9064a1aa7c84eea84e0b58770670e22f1 (about) (plain) (blame)
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
package loose

import (
	"bytes"
	"fmt"
	"io"

	"lindenii.org/go/furgit/object/header"
	"lindenii.org/go/furgit/object/id"
	"lindenii.org/go/furgit/object/typ"
)

// WriteBytesFull writes a full serialized object as "type size\x00content".
func (loose *Loose) WriteBytesFull(raw []byte) (id.ObjectID, error) {
	return loose.WriteReaderFull(bytes.NewReader(raw))
}

// WriteBytesContent writes typed content bytes as a loose object.
func (loose *Loose) WriteBytesContent(ty typ.Type, content []byte) (id.ObjectID, error) {
	return loose.WriteReaderContent(ty, uint64(len(content)), bytes.NewReader(content))
}

// WriteReaderContent writes one loose object from typed content bytes read from src.
// src must provide exactly size bytes.
// size is required because loose object headers are "type size\x00content",
// so the header must be emitted before streaming content without buffering.
func (loose *Loose) WriteReaderContent(ty typ.Type, size uint64, src io.Reader) (id.ObjectID, error) {
	headerBytes := header.Append(nil, ty, size)

	writer, err := loose.newStreamWriter(false)
	if err != nil {
		return id.ObjectID{}, err
	}

	writer.headerDone = true
	writer.expectedContentLeft = size

	err = writer.writeRawChunk(headerBytes)
	if err != nil {
		_ = writer.Close()
		_ = loose.root.Remove(writer.tmpRelPath)

		return id.ObjectID{}, err
	}

	return writeReaderIntoStreamWriter(writer, src)
}

// WriteReaderFull writes one loose object from raw bytes "type size\x00content" read from src.
func (loose *Loose) WriteReaderFull(src io.Reader) (id.ObjectID, error) {
	writer, err := loose.newStreamWriter(true)
	if err != nil {
		return id.ObjectID{}, err
	}

	return writeReaderIntoStreamWriter(writer, src)
}

// writeReaderIntoStreamWriter copies src into writer and publishes the object.
func writeReaderIntoStreamWriter(writer *streamWriter, src io.Reader) (id.ObjectID, error) {
	_, err := io.Copy(writer, src)
	if err != nil {
		_ = writer.Close()
		_ = writer.loose.root.Remove(writer.tmpRelPath)

		return id.ObjectID{}, fmt.Errorf("object/store/loose: %w", err)
	}

	err = writer.Close()
	if err != nil {
		_ = writer.loose.root.Remove(writer.tmpRelPath)

		return id.ObjectID{}, err
	}

	objectID, err := writer.finalize()
	if err != nil {
		_ = writer.loose.root.Remove(writer.tmpRelPath)

		return id.ObjectID{}, err
	}

	return objectID, nil
}