File firmwarekit-outputdir of Package firmwarekit

From: Bernhard Walle <bwalle@suse.de>
Subject: [PATCH] Change output directories

Use OUTPUT_DIR for the output. If OUTPUT_DIR is not defined, use
/root.

Signed-off-by: Bernhard Walle <bwalle@suse.de>

---
 acpitable.c   |    5 ++++-
 biostest.h    |    1 +
 firmwarekit.h |    4 +++-
 lib.c         |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 plugins.c     |    6 +++---
 ssh.c         |   10 ++++++----
 6 files changed, 67 insertions(+), 9 deletions(-)

Index: linuxfirmwarekit/acpitable.c
===================================================================
--- linuxfirmwarekit.orig/acpitable.c
+++ linuxfirmwarekit/acpitable.c
@@ -180,6 +180,7 @@ void extract_acpi_tables(void)
          * are located (should exist after compiling "acpicompile" plugin).
          * Since we're standalone, we could be called from a few different
          * places. */
+	chdir_output_dir();
 
         /* create hex-dump format of all acpi tables in file 'acpi.dump' */
         sprintf(command, "/usr/sbin/acpidump -o acpi.dump 2> /dev/null");
@@ -195,7 +196,7 @@ void extract_acpi_tables(void)
                 ret = system("cat /proc/acpi/dsdt > DSDT.dat");
                 if (ret != EXIT_SUCCESS) {
                         fprintf(stderr,"WARN (acpixtract): failed to create DSDT.dat.\n");
-                        return;
+                        goto out;
                 }
         }
 
@@ -217,6 +218,8 @@ void extract_acpi_tables(void)
 		}
 	}
 
+out:
+	chdir_back();
 }
 
 void load_acpi_tables(void) {
Index: linuxfirmwarekit/biostest.h
===================================================================
--- linuxfirmwarekit.orig/biostest.h
+++ linuxfirmwarekit/biostest.h
@@ -139,6 +139,7 @@ extern int locate_acpi_table(char *name,
 extern int e820_is_reserved(uint64_t memory);
 extern char *copy_acpi_table(void *address, char *name);
 extern char *execute_aml_method(char *method, char *execute);
+extern char *get_output_path(void);
 
 /* lib.c */
 
Index: linuxfirmwarekit/firmwarekit.h
===================================================================
--- linuxfirmwarekit.orig/firmwarekit.h
+++ linuxfirmwarekit/firmwarekit.h
@@ -61,7 +61,9 @@ extern char *result_to_ascii(int res);
 extern char *get_relative_command(char *cmd, char *options);
 extern char *get_kernel_ver(void); 
 extern char *get_lfdk_ver(void);
-
+extern void chdir_output_dir(void);
+extern void chdir_back(void);
+extern char *get_error_log(void);
 
 #endif
 
Index: linuxfirmwarekit/lib.c
===================================================================
--- linuxfirmwarekit.orig/lib.c
+++ linuxfirmwarekit/lib.c
@@ -281,3 +281,53 @@ char *get_lfdk_ver(void) {
 	chop_newline(lfdk_ver);
 	return strdup(lfdk_ver);
 }
+
+/*
+ * firmwarekit is not multi threaded
+ */
+
+static char previous_dir[PATH_MAX];
+
+char *get_output_path(void)
+{
+	char *dir;
+
+	if ((dir = getenv("OUTPUT_DIR")) == NULL)
+		dir = "/root";
+
+	return dir;
+}
+
+void chdir_output_dir(void)
+{
+	const char *dir = get_output_path();
+
+	if (getcwd(previous_dir, PATH_MAX) == NULL)
+		fprintf(stderr, "getcwd() failed in %s\n", __FUNCTION__);
+
+	if (chdir(dir) != 0) {
+		fprintf(stderr, "chdir(%s) failed\n", dir);
+		strcpy(previous_dir, "");
+	}
+}
+
+void chdir_back(void)
+{
+	if (*previous_dir) {
+		if (chdir(previous_dir) != 0) {
+			fprintf(stderr, "chdir(%s) failed\n", previous_dir);
+			strcpy(previous_dir, "");
+		}
+	}
+}
+
+char *get_error_log(void)
+{
+	static char returnpath[PATH_MAX];
+
+	if (!returnpath[0])
+		snprintf(returnpath, PATH_MAX, "%s/%s", get_output_path(), LFDK_ERRLOG);
+
+	return returnpath;
+}
+
Index: linuxfirmwarekit/plugins.c
===================================================================
--- linuxfirmwarekit.orig/plugins.c
+++ linuxfirmwarekit/plugins.c
@@ -261,15 +261,15 @@ void execute_plugins(char *directory)
 			do_plugin(buffer);
 		}
 		if (entry && strstr(entry->d_name,".exe")) {
-			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, LFDK_ERRLOG);
+			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, get_error_log());
 			do_exe_plugin(buffer);
 		}
 		if (entry && strstr(entry->d_name,".sh")) {
-			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, LFDK_ERRLOG);
+			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, get_error_log());
 			do_exe_plugin(buffer);
 		}
 		if (entry && strstr(entry->d_name,".pl")) {
-			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, LFDK_ERRLOG);
+			sprintf(buffer, "%s/%s 2>> %s", directory, entry->d_name, get_error_log());
 			do_exe_plugin(buffer);
 		}
 
Index: linuxfirmwarekit/ssh.c
===================================================================
--- linuxfirmwarekit.orig/ssh.c
+++ linuxfirmwarekit/ssh.c
@@ -67,10 +67,12 @@ int copy_via_ssh(const char *dest)
 	}
 
 	/* Copy results into tmp dir */
-	ret1 = snprintf (command, 4096, "cp /root/acpi.dump /root/DSDT* "
-			"/root/results/* /root/*.log"
-			" /var/log/firmwarekit/%s", result_dir);
-
+	ret1 = snprintf (command, 4096, "cp %s/acpi.dump %s/DSDT* "
+			"%s/{results.txt,resources.xml,results.css} %s/*.log"
+			" /var/log/firmwarekit/%s",
+			get_output_path(), get_output_path(),
+			get_output_path(), get_output_path(),
+			result_dir);
 	ret2 = system(command);
 	if (ret1 >= 4096 || ret2)
 		goto error;