| 1 |
#ifndef BUILTIN_GO_H |
| 2 |
#define BUILTIN_GO_H |
| 3 |
|
| 4 |
#include <glib.h> |
| 5 |
#include <ostree.h> |
| 6 |
#include <string.h> |
| 7 |
#include <fcntl.h> |
| 8 |
|
| 9 |
static guint32 owner_uid; |
| 10 |
static guint32 owner_gid; |
| 11 |
|
| 12 |
static void |
| 13 |
_ostree_repo_append_modifier_flags(OstreeRepoCommitModifierFlags *flags, int flag) { |
| 14 |
*flags |= flag; |
| 15 |
} |
| 16 |
|
| 17 |
struct CommitFilterData { |
| 18 |
GHashTable *mode_adds; |
| 19 |
GHashTable *skip_list; |
| 20 |
}; |
| 21 |
|
| 22 |
typedef struct CommitFilterData CommitFilterData; |
| 23 |
|
| 24 |
static char* _gptr_to_str(gpointer p) |
| 25 |
{ |
| 26 |
return (char*)p; |
| 27 |
} |
| 28 |
|
| 29 |
// The following 3 functions are wrapper functions for macros since CGO can't parse macros |
| 30 |
static OstreeRepoFile* |
| 31 |
_ostree_repo_file(GFile *file) |
| 32 |
{ |
| 33 |
return OSTREE_REPO_FILE (file); |
| 34 |
} |
| 35 |
|
| 36 |
static gpointer |
| 37 |
_guint_to_pointer (guint u) |
| 38 |
{ |
| 39 |
return GUINT_TO_POINTER (u); |
| 40 |
} |
| 41 |
|
| 42 |
static const GVariantType* |
| 43 |
_g_variant_type (char *type) |
| 44 |
{ |
| 45 |
return G_VARIANT_TYPE (type); |
| 46 |
} |
| 47 |
|
| 48 |
static int |
| 49 |
_at_fdcwd () |
| 50 |
{ |
| 51 |
return AT_FDCWD; |
| 52 |
} |
| 53 |
|
| 54 |
static guint64 |
| 55 |
_guint64_from_be (guint64 val) |
| 56 |
{ |
| 57 |
return GUINT64_FROM_BE (val); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
// These functions are wrappers for variadic functions since CGO can't parse variadic functions |
| 63 |
static void |
| 64 |
_g_printerr_onearg (char* msg, |
| 65 |
char* arg) |
| 66 |
{ |
| 67 |
g_printerr("%s %s\n", msg, arg); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
_g_set_error_onearg (GError *err, |
| 72 |
char* msg, |
| 73 |
char* arg) |
| 74 |
{ |
| 75 |
g_set_error(&err, G_IO_ERROR, G_IO_ERROR_FAILED, "%s %s", msg, arg); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
_g_variant_builder_add_twoargs (GVariantBuilder* builder, |
| 80 |
const char *format_string, |
| 81 |
char *arg1, |
| 82 |
GVariant *arg2) |
| 83 |
{ |
| 84 |
g_variant_builder_add(builder, format_string, arg1, arg2); |
| 85 |
} |
| 86 |
|
| 87 |
static GHashTable* |
| 88 |
_g_hash_table_new_full () |
| 89 |
{ |
| 90 |
return g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
_g_variant_get_commit_dump (GVariant *variant, |
| 95 |
const char *format, |
| 96 |
char **subject, |
| 97 |
char **body, |
| 98 |
guint64 *timestamp) |
| 99 |
{ |
| 100 |
return g_variant_get (variant, format, NULL, NULL, NULL, subject, body, timestamp, NULL, NULL); |
| 101 |
} |
| 102 |
|
| 103 |
static guint32 |
| 104 |
_binary_or (guint32 a, guint32 b) |
| 105 |
{ |
| 106 |
return a | b; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
_cleanup (OstreeRepo *self, |
| 111 |
OstreeRepoCommitModifier *modifier, |
| 112 |
GCancellable *cancellable, |
| 113 |
GError **out_error) |
| 114 |
{ |
| 115 |
if (self) |
| 116 |
ostree_repo_abort_transaction(self, cancellable, out_error); |
| 117 |
if (modifier) |
| 118 |
ostree_repo_commit_modifier_unref (modifier); |
| 119 |
} |
| 120 |
|
| 121 |
// The following functions make up a commit_filter function that gets passed into |
| 122 |
// another C function (and thus can't be a go function) as well as its helpers |
| 123 |
static OstreeRepoCommitFilterResult |
| 124 |
_commit_filter (OstreeRepo *self, |
| 125 |
const char *path, |
| 126 |
GFileInfo *file_info, |
| 127 |
gpointer user_data) |
| 128 |
{ |
| 129 |
struct CommitFilterData *data = user_data; |
| 130 |
GHashTable *mode_adds = data->mode_adds; |
| 131 |
GHashTable *skip_list = data->skip_list; |
| 132 |
gpointer value; |
| 133 |
|
| 134 |
if (owner_uid >= 0) |
| 135 |
g_file_info_set_attribute_uint32 (file_info, "unix::uid", owner_uid); |
| 136 |
if (owner_gid >= 0) |
| 137 |
g_file_info_set_attribute_uint32 (file_info, "unix::gid", owner_gid); |
| 138 |
|
| 139 |
if (mode_adds && g_hash_table_lookup_extended (mode_adds, path, NULL, &value)) |
| 140 |
{ |
| 141 |
guint current_mode = g_file_info_get_attribute_uint32 (file_info, "unix::mode"); |
| 142 |
guint mode_add = GPOINTER_TO_UINT (value); |
| 143 |
g_file_info_set_attribute_uint32 (file_info, "unix::mode", |
| 144 |
current_mode | mode_add); |
| 145 |
g_hash_table_remove (mode_adds, path); |
| 146 |
} |
| 147 |
|
| 148 |
if (skip_list && g_hash_table_contains (skip_list, path)) |
| 149 |
{ |
| 150 |
g_hash_table_remove (skip_list, path); |
| 151 |
return OSTREE_REPO_COMMIT_FILTER_SKIP; |
| 152 |
} |
| 153 |
|
| 154 |
return OSTREE_REPO_COMMIT_FILTER_ALLOW; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static void |
| 159 |
_set_owner_uid (guint32 uid) |
| 160 |
{ |
| 161 |
owner_uid = uid; |
| 162 |
} |
| 163 |
|
| 164 |
static void _set_owner_gid (guint32 gid) |
| 165 |
{ |
| 166 |
owner_gid = gid; |
| 167 |
} |
| 168 |
|
| 169 |
// Wrapper function for a function that takes a C function as a parameter. |
| 170 |
// That translation doesn't work in go |
| 171 |
static OstreeRepoCommitModifier* |
| 172 |
_ostree_repo_commit_modifier_new_wrapper (OstreeRepoCommitModifierFlags flags, |
| 173 |
gpointer user_data, |
| 174 |
GDestroyNotify destroy_notify) |
| 175 |
{ |
| 176 |
return ostree_repo_commit_modifier_new(flags, _commit_filter, user_data, destroy_notify); |
| 177 |
} |
| 178 |
|
| 179 |
#endif |