Skip to content

Commit

Permalink
disk: refactor epoc/vere.txt generation and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pkova committed Jul 19, 2024
1 parent a63cb27 commit 8f1bf10
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions pkg/vere/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,23 +1126,36 @@ u3_disk_epoc_zero(u3_disk* log_u)
c3_c epi_c[8193];
c3_c epv_c[8193];
snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c);
FILE* epv_f = fopen(epi_c, "w"); // XX errors
fprintf(epv_f, "%d", U3E_VERLAT);
fclose(epv_f);
snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c);
rename(epi_c, epv_c);
c3_unlink(epi_c);
FILE* epv_f = fopen(epi_c, "w"); // XX errors

if ( !epv_f
|| (0 > fprintf(epv_f, "%d", U3E_VERLAT))
|| fflush(epv_f)
|| (-1 == c3_sync(fileno(epv_f)))
|| fclose(epv_f)
|| (-1 == rename(epi_c, epv_c)) )
{
fprintf(stderr, "disk: write epoc.txt failed %s\r\n", strerror(errno));
goto fail3;
}

// create binary version file, overwriting any existing file
c3_c bii_c[8193];
c3_c biv_c[8193];
snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c);
FILE* biv_f = fopen(bii_c, "w"); // XX errors
fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover?
fclose(biv_f);
snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c);
rename(bii_c, biv_c);
c3_unlink(bii_c);
FILE* biv_f = fopen(bii_c, "w");
if ( !biv_f
|| (0 > fprintf(biv_f, URBIT_VERSION))
|| fflush(biv_f)
|| (-1 == c3_sync(fileno(biv_f)))
|| fclose(biv_f)
|| (-1 == rename(bii_c, biv_c)) )
{
fprintf(stderr, "disk: write vere.txt failed %s\r\n", strerror(errno));
goto fail3;
}

if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux?
fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno));
Expand Down Expand Up @@ -1216,23 +1229,41 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d)
c3_c epi_c[8193];
c3_c epv_c[8193];
snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c);
FILE* epv_f = fopen(epi_c, "w"); // XX errors
fprintf(epv_f, "%d", U3E_VERLAT);
fclose(epv_f);
snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c);
rename(epi_c, epv_c);
c3_unlink(epi_c);
FILE* epv_f = fopen(epi_c, "w");

if ( !epv_f
|| (0 > fprintf(epv_f, "%d", U3E_VERLAT))
|| fflush(epv_f)
|| (-1 == c3_sync(fileno(epv_f)))
|| fclose(epv_f)
|| (-1 == rename(epi_c, epv_c)) )
{
fprintf(stderr, "disk: write epoc.txt failed %s\r\n", strerror(errno));
goto fail3;
}

// create binary version file, overwriting any existing file
c3_c bii_c[8193];
c3_c biv_c[8193];
snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c);
FILE* biv_f = fopen(bii_c, "w"); // XX errors
fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover?
fclose(biv_f);
snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_c);
rename(bii_c, biv_c);
c3_unlink(bii_c);
FILE* biv_f = fopen(bii_c, "w");
if ( !biv_f
|| (0 > fprintf(biv_f, URBIT_VERSION))
|| fflush(biv_f)
|| (-1 == c3_sync(fileno(biv_f)))
|| fclose(biv_f)
|| (-1 == rename(bii_c, biv_c)) )
{
fprintf(stderr, "disk: write vere.txt failed %s\r\n", strerror(errno));
goto fail3;
}

if ( -1 == c3_sync(epo_i) ) { // XX fdatasync on linux?
fprintf(stderr, "disk: sync epoch dir 0i0 failed: %s\r\n", strerror(errno));
goto fail3;
}

// get metadata from old log
c3_d who_d[2];
Expand Down

0 comments on commit 8f1bf10

Please sign in to comment.