Extract page aligned file copying into a helper function.

RELNOTES: None.
PiperOrigin-RevId: 384361007
diff --git a/src/tools/singlejar/output_jar.cc b/src/tools/singlejar/output_jar.cc
index 7f57f32..74417ae 100644
--- a/src/tools/singlejar/output_jar.cc
+++ b/src/tools/singlejar/output_jar.cc
@@ -1011,9 +1011,8 @@
   }
 }
 
-void OutputJar::AppendCDSArchive(const std::string &cds_archive) {
-  // Align the shared archive start offset at page alignment, which is
-  // required by mmap.
+off64_t OutputJar::PageAlignedAppendFile(const std::string &file_path) {
+  // Align the file start offset at page boundary.
   off64_t cur_offset = Position();
   size_t pagesize;
 #ifdef _WIN32
@@ -1028,8 +1027,8 @@
   size_t written;
   if (gap > 0) {
     char *zeros = (char *)malloc(gap);
-    if (!zeros) {
-      return;
+    if (zeros == nullptr) {
+      diag_err(1, "%s:%d: malloc", __FILE__, __LINE__);
     }
     memset(zeros, 0, gap);
     written = fwrite(zeros, 1, gap, file_);
@@ -1037,8 +1036,16 @@
     free(zeros);
   }
 
-  // Copy archived data
-  AppendFile(options_, cds_archive.c_str());
+  // Copy file
+  AppendFile(options_, file_path.c_str());
+
+  return aligned_offset;
+}
+
+void OutputJar::AppendCDSArchive(const std::string &cds_archive) {
+  // Align the shared archive start offset at page alignment, which is
+  // required by mmap.
+  off64_t aligned_offset = OutputJar::PageAlignedAppendFile(cds_archive);
 
   // Write the file offset of the shared archive section as a manifest
   // attribute.
diff --git a/src/tools/singlejar/output_jar.h b/src/tools/singlejar/output_jar.h
index 8a3a31b..21a93fa 100644
--- a/src/tools/singlejar/output_jar.h
+++ b/src/tools/singlejar/output_jar.h
@@ -98,6 +98,8 @@
                          const std::string& resource_path);
   // Append CDS archive file.
   void AppendCDSArchive(const std::string &cds_archive);
+  // Append file starting at page boundary.
+  off64_t PageAlignedAppendFile(const std::string &file_path);
   // Append data from the file specified by file_path.
   void AppendFile(Options *options, const char *const file_path);
   // Copy 'count' bytes starting at 'offset' from the given file.