summaryrefslogtreecommitdiff
path: root/src/libostree/ostree-kernel-args.c
blob: c6300823dc122144bc99f4942afe9699ff880151 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
 * Copyright (C) 2013,2014 Colin Walters <walters@verbum.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "ostree-kernel-args.h"
#include "libglnx.h"
#include "otutil.h"

#include <string.h>

struct _OstreeKernelArgs {
  GPtrArray  *order;
  GHashTable *table;
};

struct _OstreeKernelArgsEntry {
  char *key;
  char *value;
};

char *
_ostree_kernel_args_entry_get_key (const OstreeKernelArgsEntry *e)
{
  return e->key;
}

char *
_ostree_kernel_args_entry_get_value (const OstreeKernelArgsEntry *e)
{
  return e->value;
}

void
_ostree_kernel_args_entry_set_key (OstreeKernelArgsEntry *e,
                                   char  *key)
{
  e->key = key;
}

void
_ostree_kernel_args_entry_set_value (OstreeKernelArgsEntry *e,
                                     char  *value)
{
  e->value = value;
}

char *
_ostree_kernel_args_get_key_index (const OstreeKernelArgs *kargs,
                                   int i)
{
  OstreeKernelArgsEntry *e = kargs->order->pdata[i];
  return e->key;
}

char *
_ostree_kernel_args_get_value_index (const OstreeKernelArgs *kargs,
                                     int i)
{
  OstreeKernelArgsEntry *e = kargs->order->pdata[i];
  return e->value;
}

OstreeKernelArgsEntry *
_ostree_kernel_args_entry_new (void)
{
  return g_new0 (OstreeKernelArgsEntry, 1);
}

void
_ostree_kernel_args_entry_value_free (OstreeKernelArgsEntry *e)
{
  g_clear_pointer (&e->value, g_free);
}

/* Free the value field, and the entry.  This should be set as the free
 * function, for all pointer arrays stored in the hash table.
 */
static void
kernel_args_entry_free_from_table (gpointer data)
{
  OstreeKernelArgsEntry *e = data;
  // The hash table owns the key; do not free it here.
  g_free (_ostree_kernel_args_entry_get_value (e));
  g_free (e);
}

static gboolean
kernel_args_entry_value_equal (gconstpointer data,
                               gconstpointer value)
{
  const OstreeKernelArgsEntry *e = data;
  return g_strcmp0 (_ostree_kernel_args_entry_get_value (e), value) == 0;
}

static gboolean
kernel_args_entry_key_equal (gconstpointer data,
                             gconstpointer key)
{
  const OstreeKernelArgsEntry *e = data;
  return g_strcmp0 (_ostree_kernel_args_entry_get_key (e), key) == 0;
}

static void
kernel_args_entry_replace_value (OstreeKernelArgsEntry *e,
                                 const char *value)
{
  g_assert (e);
  _ostree_kernel_args_entry_value_free (e);
  _ostree_kernel_args_entry_set_value (e, g_strdup (value));
}

static void
kernel_args_remove_entries_from_order (GPtrArray *order,
                                       GPtrArray *entries)
{
  g_assert (entries);
  for (int i = 0; i < entries->len; i++)
    g_assert (g_ptr_array_remove (order, entries->pdata[i]));
}

static char *
split_keyeq (char *arg)
{
  char *eq;

  eq = strchr (arg, '=');
  if (eq == NULL)
    return NULL;

  // Note: key/val are in a single allocation block, so we don't free val.
  *eq = '\0';
  return eq+1;
}

static gboolean
_arg_has_prefix (const char *arg,
                 char      **prefixes)
{
  char **strviter;

  for (strviter = prefixes; strviter && *strviter; strviter++)
    {
      const char *prefix = *strviter;

      if (g_str_has_prefix (arg, prefix))
        return TRUE;
    }

  return FALSE;
}

static gboolean
strcmp0_equal (gconstpointer v1,
               gconstpointer v2)
{
  return g_strcmp0 (v1, v2) == 0;
}

/**
 * ostree_kernel_args_new: (skip)
 *
 * Initializes a new OstreeKernelArgs structure and returns it
 *
 * Returns: (transfer full): A newly created #OstreeKernelArgs for kernel arguments
 *
 * Since: 2019.3
 **/
OstreeKernelArgs *
ostree_kernel_args_new (void)
{
  OstreeKernelArgs *ret;
  ret = g_new0 (OstreeKernelArgs, 1);
  /* Hash table owns the kernel args entries, since it uses keys to index,
   * and its values are used to locate entries in the order array. */
  ret->table = g_hash_table_new_full (g_str_hash, g_str_equal,
                                      g_free, (GDestroyNotify)g_ptr_array_unref);
  ret->order = g_ptr_array_new_with_free_func (NULL);
  return ret;
}

/**
 * ostree_kernel_args_free:
 * @kargs: An OstreeKernelArgs that represents kernel arguments
 *
 * Frees the kargs structure
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_free (OstreeKernelArgs *kargs)
{
  if (!kargs)
    return;
  g_ptr_array_unref (kargs->order);
  g_hash_table_unref (kargs->table);
  g_free (kargs);
}

/**
 * ostree_kernel_args_cleanup:
 * @loc: Address of an OstreeKernelArgs pointer
 *
 * Frees the OstreeKernelArgs structure pointed by *loc
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_cleanup (void *loc)
{
  ostree_kernel_args_free (*((OstreeKernelArgs**)loc));
}

/**
 * _ostree_kernel_arg_get_kargs_table:
 * @kargs: An OstreeKernelArgs that represents kernel arguments
 *
 * Returns: (transfer none): #GHashTable that associates with the @kargs
 *
 * Note: this function is private for now, since the data structures underneath might be changed
 *
 * Since: 2019.3
 **/
GHashTable*
_ostree_kernel_arg_get_kargs_table (OstreeKernelArgs *kargs)
{
  if (kargs != NULL)
    return kargs->table;
  return NULL;
}

/**
 * _ostree_kernel_arg_get_key_array:
 * @kargs: An OstreeKernelArgs that represents kernel arguments
 *
 * Returns: (transfer none) (element-type utf8): #GPtrArray that associates with @kargs
 *
 * Note: this function is private for now, since the data structures underneath might be changed
 *
 * Since: 2019.3
 **/
GPtrArray*
_ostree_kernel_arg_get_key_array (OstreeKernelArgs *kargs)
{
  if (kargs != NULL)
    return kargs->order;
  return NULL;
}

/**
 * ostree_kernel_args_new_replace:
 * @kargs: OstreeKernelArgs instance
 * @arg: a string argument
 * @error: error instance
 *
 * This function implements the basic logic behind key/value pair
 * replacement. Do note that the arg need to be properly formatted
 *
 * When replacing key with exact one value, the arg can be in
 * the form:
 * key, key=new_val, or key=old_val=new_val
 * The first one swaps the old_val with the key to an empty value
 * The second and third replace the old_val into the new_val
 *
 * When replacing key with multiple values, the arg can only be
 * in the form of:
 * key=old_val=new_val. Unless there is a special case where
 * there is an empty value associated with the key, then
 * key=new_val will work because old_val is empty. The empty
 * val will be swapped with the new_val in that case
 *
 * Returns: %TRUE on success, %FALSE on failure (and in some other instances such as:
 * 1. key not found in @kargs
 * 2. old value not found when @arg is in the form of key=old_val=new_val
 * 3. multiple old values found when @arg is in the form of key=old_val)
 *
 * Since: 2019.3
 **/
gboolean
ostree_kernel_args_new_replace (OstreeKernelArgs *kargs,
                                const char       *arg,
                                GError          **error)
{
  g_autofree char *arg_owned = g_strdup (arg);
  const char *key = arg_owned;
  const char *val = split_keyeq (arg_owned);

  GPtrArray *entries = g_hash_table_lookup (kargs->table, key);
  if (!entries)
    return glnx_throw (error, "No key '%s' found", key);
  g_assert_cmpuint (entries->len, >, 0);

  /* first handle the case where the user just wants to replace an old value */
  if (val && strchr (val, '='))
    {
      g_autofree char *old_val = g_strdup (val);
      const char *new_val = split_keyeq (old_val);
      g_assert (new_val);

      guint i = 0;
      if (!ot_ptr_array_find_with_equal_func (entries, old_val, kernel_args_entry_value_equal, &i))
        return glnx_throw (error, "No karg '%s=%s' found", key, old_val);

      kernel_args_entry_replace_value (entries->pdata[i], new_val);
      return TRUE;
    }

  /* can't know which val to replace without the old_val=new_val syntax */
  if (entries->len > 1)
    return glnx_throw (error, "Multiple values for key '%s' found", key);

  kernel_args_entry_replace_value (entries->pdata[0], val);
  return TRUE;
}

/**
 * ostree_kernel_args_delete_key_entry
 * @kargs: an OstreeKernelArgs instance
 * @key: the key to remove
 * @error: an GError instance
 *
 * This function removes the key entry from the hashtable
 * as well from the order pointer array inside kargs
 *
 * Note: since both table and order inside kernel args
 * are with free function, no extra free functions are
 * being called as they are done automatically by GLib
 *
 * Returns: %TRUE on success, %FALSE on failure
 *
 * Since: 2019.3
 **/
gboolean
ostree_kernel_args_delete_key_entry (OstreeKernelArgs *kargs,
                                     const char       *key,
                                     GError          **error)
{
  GPtrArray *entries = g_hash_table_lookup (kargs->table, key);
  if (!entries)
    return glnx_throw (error, "No key '%s' found", key);
  g_assert_cmpuint (entries->len, >, 0);

  kernel_args_remove_entries_from_order (kargs->order, entries);

  if (!g_hash_table_remove (kargs->table, key))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Failed to find kernel argument '%s'",
                   key);
      return FALSE;
    }

  return TRUE;
}

/**
 *  ostree_kernel_args_delete:
 *  @kargs: a OstreeKernelArgs instance
 *  @arg: key or key/value pair for deletion
 *  @error: an GError instance
 *
 *  There are few scenarios being handled for deletion:
 *
 *  1: for input arg with a single key(i.e without = for split),
 *  the key/value pair will be deleted if there is only
 *  one value that is associated with the key
 *
 *  2: for input arg wth key/value pair, the specific key
 *  value pair will be deleted from the pointer array
 *  if those exist.
 *
 *  3: If the found key has only one value
 *  associated with it, the key entry in the table will also
 *  be removed, and the key will be removed from order table
 *
 *  Returns: %TRUE on success, %FALSE on failure
 *
 *  Since: 2019.3
 **/
gboolean
ostree_kernel_args_delete (OstreeKernelArgs  *kargs,
                           const char        *arg,
                           GError           **error)
{
  g_autofree char *arg_owned = g_strdup (arg);
  const char *key = arg_owned;
  const char *val = split_keyeq (arg_owned);

  GPtrArray *entries = g_hash_table_lookup (kargs->table, key);
  if (!entries)
    return glnx_throw (error, "No key '%s' found", key);
  g_assert_cmpuint (entries->len, >, 0);

  /* special-case: we allow deleting by key only if there's only one val */
  if (entries->len == 1)
    {
      /* but if a specific val was passed, check that it's the same */
      OstreeKernelArgsEntry *e = entries->pdata[0];
      if (val && !strcmp0_equal (val, _ostree_kernel_args_entry_get_value (e)))
        return glnx_throw (error, "No karg '%s=%s' found", key, val);
      return ostree_kernel_args_delete_key_entry (kargs, key, error);
    }

  /* note val might be NULL here, in which case we're looking for `key`, not `key=` or
   * `key=val` */
  guint i = 0;
  if (!ot_ptr_array_find_with_equal_func (entries, val, kernel_args_entry_value_equal, &i))
    {
      if (!val)
        /* didn't find NULL -> only key= key=val1 key=val2 style things left, so the user
         * needs to be more specific */
        return glnx_throw (error, "Multiple values for key '%s' found", arg);
      return glnx_throw (error, "No karg '%s' found", arg);
    }

  g_assert (g_ptr_array_remove (kargs->order, entries->pdata[i]));
  g_assert (g_ptr_array_remove_index (entries, i));
  return TRUE;
}

/**
 * ostree_kernel_args_replace_take:
 * @kargs: a OstreeKernelArgs instance
 * @arg: (transfer full): key or key/value pair for replacement
 *
 * Finds and replaces the old key if @arg is already in the hash table,
 * otherwise adds @arg as new key and split_keyeq (arg) as value.
 * Note that when replacing old key, the old values are freed.
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_replace_take (OstreeKernelArgs   *kargs,
                                 char               *arg)
{
  gboolean existed;
  GPtrArray *entries = g_ptr_array_new_with_free_func (kernel_args_entry_free_from_table);
  const char *value = split_keyeq (arg);
  gpointer old_key;

  OstreeKernelArgsEntry *entry = g_new0 (OstreeKernelArgsEntry, 1);
  _ostree_kernel_args_entry_set_value (entry, g_strdup (value));
  g_ptr_array_add (entries, entry);

  gpointer old_entries_ptr;
  existed = g_hash_table_lookup_extended (kargs->table, arg, &old_key, &old_entries_ptr);
  GPtrArray *old_entries = old_entries_ptr;

  if (existed)
    {
      g_assert (old_entries);
      g_assert_cmpuint (old_entries->len, >, 0);

      guint old_order_index = 0;
      g_assert (ot_ptr_array_find_with_equal_func (kargs->order, old_key, kernel_args_entry_key_equal, &old_order_index));
      kernel_args_remove_entries_from_order (kargs->order, old_entries);

      g_assert_cmpstr (old_key, ==, arg);
      _ostree_kernel_args_entry_set_key (entry, old_key);
      g_ptr_array_insert (kargs->order, old_order_index, entry);
      // `arg` is freed by the `g_hash_table_insert` call.
      g_hash_table_insert (kargs->table, arg, entries);
    }
  else
    {
      _ostree_kernel_args_entry_set_key (entry, arg);
      g_hash_table_replace (kargs->table, arg, entries);
      g_ptr_array_add (kargs->order, entry);
    }
}

/**
 * ostree_kernel_args_replace:
 * @kargs: a OstreeKernelArgs instance
 * @arg: key or key/value pair for replacement
 *
 * Finds and replaces the old key if @arg is already in the hash table,
 * otherwise adds @arg as new key and split_keyeq (arg) as value.
 * Note that when replacing old key value pair, the old values are freed.
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_replace (OstreeKernelArgs  *kargs,
                            const char        *arg)
{
  ostree_kernel_args_replace_take (kargs, g_strdup (arg));
}

/**
 * ostree_kernel_args_append:
 * @kargs: a OstreeKernelArgs instance
 * @arg: key or key/value pair to be added
 *
 * Appends @arg which is in the form of key=value pair to the hash table kargs->table
 * (appends to the value list if key is already in the hash table)
 * and appends key to kargs->order if it is not in the hash table already.
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_append (OstreeKernelArgs  *kargs,
                           const char        *arg)
{
  gboolean existed = TRUE;
  GPtrArray *entries = NULL;
  char *duped = g_strdup (arg);
  const char *val = split_keyeq (duped);

  entries = g_hash_table_lookup (kargs->table, duped);
  if (!entries)
    {
      entries = g_ptr_array_new_with_free_func (kernel_args_entry_free_from_table);
      existed = FALSE;
    }

  OstreeKernelArgsEntry *entry = _ostree_kernel_args_entry_new ();
  _ostree_kernel_args_entry_set_key (entry, duped);
  _ostree_kernel_args_entry_set_value (entry, g_strdup (val));

  g_ptr_array_add (entries, entry);
  g_ptr_array_add (kargs->order, entry);

  if (!existed)
    g_hash_table_replace (kargs->table, duped, entries);
}

/**
 * ostree_kernel_args_replace_argv:
 * @kargs: a OstreeKernelArgs instance
 * @argv: an array of key or key/value pairs
 *
 * Finds and replaces each non-null arguments of @argv in the hash table,
 * otherwise adds individual arg as new key and split_keyeq (arg) as value.
 * Note that when replacing old key value pair, the old values are freed.
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_replace_argv (OstreeKernelArgs  *kargs,
                                 char             **argv)
{
  char **strviter;

  for (strviter = argv; strviter && *strviter; strviter++)
    {
      const char *arg = *strviter;
      ostree_kernel_args_replace (kargs, arg);
    }
}

/**
 * ostree_kernel_args_append_argv_filtered:
 * @kargs: a OstreeKernelArgs instance
 * @argv: an array of key=value argument pairs
 * @prefixes: an array of prefix strings
 *
 * Appends each argument that does not have one of the @prefixes as prefix to the @kargs
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_append_argv_filtered (OstreeKernelArgs  *kargs,
                                         char             **argv,
                                         char             **prefixes)
{
  char **strviter;

  for (strviter = argv; strviter && *strviter; strviter++)
    {
      const char *arg = *strviter;

      if (!_arg_has_prefix (arg, prefixes))
        ostree_kernel_args_append (kargs, arg);
    }
}

/**
 * ostree_kernel_args_append_argv:
 * @kargs: a OstreeKernelArgs instance
 * @argv: an array of key=value argument pairs
 *
 * Appends each value in @argv to the corresponding value array and
 * appends key to kargs->order if it is not in the hash table already.
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_append_argv (OstreeKernelArgs  *kargs,
                                char             **argv)
{
  ostree_kernel_args_append_argv_filtered (kargs, argv, NULL);
}

/**
 * ostree_kernel_args_append_proc_cmdline:
 * @kargs: a OstreeKernelArgs instance
 * @cancellable: optional GCancellable object, NULL to ignore
 * @error: an GError instance
 *
 * Appends the command line arguments in the file "/proc/cmdline"
 * that does not have "BOOT_IMAGE=" and "initrd=" as prefixes to the @kargs
 *
 * Returns: %TRUE on success, %FALSE on failure
 *
 * Since: 2019.3
 **/
gboolean
ostree_kernel_args_append_proc_cmdline (OstreeKernelArgs *kargs,
                                        GCancellable     *cancellable,
                                        GError          **error)
{
  g_autoptr(GFile) proc_cmdline_path = g_file_new_for_path ("/proc/cmdline");
  g_autofree char *proc_cmdline = NULL;
  gsize proc_cmdline_len = 0;
  g_auto(GStrv) proc_cmdline_args = NULL;
  /* When updating the filter list don't forget to update the list in the tests
   * e.g. tests/test-admin-deploy-karg.sh and
   * tests/test-admin-instutil-set-kargs.sh
   */
  char *filtered_prefixes[] = { "BOOT_IMAGE=", /* GRUB 2 */
                                "initrd=", /* sd-boot */
                                NULL };

  if (!g_file_load_contents (proc_cmdline_path, cancellable,
                             &proc_cmdline, &proc_cmdline_len,
                             NULL, error))
    return FALSE;

  g_strchomp (proc_cmdline);

  proc_cmdline_args = g_strsplit (proc_cmdline, " ", -1);
  ostree_kernel_args_append_argv_filtered (kargs, proc_cmdline_args,
                                            filtered_prefixes);

  return TRUE;
}

/**
 * ostree_kernel_args_parse_append:
 * @kargs: a OstreeKernelArgs instance
 * @options: a string representing command line arguments
 *
 * Parses @options by separating it by whitespaces and appends each argument to @kargs
 *
 * Since: 2019.3
 **/
void
ostree_kernel_args_parse_append (OstreeKernelArgs *kargs,
                                 const char       *options)
{
  char **args = NULL;
  char **iter;

  if (!options)
    return;

  args = g_strsplit (options, " ", -1);
  for (iter = args; *iter; iter++)
    {
      char *arg = *iter;
      ostree_kernel_args_append (kargs, arg);
    }
  g_strfreev (args);
}

/**
 * ostree_kernel_args_from_string: (skip)
 * @options: a string representing command line arguments
 *
 * Initializes a new OstreeKernelArgs then parses and appends @options
 * to the empty OstreeKernelArgs
 *
 * Returns: (transfer full): newly allocated #OstreeKernelArgs with @options appended
 *
 * Since: 2019.3
 **/
OstreeKernelArgs *
ostree_kernel_args_from_string (const char *options)
{
  OstreeKernelArgs *ret;

  ret = ostree_kernel_args_new ();
  ostree_kernel_args_parse_append (ret, options);

  return ret;
}

/**
 * ostree_kernel_args_to_strv:
 * @kargs: a OstreeKernelArgs instance
 *
 * Extracts all key value pairs in @kargs and appends to a temporary
 * array in forms of "key=value" or "key" if value is NULL, and returns
 * the temporary array with the GPtrArray wrapper freed
 *
 * Returns: (transfer full): an array of "key=value" pairs or "key" if value is NULL
 *
 * Since: 2019.3
 **/
char **
ostree_kernel_args_to_strv (OstreeKernelArgs *kargs)
{
  GPtrArray *strv = g_ptr_array_new ();
  guint i;

  for (i = 0; i < kargs->order->len; i++)
    {
      const char *key = _ostree_kernel_args_get_key_index (kargs, i);
      const char *value = _ostree_kernel_args_get_value_index (kargs, i);

      if (value == NULL)
        g_ptr_array_add (strv, g_strconcat (key, NULL));
      else
        g_ptr_array_add (strv, g_strconcat (key, "=", value, NULL));
    }
  g_ptr_array_add (strv, NULL);

  return (char**)g_ptr_array_free (strv, FALSE);
}

/**
 * ostree_kernel_args_to_string:
 * @kargs: a OstreeKernelArgs instance
 *
 * Extracts all key value pairs in @kargs and appends to a temporary
 * GString in forms of "key=value" or "key" if value is NULL separated
 * by a single whitespace, and returns the temporary string with the
 * GString wrapper freed
 *
 * Note: the application will be terminated if one of the values array
 * in @kargs is NULL
 *
 * Returns: (transfer full): a string of "key=value" pairs or "key" if value is NULL,
 * separated by single whitespaces
 *
 * Since: 2019.3
 **/
char *
ostree_kernel_args_to_string (OstreeKernelArgs *kargs)
{
  GString *buf = g_string_new ("");
  gboolean first = TRUE;
  guint i;

  for (i = 0; i < kargs->order->len; i++)
    {
      const char *key = _ostree_kernel_args_get_key_index (kargs, i);
      const char *value = _ostree_kernel_args_get_value_index (kargs, i);

      if (first)
        first = FALSE;
      else
        g_string_append_c (buf, ' ');

      g_string_append (buf, key);
      if (value != NULL)
        {
          g_string_append_c (buf, '=');
          g_string_append (buf, value);
        }
    }

  return g_string_free (buf, FALSE);
}

/**
 * ostree_kernel_args_get_last_value:
 * @kargs: a OstreeKernelArgs instance
 * @key: a key to look for in @kargs hash table
 *
 * Finds and returns the last element of value array
 * corresponding to the @key in @kargs hash table. Note that the application
 * will be terminated if the @key is found but the value array is empty
 *
 * Returns: NULL if @key is not found in the @kargs hash table,
 * otherwise returns last element of value array corresponding to @key
 *
 * Since: 2019.3
 **/
const char *
ostree_kernel_args_get_last_value (OstreeKernelArgs *kargs, const char *key)
{
  const GPtrArray *entries = g_hash_table_lookup (kargs->table, key);

  if (!entries)
    return NULL;

  g_assert (entries->len > 0);
  const OstreeKernelArgsEntry *e = entries->pdata[entries->len-1];
  return _ostree_kernel_args_entry_get_value (e);
}