Skip to content

Commit

Permalink
disk: update epoc.txt and vere.txt atomically (#669)
Browse files Browse the repository at this point in the history
Fixes #640
  • Loading branch information
pkova authored Jul 24, 2024
2 parents 692466f + 8f1bf10 commit e388128
Showing 1 changed file with 64 additions and 17 deletions.
81 changes: 64 additions & 17 deletions pkg/vere/disk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,18 +1123,39 @@ u3_disk_epoc_zero(u3_disk* log_u)
}

// create epoch version file, overwriting any existing file
c3_c epi_c[8193];
c3_c epv_c[8193];
snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c);
snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c);
FILE* epv_f = fopen(epv_c, "w"); // XX errors
fprintf(epv_f, "%d", U3E_VERLAT);
fclose(epv_f);
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(biv_c, sizeof(biv_c), "%s/vere.txt", epo_c);
FILE* biv_f = fopen(biv_c, "w"); // XX errors
fprintf(biv_f, URBIT_VERSION); // XX append a sentinel for auto-rollover?
fclose(biv_f);
snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c);
snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_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 @@ -1205,18 +1226,44 @@ _disk_epoc_roll(u3_disk* log_u, c3_d epo_d)
}

// create epoch version file, overwriting any existing file
c3_c epi_c[8193];
c3_c epv_c[8193];
snprintf(epi_c, sizeof(epv_c), "%s/epoc.tmp", epo_c);
snprintf(epv_c, sizeof(epv_c), "%s/epoc.txt", epo_c);
FILE* epv_f = fopen(epv_c, "w"); // XX errors
fprintf(epv_f, "%d", U3E_VERLAT);
fclose(epv_f);
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(biv_c, sizeof(biv_c), "%s/vere.txt", epo_c);
FILE* biv_f = fopen(biv_c, "w"); // XX errors
fprintf(biv_f, URBIT_VERSION);
fclose(biv_f);
snprintf(bii_c, sizeof(biv_c), "%s/vere.tmp", epo_c);
snprintf(biv_c, sizeof(epv_c), "%s/vere.txt", epo_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 Expand Up @@ -1707,9 +1754,9 @@ _disk_epoc_load(u3_disk* log_u, c3_d lat_d)
return _epoc_gone;
}

if ( (1 != sscanf(ver_c, "%" SCNu32 "%n", &ver_w, &car_i))
&& (0 < car_i)
&& ('\0' == *(ver_c + car_i)) )
if ( !( (1 == sscanf(ver_c, "%" SCNu32 "%n", &ver_w, &car_i))
&& (0 < car_i)
&& ('\0' == *(ver_c + car_i)) ) )
{
fprintf(stderr, "disk: failed to parse epoch version: '%s'\r\n", ver_c);
return _epoc_fail;
Expand Down

0 comments on commit e388128

Please sign in to comment.