Added support for ext4 ASEC resizing.

ASECs formatted as ext4 can now be resized using vdc asec resize.
Refactored some common code.
Requires resize2fs.

Change-Id: Ie78bb6015114a7bc4af42b16d1f299322ffc1e2a
Signed-off-by: Daniel Rosenberg <drosen@google.com>
diff --git a/Loop.cpp b/Loop.cpp
index 3f0ee1e..8672d93 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -249,6 +249,34 @@
     return 0;
 }
 
+int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
+    int fd;
+
+    if ((fd = open(file, O_RDWR)) < 0) {
+        SLOGE("Error opening imagefile (%s)", strerror(errno));
+        return -1;
+    }
+
+    SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
+
+    if (fallocate(fd, 0, 0, numSectors * 512)) {
+        if (errno == ENOSYS) {
+            SLOGW("fallocate not found. Falling back to ftruncate.");
+            if (ftruncate(fd, numSectors * 512) < 0) {
+                SLOGE("Error truncating imagefile (%s)", strerror(errno));
+                close(fd);
+                return -1;
+            }
+        } else {
+            SLOGE("Error allocating space (%s)", strerror(errno));
+            close(fd);
+            return -1;
+        }
+    }
+    close(fd);
+    return 0;
+}
+
 int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
     int fd;
     struct asec_superblock buffer;