1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package docker
18
19import (
20	"sync"
21
22	"github.com/containerd/containerd/content"
23	"github.com/containerd/containerd/errdefs"
24	"github.com/pkg/errors"
25)
26
27// Status of a content operation
28type Status struct {
29	content.Status
30
31	// UploadUUID is used by the Docker registry to reference blob uploads
32	UploadUUID string
33}
34
35// StatusTracker to track status of operations
36type StatusTracker interface {
37	GetStatus(string) (Status, error)
38	SetStatus(string, Status)
39}
40
41type memoryStatusTracker struct {
42	statuses map[string]Status
43	m        sync.Mutex
44}
45
46// NewInMemoryTracker returns a StatusTracker that tracks content status in-memory
47func NewInMemoryTracker() StatusTracker {
48	return &memoryStatusTracker{
49		statuses: map[string]Status{},
50	}
51}
52
53func (t *memoryStatusTracker) GetStatus(ref string) (Status, error) {
54	t.m.Lock()
55	defer t.m.Unlock()
56	status, ok := t.statuses[ref]
57	if !ok {
58		return Status{}, errors.Wrapf(errdefs.ErrNotFound, "status for ref %v", ref)
59	}
60	return status, nil
61}
62
63func (t *memoryStatusTracker) SetStatus(ref string, status Status) {
64	t.m.Lock()
65	t.statuses[ref] = status
66	t.m.Unlock()
67}
68