summaryrefslogtreecommitdiff
path: root/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/checkout.go
blob: 04ada17928073ab6d1998c77916c238e95a2a0c0 (plain)
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
package otbuiltin

import (
	"errors"
	"unsafe"

	glib "github.com/ostreedev/ostree-go/pkg/glibobject"
)

// #cgo pkg-config: ostree-1
// #include <stdlib.h>
// #include <glib.h>
// #include <ostree.h>
// #include "builtin.go.h"
import "C"

// checkoutOptions defines all of the options for checking commits
// out of an ostree repo
//
// Note: while this is private, fields are public and part of the API.
type checkoutOptions struct {
	// UserMode defines whether to checkout a repo in `bare-user` mode
	UserMode bool
	// Union specifies whether to overwrite existing filesystem entries
	Union bool
	// AllowNoEnt defines whether to skip filepaths that do not exist
	AllowNoent bool
	// DisableCache defines whether to disable internal repository uncompressed object cache
	DisableCache bool
	// Whiteouts defines whether to Process 'whiteout' (docker style) entries
	Whiteouts bool
	// RequireHardlinks defines whether to fall back to full copies if hard linking fails
	RequireHardlinks bool
	// SubPath specifies a sub-directory to use for checkout
	Subpath string
	// FromFile specifies an optional file containing many checkouts to process
	FromFile string
}

// NewCheckoutOptions instantiates and returns a checkoutOptions struct with default values set
func NewCheckoutOptions() checkoutOptions {
	return checkoutOptions{}
}

// Checkout checks out commit `commitRef` from a repository at `repoPath`,
// writing it to `destination`.  Returns an error if the checkout could not be processed.
func Checkout(repoPath, destination, commitRef string, opts checkoutOptions) error {
	var cancellable *glib.GCancellable

	ccommit := C.CString(commitRef)
	defer C.free(unsafe.Pointer(ccommit))

	var gerr = glib.NewGError()
	cerr := (*C.GError)(gerr.Ptr())
	defer C.free(unsafe.Pointer(cerr))

	repoPathc := C.g_file_new_for_path(C.CString(repoPath))
	defer C.g_object_unref(C.gpointer(repoPathc))
	crepo := C.ostree_repo_new(repoPathc)
	if !glib.GoBool(glib.GBoolean(C.ostree_repo_open(crepo, (*C.GCancellable)(cancellable.Ptr()), &cerr))) {
		return generateError(cerr)
	}

	// Multiple checkouts to process
	if opts.FromFile != "" {
		return processManyCheckouts(crepo, destination, cancellable)
	}

	// Simple single checkout
	var resolvedCommit *C.char
	defer C.free(unsafe.Pointer(resolvedCommit))
	if !glib.GoBool(glib.GBoolean(C.ostree_repo_resolve_rev(crepo, ccommit, C.FALSE, &resolvedCommit, &cerr))) {
		return generateError(cerr)
	}

	return processOneCheckout(crepo, resolvedCommit, destination, opts, cancellable)
}

// processOneCheckout processes one checkout from the repo
func processOneCheckout(crepo *C.OstreeRepo, resolvedCommit *C.char, destination string, opts checkoutOptions, cancellable *glib.GCancellable) error {
	cdest := C.CString(destination)
	defer C.free(unsafe.Pointer(cdest))

	var gerr = glib.NewGError()
	cerr := (*C.GError)(gerr.Ptr())
	defer C.free(unsafe.Pointer(cerr))

	// Process options into bitflags
	var repoCheckoutAtOptions C.OstreeRepoCheckoutAtOptions
	if opts.UserMode {
		repoCheckoutAtOptions.mode = C.OSTREE_REPO_CHECKOUT_MODE_USER
	}
	if opts.Union {
		repoCheckoutAtOptions.overwrite_mode = C.OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_FILES
	}

	// Checkout commit to destination
	if !glib.GoBool(glib.GBoolean(C.ostree_repo_checkout_at(crepo, &repoCheckoutAtOptions, C._at_fdcwd(), cdest, resolvedCommit, nil, &cerr))) {
		return generateError(cerr)
	}

	return nil
}

// processManyCheckouts processes many checkouts in a single batch
func processManyCheckouts(crepo *C.OstreeRepo, target string, cancellable *glib.GCancellable) error {
	return errors.New("batch checkouts processing: not implemented")
}