Merge "Don't create disk hole files in /tmp"
diff --git a/host/commands/assemble_cvd/flags.cc b/host/commands/assemble_cvd/flags.cc
index 2e6f8d3..1f3eff6 100644
--- a/host/commands/assemble_cvd/flags.cc
+++ b/host/commands/assemble_cvd/flags.cc
@@ -691,7 +691,9 @@
     }
     std::string header_path = config.AssemblyPath("gpt_header.img");
     std::string footer_path = config.AssemblyPath("gpt_footer.img");
-    CreateCompositeDisk(disk_config(), header_path, footer_path, config.composite_disk_path());
+    std::string tmp_prefix = config.AssemblyPath("disk_hole/disk");
+    CreateCompositeDisk(disk_config(), tmp_prefix, header_path, footer_path,
+                        config.composite_disk_path());
   } else {
     auto existing_size = cvd::FileSize(config.composite_disk_path());
     auto available_space = AvailableSpaceAtPath(config.composite_disk_path());
@@ -735,6 +737,7 @@
       preserving.insert("gpt_footer.img");
       preserving.insert("composite.img");
       preserving.insert("access-kregistry");
+      preserving.insert("disk_hole");
     }
     if (!CleanPriorFiles(config, preserving)) {
       LOG(ERROR) << "Failed to clean prior files";
@@ -750,6 +753,16 @@
         exit(AssemblerExitCodes::kAssemblyDirCreationError);
       }
     }
+    std::string disk_hole_dir = FLAGS_assembly_dir + "/disk_hole";
+    if (!cvd::DirectoryExists(disk_hole_dir.c_str())) {
+      LOG(INFO) << "Setting up " << disk_hole_dir << "/disk_hole";
+      if (mkdir(disk_hole_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0
+          && errno != EEXIST) {
+        LOG(ERROR) << "Failed to create assembly directory: "
+                  << disk_hole_dir << ". Error: " << errno;
+        exit(AssemblerExitCodes::kAssemblyDirCreationError);
+      }
+    }
     for (const auto& instance : config.Instances()) {
       // Create instance directory if it doesn't exist.
       if (!cvd::DirectoryExists(instance.instance_dir().c_str())) {
diff --git a/host/commands/assemble_cvd/image_aggregator.cc b/host/commands/assemble_cvd/image_aggregator.cc
index eac6a2e..b4e47ab 100644
--- a/host/commands/assemble_cvd/image_aggregator.cc
+++ b/host/commands/assemble_cvd/image_aggregator.cc
@@ -92,9 +92,13 @@
   return bpttool_input_json;
 }
 
-std::string CreateFile(size_t len) {
-  char file_template[] = "/tmp/diskXXXXXX";
-  int fd = mkstemp(file_template);
+std::string CreateFile(const std::string& path_prefix, const std::size_t len) {
+  std::string file_template = path_prefix + "XXXXXX";
+  char* file_template_cstr = (char*) malloc(file_template.size() + 1);
+  strncpy(file_template_cstr, file_template.c_str(), file_template.size() + 1);
+  int fd = mkstemp(file_template_cstr);
+  file_template = std::string(file_template_cstr);
+  free(file_template_cstr);
   if (fd < 0) {
     LOG(FATAL) << "not able to create disk hole temp file";
   }
@@ -108,11 +112,12 @@
     }
   }
   close(fd);
-  return std::string(file_template);
+  return file_template;
 }
 
 CompositeDisk MakeCompositeDiskSpec(const Json::Value& bpt_file,
                                     const std::vector<ImagePartition>& partitions,
+                                    const std::string& tmp_path_prefix,
                                     const std::string& header_file,
                                     const std::string& footer_file) {
   CompositeDisk disk;
@@ -124,7 +129,8 @@
   for (auto& bpt_partition: bpt_file["partitions"]) {
     if (bpt_partition["offset"].asUInt64() != previous_end) {
       ComponentDisk* component = disk.add_component_disks();
-      component->set_file_path(CreateFile(bpt_partition["offset"].asUInt64() - previous_end));
+      std::size_t length = bpt_partition["offset"].asUInt64() - previous_end;
+      component->set_file_path(CreateFile(tmp_path_prefix, length));
       component->set_offset(previous_end);
     }
     ComponentDisk* component = disk.add_component_disks();
@@ -140,7 +146,7 @@
   size_t footer_start = bpt_file["settings"]["disk_size"].asUInt64() - GPT_FOOTER_SIZE;
   if (footer_start != previous_end) {
     ComponentDisk* component = disk.add_component_disks();
-    component->set_file_path(CreateFile(footer_start - previous_end));
+    component->set_file_path(CreateFile(tmp_path_prefix, footer_start - previous_end));
     component->set_offset(previous_end);
   }
   ComponentDisk* footer = disk.add_component_disks();
@@ -295,6 +301,7 @@
 };
 
 void CreateCompositeDisk(std::vector<ImagePartition> partitions,
+                         const std::string& tmp_path_prefix,
                          const std::string& header_file,
                          const std::string& footer_file,
                          const std::string& output_composite_path) {
@@ -303,7 +310,8 @@
   auto table = FdToJson(table_fd);
   auto partition_table_fd = BpttoolMakePartitionTable(JsonToFd(bpttool_input_json));
   CreateGptFiles(partition_table_fd, header_file, footer_file);
-  auto composite_proto = MakeCompositeDiskSpec(table, partitions, header_file, footer_file);
+  auto composite_proto = MakeCompositeDiskSpec(table, partitions, tmp_path_prefix,
+                                               header_file, footer_file);
   std::ofstream composite(output_composite_path.c_str(), std::ios::binary | std::ios::trunc);
   composite << "composite_disk\x1d";
   composite_proto.SerializeToOstream(&composite);
diff --git a/host/commands/assemble_cvd/image_aggregator.h b/host/commands/assemble_cvd/image_aggregator.h
index d27931b..8f1cb8e 100644
--- a/host/commands/assemble_cvd/image_aggregator.h
+++ b/host/commands/assemble_cvd/image_aggregator.h
@@ -27,6 +27,7 @@
 void AggregateImage(const std::vector<ImagePartition>& partitions,
                     const std::string& output_path);
 void CreateCompositeDisk(std::vector<ImagePartition> partitions,
+                         const std::string& tmp_path_prefix,
                          const std::string& header_file,
                          const std::string& footer_file,
                          const std::string& output_composite_path);