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/postinstall_runner_action.cc b/postinstall_runner_action.cc
index 1baa9ec..934b461 100644
--- a/postinstall_runner_action.cc
+++ b/postinstall_runner_action.cc
@@ -3,9 +3,12 @@
 // found in the LICENSE file.
 
 #include "update_engine/postinstall_runner_action.h"
+
 #include <sys/mount.h>
 #include <stdlib.h>
 #include <vector>
+
+#include "update_engine/constants.h"
 #include "update_engine/subprocess.h"
 #include "update_engine/utils.h"
 
@@ -16,9 +19,6 @@
 
 namespace {
 const char kPostinstallScript[] = "/postinst";
-const char kPowerwashMarkerFile[] =
-  "/mnt/stateful_partition/factory_install_reset";
-const char kPowerwashCommand[] = "safe fast\n";
 }
 
 void PostinstallRunnerAction::PerformAction() {
@@ -55,6 +55,15 @@
   temp_dir_remover.set_should_remove(false);
   completer.set_should_complete(false);
 
+  if (install_plan.powerwash_required) {
+    if (utils::CreatePowerwashMarkerFile()) {
+      powerwash_marker_created_ = true;
+    } else {
+      completer.set_code(kActionCodePostinstallPowerwashError);
+      return;
+    }
+  }
+
   // Runs the postinstall script asynchronously to free up the main loop while
   // it's running.
   vector<string> command;
@@ -70,6 +79,11 @@
   ScopedTempUnmounter temp_unmounter(temp_rootfs_dir_);
   if (return_code != 0) {
     LOG(ERROR) << "Postinst command failed with code: " << return_code;
+
+    // Undo any changes done to trigger Powerwash using clobber-state.
+    if (powerwash_marker_created_)
+      utils::DeletePowerwashMarkerFile();
+
     if (return_code == 3) {
       // This special return code means that we tried to update firmware,
       // but couldn't because we booted from FW B, and we need to reboot
@@ -83,18 +97,6 @@
   CHECK(HasInputObject());
   const InstallPlan install_plan = GetInputObject();
 
-  if (install_plan.powerwash_required) {
-    if (utils::WriteFile(kPowerwashMarkerFile,
-                         kPowerwashCommand,
-                         strlen(kPowerwashCommand))) {
-      LOG(INFO) << "Configured clobber-state to do powerwash on next reboot";
-    } else {
-      LOG(ERROR) << "Error in configuring clobber-state to do powerwash";
-      completer.set_code(kActionCodePostinstallPowerwashError);
-      return;
-    }
-  }
-
   if (HasOutputPipe())
     SetOutputObject(install_plan);