summaryrefslogtreecommitdiff
path: root/vendor/github.com/ostreedev/ostree-go/pkg/otbuiltin/builtin.go
blob: 8b0236ff6051cba5313e0e10ece9ebc4e72d3521 (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
109
110
111
112
113
114
115
116
117
118
119
// Package otbuiltin contains all of the basic commands for creating and
// interacting with an ostree repository
package otbuiltin

import (
	"errors"
	"fmt"
	"runtime"
	"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"

// Repo represents a local ostree repository
type Repo struct {
	ptr unsafe.Pointer
}

// isInitialized checks if the repo has been initialized
func (r *Repo) isInitialized() bool {
	if r == nil || r.ptr == nil {
		return false
	}
	return true
}

// native converts an ostree repo struct to its C equivalent
func (r *Repo) native() *C.OstreeRepo {
	if !r.isInitialized() {
		return nil
	}
	return (*C.OstreeRepo)(r.ptr)
}

// repoFromNative takes a C ostree repo and converts it to a Go struct
func repoFromNative(or *C.OstreeRepo) *Repo {
	if or == nil {
		return nil
	}
	r := &Repo{unsafe.Pointer(or)}
	return r
}

// OpenRepo attempts to open the repo at the given path
func OpenRepo(path string) (*Repo, error) {
	if path == "" {
		return nil, errors.New("empty path")
	}

	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))
	repoPath := C.g_file_new_for_path(cpath)
	defer C.g_object_unref(C.gpointer(repoPath))
	crepo := C.ostree_repo_new(repoPath)
	repo := repoFromNative(crepo)

	var cerr *C.GError
	r := glib.GoBool(glib.GBoolean(C.ostree_repo_open(crepo, nil, &cerr)))
	if !r {
		return nil, generateError(cerr)
	}

	return repo, nil
}

// enableTombstoneCommits enables support for tombstone commits.
//
// This allows to distinguish between intentional deletions and accidental removals
// of commits.
func (r *Repo) enableTombstoneCommits() error {
	if !r.isInitialized() {
		return errors.New("repo not initialized")
	}

	config := C.ostree_repo_get_config(r.native())
	groupC := C.CString("core")
	defer C.free(unsafe.Pointer(groupC))
	keyC := C.CString("tombstone-commits")
	defer C.free(unsafe.Pointer(keyC))
	valueC := C.g_key_file_get_boolean(config, (*C.gchar)(groupC), (*C.gchar)(keyC), nil)
	tombstoneCommits := glib.GoBool(glib.GBoolean(valueC))

	// tombstoneCommits is false only if it really is false or if it is set to FALSE in the config file
	if !tombstoneCommits {
		var cerr *C.GError
		C.g_key_file_set_boolean(config, (*C.gchar)(groupC), (*C.gchar)(keyC), C.TRUE)
		if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_config(r.native(), config, &cerr))) {
			return generateError(cerr)
		}
	}
	return nil
}

// generateError wraps a GLib error into a Go one.
func generateError(err *C.GError) error {
	if err == nil {
		return errors.New("nil GError")
	}

	goErr := glib.ConvertGError(glib.ToGError(unsafe.Pointer(err)))
	_, file, line, ok := runtime.Caller(1)
	if ok {
		return fmt.Errorf("%s:%d - %s", file, line, goErr)
	}
	return goErr
}

// isOk wraps a gboolean return value into a bool.
// 0 is false/error, everything else is true/ok.
func isOk(v C.gboolean) bool {
	return glib.GoBool(glib.GBoolean(v))
}