Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sysroot support #10

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
9 changes: 8 additions & 1 deletion pkg/glibobject/gcancellable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ import (
// GIO types

type GCancellable struct {
*GObject
*Object
}

func (self *GCancellable) native() *C.GCancellable {
return (*C.GCancellable)(unsafe.Pointer(self))
}

func (self *GCancellable) Native() uintptr {
if self == nil || self.Object == nil {
return uintptr(unsafe.Pointer(nil))
}
return uintptr(unsafe.Pointer(self.Object))
}

func (self *GCancellable) Ptr() unsafe.Pointer {
return unsafe.Pointer(self)
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/glibobject/gkeyfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2013 Conformal Systems <[email protected]>
* Copyright (c) 2017 Collabora Ltd <[email protected]>
*
* This file originated from: http://opensource.conformal.com/
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package glibobject

// #cgo pkg-config: glib-2.0
// #include <glib.h>
import "C"
import (
"runtime"
"unsafe"
)

/*
* GKeyFile
*/

type GKeyFile struct {
keyfile *C.struct__GKeyFile
}

func (kf *GKeyFile) Native() uintptr {
if kf == nil || kf.keyfile == nil {
return uintptr(unsafe.Pointer(nil))
}
return uintptr(unsafe.Pointer(kf.keyfile))
}

func (kf *GKeyFile) Ref() {
C.g_key_file_ref(kf.keyfile)
}

func (kf *GKeyFile) Unref() {
C.g_key_file_unref(kf.keyfile)
}

func WrapGKeyFile(keyfile uintptr) *GKeyFile {
ckf := (*C.struct__GKeyFile)(unsafe.Pointer(keyfile))
if ckf == nil {
return nil
}
kf := &GKeyFile{ckf}
runtime.SetFinalizer(kf, (*GKeyFile).Unref)

return kf
}
6 changes: 6 additions & 0 deletions pkg/glibobject/glibobject.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ _g_variant_lookup_string (GVariant *v, const char *key)
return r;
return NULL;
}

static GObject *
toGObject(void *p)
{
return (G_OBJECT(p));
}
45 changes: 24 additions & 21 deletions pkg/glibobject/gobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,50 @@ import (
* GObject
*/

// ToGObject type converts an unsafe.Pointer as a native C GObject.
// This function is exported for visibility in other gotk3 packages and
// is not meant to be used by applications.
func ToGObject(p unsafe.Pointer) *C.GObject {
return C.toGObject(p)
}

// IObject is an interface type implemented by Object and all types which embed
// an Object. It is meant to be used as a type for function arguments which
// require GObjects or any subclasses thereof.
type IObject interface {
toGObject() *C.GObject
ToObject() *GObject
ToObject() *Object
}

// GObject is a representation of GLib's GObject.
type GObject struct {
ptr unsafe.Pointer
}

func (v *GObject) Ptr() unsafe.Pointer {
return v.ptr
type Object struct {
GObject *C.GObject
}

func (v *GObject) native() *C.GObject {
if v == nil {
return nil
func (v *Object) Native() uintptr {
if v == nil || v.GObject == nil {
return uintptr(unsafe.Pointer(nil))
}
return (*C.GObject)(v.ptr)
return uintptr(unsafe.Pointer(v.GObject))
}

func (v *GObject) Ref() {
C.g_object_ref(C.gpointer(v.Ptr()))
func (v *Object) Ref() {
C.g_object_ref(C.gpointer(v.GObject))
}

func (v *GObject) Unref() {
C.g_object_unref(C.gpointer(v.Ptr()))
func (v *Object) Unref() {
C.g_object_unref(C.gpointer(v.GObject))
}

func (v *GObject) RefSink() {
C.g_object_ref_sink(C.gpointer(v.native()))
func (v *Object) RefSink() {
C.g_object_ref_sink(C.gpointer(v.GObject))
}

func (v *GObject) IsFloating() bool {
c := C.g_object_is_floating(C.gpointer(v.native()))
func (v *Object) IsFloating() bool {
c := C.g_object_is_floating(C.gpointer(v.GObject))
return GoBool(GBoolean(c))
}

func (v *GObject) ForceFloating() {
C.g_object_force_floating(v.native())
func (v *Object) ForceFloating() {
C.g_object_force_floating(v.GObject)
}
3 changes: 3 additions & 0 deletions pkg/glibobject/gvariant.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (v *GVariant) native() *C.GVariant {
}

func (v *GVariant) Ptr() unsafe.Pointer {
if v == nil {
return nil
}
return v.ptr
}

Expand Down
87 changes: 87 additions & 0 deletions pkg/otbuiltin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type Repo struct {
ptr unsafe.Pointer
}

func cCancellable(c *glib.GCancellable) *C.GCancellable {
return (*C.GCancellable)(unsafe.Pointer(c.Native()))
}

// Converts an ostree repo struct to its C equivalent
func (r *Repo) native() *C.OstreeRepo {
//return (*C.OstreeRepo)(r.Ptr())
Expand All @@ -48,6 +52,24 @@ func (r *Repo) isInitialized() bool {
return false
}

func (repo *Repo) ResolveRev(refspec string, allowNoent bool) (string, error) {
var cerr *C.GError = nil
var coutrev *C.char = nil

crefspec := C.CString(refspec)
defer C.free(unsafe.Pointer(crefspec))

r := C.ostree_repo_resolve_rev(repo.native(), crefspec, (C.gboolean)(glib.GBool(allowNoent)), &coutrev, &cerr)
if !gobool(r) {
return "", generateError(cerr)
}

outrev := C.GoString(coutrev)
C.free(unsafe.Pointer(coutrev))

return outrev, nil
}

// Attempts to open the repo at the given path
func OpenRepo(path string) (*Repo, error) {
var cerr *C.GError = nil
Expand All @@ -63,6 +85,56 @@ func OpenRepo(path string) (*Repo, error) {
return repo, nil
}

type PullOptions struct {
OverrideRemoteName string
Refs []string
}

func (repo *Repo) PullWithOptions(remoteName string, options PullOptions, progress *AsyncProgress, cancellable *glib.GCancellable) error {
var cerr *C.GError = nil

cremoteName := C.CString(remoteName)
defer C.free(unsafe.Pointer(cremoteName))

builder := C.g_variant_builder_new(C._g_variant_type(C.CString("a{sv}")))
if options.OverrideRemoteName != "" {
cstr := C.CString(options.OverrideRemoteName)
v := C.g_variant_new_take_string((*C.gchar)(cstr))
k := C.CString("override-remote-name")
defer C.free(unsafe.Pointer(k))
C._g_variant_builder_add_twoargs(builder, C.CString("{sv}"), k, v)
}

if len(options.Refs) != 0 {
crefs := make([]*C.gchar, len(options.Refs))
for i, s := range options.Refs {
crefs[i] = (*C.gchar)(C.CString(s))
}

v := C.g_variant_new_strv((**C.gchar)(&crefs[0]), (C.gssize)(len(crefs)))

for i, s := range crefs {
crefs[i] = nil
C.free(unsafe.Pointer(s))
}

k := C.CString("refs")
defer C.free(unsafe.Pointer(k))

C._g_variant_builder_add_twoargs(builder, C.CString("{sv}"), k, v)
}

coptions := C.g_variant_builder_end(builder)

r := C.ostree_repo_pull_with_options(repo.native(), cremoteName, coptions, progress.native(), cCancellable(cancellable), &cerr)

if !gobool(r) {
return generateError(cerr)
}

return nil
}

// Enable support for tombstone commits, which allow the repo to distinguish between
// commits that were intentionally deleted and commits that were removed accidentally
func enableTombstoneCommits(repo *Repo) error {
Expand Down Expand Up @@ -91,3 +163,18 @@ func generateError(err *C.GError) error {
return goErr
}
}

func gobool(b C.gboolean) bool {
return b != C.FALSE
}

type AsyncProgress struct {
*glib.Object
}

func (a *AsyncProgress) native() *C.OstreeAsyncProgress {
if a == nil {
return nil
}
return (*C.OstreeAsyncProgress)(unsafe.Pointer(a.Native()))
}
12 changes: 6 additions & 6 deletions pkg/otbuiltin/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func NewCommitOptions() commitOptions {
}

type OstreeRepoTransactionStats struct {
metadata_objects_total int32
metadata_objects_total int32
metadata_objects_written int32
content_objects_total int32
content_objects_written int32
content_bytes_written uint64
content_objects_total int32
content_objects_written int32
content_bytes_written uint64
}

func (repo *Repo) PrepareTransaction() (bool, error) {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (repo *Repo) Commit(commitPath, branch string, opts commitOptions) (string,
var cerr *C.GError
defer C.free(unsafe.Pointer(cerr))
var metadata *C.GVariant = nil
defer func(){
defer func() {
if metadata != nil {
defer C.g_variant_unref(metadata)
}
Expand Down Expand Up @@ -476,7 +476,7 @@ func handleStatOverrideLine(line string, table *glib.GHashTable) error {

// Handle an individual line from a Skiplist file
func handleSkipListline(line string, table *glib.GHashTable) error {
C.g_hash_table_add((*C.GHashTable)(table.Ptr()), C.gpointer( C.g_strdup((*C.gchar)(C.CString(line)))))
C.g_hash_table_add((*C.GHashTable)(table.Ptr()), C.gpointer(C.g_strdup((*C.gchar)(C.CString(line)))))

return nil
}
36 changes: 36 additions & 0 deletions pkg/otbuiltin/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package otbuiltin

import (
"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"

type Deployment struct {
*glib.Object
}

func wrapDeployment(d *C.OstreeDeployment) *Deployment {
g := glib.ToGObject(unsafe.Pointer(d))
obj := &glib.Object{g}
deployment := &Deployment{obj}

runtime.SetFinalizer(deployment, (*Deployment).Unref)

return deployment
}

func (d *Deployment) native() *C.OstreeDeployment {
if d == nil || d.GObject == nil {
return nil
}
return (*C.OstreeDeployment)(unsafe.Pointer(d.GObject))
}
32 changes: 32 additions & 0 deletions pkg/otbuiltin/remote.go
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
package otbuiltin

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

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

func (repo *Repo) RemoteAdd(name, url string, options *glib.GVariant,
cancellable *glib.GCancellable) error {

var cerr *C.GError = nil

cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))

curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))

r := C.ostree_repo_remote_add(repo.native(), cname, curl, (*C.GVariant)(options.Ptr()), cCancellable(cancellable), &cerr)

if !gobool(r) {
return generateError(cerr)
}

return nil
}
Loading