Don't write artifacts under staging

The staging dir (.../com.android.art/staging) is where odrefresh
creates artifacts before moving (well, copying) them to the final
target directory, typically .../com.android.art/dalvik-cache, or
compos-pending in our case).

But we were creating directories .../com.android.art/staging/staging
and .../com.android.art/staging/dalvik-cache instead.

So we need to pass the staging dir and the output dir separately, and
also specify the sub-directory name under the output directory where
the final artifacts should end up.

While I'm modifying the signature of odrefresh() I also made it return
just the odrefresh exit code.

Modified the implementation and callers to handle the new signature.

Also made a couple of small fixes.

Bug: 209572296
Bug: 210460516
Test: composd_cmd forced-odrefresh
Test: composd_cmd async-odrefresh
Test: (both with selinux in microdroid disabled)
Change-Id: I3dcdab7d9ae042e46f73d716f81f834de0f4c4f0
diff --git a/compos/composd/src/odrefresh.rs b/compos/composd/src/odrefresh.rs
index 3f6bdf1..d6a3435 100644
--- a/compos/composd/src/odrefresh.rs
+++ b/compos/composd/src/odrefresh.rs
@@ -34,7 +34,9 @@
 
 // TODO: What if this changes?
 const EX_MAX: i8 = 78;
+
 const ODREFRESH_BIN: &str = "/apex/com.android.art/bin/odrefresh";
+const ART_APEX_DATA: &str = "/data/misc/apexdata/com.android.art";
 
 #[derive(Debug, PartialEq, Eq, FromPrimitive)]
 #[repr(i8)]
@@ -97,22 +99,27 @@
     }
 }
 
-pub fn run_in_vm(service: Strong<dyn ICompOsService>, _target_dir_name: &str) -> Result<ExitCode> {
-    // TODO: Use target_dir_name
+pub fn run_in_vm(service: Strong<dyn ICompOsService>, target_dir_name: &str) -> Result<ExitCode> {
     let staging_dir = open_dir(composd_native::palette_create_odrefresh_staging_directory()?)?;
     let system_dir = open_dir(Path::new("/system"))?;
+    let output_dir = open_dir(Path::new(ART_APEX_DATA))?;
 
     // Spawn a fd_server to serve the FDs.
     let fd_server_config = FdServerConfig {
         ro_dir_fds: vec![system_dir.as_raw_fd()],
-        rw_dir_fds: vec![staging_dir.as_raw_fd()],
+        rw_dir_fds: vec![staging_dir.as_raw_fd(), output_dir.as_raw_fd()],
         ..Default::default()
     };
     let fd_server_raii = fd_server_config.into_fd_server()?;
 
     let zygote_arch = system_properties::read("ro.zygote")?;
-    let exit_code =
-        service.odrefresh(system_dir.as_raw_fd(), staging_dir.as_raw_fd(), &zygote_arch)?.exitCode;
+    let exit_code = service.odrefresh(
+        system_dir.as_raw_fd(),
+        output_dir.as_raw_fd(),
+        staging_dir.as_raw_fd(),
+        target_dir_name,
+        &zygote_arch,
+    )?;
 
     drop(fd_server_raii);
     if let Some(exit_code) = FromPrimitive::from_i8(exit_code) {
diff --git a/compos/composd/src/service.rs b/compos/composd/src/service.rs
index 3f31adb..6ce462c 100644
--- a/compos/composd/src/service.rs
+++ b/compos/composd/src/service.rs
@@ -108,7 +108,7 @@
     ) -> Result<Strong<dyn ICompilationTask>> {
         let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
 
-        let target_dir_name = "test-instance".to_owned();
+        let target_dir_name = "test-artifacts".to_owned();
         let task = OdrefreshTask::start(comp_os, target_dir_name, callback)?;
 
         Ok(BnCompilationTask::new_binder(task, BinderFeatures::default()))