aboutsummaryrefslogtreecommitdiff
path: root/receivepack/service/ingest_quarantine.go
blob: 5b2b706b062ddd3a9659048f8c7514f702be1a4d (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
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
package service

import (
	"os"

	"codeberg.org/lindenii/furgit/format/pack/ingest"
	"codeberg.org/lindenii/furgit/internal/utils"
)

func (service *Service) ingestQuarantine(
	result *Result,
	commands []Command,
	req *Request,
) (string, *os.Root, bool) {
	if !req.PackExpected {
		return "", nil, true
	}

	if req.Pack == nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: missing pack stream.\n")

		result.UnpackError = "missing pack stream"
		fillCommandErrors(result, commands, "missing pack stream")

		return "", nil, false
	}

	if service.opts.ObjectsRoot == nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: objects root not configured.\n")

		result.UnpackError = "objects root not configured"
		fillCommandErrors(result, commands, "objects root not configured")

		return "", nil, false
	}

	var err error

	err = service.opts.ExistingObjects.Refresh()
	if err != nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: refresh existing objects: %v.\n", err)

		result.UnpackError = err.Error()
		fillCommandErrors(result, commands, err.Error())

		return "", nil, false
	}

	pending, err := ingest.Ingest(
		req.Pack,
		service.opts.Algorithm,
		ingest.Options{
			FixThin:       true,
			WriteRev:      true,
			Base:          service.opts.ExistingObjects,
			Progress:      service.opts.Progress,
			ProgressFlush: service.opts.ProgressFlush,
		},
	)
	if err != nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: %v.\n", err)

		result.UnpackError = err.Error()
		fillCommandErrors(result, commands, err.Error())

		return "", nil, false
	}

	if pending.Header().ObjectCount == 0 {
		discarded, err := pending.Discard()
		if err != nil {
			utils.BestEffortFprintf(service.opts.Progress, "unpack failed: %v.\n", err)

			result.UnpackError = err.Error()
			fillCommandErrors(result, commands, err.Error())

			return "", nil, false
		}

		result.Ingest = &ingest.Result{
			PackHash:    discarded.PackHash,
			ObjectCount: discarded.ObjectCount,
		}

		utils.BestEffortFprintf(
			service.opts.Progress,
			"unpacking: done (%d objects, %s).\n",
			discarded.ObjectCount,
			discarded.PackHash,
		)

		return "", nil, true
	}

	utils.BestEffortFprintf(service.opts.Progress, "creating quarantine...\r")

	quarantineName, quarantineRoot, err := service.createQuarantineRoot()
	if err != nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: %v.\n", err)

		result.UnpackError = err.Error()
		fillCommandErrors(result, commands, err.Error())

		return "", nil, false
	}

	quarantinePackRoot, err := service.openQuarantinePackRoot(quarantineRoot)
	if err != nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: %v.\n", err)

		result.UnpackError = err.Error()
		fillCommandErrors(result, commands, err.Error())

		_ = quarantineRoot.Close()
		_ = service.opts.ObjectsRoot.RemoveAll(quarantineName)

		return "", nil, false
	}

	utils.BestEffortFprintf(service.opts.Progress, "creating quarantine: done.\n")
	utils.BestEffortFprintf(service.opts.Progress, "unpacking...\r")

	ingested, err := pending.Continue(quarantinePackRoot)

	_ = quarantinePackRoot.Close()

	if err != nil {
		utils.BestEffortFprintf(service.opts.Progress, "unpack failed: %v.\n", err)

		result.UnpackError = err.Error()
		fillCommandErrors(result, commands, err.Error())

		_ = quarantineRoot.Close()
		_ = service.opts.ObjectsRoot.RemoveAll(quarantineName)

		return "", nil, false
	}

	utils.BestEffortFprintf(service.opts.Progress, "unpacking: done (%d objects, %s).\n", ingested.ObjectCount, ingested.PackHash)

	result.Ingest = &ingested

	return quarantineName, quarantineRoot, true
}