Debian Codegraph @

pkg/otbuiltin/builtin.go

raw
1 // Package otbuiltin contains all of the basic commands for creating and
2 // interacting with an ostree repository
3 package otbuiltin
4
5 import (
6 "errors"
7 "fmt"
8 "runtime"
9 "unsafe"
10
11 glib "github.com/ostreedev/ostree-go/pkg/glibobject"
12 )
13
14 // #cgo pkg-config: ostree-1
15 // #include <stdlib.h>
16 // #include <glib.h>
17 // #include <ostree.h>
18 // #include "builtin.go.h"
19 import "C"
20
21 // Repo represents a local ostree repository
22 type Repo struct {
23 ptr unsafe.Pointer
24 }
25
26 // isInitialized checks if the repo has been initialized
27 func (r *Repo) isInitialized() bool {
28 if r == nil || r.ptr == nil {
29 return false
30 }
31 return true
32 }
33
34 // native converts an ostree repo struct to its C equivalent
35 func (r *Repo) native() *C.OstreeRepo {
36 if !r.isInitialized() {
37 return nil
38 }
39 return (*C.OstreeRepo)(r.ptr)
40 }
41
42 // repoFromNative takes a C ostree repo and converts it to a Go struct
43 func repoFromNative(or *C.OstreeRepo) *Repo {
44 if or == nil {
45 return nil
46 }
47 r := &Repo{unsafe.Pointer(or)}
48 return r
49 }
50
51 // OpenRepo attempts to open the repo at the given path
52 func OpenRepo(path string) (*Repo, error) {
53 if path == "" {
54 return nil, errors.New("empty path")
55 }
56
57 cpath := C.CString(path)
58 defer C.free(unsafe.Pointer(cpath))
59 repoPath := C.g_file_new_for_path(cpath)
60 defer C.g_object_unref(C.gpointer(repoPath))
61 crepo := C.ostree_repo_new(repoPath)
62 repo := repoFromNative(crepo)
63
64 var cerr *C.GError
65 r := glib.GoBool(glib.GBoolean(C.ostree_repo_open(crepo, nil, &cerr)))
66 if !r {
67 return nil, generateError(cerr)
68 }
69
70 return repo, nil
71 }
72
73 // enableTombstoneCommits enables support for tombstone commits.
74 //
75 // This allows to distinguish between intentional deletions and accidental removals
76 // of commits.
77 func (r *Repo) enableTombstoneCommits() error {
78 if !r.isInitialized() {
79 return errors.New("repo not initialized")
80 }
81
82 config := C.ostree_repo_get_config(r.native())
83 groupC := C.CString("core")
84 defer C.free(unsafe.Pointer(groupC))
85 keyC := C.CString("tombstone-commits")
86 defer C.free(unsafe.Pointer(keyC))
87 valueC := C.g_key_file_get_boolean(config, (*C.gchar)(groupC), (*C.gchar)(keyC), nil)
88 tombstoneCommits := glib.GoBool(glib.GBoolean(valueC))
89
90 // tombstoneCommits is false only if it really is false or if it is set to FALSE in the config file
91 if !tombstoneCommits {
92 var cerr *C.GError
93 C.g_key_file_set_boolean(config, (*C.gchar)(groupC), (*C.gchar)(keyC), C.TRUE)
94 if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_config(r.native(), config, &cerr))) {
95 return generateError(cerr)
96 }
97 }
98 return nil
99 }
100
101 // generateError wraps a GLib error into a Go one.
102 func generateError(err *C.GError) error {
103 if err == nil {
104 return errors.New("nil GError")
105 }
106
107 goErr := glib.ConvertGError(glib.ToGError(unsafe.Pointer(err)))
108 _, file, line, ok := runtime.Caller(1)
109 if ok {
110 return fmt.Errorf("%s:%d - %s", file, line, goErr)
111 }
112 return goErr
113 }
114
115 // isOk wraps a gboolean return value into a bool.
116 // 0 is false/error, everything else is true/ok.
117 func isOk(v C.gboolean) bool {
118 return glib.GoBool(glib.GBoolean(v))
119 }