Merge "applypatch: {Load,Save}FileContents and ParseSha1 take std::string." am: b4181566f9 am: 839c40513a
am: d9f532e7a2

Change-Id: I87cf9ea66c3a8a7632650bf8f740a2aa8a959e9c
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index afea1da..e6fd5f6 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -49,20 +49,20 @@
                           const std::string& target_filename,
                           const uint8_t target_sha1[SHA_DIGEST_LENGTH], const Value* bonus_data);
 
-int LoadFileContents(const char* filename, FileContents* file) {
+int LoadFileContents(const std::string& filename, FileContents* file) {
   // A special 'filename' beginning with "EMMC:" means to load the contents of a partition.
-  if (strncmp(filename, "EMMC:", 5) == 0) {
+  if (android::base::StartsWith(filename, "EMMC:")) {
     return LoadPartitionContents(filename, file);
   }
 
   struct stat sb;
-  if (stat(filename, &sb) == -1) {
+  if (stat(filename.c_str(), &sb) == -1) {
     PLOG(ERROR) << "Failed to stat \"" << filename << "\"";
     return -1;
   }
 
   std::vector<unsigned char> data(sb.st_size);
-  unique_file f(ota_fopen(filename, "rb"));
+  unique_file f(ota_fopen(filename.c_str(), "rb"));
   if (!f) {
     PLOG(ERROR) << "Failed to open \"" << filename << "\"";
     return -1;
@@ -153,7 +153,7 @@
     SHA1_Final(sha_so_far, &temp_ctx);
 
     uint8_t parsed_sha[SHA_DIGEST_LENGTH];
-    if (ParseSha1(current_sha1.c_str(), parsed_sha) != 0) {
+    if (ParseSha1(current_sha1, parsed_sha) != 0) {
       LOG(ERROR) << "Failed to parse SHA-1 \"" << current_sha1 << "\" in " << filename;
       return -1;
     }
@@ -180,8 +180,9 @@
   return 0;
 }
 
-int SaveFileContents(const char* filename, const FileContents* file) {
-  unique_fd fd(ota_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
+int SaveFileContents(const std::string& filename, const FileContents* file) {
+  unique_fd fd(
+      ota_open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR));
   if (fd == -1) {
     PLOG(ERROR) << "Failed to open \"" << filename << "\" for write";
     return -1;
@@ -337,8 +338,8 @@
   return 0;
 }
 
-int ParseSha1(const char* str, uint8_t* digest) {
-  const char* ps = str;
+int ParseSha1(const std::string& str, uint8_t* digest) {
+  const char* ps = str.c_str();
   uint8_t* pd = digest;
   for (int i = 0; i < SHA_DIGEST_LENGTH * 2; ++i, ++ps) {
     int digit;
@@ -367,7 +368,7 @@
 static int FindMatchingPatch(const uint8_t* sha1, const std::vector<std::string>& patch_sha1s) {
   for (size_t i = 0; i < patch_sha1s.size(); ++i) {
     uint8_t patch_sha1[SHA_DIGEST_LENGTH];
-    if (ParseSha1(patch_sha1s[i].c_str(), patch_sha1) == 0 &&
+    if (ParseSha1(patch_sha1s[i], patch_sha1) == 0 &&
         memcmp(patch_sha1, sha1, SHA_DIGEST_LENGTH) == 0) {
       return i;
     }
@@ -387,7 +388,7 @@
     // If the source file is missing or corrupted, it might be because we were killed in the middle
     // of patching it. A copy should have been made in cache_temp_source. If that file exists and
     // matches the SHA-1 we're looking for, the check still passes.
-    if (LoadFileContents(Paths::Get().cache_temp_source().c_str(), &file) != 0) {
+    if (LoadFileContents(Paths::Get().cache_temp_source(), &file) != 0) {
       LOG(ERROR) << "Failed to load cache file";
       return 1;
     }
@@ -486,7 +487,7 @@
   LOG(INFO) << "Source file is bad; trying copy";
 
   FileContents copy_file;
-  if (LoadFileContents(Paths::Get().cache_temp_source().c_str(), &copy_file) < 0) {
+  if (LoadFileContents(Paths::Get().cache_temp_source(), &copy_file) < 0) {
     LOG(ERROR) << "Failed to read copy file";
     return 1;
   }
@@ -574,7 +575,7 @@
     LOG(ERROR) << "Not enough free space on /cache";
     return 1;
   }
-  if (SaveFileContents(Paths::Get().cache_temp_source().c_str(), &source_file) < 0) {
+  if (SaveFileContents(Paths::Get().cache_temp_source(), &source_file) < 0) {
     LOG(ERROR) << "Failed to back up source file";
     return 1;
   }
diff --git a/applypatch/applypatch_modes.cpp b/applypatch/applypatch_modes.cpp
index 0728db9..6437e1b 100644
--- a/applypatch/applypatch_modes.cpp
+++ b/applypatch/applypatch_modes.cpp
@@ -60,7 +60,7 @@
         }
 
         uint8_t digest[SHA_DIGEST_LENGTH];
-        if (ParseSha1(pieces[0].c_str(), digest) != 0) {
+        if (ParseSha1(pieces[0], digest) != 0) {
           LOG(ERROR) << "Failed to parse SHA-1 \"" << argv[i] << "\"";
           return false;
         }
diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index ef133f2..f074e36 100644
--- a/applypatch/include/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -50,7 +50,7 @@
 
 // Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the
 // digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error.
-int ParseSha1(const char* str, uint8_t* digest);
+int ParseSha1(const std::string& str, uint8_t* digest);
 
 // Applies binary patches to eMMC target files in a way that is safe (the original file is not
 // touched until we have the desired replacement for it) and idempotent (it's okay to run this
@@ -91,10 +91,10 @@
 
 // Reads a file into memory; stores the file contents and associated metadata in *file. Returns 0
 // on success, or -1 on error.
-int LoadFileContents(const char* filename, FileContents* file);
+int LoadFileContents(const std::string& filename, FileContents* file);
 
 // Saves the given FileContents object to the given filename. Returns 0 on success, or -1 on error.
-int SaveFileContents(const char* filename, const FileContents* file);
+int SaveFileContents(const std::string& filename, const FileContents* file);
 
 // bspatch.cpp
 
diff --git a/updater/install.cpp b/updater/install.cpp
index 00a05b8..6f83440 100644
--- a/updater/install.cpp
+++ b/updater/install.cpp
@@ -319,7 +319,7 @@
     uint8_t arg_digest[SHA_DIGEST_LENGTH];
     if (args[i]->type != VAL_STRING) {
       LOG(ERROR) << name << "(): arg " << i << " is not a string; skipping";
-    } else if (ParseSha1(args[i]->data.c_str(), arg_digest) != 0) {
+    } else if (ParseSha1(args[i]->data, arg_digest) != 0) {
       // Warn about bad args and skip them.
       LOG(ERROR) << name << "(): error parsing \"" << args[i]->data << "\" as sha-1; skipping";
     } else if (memcmp(digest, arg_digest, SHA_DIGEST_LENGTH) == 0) {