| 1 |
package otbuiltin |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
"time" |
| 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 |
// Declare global variable to store commitOptions |
| 22 |
var options commitOptions |
| 23 |
|
| 24 |
// Declare a function prototype for being passed into another function |
| 25 |
type handleLineFunc func(string, *glib.GHashTable) error |
| 26 |
|
| 27 |
// Contains all of the options for commmiting to an ostree repo. Initialize |
| 28 |
// with NewCommitOptions() |
| 29 |
type commitOptions struct { |
| 30 |
Subject string // One line subject |
| 31 |
Body string // Full description |
| 32 |
Parent string // Parent of the commit |
| 33 |
Tree []string // 'dir=PATH' or 'tar=TARFILE' or 'ref=COMMIT': overlay the given argument as a tree |
| 34 |
AddMetadataString []string // Add a key/value pair to metadata |
| 35 |
AddDetachedMetadataString []string // Add a key/value pair to detached metadata |
| 36 |
OwnerUID int // Set file ownership to user id |
| 37 |
OwnerGID int // Set file ownership to group id |
| 38 |
NoXattrs bool // Do not import extended attributes |
| 39 |
LinkCheckoutSpeedup bool // Optimize for commits of trees composed of hardlinks in the repository |
| 40 |
TarAutoCreateParents bool // When loading tar archives, automatically create parent directories as needed |
| 41 |
SkipIfUnchanged bool // If the contents are unchanged from a previous commit, do nothing |
| 42 |
StatOverrideFile string // File containing list of modifications to make permissions |
| 43 |
SkipListFile string // File containing list of file paths to skip |
| 44 |
GenerateSizes bool // Generate size information along with commit metadata |
| 45 |
GpgSign []string // GPG Key ID with which to sign the commit (if you have GPGME - GNU Privacy Guard Made Easy) |
| 46 |
GpgHomedir string // GPG home directory to use when looking for keyrings (if you have GPGME - GNU Privacy Guard Made Easy) |
| 47 |
Timestamp time.Time // Override the timestamp of the commit |
| 48 |
Orphan bool // Commit does not belong to a branch |
| 49 |
Fsync bool // Specify whether fsync should be used or not. Default to true |
| 50 |
} |
| 51 |
|
| 52 |
// Initializes a commitOptions struct and sets default values |
| 53 |
func NewCommitOptions() commitOptions { |
| 54 |
var co commitOptions |
| 55 |
co.OwnerUID = -1 |
| 56 |
co.OwnerGID = -1 |
| 57 |
co.Fsync = true |
| 58 |
return co |
| 59 |
} |
| 60 |
|
| 61 |
type OstreeRepoTransactionStats struct { |
| 62 |
metadata_objects_total int32 |
| 63 |
metadata_objects_written int32 |
| 64 |
content_objects_total int32 |
| 65 |
content_objects_written int32 |
| 66 |
content_bytes_written uint64 |
| 67 |
} |
| 68 |
|
| 69 |
func (repo *Repo) PrepareTransaction() (bool, error) { |
| 70 |
var cerr *C.GError = nil |
| 71 |
var resume C.gboolean |
| 72 |
|
| 73 |
r := glib.GoBool(glib.GBoolean(C.ostree_repo_prepare_transaction(repo.native(), &resume, nil, &cerr))) |
| 74 |
if !r { |
| 75 |
return false, generateError(cerr) |
| 76 |
} |
| 77 |
return glib.GoBool(glib.GBoolean(resume)), nil |
| 78 |
} |
| 79 |
|
| 80 |
func (repo *Repo) CommitTransaction() (*OstreeRepoTransactionStats, error) { |
| 81 |
var cerr *C.GError = nil |
| 82 |
var stats OstreeRepoTransactionStats = OstreeRepoTransactionStats{} |
| 83 |
statsPtr := (*C.OstreeRepoTransactionStats)(unsafe.Pointer(&stats)) |
| 84 |
r := glib.GoBool(glib.GBoolean(C.ostree_repo_commit_transaction(repo.native(), statsPtr, nil, &cerr))) |
| 85 |
if !r { |
| 86 |
return nil, generateError(cerr) |
| 87 |
} |
| 88 |
return &stats, nil |
| 89 |
} |
| 90 |
|
| 91 |
func (repo *Repo) TransactionSetRef(remote string, ref string, checksum string) { |
| 92 |
var cRemote *C.char = nil |
| 93 |
var cRef *C.char = nil |
| 94 |
var cChecksum *C.char = nil |
| 95 |
|
| 96 |
if remote != "" { |
| 97 |
cRemote = C.CString(remote) |
| 98 |
} |
| 99 |
if ref != "" { |
| 100 |
cRef = C.CString(ref) |
| 101 |
} |
| 102 |
if checksum != "" { |
| 103 |
cChecksum = C.CString(checksum) |
| 104 |
} |
| 105 |
C.ostree_repo_transaction_set_ref(repo.native(), cRemote, cRef, cChecksum) |
| 106 |
} |
| 107 |
|
| 108 |
func (repo *Repo) AbortTransaction() error { |
| 109 |
var cerr *C.GError = nil |
| 110 |
r := glib.GoBool(glib.GBoolean(C.ostree_repo_abort_transaction(repo.native(), nil, &cerr))) |
| 111 |
if !r { |
| 112 |
return generateError(cerr) |
| 113 |
} |
| 114 |
return nil |
| 115 |
} |
| 116 |
|
| 117 |
func (repo *Repo) RegenerateSummary() error { |
| 118 |
var cerr *C.GError = nil |
| 119 |
r := glib.GoBool(glib.GBoolean(C.ostree_repo_regenerate_summary(repo.native(), nil, nil, &cerr))) |
| 120 |
if !r { |
| 121 |
return generateError(cerr) |
| 122 |
} |
| 123 |
return nil |
| 124 |
} |
| 125 |
|
| 126 |
// Commits a directory, specified by commitPath, to an ostree repo as a given branch |
| 127 |
func (repo *Repo) Commit(commitPath, branch string, opts commitOptions) (string, error) { |
| 128 |
// TODO(lucab): `options` is global un-synchronized mutable state, get rid of it. |
| 129 |
options = opts |
| 130 |
|
| 131 |
var err error |
| 132 |
var modeAdds *glib.GHashTable |
| 133 |
var skipList *glib.GHashTable |
| 134 |
var objectToCommit *glib.GFile |
| 135 |
var skipCommit bool = false |
| 136 |
var ccommitChecksum *C.char |
| 137 |
defer C.free(unsafe.Pointer(ccommitChecksum)) |
| 138 |
var flags C.OstreeRepoCommitModifierFlags = 0 |
| 139 |
var filter_data C.CommitFilterData |
| 140 |
|
| 141 |
var cerr *C.GError |
| 142 |
defer C.free(unsafe.Pointer(cerr)) |
| 143 |
var metadata *C.GVariant = nil |
| 144 |
defer func() { |
| 145 |
if metadata != nil { |
| 146 |
defer C.g_variant_unref(metadata) |
| 147 |
} |
| 148 |
}() |
| 149 |
|
| 150 |
var detachedMetadata *C.GVariant = nil |
| 151 |
defer C.free(unsafe.Pointer(detachedMetadata)) |
| 152 |
var mtree *C.OstreeMutableTree |
| 153 |
defer C.free(unsafe.Pointer(mtree)) |
| 154 |
var root *C.GFile |
| 155 |
defer C.free(unsafe.Pointer(root)) |
| 156 |
var modifier *C.OstreeRepoCommitModifier |
| 157 |
defer C.free(unsafe.Pointer(modifier)) |
| 158 |
var cancellable *C.GCancellable |
| 159 |
defer C.free(unsafe.Pointer(cancellable)) |
| 160 |
|
| 161 |
cpath := C.CString(commitPath) |
| 162 |
defer C.free(unsafe.Pointer(cpath)) |
| 163 |
csubject := C.CString(options.Subject) |
| 164 |
defer C.free(unsafe.Pointer(csubject)) |
| 165 |
cbody := C.CString(options.Body) |
| 166 |
defer C.free(unsafe.Pointer(cbody)) |
| 167 |
cbranch := C.CString(branch) |
| 168 |
defer C.free(unsafe.Pointer(cbranch)) |
| 169 |
cparent := C.CString(options.Parent) |
| 170 |
defer C.free(unsafe.Pointer(cparent)) |
| 171 |
|
| 172 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_is_writable(repo.native(), &cerr))) { |
| 173 |
goto out |
| 174 |
} |
| 175 |
|
| 176 |
// If the user provided a stat override file |
| 177 |
if strings.Compare(options.StatOverrideFile, "") != 0 { |
| 178 |
modeAdds = glib.ToGHashTable(unsafe.Pointer(C._g_hash_table_new_full())) |
| 179 |
if err = parseFileByLine(options.StatOverrideFile, handleStatOverrideLine, modeAdds, cancellable); err != nil { |
| 180 |
goto out |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// If the user provided a skiplist file |
| 185 |
if strings.Compare(options.SkipListFile, "") != 0 { |
| 186 |
skipList = glib.ToGHashTable(unsafe.Pointer(C._g_hash_table_new_full())) |
| 187 |
if err = parseFileByLine(options.SkipListFile, handleSkipListline, skipList, cancellable); err != nil { |
| 188 |
goto out |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if options.AddMetadataString != nil { |
| 193 |
metadata, err = parseKeyValueStrings(options.AddMetadataString) |
| 194 |
if err != nil { |
| 195 |
goto out |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if options.AddDetachedMetadataString != nil { |
| 200 |
_, err = parseKeyValueStrings(options.AddDetachedMetadataString) |
| 201 |
if err != nil { |
| 202 |
goto out |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if strings.Compare(branch, "") == 0 && !options.Orphan { |
| 207 |
err = errors.New("A branch must be specified or use commitOptions.Orphan") |
| 208 |
goto out |
| 209 |
} |
| 210 |
|
| 211 |
if options.NoXattrs { |
| 212 |
C._ostree_repo_append_modifier_flags(&flags, C.OSTREE_REPO_COMMIT_MODIFIER_FLAGS_SKIP_XATTRS) |
| 213 |
} |
| 214 |
if options.GenerateSizes { |
| 215 |
C._ostree_repo_append_modifier_flags(&flags, C.OSTREE_REPO_COMMIT_MODIFIER_FLAGS_GENERATE_SIZES) |
| 216 |
} |
| 217 |
if !options.Fsync { |
| 218 |
C.ostree_repo_set_disable_fsync(repo.native(), C.TRUE) |
| 219 |
} |
| 220 |
|
| 221 |
if flags != 0 || options.OwnerUID >= 0 || options.OwnerGID >= 0 || strings.Compare(options.StatOverrideFile, "") != 0 || options.NoXattrs { |
| 222 |
filter_data.mode_adds = (*C.GHashTable)(modeAdds.Ptr()) |
| 223 |
filter_data.skip_list = (*C.GHashTable)(skipList.Ptr()) |
| 224 |
C._set_owner_uid((C.guint32)(options.OwnerUID)) |
| 225 |
C._set_owner_gid((C.guint32)(options.OwnerGID)) |
| 226 |
modifier = C._ostree_repo_commit_modifier_new_wrapper(flags, C.gpointer(&filter_data), nil) |
| 227 |
} |
| 228 |
|
| 229 |
if strings.Compare(options.Parent, "") != 0 { |
| 230 |
if strings.Compare(options.Parent, "none") == 0 { |
| 231 |
options.Parent = "" |
| 232 |
} |
| 233 |
} else if !options.Orphan { |
| 234 |
cerr = nil |
| 235 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_resolve_rev(repo.native(), cbranch, C.TRUE, &cparent, &cerr))) { |
| 236 |
goto out |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if options.LinkCheckoutSpeedup && !glib.GoBool(glib.GBoolean(C.ostree_repo_scan_hardlinks(repo.native(), cancellable, &cerr))) { |
| 241 |
goto out |
| 242 |
} |
| 243 |
|
| 244 |
mtree = C.ostree_mutable_tree_new() |
| 245 |
|
| 246 |
if len(commitPath) == 0 && (len(options.Tree) == 0 || len(options.Tree[0]) == 0) { |
| 247 |
currentDir := (*C.char)(C.g_get_current_dir()) |
| 248 |
objectToCommit = glib.ToGFile(unsafe.Pointer(C.g_file_new_for_path(currentDir))) |
| 249 |
C.g_free(C.gpointer(currentDir)) |
| 250 |
|
| 251 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_directory_to_mtree(repo.native(), (*C.GFile)(objectToCommit.Ptr()), mtree, modifier, cancellable, &cerr))) { |
| 252 |
goto out |
| 253 |
} |
| 254 |
} else if len(options.Tree) != 0 { |
| 255 |
var eq int = -1 |
| 256 |
cerr = nil |
| 257 |
for tree := range options.Tree { |
| 258 |
eq = strings.Index(options.Tree[tree], "=") |
| 259 |
if eq == -1 { |
| 260 |
C._g_set_error_onearg(cerr, C.CString("Missing type in tree specification"), C.CString(options.Tree[tree])) |
| 261 |
goto out |
| 262 |
} |
| 263 |
treeType := options.Tree[tree][:eq] |
| 264 |
treeVal := options.Tree[tree][eq+1:] |
| 265 |
|
| 266 |
if strings.Compare(treeType, "dir") == 0 { |
| 267 |
objectToCommit = glib.ToGFile(unsafe.Pointer(C.g_file_new_for_path(C.CString(treeVal)))) |
| 268 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_directory_to_mtree(repo.native(), (*C.GFile)(objectToCommit.Ptr()), mtree, modifier, cancellable, &cerr))) { |
| 269 |
goto out |
| 270 |
} |
| 271 |
} else if strings.Compare(treeType, "tar") == 0 { |
| 272 |
objectToCommit = glib.ToGFile(unsafe.Pointer(C.g_file_new_for_path(C.CString(treeVal)))) |
| 273 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_archive_to_mtree(repo.native(), (*C.GFile)(objectToCommit.Ptr()), mtree, modifier, (C.gboolean)(glib.GBool(opts.TarAutoCreateParents)), cancellable, &cerr))) { |
| 274 |
fmt.Println("error 1") |
| 275 |
goto out |
| 276 |
} |
| 277 |
} else if strings.Compare(treeType, "ref") == 0 { |
| 278 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_read_commit(repo.native(), C.CString(treeVal), (**C.GFile)(objectToCommit.Ptr()), nil, cancellable, &cerr))) { |
| 279 |
goto out |
| 280 |
} |
| 281 |
|
| 282 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_directory_to_mtree(repo.native(), (*C.GFile)(objectToCommit.Ptr()), mtree, modifier, cancellable, &cerr))) { |
| 283 |
goto out |
| 284 |
} |
| 285 |
} else { |
| 286 |
C._g_set_error_onearg(cerr, C.CString("Missing type in tree specification"), C.CString(treeVal)) |
| 287 |
goto out |
| 288 |
} |
| 289 |
} |
| 290 |
} else { |
| 291 |
objectToCommit = glib.ToGFile(unsafe.Pointer(C.g_file_new_for_path(cpath))) |
| 292 |
cerr = nil |
| 293 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_directory_to_mtree(repo.native(), (*C.GFile)(objectToCommit.Ptr()), mtree, modifier, cancellable, &cerr))) { |
| 294 |
goto out |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if modeAdds != nil && C.g_hash_table_size((*C.GHashTable)(modeAdds.Ptr())) > 0 { |
| 299 |
var hashIter *C.GHashTableIter |
| 300 |
|
| 301 |
var key, value C.gpointer |
| 302 |
|
| 303 |
C.g_hash_table_iter_init(hashIter, (*C.GHashTable)(modeAdds.Ptr())) |
| 304 |
|
| 305 |
for glib.GoBool(glib.GBoolean(C.g_hash_table_iter_next(hashIter, &key, &value))) { |
| 306 |
C._g_printerr_onearg(C.CString("Unmatched StatOverride path: "), C._gptr_to_str(key)) |
| 307 |
} |
| 308 |
err = errors.New("Unmatched StatOverride paths") |
| 309 |
C.free(unsafe.Pointer(hashIter)) |
| 310 |
C.free(unsafe.Pointer(key)) |
| 311 |
C.free(unsafe.Pointer(value)) |
| 312 |
goto out |
| 313 |
} |
| 314 |
|
| 315 |
if skipList != nil && C.g_hash_table_size((*C.GHashTable)(skipList.Ptr())) > 0 { |
| 316 |
var hashIter *C.GHashTableIter |
| 317 |
var key, value C.gpointer |
| 318 |
|
| 319 |
C.g_hash_table_iter_init(hashIter, (*C.GHashTable)(skipList.Ptr())) |
| 320 |
|
| 321 |
for glib.GoBool(glib.GBoolean(C.g_hash_table_iter_next(hashIter, &key, &value))) { |
| 322 |
C._g_printerr_onearg(C.CString("Unmatched SkipList path: "), C._gptr_to_str(key)) |
| 323 |
} |
| 324 |
err = errors.New("Unmatched SkipList paths") |
| 325 |
C.free(unsafe.Pointer(hashIter)) |
| 326 |
C.free(unsafe.Pointer(key)) |
| 327 |
C.free(unsafe.Pointer(value)) |
| 328 |
goto out |
| 329 |
} |
| 330 |
|
| 331 |
cerr = nil |
| 332 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_mtree(repo.native(), mtree, &root, cancellable, &cerr))) { |
| 333 |
goto out |
| 334 |
} |
| 335 |
|
| 336 |
if options.SkipIfUnchanged && strings.Compare(options.Parent, "") != 0 { |
| 337 |
var parentRoot *C.GFile |
| 338 |
|
| 339 |
cerr = nil |
| 340 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_read_commit(repo.native(), cparent, &parentRoot, nil, cancellable, &cerr))) { |
| 341 |
C.free(unsafe.Pointer(parentRoot)) |
| 342 |
goto out |
| 343 |
} |
| 344 |
|
| 345 |
if glib.GoBool(glib.GBoolean(C.g_file_equal(root, parentRoot))) { |
| 346 |
skipCommit = true |
| 347 |
} |
| 348 |
C.free(unsafe.Pointer(parentRoot)) |
| 349 |
} |
| 350 |
|
| 351 |
if !skipCommit { |
| 352 |
var timestamp C.guint64 |
| 353 |
|
| 354 |
if options.Timestamp.IsZero() { |
| 355 |
var now *C.GDateTime = C.g_date_time_new_now_utc() |
| 356 |
timestamp = (C.guint64)(C.g_date_time_to_unix(now)) |
| 357 |
C.g_date_time_unref(now) |
| 358 |
|
| 359 |
cerr = nil |
| 360 |
ret := C.ostree_repo_write_commit(repo.native(), cparent, csubject, cbody, metadata, C._ostree_repo_file(root), &ccommitChecksum, cancellable, &cerr) |
| 361 |
if !glib.GoBool(glib.GBoolean(ret)) { |
| 362 |
goto out |
| 363 |
} |
| 364 |
} else { |
| 365 |
timestamp = (C.guint64)(options.Timestamp.Unix()) |
| 366 |
|
| 367 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_write_commit_with_time(repo.native(), cparent, csubject, cbody, |
| 368 |
metadata, C._ostree_repo_file(root), timestamp, &ccommitChecksum, cancellable, &cerr))) { |
| 369 |
goto out |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if detachedMetadata != nil { |
| 374 |
C.ostree_repo_write_commit_detached_metadata(repo.native(), ccommitChecksum, detachedMetadata, cancellable, &cerr) |
| 375 |
} |
| 376 |
|
| 377 |
if len(options.GpgSign) != 0 { |
| 378 |
for key := range options.GpgSign { |
| 379 |
if !glib.GoBool(glib.GBoolean(C.ostree_repo_sign_commit(repo.native(), (*C.gchar)(ccommitChecksum), (*C.gchar)(C.CString(options.GpgSign[key])), (*C.gchar)(C.CString(options.GpgHomedir)), cancellable, &cerr))) { |
| 380 |
goto out |
| 381 |
} |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if strings.Compare(branch, "") != 0 { |
| 386 |
C.ostree_repo_transaction_set_ref(repo.native(), nil, cbranch, ccommitChecksum) |
| 387 |
} else if !options.Orphan { |
| 388 |
goto out |
| 389 |
} else { |
| 390 |
// TODO: Looks like I forgot to implement this. |
| 391 |
} |
| 392 |
} else { |
| 393 |
ccommitChecksum = C.CString(options.Parent) |
| 394 |
} |
| 395 |
|
| 396 |
return C.GoString(ccommitChecksum), nil |
| 397 |
out: |
| 398 |
if repo.native() != nil { |
| 399 |
C.ostree_repo_abort_transaction(repo.native(), cancellable, nil) |
| 400 |
//C.free(unsafe.Pointer(repo.native())) |
| 401 |
} |
| 402 |
if modifier != nil { |
| 403 |
C.ostree_repo_commit_modifier_unref(modifier) |
| 404 |
} |
| 405 |
if err != nil { |
| 406 |
return "", err |
| 407 |
} |
| 408 |
return "", generateError(cerr) |
| 409 |
} |
| 410 |
|
| 411 |
// Parse an array of key value pairs of the format KEY=VALUE and add them to a GVariant |
| 412 |
func parseKeyValueStrings(pairs []string) (*C.GVariant, error) { |
| 413 |
builder := C.g_variant_builder_new(C._g_variant_type(C.CString("a{sv}"))) |
| 414 |
defer C.g_variant_builder_unref(builder) |
| 415 |
|
| 416 |
for iter := range pairs { |
| 417 |
index := strings.Index(pairs[iter], "=") |
| 418 |
if index <= 0 { |
| 419 |
var buffer bytes.Buffer |
| 420 |
buffer.WriteString("Missing '=' in KEY=VALUE metadata '%s'") |
| 421 |
buffer.WriteString(pairs[iter]) |
| 422 |
return nil, errors.New(buffer.String()) |
| 423 |
} |
| 424 |
|
| 425 |
key := C.CString(pairs[iter][:index]) |
| 426 |
value := C.CString(pairs[iter][index+1:]) |
| 427 |
|
| 428 |
valueVariant := C.g_variant_new_string((*C.gchar)(value)) |
| 429 |
|
| 430 |
C._g_variant_builder_add_twoargs(builder, C.CString("{sv}"), key, valueVariant) |
| 431 |
} |
| 432 |
|
| 433 |
metadata := C.g_variant_builder_end(builder) |
| 434 |
return C.g_variant_ref_sink(metadata), nil |
| 435 |
} |
| 436 |
|
| 437 |
// Parse a file linue by line and handle the line with the handleLineFunc |
| 438 |
func parseFileByLine(path string, fn handleLineFunc, table *glib.GHashTable, cancellable *C.GCancellable) error { |
| 439 |
var contents *C.char |
| 440 |
var file *glib.GFile |
| 441 |
var lines []string |
| 442 |
var gerr = glib.NewGError() |
| 443 |
cerr := (*C.GError)(gerr.Ptr()) |
| 444 |
|
| 445 |
file = glib.ToGFile(unsafe.Pointer(C.g_file_new_for_path(C.CString(path)))) |
| 446 |
if !glib.GoBool(glib.GBoolean(C.g_file_load_contents((*C.GFile)(file.Ptr()), cancellable, &contents, nil, nil, &cerr))) { |
| 447 |
return generateError(cerr) |
| 448 |
} |
| 449 |
|
| 450 |
lines = strings.Split(C.GoString(contents), "\n") |
| 451 |
for line := range lines { |
| 452 |
if strings.Compare(lines[line], "") == 0 { |
| 453 |
continue |
| 454 |
} |
| 455 |
|
| 456 |
if err := fn(lines[line], table); err != nil { |
| 457 |
return generateError(cerr) |
| 458 |
} |
| 459 |
} |
| 460 |
return nil |
| 461 |
} |
| 462 |
|
| 463 |
// Handle an individual line from a Statoverride file |
| 464 |
func handleStatOverrideLine(line string, table *glib.GHashTable) error { |
| 465 |
var space int |
| 466 |
var modeAdd C.guint |
| 467 |
|
| 468 |
if space = strings.IndexRune(line, ' '); space == -1 { |
| 469 |
return errors.New("Malformed StatOverrideFile (no space found)") |
| 470 |
} |
| 471 |
|
| 472 |
modeAdd = (C.guint)(C.g_ascii_strtod((*C.gchar)(C.CString(line)), nil)) |
| 473 |
C.g_hash_table_insert((*C.GHashTable)(table.Ptr()), C.gpointer(C.g_strdup((*C.gchar)(C.CString(line[space+1:])))), C._guint_to_pointer(modeAdd)) |
| 474 |
|
| 475 |
return nil |
| 476 |
} |
| 477 |
|
| 478 |
// Handle an individual line from a Skiplist file |
| 479 |
func handleSkipListline(line string, table *glib.GHashTable) error { |
| 480 |
C.g_hash_table_add((*C.GHashTable)(table.Ptr()), C.gpointer(C.g_strdup((*C.gchar)(C.CString(line))))) |
| 481 |
|
| 482 |
return nil |
| 483 |
} |