Implement UpdateAttempterAndroid::AllocateSpaceForPayload

- If insufficient space, error is not set, and required size
is returned.
- If successful, error is not set, and 0 is returned.
- If other failures, error is set, and 0 is returned.

Test: apply OTA
Bug: 138808058
Change-Id: If0a8834afb841ce714379556836fe99c5e3da8ac
diff --git a/update_attempter_android.cc b/update_attempter_android.cc
index cc891e7..3292dd5 100644
--- a/update_attempter_android.cc
+++ b/update_attempter_android.cc
@@ -893,8 +893,34 @@
     const std::string& metadata_filename,
     const vector<string>& key_value_pair_headers,
     brillo::ErrorPtr* error) {
-  // TODO(elsk): implement b/138808058
-  LogAndSetError(error, FROM_HERE, "Not implemented.");
+  DeltaArchiveManifest manifest;
+  if (!VerifyPayloadParseManifest(metadata_filename, &manifest, error)) {
+    return 0;
+  }
+  std::map<string, string> headers;
+  if (!ParseKeyValuePairHeaders(key_value_pair_headers, &headers, error)) {
+    return 0;
+  }
+
+  string payload_id = GetPayloadId(headers);
+  uint64_t required_size = 0;
+  if (!DeltaPerformer::PreparePartitionsForUpdate(prefs_,
+                                                  boot_control_,
+                                                  GetTargetSlot(),
+                                                  manifest,
+                                                  payload_id,
+                                                  &required_size)) {
+    if (required_size == 0) {
+      LogAndSetError(error, FROM_HERE, "Failed to allocate space for payload.");
+      return 0;
+    } else {
+      LOG(ERROR) << "Insufficient space for payload: " << required_size
+                 << " bytes";
+      return required_size;
+    }
+  }
+
+  LOG(INFO) << "Successfully allocated space for payload.";
   return 0;
 }