Exclude subdirectories when pruning the dex cache.

This requires a companion package manager change to prune
each instruction specific dex cache individually.

bug: 15677279

Change-Id: I5891981512bde20e49bff65b1842c28886f2b177
diff --git a/cmds/installd/commands.c b/cmds/installd/commands.c
index 566ce55..6d6a50d 100644
--- a/cmds/installd/commands.c
+++ b/cmds/installd/commands.c
@@ -1384,10 +1384,11 @@
 
 static int prune_dex_exclusion_predicate(const char *file_name, const int is_dir)
 {
-    // Don't exclude any directories, we want to inspect them
-    // recusively for files.
+    // Exclude all directories. The top level command will be
+    // given a list of ISA specific directories that are assumed
+    // to be flat.
     if (is_dir) {
-      return 0;
+      return 1;
     }
 
 
@@ -1405,6 +1406,26 @@
     return 1;
 }
 
-int prune_dex_cache() {
-  return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
+int prune_dex_cache(const char* subdir) {
+    // "." is handled as a special case, and refers to
+    // DALVIK_CACHE_PREFIX (usually /data/dalvik-cache).
+    const bool is_dalvik_cache_root = !strcmp(subdir, ".");
+
+    // Don't allow the path to contain "." or ".." except for the
+    // special case above. This is much stricter than we need to be,
+    // but there's no good reason to support them.
+    if (strchr(subdir, '.' ) != NULL && !is_dalvik_cache_root) {
+        return -1;
+    }
+
+    if (!is_dalvik_cache_root) {
+        char full_path[PKG_PATH_MAX];
+        snprintf(full_path, sizeof(full_path), "%s%s", DALVIK_CACHE_PREFIX, subdir);
+        return delete_dir_contents(full_path, 0, &prune_dex_exclusion_predicate);
+    }
+
+
+    // When subdir == ".", clean the contents of the top level
+    // dalvik-cache directory.
+    return delete_dir_contents(DALVIK_CACHE_PREFIX, 0, &prune_dex_exclusion_predicate);
 }