Cancel the current download if user chooses a different channel.

In my earlier CL, to keep the implementation simple, we disallowed changing
a channel until the previous change completed in its entirety. Given that
the UI is not going to be updated for M27, such a restriction turned out
to be very confusing when playing around with channel changing. So, we
decided to implement a simple form of canceling the download if the
user selected a different channel while we're downloading the bits. This
implementation can easily be extended to support a general form of cancel
in the future, if required.

This CL also adds validation of libchromeos API calls when interpreting
the policy values. It also cleans up some bogus error messages that were
logged earlier when we abort a download.

BUG=chromium:222617
TEST=All scenarios pass on ZGB. Unit Tests pass.

Change-Id: I7cd691fe461d9ce47314299f6e2598944650ee33
Reviewed-on: https://gerrit.chromium.org/gerrit/46095
Commit-Queue: Jay Srinivasan <jaysri@chromium.org>
Reviewed-by: Jay Srinivasan <jaysri@chromium.org>
Tested-by: Jay Srinivasan <jaysri@chromium.org>
diff --git a/utils.cc b/utils.cc
index 9337cc7..756ef8a 100644
--- a/utils.cc
+++ b/utils.cc
@@ -31,6 +31,7 @@
 #include <google/protobuf/stubs/common.h>
 #include <rootdev/rootdev.h>
 
+#include "update_engine/constants.h"
 #include "update_engine/file_writer.h"
 #include "update_engine/omaha_request_params.h"
 #include "update_engine/subprocess.h"
@@ -52,7 +53,6 @@
 // one second.
 const int kUnmountMaxNumOfRetries = 5;
 const int kUnmountRetryIntervalInMicroseconds = 200 * 1000;  // 200 ms
-
 }  // namespace
 
 namespace utils {
@@ -873,6 +873,8 @@
       return "kActionCodeOmahaUpdateDeferredForBackoff";
     case kActionCodePostinstallPowerwashError:
       return "kActionCodePostinstallPowerwashError";
+    case kActionCodeUpdateCanceledByChannelChange:
+      return "kActionCodeUpdateCanceledByChannelChange";
     case kActionCodeUmaReportedMax:
       return "kActionCodeUmaReportedMax";
     case kActionCodeOmahaRequestHTTPResponseBase:
@@ -894,6 +896,34 @@
   return "Unknown error: " + base::UintToString(static_cast<unsigned>(code));
 }
 
+bool CreatePowerwashMarkerFile() {
+  bool result = utils::WriteFile(kPowerwashMarkerFile,
+                                 kPowerwashCommand,
+                                 strlen(kPowerwashCommand));
+  if (result)
+    LOG(INFO) << "Created " << kPowerwashMarkerFile
+              << " to powerwash on next reboot";
+  else
+    PLOG(ERROR) << "Error in creating powerwash marker file: "
+                << kPowerwashMarkerFile;
+
+  return result;
+}
+
+bool DeletePowerwashMarkerFile() {
+  const FilePath kPowerwashMarkerPath(kPowerwashMarkerFile);
+  bool result = file_util::Delete(kPowerwashMarkerPath, false);
+
+  if (result)
+    LOG(INFO) << "Successfully deleted the powerwash marker file : "
+              << kPowerwashMarkerFile;
+  else
+    PLOG(ERROR) << "Could not delete the powerwash marker file : "
+                << kPowerwashMarkerFile;
+
+  return result;
+}
+
 }  // namespace utils
 
 }  // namespace chromeos_update_engine