aboutsummaryrefslogtreecommitdiff
path: root/refstore/files/transaction.go
blob: 27ce5e66a2cbb6201601ce0fa159367675b1c715 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package files

import (
	"errors"
	"fmt"
	"os"
	"strings"

	"codeberg.org/lindenii/furgit/objectid"
	"codeberg.org/lindenii/furgit/ref/refname"
	"codeberg.org/lindenii/furgit/refstore"
)

type txKind uint8

const (
	txCreate txKind = iota
	txUpdate
	txDelete
	txVerify
	txCreateSymbolic
	txUpdateSymbolic
	txDeleteSymbolic
	txVerifySymbolic
)

type txOp struct {
	name      string
	kind      txKind
	newID     objectid.ObjectID
	oldID     objectid.ObjectID
	newTarget string
	oldTarget string
}

type directKind uint8

const (
	directMissing directKind = iota
	directDetached
	directSymbolic
)

type directRef struct {
	kind     directKind
	name     string
	id       objectid.ObjectID
	target   string
	isLoose  bool
	isPacked bool
}

type resolvedWriteTarget struct {
	name string
	loc  refPath
	ref  directRef
}

type preparedTxOp struct {
	op     txOp
	target resolvedWriteTarget
}

type Transaction struct {
	store  *Store
	ops    []txOp
	closed bool
}

var _ refstore.Transaction = (*Transaction)(nil)

// BeginTransaction creates one new files transaction.
//
//nolint:ireturn
func (store *Store) BeginTransaction() (refstore.Transaction, error) {
	return &Transaction{
		store: store,
		ops:   make([]txOp, 0, 8),
	}, nil
}

func (tx *Transaction) Create(name string, newID objectid.ObjectID) error {
	return tx.queue(txOp{name: name, kind: txCreate, newID: newID})
}

func (tx *Transaction) Update(name string, newID, oldID objectid.ObjectID) error {
	return tx.queue(txOp{name: name, kind: txUpdate, newID: newID, oldID: oldID})
}

func (tx *Transaction) Delete(name string, oldID objectid.ObjectID) error {
	return tx.queue(txOp{name: name, kind: txDelete, oldID: oldID})
}

func (tx *Transaction) Verify(name string, oldID objectid.ObjectID) error {
	return tx.queue(txOp{name: name, kind: txVerify, oldID: oldID})
}

func (tx *Transaction) CreateSymbolic(name, newTarget string) error {
	return tx.queue(txOp{name: name, kind: txCreateSymbolic, newTarget: newTarget})
}

func (tx *Transaction) UpdateSymbolic(name, newTarget, oldTarget string) error {
	return tx.queue(txOp{name: name, kind: txUpdateSymbolic, newTarget: newTarget, oldTarget: oldTarget})
}

func (tx *Transaction) DeleteSymbolic(name, oldTarget string) error {
	return tx.queue(txOp{name: name, kind: txDeleteSymbolic, oldTarget: oldTarget})
}

func (tx *Transaction) VerifySymbolic(name, oldTarget string) error {
	return tx.queue(txOp{name: name, kind: txVerifySymbolic, oldTarget: oldTarget})
}

func (tx *Transaction) Commit() error {
	err := tx.ensureOpen()
	if err != nil {
		return err
	}

	prepared, err := tx.prepare()
	if err != nil {
		tx.closed = true

		return err
	}

	defer func() {
		_ = tx.cleanup(prepared)
	}()

	for _, item := range prepared {
		if item.op.kind == txDelete || item.op.kind == txDeleteSymbolic || item.op.kind == txVerify || item.op.kind == txVerifySymbolic {
			continue
		}

		err = tx.writeLoose(item)
		if err != nil {
			tx.closed = true

			return err
		}
	}

	err = tx.applyPackedDeletes(prepared)
	if err != nil {
		tx.closed = true

		return err
	}

	for _, item := range prepared {
		switch item.op.kind {
		case txDelete, txDeleteSymbolic:
			if item.target.ref.isLoose {
				err = tx.store.rootFor(item.target.loc.root).Remove(item.target.loc.path)
				if err != nil && !errors.Is(err, os.ErrNotExist) {
					tx.closed = true

					return err
				}

				tx.tryRemoveEmptyParents(item.target.name)
			}
		case txCreate, txUpdate, txVerify, txCreateSymbolic, txUpdateSymbolic, txVerifySymbolic:
		}
	}

	tx.closed = true

	return nil
}

func (tx *Transaction) Abort() error {
	err := tx.ensureOpen()
	if err != nil {
		return err
	}

	tx.closed = true

	return nil
}

func (tx *Transaction) ensureOpen() error {
	if tx.closed {
		return fmt.Errorf("refstore/files: transaction already closed")
	}

	return nil
}

func (tx *Transaction) queue(op txOp) error {
	err := tx.ensureOpen()
	if err != nil {
		return err
	}

	err = tx.validateOp(op)
	if err != nil {
		return err
	}

	tx.ops = append(tx.ops, op)

	return nil
}

func (tx *Transaction) validateOp(op txOp) error {
	if op.name == "" {
		return fmt.Errorf("refstore/files: empty reference name")
	}

	switch op.kind {
	case txCreate, txUpdate:
		err := refname.ValidateUpdateName(op.name, true)
		if err != nil {
			return err
		}

		if op.newID.Size() == 0 {
			return objectid.ErrInvalidAlgorithm
		}
	case txDelete, txVerify:
		err := refname.ValidateUpdateName(op.name, false)
		if err != nil {
			return err
		}

		if op.oldID.Size() == 0 {
			return objectid.ErrInvalidAlgorithm
		}
	case txCreateSymbolic, txUpdateSymbolic:
		err := refname.ValidateUpdateName(op.name, true)
		if err != nil {
			return err
		}

		if strings.TrimSpace(op.newTarget) == "" {
			return fmt.Errorf("refstore/files: empty symbolic target")
		}

		err = refname.ValidateSymbolicTarget(op.name, strings.TrimSpace(op.newTarget))
		if err != nil {
			return err
		}
	case txDeleteSymbolic, txVerifySymbolic:
		err := refname.ValidateUpdateName(op.name, false)
		if err != nil {
			return err
		}
	default:
		return fmt.Errorf("refstore/files: unsupported transaction operation %d", op.kind)
	}

	if op.kind == txUpdateSymbolic || op.kind == txDeleteSymbolic || op.kind == txVerifySymbolic {
		if strings.TrimSpace(op.oldTarget) == "" {
			return fmt.Errorf("refstore/files: empty symbolic old target")
		}
	}

	return nil
}