summaryrefslogtreecommitdiff
path: root/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
committerMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
commita031b83a09a8628435317a03f199cdc18b78262f (patch)
treebc017a96769ce6de33745b8b0b1304ccf38e9df0 /vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go
parent2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff)
downloadpodman-a031b83a09a8628435317a03f199cdc18b78262f.tar.gz
podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.bz2
podman-a031b83a09a8628435317a03f199cdc18b78262f.zip
Initial checkin from CRI-O repo
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go')
-rw-r--r--vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go b/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go
new file mode 100644
index 000000000..c1ca2dc7e
--- /dev/null
+++ b/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/init.go
@@ -0,0 +1,90 @@
+package otbuiltin
+
+import (
+ "errors"
+ "strings"
+ "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"
+
+// Declare variables for options
+var initOpts initOptions
+
+// Contains all of the options for initializing an ostree repo
+type initOptions struct {
+ Mode string // either bare, archive-z2, or bare-user
+
+ repoMode C.OstreeRepoMode
+}
+
+// Instantiates and returns an initOptions struct with default values set
+func NewInitOptions() initOptions {
+ io := initOptions{}
+ io.Mode = "bare"
+ io.repoMode = C.OSTREE_REPO_MODE_BARE
+ return io
+}
+
+// Initializes a new ostree repository at the given path. Returns true
+// if the repo exists at the location, regardless of whether it was initialized
+// by the function or if it already existed. Returns an error if the repo could
+// not be initialized
+func Init(path string, options initOptions) (bool, error) {
+ initOpts = options
+ err := parseMode()
+ if err != nil {
+ return false, err
+ }
+
+ // Create a repo struct from the path
+ var cerr *C.GError
+ defer C.free(unsafe.Pointer(cerr))
+ cpath := C.CString(path)
+ defer C.free(unsafe.Pointer(cpath))
+ pathc := C.g_file_new_for_path(cpath)
+ defer C.g_object_unref(C.gpointer(pathc))
+ crepo := C.ostree_repo_new(pathc)
+
+ // If the repo exists in the filesystem, return an error but set exists to true
+ /* var exists C.gboolean = 0
+ success := glib.GoBool(glib.GBoolean(C.ostree_repo_exists(crepo, &exists, &cerr)))
+ if exists != 0 {
+ err = errors.New("repository already exists")
+ return true, err
+ } else if !success {
+ return false, generateError(cerr)
+ }*/
+
+ cerr = nil
+ created := glib.GoBool(glib.GBoolean(C.ostree_repo_create(crepo, initOpts.repoMode, nil, &cerr)))
+ if !created {
+ errString := generateError(cerr).Error()
+ if strings.Contains(errString, "File exists") {
+ return true, generateError(cerr)
+ }
+ return false, generateError(cerr)
+ }
+ return true, nil
+}
+
+// Converts the mode string to a C.OSTREE_REPO_MODE enum value
+func parseMode() error {
+ if strings.EqualFold(initOpts.Mode, "bare") {
+ initOpts.repoMode = C.OSTREE_REPO_MODE_BARE
+ } else if strings.EqualFold(initOpts.Mode, "bare-user") {
+ initOpts.repoMode = C.OSTREE_REPO_MODE_BARE_USER
+ } else if strings.EqualFold(initOpts.Mode, "archive-z2") {
+ initOpts.repoMode = C.OSTREE_REPO_MODE_ARCHIVE_Z2
+ } else {
+ return errors.New("Invalid option for mode")
+ }
+ return nil
+}