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/src/compilation.rs b/compos/src/compilation.rs
index 44b4049..f8a66c2 100644
--- a/compos/src/compilation.rs
+++ b/compos/src/compilation.rs
@@ -15,10 +15,10 @@
  */
 
 use anyhow::{anyhow, bail, Context, Result};
-use log::error;
+use log::{debug, error, info};
 use minijail::{self, Minijail};
 use std::env;
-use std::fs::{create_dir, File};
+use std::fs::File;
 use std::os::unix::io::{AsRawFd, RawFd};
 use std::path::{Path, PathBuf};
 
@@ -60,11 +60,13 @@
 
 pub fn odrefresh(
     odrefresh_path: &Path,
+    target_dir_name: &str,
     system_dir_fd: i32,
     output_dir_fd: i32,
+    staging_dir_fd: i32,
     zygote_arch: &str,
     authfs_service: Strong<dyn IAuthFsService>,
-) -> Result<CompilerOutput> {
+) -> Result<i8> {
     // Mount authfs (via authfs_service). The authfs instance unmounts once the `authfs` variable
     // is out of scope.
     let authfs_config = AuthFsConfig {
@@ -75,7 +77,10 @@
             manifestPath: "/dev/null".to_string(),
             prefix: "/system".to_string(),
         }],
-        outputDirFdAnnotations: vec![OutputDirFdAnnotation { fd: output_dir_fd }],
+        outputDirFdAnnotations: vec![
+            OutputDirFdAnnotation { fd: output_dir_fd },
+            OutputDirFdAnnotation { fd: staging_dir_fd },
+        ],
         ..Default::default()
     };
     let authfs = authfs_service.mount(&authfs_config)?;
@@ -91,26 +96,25 @@
     env::set_var("ART_APEX_DATA", &art_apex_data);
 
     let mut staging_dir = mountpoint;
-    staging_dir.push(output_dir_fd.to_string());
-    staging_dir.push("staging");
-    create_dir(&staging_dir)
-        .with_context(|| format!("Create staging directory {}", staging_dir.display()))?;
+    staging_dir.push(staging_dir_fd.to_string());
 
     let args = vec![
         "odrefresh".to_string(),
         format!("--zygote-arch={}", zygote_arch),
+        format!("--dalvik-cache={}", target_dir_name),
         "--no-refresh".to_string(),
         format!("--staging-dir={}", staging_dir.display()),
         "--force-compile".to_string(),
     ];
+    debug!("Running odrefresh with args: {:?}", &args);
     let jail = spawn_jailed_task(odrefresh_path, &args, Vec::new() /* fd_mapping */)
         .context("Spawn odrefresh")?;
     match jail.wait() {
         // TODO(161471326): On success, sign all files in the output directory.
-        Ok(()) => Ok(CompilerOutput::ExitCode(0)),
+        Ok(()) => Ok(0i8),
         Err(minijail::Error::ReturnCode(exit_code)) => {
-            error!("odrefresh failed with exit code {}", exit_code);
-            Ok(CompilerOutput::ExitCode(exit_code as i8))
+            info!("odrefresh exited with exit code {}", exit_code);
+            Ok(exit_code as i8)
         }
         Err(e) => {
             bail!("Unexpected minijail error: {}", e)
diff --git a/compos/src/compsvc.rs b/compos/src/compsvc.rs
index 19f2f47..4540cd8 100644
--- a/compos/src/compsvc.rs
+++ b/compos/src/compsvc.rs
@@ -106,9 +106,11 @@
         &self,
         system_dir_fd: i32,
         output_dir_fd: i32,
+        staging_dir_fd: i32,
+        target_dir_name: &str,
         zygote_arch: &str,
-    ) -> BinderResult<CompilationResult> {
-        if system_dir_fd < 0 || output_dir_fd < 0 {
+    ) -> BinderResult<i8> {
+        if system_dir_fd < 0 || output_dir_fd < 0 || staging_dir_fd < 0 {
             return Err(new_binder_exception(
                 ExceptionCode::ILLEGAL_ARGUMENT,
                 "The remote FDs are expected to be non-negative",
@@ -122,10 +124,12 @@
         }
 
         let authfs_service = get_authfs_service()?;
-        let output = odrefresh(
+        odrefresh(
             &self.odrefresh_path,
+            target_dir_name,
             system_dir_fd,
             output_dir_fd,
+            staging_dir_fd,
             zygote_arch,
             authfs_service,
         )
@@ -135,13 +139,7 @@
                 ExceptionCode::SERVICE_SPECIFIC,
                 format!("odrefresh failed: {}", e),
             )
-        })?;
-        match output {
-            CompilerOutput::ExitCode(exit_code) => {
-                Ok(CompilationResult { exitCode: exit_code, ..Default::default() })
-            }
-            _ => Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "odrefresh failed")),
-        }
+        })
     }
 
     fn compile_cmd(