[fvm] create images with specified length

It is sometimes useful to be able to specify the size that an FVM image
should be on the command line. The length flag can be used for this
purpose, the file just needs to be truncated to that size.

Change-Id: I9ce00b2c32858d56b6789e24649d393274f3f19a
diff --git a/system/host/fvm/main.cpp b/system/host/fvm/main.cpp
index 7e3ab22..163664b 100644
--- a/system/host/fvm/main.cpp
+++ b/system/host/fvm/main.cpp
@@ -4,6 +4,7 @@
 
 #include <fbl/alloc_checker.h>
 #include <fbl/unique_ptr.h>
+#include <unistd.h>
 
 #include "fvm/container.h"
 
@@ -112,6 +113,17 @@
     }
 
     if (!strcmp(command, "create")) {
+
+        // If length was specified, an offset was not, we were asked to create a
+        // file, and the file does not exist, truncate it to the given length.
+        if (length != 0 && offset == 0) {
+            fbl::unique_fd fd(open(path, O_CREAT|O_EXCL|O_WRONLY, 0644));
+
+            if (fd) {
+                ftruncate(fd.get(), length);
+            }
+        }
+
         fbl::unique_ptr<FvmContainer> fvmContainer;
         if (FvmContainer::Create(path, slice_size, offset, length, &fvmContainer) != ZX_OK) {
             return -1;