Blame SOURCES/0242-dd-introduce-dd_get_env_variable.patch

b12e4c
From af5ef029fe96a8703a1de7a60063c473d17d37a4 Mon Sep 17 00:00:00 2001
b12e4c
From: Martin Kutlak <mkutlak@redhat.com>
b12e4c
Date: Tue, 21 May 2019 10:45:55 +0200
b12e4c
Subject: [PATCH] dd: introduce dd_get_env_variable
b12e4c
b12e4c
I was thinking about adding 'dd_open_element' function returning file
b12e4c
descriptor but I thrown this idea because we should try to hide
b12e4c
implementation details and do not let users to parse elements on their
b12e4c
own.
b12e4c
b12e4c
Cherry-picked from https://github.com/abrt/libreport/commit/a1d7a0397b8251be58783dd5e8665099bf8ce868
b12e4c
---
b12e4c
 src/include/dump_dir.h | 11 +++++++++++
b12e4c
 src/lib/dump_dir.c     | 11 +++++++++++
b12e4c
 tests/dump_dir.at      | 31 +++++++++++++++++++++++++++++++
b12e4c
 3 files changed, 53 insertions(+)
b12e4c
b12e4c
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
b12e4c
index b617c6c..a7ed431 100644
b12e4c
--- a/src/include/dump_dir.h
b12e4c
+++ b/src/include/dump_dir.h
b12e4c
@@ -118,6 +118,17 @@ int dd_get_next_file(struct dump_dir *dd, char **short_name, char **full_name);
b12e4c
 
b12e4c
 char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned flags);
b12e4c
 char* dd_load_text(const struct dump_dir *dd, const char *name);
b12e4c
+
b12e4c
+/* Returns value of environment variable with given name.
b12e4c
+ *
b12e4c
+ * @param dd Dump directory
b12e4c
+ * @param name Variables's name
b12e4c
+ * @param value Return value.
b12e4c
+ * @return 0 no success, or negative value if an error occurred (-ENOENT if the
b12e4c
+ * given dd does not support environment variables).
b12e4c
+ */
b12e4c
+int dd_get_env_variable(struct dump_dir *dd, const char *name, char **value);
b12e4c
+
b12e4c
 void dd_save_text(struct dump_dir *dd, const char *name, const char *data);
b12e4c
 void dd_save_binary(struct dump_dir *dd, const char *name, const char *data, unsigned size);
b12e4c
 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path);
b12e4c
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
b12e4c
index 5e32c08..0da96cd 100644
b12e4c
--- a/src/lib/dump_dir.c
b12e4c
+++ b/src/lib/dump_dir.c
b12e4c
@@ -1242,6 +1242,17 @@ char* dd_load_text(const struct dump_dir *dd, const char *name)
b12e4c
     return dd_load_text_ext(dd, name, /*flags:*/ 0);
b12e4c
 }
b12e4c
 
b12e4c
+int dd_get_env_variable(struct dump_dir *dd, const char *name, char **value)
b12e4c
+{
b12e4c
+    const int fd = openat(dd->dd_fd, FILENAME_ENVIRON, O_RDONLY | O_NOFOLLOW);
b12e4c
+    if (fd < 0)
b12e4c
+        return -errno;
b12e4c
+
b12e4c
+    const int r  = get_env_variable_ext(fd, '\n', name, value);
b12e4c
+    close(fd);
b12e4c
+    return r;
b12e4c
+}
b12e4c
+
b12e4c
 void dd_save_text(struct dump_dir *dd, const char *name, const char *data)
b12e4c
 {
b12e4c
     if (!dd->locked)
b12e4c
diff --git a/tests/dump_dir.at b/tests/dump_dir.at
b12e4c
index 98d564c..12a0c5c 100644
b12e4c
--- a/tests/dump_dir.at
b12e4c
+++ b/tests/dump_dir.at
b12e4c
@@ -615,3 +615,34 @@ int main(int argc, char **argv)
b12e4c
     return 0;
b12e4c
 }
b12e4c
 ]])
b12e4c
+## ------------------- ##
b12e4c
+## dd_get_env_variable ##
b12e4c
+## ------------------- ##
b12e4c
+
b12e4c
+AT_TESTFUN([dd_get_env_variable],
b12e4c
+[[
b12e4c
+#include "testsuite.h"
b12e4c
+#include "testsuite_tools.h"
b12e4c
+
b12e4c
+TS_MAIN
b12e4c
+{
b12e4c
+    struct dump_dir *dd = testsuite_dump_dir_create(-1, -1, 0);
b12e4c
+
b12e4c
+    dd_create_basic_files(dd, geteuid(), NULL);
b12e4c
+
b12e4c
+    char *value = NULL;
b12e4c
+    TS_ASSERT_SIGNED_EQ(dd_get_env_variable(dd, "FROG", &value), -ENOENT);
b12e4c
+    TS_ASSERT_PTR_IS_NULL_MESSAGE(value, "Untouched return value on -ENOENT");
b12e4c
+
b12e4c
+    dd_save_text(dd, FILENAME_ENVIRON, "FROG=ribbit\nROOSTER=cockle-doodle-doo");
b12e4c
+
b12e4c
+    TS_ASSERT_FUNCTION(dd_get_env_variable(dd, "HORSE", &value));
b12e4c
+    TS_ASSERT_PTR_IS_NULL_MESSAGE(value, "Untouched return value on missing variable");
b12e4c
+
b12e4c
+    TS_ASSERT_FUNCTION(dd_get_env_variable(dd, "ROOSTER", &value));
b12e4c
+    TS_ASSERT_STRING_EQ(value, "cockle-doodle-doo", "Variable ROOSTER loaded from the dump dir");
b12e4c
+
b12e4c
+    testsuite_dump_dir_delete(dd);
b12e4c
+}
b12e4c
+TS_RETURN_MAIN
b12e4c
+]])
b12e4c
-- 
b12e4c
2.21.0
b12e4c