projects/sencrypt

changeset 20:c45f17f58de1

Simplify key file reading
author Guido Berhoerster <guido+sencrypt@berhoerster.name>
date Wed Jul 31 13:28:37 2019 +0200 (2019-07-31)
parents 0bd16b428fc0
children 5f9dc8f3c53e
files sencrypt.c
line diff
     1.1 --- a/sencrypt.c	Wed Jul 31 11:21:59 2019 +0200
     1.2 +++ b/sencrypt.c	Wed Jul 31 13:28:37 2019 +0200
     1.3 @@ -69,7 +69,6 @@
     1.4  {
     1.5  	size_t		keyfile_size = 0;
     1.6  	FILE		*fp = NULL;
     1.7 -	struct		stat statbuf;
     1.8  
     1.9  	fp = fopen(filename, "r");
    1.10  	if (fp == NULL) {
    1.11 @@ -77,32 +76,15 @@
    1.12  		goto out;
    1.13  	}
    1.14  
    1.15 -	if (fstat(fileno(fp), &statbuf) == -1) {
    1.16 -		warn("could not stat key file \"%s\"", filename);
    1.17 +	keyfile_size = fread(key, 1, key_size_max, fp);
    1.18 +	if (ferror(fp)) {
    1.19 +		warn("failed to read key file \"%s\"", filename);
    1.20  		goto out;
    1.21 -	}
    1.22 -
    1.23 -	if (!S_ISREG(statbuf.st_mode)) {
    1.24 -		warnx("key file \"%s\" is not a regular file", filename);
    1.25 -		goto out;
    1.26 -	}
    1.27 -
    1.28 -	if ((uintmax_t)statbuf.st_size > SIZE_MAX) {
    1.29 -		warnx("key file \"%s\" is too large", filename);
    1.30 -		goto out;
    1.31 -	}
    1.32 -	keyfile_size = (size_t)statbuf.st_size;
    1.33 -	if ((keyfile_size > key_size_max) ||
    1.34 -	    (keyfile_size == 0)) {
    1.35 +	} else if (!feof(fp) || (keyfile_size < 1)) {
    1.36  		warnx("invalid key size");
    1.37  		goto out;
    1.38  	}
    1.39  
    1.40 -	if (fread(key, 1, keyfile_size, fp) != keyfile_size) {
    1.41 -		warnx("could not read key file \"%s\"", filename);
    1.42 -		goto out;
    1.43 -	}
    1.44 -
    1.45  out:
    1.46  	if (fp != NULL) {
    1.47  		fclose(fp);
    1.48 @@ -589,8 +571,7 @@
    1.49  	}
    1.50  
    1.51  	if (kflag) {
    1.52 -		key_file_len = read_keyfile(key_filename, key,
    1.53 -		    (off_t)sizeof (key));
    1.54 +		key_file_len = read_keyfile(key_filename, key, sizeof (key));
    1.55  		if (key_file_len < 1) {
    1.56  			status = EXIT_FAILURE;
    1.57  			goto out;