Merge "Extend vold support for creating ext4 images."
diff --git a/DirectVolume.h b/DirectVolume.h
index de1ed8b..c0139d4 100644
--- a/DirectVolume.h
+++ b/DirectVolume.h
@@ -27,7 +27,7 @@
 
 class DirectVolume : public Volume {
 public:
-    static const int MAX_PARTITIONS = 4;
+    static const int MAX_PARTITIONS = 32;
 protected:
     PathCollection *mPaths;
     int            mDiskMajor;
@@ -37,7 +37,7 @@
     int            mOrigDiskMinor;
     int            mOrigPartMinors[MAX_PARTITIONS];
     int            mDiskNumParts;
-    unsigned char  mPendingPartMap;
+    unsigned int   mPendingPartMap;
     int            mIsDecrypted;
     int            mFlags;
 
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 9867bbb..f2e2d07 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -986,6 +986,19 @@
     return 0;
 }
 
+Volume* VolumeManager::getVolumeForFile(const char *fileName) {
+    VolumeCollection::iterator i;
+
+    for (i = mVolumes->begin(); i != mVolumes->end(); ++i) {
+        const char* mountPoint = (*i)->getMountpoint();
+        if (!strncmp(fileName, mountPoint, strlen(mountPoint))) {
+            return *i;
+        }
+    }
+
+    return NULL;
+}
+
 /**
  * Mounts an image file <code>img</code>.
  */
@@ -1114,7 +1127,7 @@
     }
 
     // Create a string to compare against that has a trailing slash
-    int loopDirLen = sizeof(Volume::LOOPDIR);
+    int loopDirLen = strlen(Volume::LOOPDIR);
     char loopDir[loopDirLen + 2];
     strcpy(loopDir, Volume::LOOPDIR);
     loopDir[loopDirLen++] = '/';
@@ -1463,25 +1476,35 @@
 }
 
 int VolumeManager::cleanupAsec(Volume *v, bool force) {
-    while(mActiveContainers->size()) {
-        AsecIdCollection::iterator it = mActiveContainers->begin();
+    int rc = unmountAllAsecsInDir(Volume::SEC_ASECDIR_EXT);
+
+    AsecIdCollection toUnmount;
+    // Find the remaining OBB files that are on external storage.
+    for (AsecIdCollection::iterator it = mActiveContainers->begin(); it != mActiveContainers->end();
+            ++it) {
         ContainerData* cd = *it;
-        SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
+
         if (cd->type == ASEC) {
-            if (unmountAsec(cd->id, force)) {
-                SLOGE("Failed to unmount ASEC %s (%s)", cd->id, strerror(errno));
-                return -1;
-            }
+            // nothing
         } else if (cd->type == OBB) {
-            if (unmountObb(cd->id, force)) {
-                SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
-                return -1;
+            if (v == getVolumeForFile(cd->id)) {
+                toUnmount.push_back(cd);
             }
         } else {
             SLOGE("Unknown container type %d!", cd->type);
-            return -1;
         }
     }
-    return 0;
+
+    for (AsecIdCollection::iterator it = toUnmount.begin(); it != toUnmount.end(); ++it) {
+        ContainerData *cd = *it;
+        SLOGI("Unmounting ASEC %s (dependant on %s)", cd->id, v->getMountpoint());
+        if (unmountObb(cd->id, force)) {
+            SLOGE("Failed to unmount OBB %s (%s)", cd->id, strerror(errno));
+            rc = -1;
+        }
+    }
+
+    return rc;
+
 }
 
diff --git a/VolumeManager.h b/VolumeManager.h
index 4399b76..198b5a9 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -117,6 +117,8 @@
     int unmountObb(const char *fileName, bool force);
     int getObbMountPath(const char *id, char *buffer, int maxlen);
 
+    Volume* getVolumeForFile(const char *fileName);
+
     /* Shared between ASEC and Loopback images */
     int unmountLoopImage(const char *containerId, const char *loopId,
             const char *fileName, const char *mountPoint, bool force);