Browse Source

Force DAG file creation if file already existed but with wrong size

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
94eee46b2a
  1. 49
      internal.c
  2. 62
      io.c
  3. 5
      io.h

49
internal.c

@ -373,6 +373,24 @@ ethash_cache_t *ethash_light_acquire_cache(ethash_light_t light)
return ret;
}
static bool ethash_mmap(struct ethash_full* ret, FILE* f)
{
int fd;
ret->file = f;
if ((fd = ethash_fileno(ret->file)) == -1) {
return false;
}
ret->data = mmap(
NULL,
(size_t)ret->file_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0
);
return ret->data != MAP_FAILED;
}
ethash_full_t ethash_full_new(
char const* dirname,
ethash_h256_t const* seed_hash,
@ -382,39 +400,30 @@ ethash_full_t ethash_full_new(
)
{
struct ethash_full* ret;
int fd;
FILE *f = NULL;
bool match = false;
ret = calloc(sizeof(*ret), 1);
if (!ret) {
return NULL;
}
ret->file_size = (size_t)full_size;
switch (ethash_io_prepare(dirname, *seed_hash, &f, (size_t)full_size)) {
switch (ethash_io_prepare(dirname, *seed_hash, &f, (size_t)full_size, false)) {
case ETHASH_IO_FAIL:
case ETHASH_IO_MEMO_SIZE_MISMATCH:
goto fail_free_full;
case ETHASH_IO_MEMO_MATCH:
match = true;
case ETHASH_IO_MEMO_MISMATCH:
ret->file = f;
if ((fd = ethash_fileno(ret->file)) == -1) {
if (!ethash_mmap(ret, f)) {
goto fail_close_file;
}
return ret;
case ETHASH_IO_MEMO_SIZE_MISMATCH:
// if a DAG of same filename but unexpected size is found, silently force new file creation
if (ethash_io_prepare(dirname, *seed_hash, &f, (size_t)full_size, true) != ETHASH_IO_MEMO_MISMATCH) {
goto fail_free_full;
}
ret->data = mmap(
NULL,
(size_t)full_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0
);
if (ret->data == MAP_FAILED) {
// fallthrough to the mismatch case here, DO NOT go through match
case ETHASH_IO_MEMO_MISMATCH:
if (!ethash_mmap(ret, f)) {
goto fail_close_file;
}
if (match) {
return ret;
}
break;
}

62
io.c

@ -26,7 +26,8 @@ enum ethash_io_rc ethash_io_prepare(
char const* dirname,
ethash_h256_t const seedhash,
FILE** output_file,
size_t file_size
size_t file_size,
bool force_create
)
{
char mutable_name[DAG_MUTABLE_NAME_MAX_SIZE];
@ -43,35 +44,40 @@ enum ethash_io_rc ethash_io_prepare(
goto end;
}
// try to open the file
FILE* f = ethash_fopen(tmpfile, "rb+");
if (f) {
size_t found_size;
if (!ethash_file_size(f, &found_size)) {
fclose(f);
goto free_memo;
FILE *f;
if (!force_create) {
// try to open the file
f = ethash_fopen(tmpfile, "rb+");
if (f) {
size_t found_size;
if (!ethash_file_size(f, &found_size)) {
fclose(f);
goto free_memo;
}
if (file_size != found_size) {
fclose(f);
ret = ETHASH_IO_MEMO_SIZE_MISMATCH;
goto free_memo;
}
ret = ETHASH_IO_MEMO_MATCH;
goto set_file;
}
if (file_size != found_size) {
fclose(f);
ret = ETHASH_IO_MEMO_SIZE_MISMATCH;
goto free_memo;
}
} else {
// file does not exist, will need to be created
f = ethash_fopen(tmpfile, "wb+");
if (!f) {
goto free_memo;
}
// make sure it's of the proper size
if (fseek(f, file_size - 1, SEEK_SET) != 0) {
fclose(f);
goto free_memo;
}
fputc('\n', f);
fflush(f);
ret = ETHASH_IO_MEMO_MISMATCH;
goto set_file;
}
// file does not exist, will need to be created
f = ethash_fopen(tmpfile, "wb+");
if (!f) {
goto free_memo;
}
// make sure it's of the proper size
if (fseek(f, file_size - 1, SEEK_SET) != 0) {
fclose(f);
goto free_memo;
}
fputc('\n', f);
fflush(f);
ret = ETHASH_IO_MEMO_MISMATCH;
goto set_file;
ret = ETHASH_IO_MEMO_MATCH;
set_file:

5
io.h

@ -69,13 +69,16 @@ enum ethash_io_rc {
* mode, while on the case of mismatch a new file is created
* on write mode
* @param[in] file_size The size that the DAG file should have on disk
* @param[out] force_create If true then there is no check to see if the file
* already exists
* @return For possible return values @see enum ethash_io_rc
*/
enum ethash_io_rc ethash_io_prepare(
char const* dirname,
ethash_h256_t const seedhash,
FILE** output_file,
size_t file_size
size_t file_size,
bool force_create
);
/**

Loading…
Cancel
Save