| 1 |
package otbuiltin |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"unsafe" |
| 6 |
|
| 7 |
glib "github.com/ostreedev/ostree-go/pkg/glibobject" |
| 8 |
) |
| 9 |
|
| 10 |
// #cgo pkg-config: ostree-1 |
| 11 |
// #include <stdlib.h> |
| 12 |
// #include <glib.h> |
| 13 |
// #include <ostree.h> |
| 14 |
// #include "builtin.go.h" |
| 15 |
import "C" |
| 16 |
|
| 17 |
// checkoutOptions defines all of the options for checking commits |
| 18 |
// out of an ostree repo |
| 19 |
// |
| 20 |
// Note: while this is private, fields are public and part of the API. |
| 21 |
type checkoutOptions struct { |
| 22 |
// UserMode defines whether to checkout a repo in `bare-user` mode |
| 23 |
UserMode bool |
| 24 |
// Union specifies whether to overwrite existing filesystem entries |
| 25 |
Union bool |
| 26 |
// AllowNoEnt defines whether to skip filepaths that do not exist |
| 27 |
AllowNoent bool |
| 28 |
// DisableCache defines whether to disable internal repository uncompressed object cache |
| 29 |
DisableCache bool |
| 30 |
// Whiteouts defines whether to Process 'whiteout' (docker style) entries |
| 31 |
Whiteouts bool |
| 32 |
// RequireHardlinks defines whether to fall back to full copies if hard linking fails |
| 33 |
RequireHardlinks bool |
| 34 |
// SubPath specifies a sub-directory to use for checkout |
| 35 |
Subpath string |
| 36 |
// FromFile specifies an optional file containing many checkouts to process |
| 37 |
FromFile string |
| 38 |
} |
| 39 |
|
| 40 |
// NewCheckoutOptions instantiates and returns a checkoutOptions struct with default values set |
| 41 |
func NewCheckoutOptions() checkoutOptions { |
| 42 |
return checkoutOptions{} |
| 43 |
} |
| 44 |
|
| 45 |
// Checkout checks out commit `commitRef` from a repository at `repoPath`, |
| 46 |
// writing it to `destination`. Returns an error if the checkout could not be processed. |
| 47 |
func Checkout(repoPath, destination, commitRef string, opts checkoutOptions) error { |
| 48 |
var cancellable *glib.GCancellable |
| 49 |
|
| 50 |
ccommit := C.CString(commitRef) |
| 51 |
defer C.free(unsafe.Pointer(ccommit)) |
| 52 |
|
| 53 |
var gerr = glib.NewGError() |
| 54 |
cerr := (*C.GError)(gerr.Ptr()) |
| 55 |
defer C.free(unsafe.Pointer(cerr)) |
| 56 |
|
| 57 |
repoPathc := C.g_file_new_for_path(C.CString(repoPath)) |
| 58 |
defer C.g_object_unref(C.gpointer(repoPathc)) |
| 59 |
crepo := C.ostree_repo_new(repoPathc) |
| 60 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_open(crepo, (*C.GCancellable)(cancellable.Ptr()), &cerr))) { |
| 61 |
return generateError(cerr) |
| 62 |
} |
| 63 |
|
| 64 |
// Multiple checkouts to process |
| 65 |
if opts.FromFile != "" { |
| 66 |
return processManyCheckouts(crepo, destination, cancellable) |
| 67 |
} |
| 68 |
|
| 69 |
// Simple single checkout |
| 70 |
var resolvedCommit *C.char |
| 71 |
defer C.free(unsafe.Pointer(resolvedCommit)) |
| 72 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_resolve_rev(crepo, ccommit, C.FALSE, &resolvedCommit, &cerr))) { |
| 73 |
return generateError(cerr) |
| 74 |
} |
| 75 |
|
| 76 |
return processOneCheckout(crepo, resolvedCommit, destination, opts, cancellable) |
| 77 |
} |
| 78 |
|
| 79 |
// processOneCheckout processes one checkout from the repo |
| 80 |
func processOneCheckout(crepo *C.OstreeRepo, resolvedCommit *C.char, destination string, opts checkoutOptions, cancellable *glib.GCancellable) error { |
| 81 |
cdest := C.CString(destination) |
| 82 |
defer C.free(unsafe.Pointer(cdest)) |
| 83 |
|
| 84 |
var gerr = glib.NewGError() |
| 85 |
cerr := (*C.GError)(gerr.Ptr()) |
| 86 |
defer C.free(unsafe.Pointer(cerr)) |
| 87 |
|
| 88 |
// Process options into bitflags |
| 89 |
var repoCheckoutAtOptions C.OstreeRepoCheckoutAtOptions |
| 90 |
if opts.UserMode { |
| 91 |
repoCheckoutAtOptions.mode = C.OSTREE_REPO_CHECKOUT_MODE_USER |
| 92 |
} |
| 93 |
if opts.Union { |
| 94 |
repoCheckoutAtOptions.overwrite_mode = C.OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_FILES |
| 95 |
} |
| 96 |
if opts.RequireHardlinks { |
| 97 |
repoCheckoutAtOptions.no_copy_fallback = C.TRUE |
| 98 |
} |
| 99 |
|
| 100 |
// Checkout commit to destination |
| 101 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_checkout_at(crepo, &repoCheckoutAtOptions, C._at_fdcwd(), cdest, resolvedCommit, nil, &cerr))) { |
| 102 |
return generateError(cerr) |
| 103 |
} |
| 104 |
|
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
// processManyCheckouts processes many checkouts in a single batch |
| 109 |
func processManyCheckouts(crepo *C.OstreeRepo, target string, cancellable *glib.GCancellable) error { |
| 110 |
return errors.New("batch checkouts processing: not implemented") |
| 111 |
} |