Merge pull request #1116 from mrdomino/random-cleanup

Random cleanup
This commit is contained in:
Joe Bryan 2018-12-28 00:00:03 -05:00 committed by GitHub
commit 991f39deb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,8 +13,6 @@
#include "vere/vere.h" #include "vere/vere.h"
#if defined(U3_OS_linux) #if defined(U3_OS_linux)
#include <stdio_ext.h>
#define fpurge(fd) __fpurge(fd)
#define DEVRANDOM "/dev/urandom" #define DEVRANDOM "/dev/urandom"
#else #else
#define DEVRANDOM "/dev/random" #define DEVRANDOM "/dev/random"
@ -42,18 +40,15 @@ u3_sist_pack(c3_w tem_w, c3_w typ_w, c3_w* bob_w, c3_w len_w)
lar_u.len_w = len_w; lar_u.len_w = len_w;
if ( -1 == lseek64(lug_u->fid_i, 4ULL * tar_d, SEEK_SET) ) { if ( -1 == lseek64(lug_u->fid_i, 4ULL * tar_d, SEEK_SET) ) {
perror("lseek"); uL(fprintf(uH, "sist_pack: seek failed, lseek: %s\n", strerror(errno)));
uL(fprintf(uH, "sist_pack: seek failed\n"));
c3_assert(0); c3_assert(0);
} }
if ( sizeof(lar_u) != write(lug_u->fid_i, &lar_u, sizeof(lar_u)) ) { if ( sizeof(lar_u) != write(lug_u->fid_i, &lar_u, sizeof(lar_u)) ) {
perror("write"); uL(fprintf(uH, "sist_pack: write failed, write: %s\n", strerror(errno)));
uL(fprintf(uH, "sist_pack: write failed\n"));
c3_assert(0); c3_assert(0);
} }
if ( -1 == lseek64(lug_u->fid_i, 4ULL * lug_u->len_d, SEEK_SET) ) { if ( -1 == lseek64(lug_u->fid_i, 4ULL * lug_u->len_d, SEEK_SET) ) {
perror("lseek"); uL(fprintf(uH, "sist_pack: seek failed, lseek: %s\n", strerror(errno)));
uL(fprintf(uH, "sist_pack: seek failed\n"));
c3_assert(0); c3_assert(0);
} }
#if 0 #if 0
@ -65,8 +60,7 @@ u3_sist_pack(c3_w tem_w, c3_w typ_w, c3_w* bob_w, c3_w len_w)
lar_u.mug_w)); lar_u.mug_w));
#endif #endif
if ( (4 * len_w) != write(lug_u->fid_i, bob_w, (4 * len_w)) ) { if ( (4 * len_w) != write(lug_u->fid_i, bob_w, (4 * len_w)) ) {
perror("write"); uL(fprintf(uH, "sist_pack: write failed, write: %s\n", strerror(errno)));
uL(fprintf(uH, "sist_pack: write failed\n"));
c3_assert(0); c3_assert(0);
} }
lug_u->len_d += (c3_d)(lar_u.len_w + c3_wiseof(lar_u)); lug_u->len_d += (c3_d)(lar_u.len_w + c3_wiseof(lar_u));
@ -102,20 +96,19 @@ u3_sist_put(const c3_c* key_c, const c3_y* val_y, size_t siz_i)
c3_assert(ret_i < 2048); c3_assert(ret_i < 2048);
if ( (fid_i = open(ful_c, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0 ) { if ( (fid_i = open(ful_c, O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0 ) {
uL(fprintf(uH, "sist: could not put %s\n", key_c)); uL(fprintf(uH, "sist: could not put %s: %s\n", key_c, strerror(errno)));
perror("open");
u3_lo_bail(); u3_lo_bail();
} }
if ( (ret_i = write(fid_i, val_y, siz_i)) != siz_i ) { if ( (ret_i = write(fid_i, val_y, siz_i)) != siz_i ) {
uL(fprintf(uH, "sist: could not write %s\n", key_c)); uL(fprintf(uH, "sist: could not write %s\n", key_c));
if ( ret_i < 0 ) { if ( ret_i < 0 ) {
perror("write"); uL(fprintf(uH, "write: %s\n", strerror(errno)));
} }
u3_lo_bail(); u3_lo_bail();
} }
ret_i = c3_sync(fid_i); ret_i = c3_sync(fid_i);
if ( ret_i < 0 ) { if ( ret_i < 0 ) {
perror("sync"); uL(fprintf(uH, "sync: %s\n", strerror(errno)));
} }
ret_i = close(fid_i); ret_i = close(fid_i);
c3_assert(0 == ret_i); c3_assert(0 == ret_i);
@ -138,8 +131,7 @@ u3_sist_has(const c3_c* key_c)
return -1; return -1;
} }
else { else {
uL(fprintf(uH, "sist: could not stat %s\n", key_c)); uL(fprintf(uH, "sist: could not stat %s: %s\n", key_c, strerror(errno)));
perror("stat");
u3_lo_bail(); u3_lo_bail();
} }
} }
@ -163,19 +155,17 @@ u3_sist_get(const c3_c* key_c, c3_y* val_y)
c3_assert(ret_i < 2048); c3_assert(ret_i < 2048);
if ( (fid_i = open(ful_c, O_RDONLY)) < 0 ) { if ( (fid_i = open(ful_c, O_RDONLY)) < 0 ) {
uL(fprintf(uH, "sist: could not get %s\n", key_c)); uL(fprintf(uH, "sist: could not get %s: %s\n", key_c, strerror(errno)));
perror("open");
u3_lo_bail(); u3_lo_bail();
} }
if ( (ret_i = fstat(fid_i, &sat_u)) < 0 ) { if ( (ret_i = fstat(fid_i, &sat_u)) < 0 ) {
uL(fprintf(uH, "sist: could not stat %s\n", key_c)); uL(fprintf(uH, "sist: could not stat %s: %s\n", key_c, strerror(errno)));
perror("fstat");
u3_lo_bail(); u3_lo_bail();
} }
if ( (ret_i = read(fid_i, val_y, sat_u.st_size)) != sat_u.st_size ) { if ( (ret_i = read(fid_i, val_y, sat_u.st_size)) != sat_u.st_size ) {
uL(fprintf(uH, "sist: could not read %s\n", key_c)); uL(fprintf(uH, "sist: could not read %s\n", key_c));
if ( ret_i < 0 ) { if ( ret_i < 0 ) {
perror("read"); uL(fprintf(uH, "read: %s\n", strerror(errno)));
} }
u3_lo_bail(); u3_lo_bail();
} }
@ -199,8 +189,8 @@ u3_sist_nil(const c3_c* key_c)
return; return;
} }
else { else {
uL(fprintf(uH, "sist: could not unlink %s\n", key_c)); uL(fprintf(uH, "sist: could not unlink %s: %s\n", key_c,
perror("unlink"); strerror(errno)));
u3_lo_bail(); u3_lo_bail();
} }
} }
@ -361,7 +351,7 @@ _sist_text(c3_c* pom_c)
printf("%s: ", pom_c); printf("%s: ", pom_c);
paw_c[0] = 0; paw_c[0] = 0;
fpurge(stdin); c3_fpurge(stdin);
fgets(paw_c, 179, stdin); fgets(paw_c, 179, stdin);
if ( '\n' == paw_c[0] ) { if ( '\n' == paw_c[0] ) {
@ -552,23 +542,15 @@ _sist_zest()
u3Z->lug_u.fid_i = fid_i; u3Z->lug_u.fid_i = fid_i;
} }
// Generate a 31-bit salt. // Generate a 31-bit salt and 64-bit passcode.
// //
{ {
c3_w rad_w[16]; u3_noun pas;
c3_w rad_w[16];
c3_rand(rad_w); c3_rand(rad_w);
sal_l = (0x7fffffff & rad_w[0]); sal_l = (0x7fffffff & rad_w[0]);
} pas = u3i_words(2, rad_w + 1);
// Create and save a passcode.
//
{
c3_w rad_w[16];
u3_noun pas;
c3_rand(rad_w);
pas = u3i_words(2, rad_w);
u3A->key = _sist_fatt(sal_l, u3k(pas)); u3A->key = _sist_fatt(sal_l, u3k(pas));
_sist_fast(pas, u3r_mug(u3A->key)); _sist_fast(pas, u3r_mug(u3A->key));
@ -622,8 +604,7 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
c3_assert(led_u.mag_l == u3r_mug('f')); c3_assert(led_u.mag_l == u3r_mug('f'));
if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) {
uL(fprintf(uH, "rest_nuu failed (a)\n")); uL(fprintf(uH, "rest_nuu failed (a), lseek64: %s\n", strerror(errno)));
perror("lseek64");
u3_lo_bail(); u3_lo_bail();
} }
@ -636,13 +617,11 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
tar_d = (end_d - (c3_d)c3_wiseof(u3_olar)); tar_d = (end_d - (c3_d)c3_wiseof(u3_olar));
if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) { if ( -1 == lseek64(fid_i, 4ULL * tar_d, SEEK_SET) ) {
uL(fprintf(uH, "rest_nuu failed (b)\n")); uL(fprintf(uH, "rest_nuu failed (b), lseek64: %s\n", strerror(errno)));
perror("lseek64");
u3_lo_bail(); u3_lo_bail();
} }
if ( sizeof(u3_olar) != read(fid_i, &lar_u, sizeof(u3_olar)) ) { if ( sizeof(u3_olar) != read(fid_i, &lar_u, sizeof(u3_olar)) ) {
uL(fprintf(uH, "rest_nuu failed (c)\n")); uL(fprintf(uH, "rest_nuu failed (c), read: %s\n", strerror(errno)));
perror("read");
u3_lo_bail(); u3_lo_bail();
} }
@ -655,13 +634,11 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
end_d = (tar_d - (c3_d)lar_u.len_w); end_d = (tar_d - (c3_d)lar_u.len_w);
if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) {
uL(fprintf(uH, "rest_nuu failed (e)\n")); uL(fprintf(uH, "rest_nuu failed (e), lseek64: %s\n", strerror(errno)));
perror("lseek64");
u3_lo_bail(); u3_lo_bail();
} }
if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) { if ( (4 * lar_u.len_w) != read(fid_i, img_w, (4 * lar_u.len_w)) ) {
uL(fprintf(uH, "rest_nuu failed (f)\n")); uL(fprintf(uH, "rest_nuu failed (f), read: %s\n", strerror(errno)));
perror("read");
u3_lo_bail(); u3_lo_bail();
} }
@ -677,8 +654,7 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
} }
if ( 0 != close(fid_i) ) { if ( 0 != close(fid_i) ) {
uL(fprintf(uH, "rest: could not close\n")); uL(fprintf(uH, "rest: could not close, close: %s\n", strerror(errno)));
perror("close");
u3_lo_bail(); u3_lo_bail();
} }
@ -686,15 +662,14 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
c3_assert(ret_i < 2048); c3_assert(ret_i < 2048);
if ( (fud_i = open(nuu_c, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0 ) { if ( (fud_i = open(nuu_c, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0 ) {
uL(fprintf(uH, "rest: can't open record (%s)\n", nuu_c)); uL(fprintf(uH, "rest: can't open record (%s), open: %s\n", nuu_c,
perror("open"); strerror(errno)));
u3_lo_bail(); u3_lo_bail();
} }
led_u.mag_l = u3r_mug('g'); led_u.mag_l = u3r_mug('g');
if ( (sizeof(led_u) != write(fud_i, &led_u, sizeof(led_u))) ) { if ( (sizeof(led_u) != write(fud_i, &led_u, sizeof(led_u))) ) {
uL(fprintf(uH, "rest: can't write header\n")); uL(fprintf(uH, "rest: can't write header, write: %s\n", strerror(errno)));
perror("write");
u3_lo_bail(); u3_lo_bail();
} }
@ -724,13 +699,11 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
u3z(ovo); u3z(ovo);
if ( (lar_u.len_w << 2) != write(fud_i, img_w, lar_u.len_w << 2) ) { if ( (lar_u.len_w << 2) != write(fud_i, img_w, lar_u.len_w << 2) ) {
uL(fprintf(uH, "rest_nuu failed (h)\n")); uL(fprintf(uH, "rest_nuu failed (h), write: %s\n", strerror(errno)));
perror("write");
u3_lo_bail(); u3_lo_bail();
} }
if ( sizeof(u3_ular) != write(fud_i, &lar_u, sizeof(u3_ular)) ) { if ( sizeof(u3_ular) != write(fud_i, &lar_u, sizeof(u3_ular)) ) {
uL(fprintf(uH, "rest_nuu failed (i)\n")); uL(fprintf(uH, "rest_nuu failed (i), write: %s\n", strerror(errno)));
perror("write");
u3_lo_bail(); u3_lo_bail();
} }
@ -740,13 +713,11 @@ _sist_rest_nuu(u3_ulog* lug_u, u3_uled led_u, c3_c* old_c)
} }
} }
if ( 0 != rename(nuu_c, old_c) ) { if ( 0 != rename(nuu_c, old_c) ) {
uL(fprintf(uH, "rest_nuu failed (k)\n")); uL(fprintf(uH, "rest_nuu failed (k), rename: %s\n", strerror(errno)));
perror("rename");
u3_lo_bail(); u3_lo_bail();
} }
if ( -1 == lseek64(fud_i, sizeof(u3_uled), SEEK_SET) ) { if ( -1 == lseek64(fud_i, sizeof(u3_uled), SEEK_SET) ) {
uL(fprintf(uH, "rest_nuu failed (l)\n")); uL(fprintf(uH, "rest_nuu failed (l), lseek64: %s\n", strerror(errno)));
perror("lseek64");
u3_lo_bail(); u3_lo_bail();
} }
lug_u->fid_i = fud_i; lug_u->fid_i = fud_i;
@ -882,8 +853,8 @@ _sist_rest()
ent_d = 0; ent_d = 0;
if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) { if ( -1 == lseek64(fid_i, 4ULL * end_d, SEEK_SET) ) {
fprintf(stderr, "end_d %" PRIu64 "\n", end_d); uL(fprintf(uH, "end_d %" PRIu64 ", lseek64: %s\n", end_d,
perror("lseek"); strerror(errno)));
uL(fprintf(uH, "record (%s) is corrupt (c)\n", ful_c)); uL(fprintf(uH, "record (%s) is corrupt (c)\n", ful_c));
u3_lo_bail(); u3_lo_bail();
} }