summaryrefslogtreecommitdiff
path: root/src/libotutil/ot-fs-utils.c
diff options
context:
space:
mode:
authorJonathan Lebon <jlebon@redhat.com>2018-02-01 22:06:50 +0000
committerAtomic Bot <atomic-devel@projectatomic.io>2018-02-02 22:36:49 +0000
commit5cba67520e3370db6f3fd3e06d000f733d4cc1e6 (patch)
tree8b9b4dc56e142cb78c943b13553bde8af8b16871 /src/libotutil/ot-fs-utils.c
parent4a98a86b7238f13881c8615b4435ae828c7068f3 (diff)
downloadostree-5cba67520e3370db6f3fd3e06d000f733d4cc1e6.tar.gz
libotutil: factor out utility to parse file by line
This will be used in the checkout CLI as well. Closes: #1442 Approved by: cgwalters
Diffstat (limited to 'src/libotutil/ot-fs-utils.c')
-rw-r--r--src/libotutil/ot-fs-utils.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libotutil/ot-fs-utils.c b/src/libotutil/ot-fs-utils.c
index affac83a..c4fcd56f 100644
--- a/src/libotutil/ot-fs-utils.c
+++ b/src/libotutil/ot-fs-utils.c
@@ -221,3 +221,29 @@ ot_map_anonymous_tmpfile_from_content (GInputStream *instream,
return NULL;
return g_mapped_file_get_bytes (mfile);
}
+
+gboolean
+ot_parse_file_by_line (const char *path,
+ gboolean (*cb)(const char*, void*, GError**),
+ void *cbdata,
+ GCancellable *cancellable,
+ GError **error)
+{
+ g_autofree char *contents =
+ glnx_file_get_contents_utf8_at (AT_FDCWD, path, NULL, cancellable, error);
+ if (!contents)
+ return FALSE;
+
+ g_auto(GStrv) lines = g_strsplit (contents, "\n", -1);
+ for (char **iter = lines; iter && *iter; iter++)
+ {
+ /* skip empty lines at least */
+ if (**iter == '\0')
+ continue;
+
+ if (!cb (*iter, cbdata, error))
+ return FALSE;
+ }
+
+ return TRUE;
+}