Fix cryptfs to work with a raw block device for key storage

If a raw block is specified for key storage, do not try to force the size
of the file to 16 Kbytes when writing the keys, and do not complain if
the size is not 16 Kbytes when reading the keys.  Only do them if the
keyfile is a regular file.

Change-Id: I4de1cb7c3614479d93289d4f2767ca6ce1bbbc73
diff --git a/cryptfs.c b/cryptfs.c
index 3107e3e..e3672f7 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -132,6 +132,7 @@
   int rc = -1;
   char *fname;
   char key_loc[PROPERTY_VALUE_MAX];
+  struct stat statbuf;
 
   property_get(KEY_LOC_PROP, key_loc, KEY_IN_FOOTER);
 
@@ -203,9 +204,11 @@
     }
   }
 
-  if (key_loc[0] == '/') {
+  fstat(fd, &statbuf);
+  /* If the keys are kept on a raw block device, do not try to truncate it. */
+  if (S_ISREG(statbuf.st_mode) && (key_loc[0] == '/')) {
     if (ftruncate(fd, 0x4000)) {
-      SLOGE("Cannot set footer file sizen", fname);
+      SLOGE("Cannot set footer file size\n", fname);
       goto errout;
     }
   }
@@ -263,7 +266,7 @@
 
     /* Make sure it's 16 Kbytes in length */
     fstat(fd, &statbuf);
-    if (statbuf.st_size != 0x4000) {
+    if (S_ISREG(statbuf.st_mode) && (statbuf.st_size != 0x4000)) {
       SLOGE("footer file %s is not the expected size!\n", fname);
       goto errout;
     }