Add a singleton CacheLocation to replace the hard coded locations

This class allows us to set the following locations dynamically:
cache_temp_source, last_command_file, stash_directory_base.

In the updater's main function, we reset the values of these variables
to their default locations in /cache; while we can set them to temp
files in unit tests or host simulation.

Test: unit tests pass
Change-Id: I528652650caa41373617ab055d41b1f1a4ec0f87
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index 73701ab..7645a40 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -40,10 +40,9 @@
 
 #include "edify/expr.h"
 #include "otafault/ota_io.h"
+#include "otautil/cache_location.h"
 #include "otautil/print_sha1.h"
 
-std::string cache_temp_source = "/cache/saved.file";
-
 static int LoadPartitionContents(const std::string& filename, FileContents* file);
 static size_t FileSink(const unsigned char* data, size_t len, int fd);
 static int GenerateTarget(const FileContents& source_file, const std::unique_ptr<Value>& patch,
@@ -404,7 +403,7 @@
     // If the source file is missing or corrupted, it might be because we were killed in the middle
     // of patching it.  A copy of it should have been made in cache_temp_source.  If that file
     // exists and matches the sha1 we're looking for, the check still passes.
-    if (LoadFileContents(cache_temp_source.c_str(), &file) != 0) {
+    if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &file) != 0) {
       printf("failed to load cache file\n");
       return 1;
     }
@@ -526,7 +525,7 @@
   printf("source file is bad; trying copy\n");
 
   FileContents copy_file;
-  if (LoadFileContents(cache_temp_source.c_str(), &copy_file) < 0) {
+  if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &copy_file) < 0) {
     printf("failed to read copy file\n");
     return 1;
   }
@@ -621,7 +620,7 @@
     printf("not enough free space on /cache\n");
     return 1;
   }
-  if (SaveFileContents(cache_temp_source.c_str(), &source_file) < 0) {
+  if (SaveFileContents(CacheLocation::location().cache_temp_source().c_str(), &source_file) < 0) {
     printf("failed to back up source file\n");
     return 1;
   }
@@ -667,7 +666,7 @@
   }
 
   // Delete the backup copy of the source.
-  unlink(cache_temp_source.c_str());
+  unlink(CacheLocation::location().cache_temp_source().c_str());
 
   // Success!
   return 0;
diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp
index ec1d20c..ea364d8 100644
--- a/applypatch/freecache.cpp
+++ b/applypatch/freecache.cpp
@@ -33,6 +33,7 @@
 #include <android-base/stringprintf.h>
 
 #include "applypatch/applypatch.h"
+#include "otautil/cache_location.h"
 
 static int EliminateOpenFiles(std::set<std::string>* files) {
   std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
@@ -92,7 +93,7 @@
 
       // We can't delete cache_temp_source; if it's there we might have restarted during
       // installation and could be depending on it to be there.
-      if (path == cache_temp_source) {
+      if (path == CacheLocation::location().cache_temp_source()) {
         continue;
       }
 
diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index c8ad915..912ead1 100644
--- a/applypatch/include/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -34,12 +34,6 @@
   std::vector<unsigned char> data;
 };
 
-// When there isn't enough room on the target filesystem to hold the patched version of the file,
-// we copy the original here and delete it to free up space.  If the expected source file doesn't
-// exist, or is corrupted, we look to see if the cached file contains the bits we want and use it as
-// the source instead.  The default location for the cached source is "/cache/saved.file".
-extern std::string cache_temp_source;
-
 using SinkFn = std::function<size_t(const unsigned char*, size_t)>;
 
 // applypatch.cpp