lib/bootconfig-parser: Remove support to preserve comments in BLS files

OSTree has some logic to preserve comment lines in the BLS fragments, but
the BLS fragments are always created on new deployments so the comments
are never carried.

Also, OSTree never writes BLS fragments with comments so these will only
be present in BLS files that were modified outside of OSTree. Something
that should be avoided in general.

Finally, there is a bug in the logic that causes BLS files to have lines
with only a newline character.

The ostree_bootconfig_parser_parse_at() function reads the bootconfig file
using glnx_fd_readall_utf8() but this function NUL terminates the returned
string with the file contents.

So when the string is later split using '\n' as delimiter, the last token
is set to '\0' and a wrong GVariant will be added to the lines GPtrArray
in the OstreeBootconfigParser struct.

This will lead to bootconfig files that contains lines with only a newline
character, since the key in the GVariant would be set to NUL and won't be
present in the options GHashTable of the OstreeBootconfigParser struct.

So let's just remove that logic since is never used and makes BLS files to
have wrong empty lines.

Before this patch:

$ tail -n 4 /boot/loader/entries/ostree-1-testos.conf | hexdump -C
00000000  74 69 74 6c 65 20 54 65  73 74 4f 53 20 34 32 20  |title TestOS 42 |
00000010  32 30 31 39 30 38 32 34  2e 30 20 28 6f 73 74 72  |20190824.0 (ostr|
00000020  65 65 29 0a 0a 0a 0a                              |ee)....|
00000027

After this patch:

$ tail -n 4 /boot/loader/entries/ostree-1-testos.conf | hexdump -C
00000000  76 65 72 73 69 6f 6e 20  31 0a 6f 70 74 69 6f 6e  |version 1.option|
00000010  73 20 72 6f 6f 74 3d 4c  41 42 45 4c 3d 4d 4f 4f  |s root=LABEL=MOO|
00000020  20 71 75 69 65 74 20 6f  73 74 72 65 65 3d 2f 6f  | quiet ostree=/o|
00000030  73 74 72 65 65 2f 62 6f  6f 74 2e 31 2f 74 65 73  |stree/boot.1/tes|
00000040  74 6f 73 2f 61 65 34 36  34 39 36 38 30 64 33 65  |tos/ae4649680d3e|
00000050  38 33 62 32 34 65 34 37  66 38 64 66 31 30 38 31  |83b24e47f8df1081|
00000060  38 62 66 36 39 38 39 64  36 34 37 61 62 32 38 38  |8bf6989d647ab288|
00000070  64 31 63 30 39 38 30 36  65 34 61 33 36 61 34 65  |d1c09806e4a36a4e|
00000080  62 62 66 36 2f 30 0a 6c  69 6e 75 78 20 2f 6f 73  |bbf6/0.linux /os|
00000090  74 72 65 65 2f 74 65 73  74 6f 73 2d 61 65 34 36  |tree/testos-ae46|
000000a0  34 39 36 38 30 64 33 65  38 33 62 32 34 65 34 37  |49680d3e83b24e47|
000000b0  66 38 64 66 31 30 38 31  38 62 66 36 39 38 39 64  |f8df10818bf6989d|
000000c0  36 34 37 61 62 32 38 38  64 31 63 30 39 38 30 36  |647ab288d1c09806|
000000d0  65 34 61 33 36 61 34 65  62 62 66 36 2f 76 6d 6c  |e4a36a4ebbf6/vml|
000000e0  69 6e 75 7a 2d 33 2e 36  2e 30 0a 74 69 74 6c 65  |inuz-3.6.0.title|
000000f0  20 54 65 73 74 4f 53 20  34 32 20 32 30 31 39 30  | TestOS 42 20190|
00000100  38 32 34 2e 30 20 28 6f  73 74 72 65 65 29 0a     |824.0 (ostree).|
0000010f

Closes: #1904
Approved by: cgwalters
This commit is contained in:
Javier Martinez Canillas 2019-08-27 02:12:19 +02:00 committed by Atomic Bot
parent d85366d52a
commit f82f825fed

View File

@ -30,7 +30,6 @@ struct _OstreeBootconfigParser
const char *separators;
GHashTable *options;
GPtrArray *lines;
};
typedef GObjectClass OstreeBootconfigParserClass;
@ -48,9 +47,6 @@ ostree_bootconfig_parser_clone (OstreeBootconfigParser *self)
{
OstreeBootconfigParser *parser = ostree_bootconfig_parser_new ();
for (guint i = 0; i < self->lines->len; i++)
g_ptr_array_add (parser->lines, g_variant_ref (self->lines->pdata[i]));
GLNX_HASH_TABLE_FOREACH_KV (self->options, const char*, k, const char*, v)
g_hash_table_replace (parser->options, g_strdup (k), g_strdup (v));
@ -84,7 +80,6 @@ ostree_bootconfig_parser_parse_at (OstreeBootconfigParser *self,
for (char **iter = lines; *iter; iter++)
{
const char *line = *iter;
char *keyname = "";
if (g_ascii_isalpha (*line))
{
@ -92,7 +87,6 @@ ostree_bootconfig_parser_parse_at (OstreeBootconfigParser *self,
items = g_strsplit_set (line, self->separators, 2);
if (g_strv_length (items) == 2 && items[0][0] != '\0')
{
keyname = items[0];
g_hash_table_insert (self->options, items[0], items[1]);
g_free (items); /* Transfer ownership */
}
@ -101,7 +95,6 @@ ostree_bootconfig_parser_parse_at (OstreeBootconfigParser *self,
g_strfreev (items);
}
}
g_ptr_array_add (self->lines, g_variant_new ("(ss)", keyname, line));
}
self->parsed = TRUE;
@ -154,36 +147,9 @@ ostree_bootconfig_parser_write_at (OstreeBootconfigParser *self,
GError **error)
{
g_autoptr(GString) buf = g_string_new ("");
g_autoptr(GHashTable) written_overrides = g_hash_table_new (g_str_hash, g_str_equal);
for (guint i = 0; i < self->lines->len; i++)
{
GVariant *linedata = self->lines->pdata[i];
const char *key;
const char *value;
const char *line;
g_variant_get (linedata, "(&s&s)", &key, &line);
value = g_hash_table_lookup (self->options, key);
if (value == NULL)
{
g_string_append (buf, line);
g_string_append_c (buf, '\n');
}
else
{
write_key (self, buf, key, value);
g_hash_table_add (written_overrides, (gpointer)key);
}
}
GLNX_HASH_TABLE_FOREACH_KV (self->options, const char*, k, const char*, v)
{
if (g_hash_table_lookup (written_overrides, k))
continue;
write_key (self, buf, k, v);
}
write_key (self, buf, k, v);
if (!glnx_file_replace_contents_at (dfd, path, (guint8*)buf->str, buf->len,
GLNX_FILE_REPLACE_NODATASYNC,
@ -210,7 +176,6 @@ ostree_bootconfig_parser_finalize (GObject *object)
OstreeBootconfigParser *self = OSTREE_BOOTCONFIG_PARSER (object);
g_hash_table_unref (self->options);
g_ptr_array_unref (self->lines);
G_OBJECT_CLASS (ostree_bootconfig_parser_parent_class)->finalize (object);
}
@ -219,7 +184,6 @@ static void
ostree_bootconfig_parser_init (OstreeBootconfigParser *self)
{
self->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
self->lines = g_ptr_array_new_with_free_func ((GDestroyNotify)g_variant_unref);
}
void