Encapsulates zip_writer_ on Dumpstate class.

BUG: 26379932
Test: DumpstateTest passes and manual verification of zip file.
Change-Id: I0113bec61cd32015fdd625afd2dca026bbf0ceab
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index 537ba21..740eb19 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -48,7 +48,6 @@
 #include <private/android_logger.h>
 
 #include "dumpstate.h"
-#include "ziparchive/zip_writer.h"
 
 #include <openssl/sha.h>
 
@@ -58,10 +57,6 @@
 
 // TODO: variables and functions below should be part of dumpstate object
 
-// TODO: Can't be added to dumpstate.h because including "ziparchive/zip_writer.h" would not work.
-// That's probably because of the dumpstate -> libdumpstate -> device implementation setup, which
-// might be changed anyways - let's keep it here and wait
-static std::unique_ptr<ZipWriter> zip_writer;
 static std::set<std::string> mount_points;
 void add_mountinfo();
 
@@ -695,7 +690,7 @@
 }
 
 bool Dumpstate::IsZipping() const {
-    return zip_writer != nullptr;
+    return zip_writer_ != nullptr;
 }
 
 // List of file extensions that can cause a zip file attachment to be rejected by some email
@@ -727,10 +722,10 @@
 
     // Logging statement  below is useful to time how long each entry takes, but it's too verbose.
     // MYLOGD("Adding zip entry %s\n", entry_name.c_str());
-    int32_t err = zip_writer->StartEntryWithTime(valid_name.c_str(), ZipWriter::kCompress,
-                                                 get_mtime(fd, ds.now_));
+    int32_t err = zip_writer_->StartEntryWithTime(valid_name.c_str(), ZipWriter::kCompress,
+                                                  get_mtime(fd, ds.now_));
     if (err != 0) {
-        MYLOGE("zip_writer->StartEntryWithTime(%s): %s\n", valid_name.c_str(),
+        MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", valid_name.c_str(),
                ZipWriter::ErrorCodeString(err));
         return false;
     }
@@ -744,16 +739,16 @@
             MYLOGE("read(%s): %s\n", entry_name.c_str(), strerror(errno));
             return false;
         }
-        err = zip_writer->WriteBytes(buffer.data(), bytes_read);
+        err = zip_writer_->WriteBytes(buffer.data(), bytes_read);
         if (err) {
-            MYLOGE("zip_writer->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
+            MYLOGE("zip_writer_->WriteBytes(): %s\n", ZipWriter::ErrorCodeString(err));
             return false;
         }
     }
 
-    err = zip_writer->FinishEntry();
+    err = zip_writer_->FinishEntry();
     if (err != 0) {
-        MYLOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
+        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
         return false;
     }
 
@@ -793,23 +788,23 @@
         return false;
     }
     MYLOGD("Adding zip text entry %s\n", entry_name.c_str());
-    int32_t err = zip_writer->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, ds.now_);
+    int32_t err = zip_writer_->StartEntryWithTime(entry_name.c_str(), ZipWriter::kCompress, ds.now_);
     if (err != 0) {
-        MYLOGE("zip_writer->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
+        MYLOGE("zip_writer_->StartEntryWithTime(%s): %s\n", entry_name.c_str(),
                ZipWriter::ErrorCodeString(err));
         return false;
     }
 
-    err = zip_writer->WriteBytes(content.c_str(), content.length());
+    err = zip_writer_->WriteBytes(content.c_str(), content.length());
     if (err != 0) {
-        MYLOGE("zip_writer->WriteBytes(%s): %s\n", entry_name.c_str(),
+        MYLOGE("zip_writer_->WriteBytes(%s): %s\n", entry_name.c_str(),
                ZipWriter::ErrorCodeString(err));
         return false;
     }
 
-    err = zip_writer->FinishEntry();
+    err = zip_writer_->FinishEntry();
     if (err != 0) {
-        MYLOGE("zip_writer->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
+        MYLOGE("zip_writer_->FinishEntry(): %s\n", ZipWriter::ErrorCodeString(err));
         return false;
     }
 
@@ -1254,9 +1249,9 @@
     redirect_to_existing_file(stderr, const_cast<char*>(ds.log_path_.c_str()));
     fprintf(stderr, "\n");
 
-    int32_t err = zip_writer->Finish();
+    int32_t err = zip_writer_->Finish();
     if (err != 0) {
-        MYLOGE("zip_writer->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
+        MYLOGE("zip_writer_->Finish(): %s\n", ZipWriter::ErrorCodeString(err));
         return false;
     }
 
@@ -1503,7 +1498,7 @@
                 MYLOGE("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno));
                 do_zip_file = 0;
             } else {
-                zip_writer.reset(new ZipWriter(ds.zip_file.get()));
+                ds.zip_writer_.reset(new ZipWriter(ds.zip_file.get()));
             }
             ds.AddTextZipEntry("version.txt", ds.version_);
         }
diff --git a/cmds/dumpstate/dumpstate.h b/cmds/dumpstate/dumpstate.h
index 19f46e2..950f185 100644
--- a/cmds/dumpstate/dumpstate.h
+++ b/cmds/dumpstate/dumpstate.h
@@ -17,8 +17,6 @@
 #ifndef FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
 #define FRAMEWORK_NATIVE_CMD_DUMPSTATE_H_
 
-#include <android-base/macros.h>
-
 #ifndef MYLOGD
 #define MYLOGD(...) fprintf(stderr, __VA_ARGS__); ALOGD(__VA_ARGS__);
 #endif
@@ -39,6 +37,9 @@
 #include <string>
 #include <vector>
 
+#include <android-base/macros.h>
+#include <ziparchive/zip_writer.h>
+
 // Workaround for const char *args[MAX_ARGS_ARRAY_SIZE] variables until they're converted to
 // std::vector<std::string>
 // TODO: remove once not used
@@ -383,6 +384,9 @@
     // Pointer to the zipped file.
     std::unique_ptr<FILE, int (*)(FILE*)> zip_file{nullptr, fclose};
 
+    // Pointer to the zip structure.
+    std::unique_ptr<ZipWriter> zip_writer_;
+
   private:
     // Used by GetInstance() only.
     Dumpstate(const std::string& version = VERSION_CURRENT, bool dry_run = false,