From 9f6be0ce4b895eb0941e4c7063520a7d95fb7de9 Mon Sep 17 00:00:00 2001 From: Joseph Quadrino Date: Wed, 31 Jul 2013 03:07:51 -0400 Subject: [PATCH] stat file before attempting to open --- other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 8e278b28..4f28fb3c 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c @@ -123,11 +123,12 @@ void manage_keys(char *keys_file) { const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint8_t keys[KEYS_SIZE]; - - /* TODO: stat the file before trying to open it. We aren't cave people! */ - FILE *keysf = fopen(keys_file, "r"); - if (keysf != NULL) { - /* if file was opened successfully -- load keys */ + struct stat existence; + FILE *keysf; + + /* Check if file exits, proceed to open and load keys */ + if(stat(keys_file,&existence) >= 0) { + keysf = fopen(keys_file, "r"); size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keysf); if (read_size != KEYS_SIZE) { printf("Error while reading the key file\nExiting.\n");