File: writer.go

package info (click to toggle)
golang-github-containers-image 5.23.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,368 kB
  • sloc: sh: 165; makefile: 75
file content (103 lines) | stat: -rw-r--r-- 3,642 bytes parent folder | download
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
package archive

import (
	"errors"
	"fmt"
	"io"
	"os"
	"sync"

	"github.com/containers/image/v5/docker/internal/tarfile"
	"github.com/containers/image/v5/docker/reference"
	"github.com/containers/image/v5/types"
)

// Writer manages a single in-progress Docker archive and allows adding images to it.
type Writer struct {
	path        string // The original, user-specified path; not the maintained temporary file, if any
	regularFile bool   // path refers to a regular file (e.g. not a pipe)
	archive     *tarfile.Writer
	writer      io.Closer

	// The following state can only be acccessed with the mutex held.
	mutex     sync.Mutex
	hadCommit bool // At least one successful commit has happened
}

// NewWriter returns a Writer for path.
// The caller should call .Close() on the returned object.
func NewWriter(sys *types.SystemContext, path string) (*Writer, error) {
	// path can be either a pipe or a regular file
	// in the case of a pipe, we require that we can open it for write
	// in the case of a regular file, we don't want to overwrite any pre-existing file
	// so we check for Size() == 0 below (This is racy, but using O_EXCL would also be racy,
	// only in a different way. Either way, it’s up to the user to not have two writers to the same path.)
	fh, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		return nil, fmt.Errorf("opening file %q: %w", path, err)
	}
	succeeded := false
	defer func() {
		if !succeeded {
			fh.Close()
		}
	}()

	fhStat, err := fh.Stat()
	if err != nil {
		return nil, fmt.Errorf("statting file %q: %w", path, err)
	}
	regularFile := fhStat.Mode().IsRegular()
	if regularFile && fhStat.Size() != 0 {
		return nil, errors.New("docker-archive doesn't support modifying existing images")
	}

	archive := tarfile.NewWriter(fh)

	succeeded = true
	return &Writer{
		path:        path,
		regularFile: regularFile,
		archive:     archive,
		writer:      fh,
		hadCommit:   false,
	}, nil
}

// imageCommitted notifies the Writer that at least one image was successfully commited to the stream.
func (w *Writer) imageCommitted() {
	w.mutex.Lock()
	defer w.mutex.Unlock()
	w.hadCommit = true
}

// Close writes all outstanding data about images to the archive, and
// releases state associated with the Writer, if any.
// No more images can be added after this is called.
func (w *Writer) Close() error {
	err := w.archive.Close()
	if err2 := w.writer.Close(); err2 != nil && err == nil {
		err = err2
	}
	if err == nil && w.regularFile && !w.hadCommit {
		// Writing to the destination never had a success; delete the destination if we created it.
		// This is done primarily because we don’t implement adding another image to a pre-existing image, so if we
		// left a partial archive around (notably because reading from the _source_ has failed), we couldn’t retry without
		// the caller manually deleting the partial archive. So, delete it instead.
		//
		// Archives with at least one successfully created image are left around; they might still be valuable.
		//
		// Note a corner case: If there _originally_ was an empty file (which is not a valid archive anyway), this deletes it.
		// Ideally, if w.regularFile, we should write the full contents to a temporary file and use os.Rename here, only on success.
		if err2 := os.Remove(w.path); err2 != nil {
			err = err2
		}
	}
	return err
}

// NewReference returns an ImageReference that allows adding an image to Writer,
// with an optional reference.
func (w *Writer) NewReference(destinationRef reference.NamedTagged) (types.ImageReference, error) {
	return newReference(w.path, destinationRef, -1, nil, w)
}