Expose functions used to add entries to the zip file.

Currently only dumpstate.cpp is using the new functions and variables
used to add an entry to the zip file so they're defined locally, but
they should be exposed through the .h so they can be used by the
device-specific dumpstate_board() implementations.

This CL moves the following definitions to the .h:

- bugreport_dir
- ZIP_ROOT_DIR
- add_zip_entry()
- add_zip_entry_from_fd()

It also refactored functions that were taking a specific file location
to use bugreport_dir instead, to avoid pollution on main().

BUG: 28867848
Change-Id: Ic5faa203b0863bc950d640f22fb91977e52b6bfc
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index 16514c3..03f5f96 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -61,9 +61,10 @@
 static std::unique_ptr<ZipWriter> zip_writer;
 static std::set<std::string> mount_points;
 void add_mountinfo();
-static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path);
-static bool add_zip_entry_from_fd(const std::string& entry_name, int fd);
 static int control_socket_fd;
+/* suffix of the bugreport files - it's typically the date (when invoked with -d),
+ * although it could be changed by the user using a system property */
+static std::string suffix;
 
 #define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
 
@@ -84,8 +85,8 @@
 
 static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
 
-// Root dir for all files copied as-is into the bugreport
-const std::string& ZIP_ROOT_DIR = "FS";
+const std::string ZIP_ROOT_DIR = "FS";
+std::string bugreport_dir;
 
 /*
  * List of supported zip format versions.
@@ -175,11 +176,12 @@
     closedir(d);
 }
 
-static void dump_systrace(const std::string& systrace_path) {
+static void dump_systrace() {
     if (!zip_writer) {
         MYLOGD("Not dumping systrace because zip_writer is not set\n");
         return;
     }
+    std::string systrace_path = bugreport_dir + "/systrace-" + suffix + ".txt";
     if (systrace_path.empty()) {
         MYLOGE("Not dumping systrace because path is empty\n");
         return;
@@ -527,8 +529,7 @@
     printf("\n");
 }
 
-/* adds a new entry to the existing zip file. */
-static bool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
+bool add_zip_entry_from_fd(const std::string& entry_name, int fd) {
     if (!zip_writer) {
         MYLOGD("Not adding zip entry %s from fd because zip_writer is not set\n",
                 entry_name.c_str());
@@ -569,8 +570,7 @@
     return true;
 }
 
-/* adds a new entry to the existing zip file. */
-static bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
+bool add_zip_entry(const std::string& entry_name, const std::string& entry_path) {
     ScopedFd fd(TEMP_FAILURE_RETRY(open(entry_path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
     if (fd.get() == -1) {
         MYLOGE("open(%s): %s\n", entry_path.c_str(), strerror(errno));
@@ -1131,9 +1131,6 @@
         control_socket_fd = open_socket("dumpstate");
     }
 
-    /* full path of the directory where the bugreport files will be written */
-    std::string bugreport_dir;
-
     /* full path of the temporary file containing the bugreport */
     std::string tmp_path;
 
@@ -1149,10 +1146,6 @@
     /* base name (without suffix or extensions) of the bugreport files */
     std::string base_name;
 
-    /* suffix of the bugreport files - it's typically the date (when invoked with -d),
-     * although it could be changed by the user using a system property */
-    std::string suffix;
-
     /* pointer to the actual path, be it zip or text */
     std::string path;
 
@@ -1184,7 +1177,6 @@
         tmp_path = bugreport_dir + "/" + base_name + "-" + suffix + ".tmp";
         log_path = bugreport_dir + "/dumpstate_log-" + suffix + "-"
                 + std::to_string(getpid()) + ".txt";
-        systrace_path = bugreport_dir + "/systrace-" + suffix + ".txt";
 
         MYLOGD("Bugreport dir: %s\n"
                 "Base name: %s\n"
@@ -1279,7 +1271,7 @@
     print_header(version);
 
     // Dumps systrace right away, otherwise it will be filled with unnecessary events.
-    dump_systrace(systrace_path);
+    dump_systrace();
 
     // Invoking the following dumpsys calls before dump_traces() to try and
     // keep the system stats as close to its initial state as possible.