Add getmtime command for keys

This allows you to check when a key was last modified.

Change-Id: I167844d9a50e26aadfc73a2252b937d2ef09f09d
diff --git a/keystore/keystore.cpp b/keystore/keystore.cpp
index d90b999..ef4c7d4 100644
--- a/keystore/keystore.cpp
+++ b/keystore/keystore.cpp
@@ -1273,6 +1273,39 @@
     return keyStore->removeGrant(filename, granteeData) ? NO_ERROR : KEY_NOT_FOUND;
 }
 
+static ResponseCode getmtime(KeyStore*, int sock, uid_t uid, Value* keyName,
+        Value*, Value*) {
+    char filename[NAME_MAX];
+    encode_key_for_uid(filename, uid, keyName);
+    if (access(filename, R_OK) == -1) {
+        return (errno != ENOENT) ? SYSTEM_ERROR : KEY_NOT_FOUND;
+    }
+
+    int fd = open(filename, O_NOFOLLOW, O_RDONLY);
+    if (fd < 0) {
+        return SYSTEM_ERROR;
+    }
+
+    struct stat s;
+    int ret = fstat(fd, &s);
+    close(fd);
+    if (ret == -1) {
+        return SYSTEM_ERROR;
+    }
+
+    uint8_t *data;
+    int dataLength = asprintf(reinterpret_cast<char**>(&data), "%lu", s.st_mtime);
+    if (dataLength < 0) {
+        return SYSTEM_ERROR;
+    }
+
+    send_code(sock, NO_ERROR);
+    send_message(sock, data, dataLength);
+    free(data);
+
+    return NO_ERROR_RESPONSE_CODE_SENT;
+}
+
 /* Here are the permissions, actions, users, and the main function. */
 enum perm {
     P_TEST     = 1 << TEST,
@@ -1322,6 +1355,7 @@
     {del_key,    CommandCodes[DEL_KEY],    STATE_ANY,      P_DELETE,   {KEY_SIZE, 0, 0}},
     {grant,      CommandCodes[GRANT],      STATE_NO_ERROR, P_GRANT,    {KEY_SIZE, KEY_SIZE, 0}},
     {ungrant,    CommandCodes[UNGRANT],    STATE_NO_ERROR, P_GRANT,    {KEY_SIZE, KEY_SIZE, 0}},
+    {getmtime,   CommandCodes[GETMTIME],   STATE_ANY,      P_SAW,      {KEY_SIZE, 0, 0}},
     {NULL,       0,                        STATE_ANY,      0,          {0, 0, 0}},
 };
 
diff --git a/keystore/keystore.h b/keystore/keystore.h
index fe2ce56..f5e4439 100644
--- a/keystore/keystore.h
+++ b/keystore/keystore.h
@@ -63,12 +63,13 @@
     DEL_KEY = 16,
     GRANT = 17,
     UNGRANT = 18,
+    GETMTIME = 19,
 };
 
 typedef uint8_t command_code_t;
 
 // Taken: a b c d e f g h i j k l m n o p q r s t u v w x y z
-//        * *   * *   *   *   * * * *   *   * * * * *   *   *
+//        * * * * *   *   *   * * * *   *   * * * * *   *   *
 command_code_t CommandCodes[] = {
     't', // TEST
     'g', // GET
@@ -89,6 +90,7 @@
     'k', // DEL_KEY
     'x', // GRANT
     'y', // UNGRANT
+    'c', // GETMTIME
 };
 
 /**