diff --git a/io.h b/io.h index 609c2769e..0559377b1 100644 --- a/io.h +++ b/io.h @@ -136,6 +136,21 @@ bool ethash_file_size(FILE* f, size_t* ret_size); */ int ethash_fileno(FILE* f); +/** + * Create the filename for the DAG. + * + * @param dirname The directory name in which the DAG file should reside + * If it does not end with a directory separator it is appended. + * @param filename The actual name of the file + * @param filename_length The length of the filename in bytes + * @return A char* containing the full name. User must deallocate. + */ +char* ethash_io_create_filename( + char const* dirname, + char const* filename, + size_t filename_length +); + static inline bool ethash_io_mutable_name( uint32_t revision, ethash_h256_t const* seed_hash, @@ -149,26 +164,6 @@ static inline bool ethash_io_mutable_name( return snprintf(output, DAG_MUTABLE_NAME_MAX_SIZE, "%u_%016" PRIx64, revision, hash) >= 0; } -static inline char* ethash_io_create_filename( - char const* dirname, - char const* filename, - size_t filename_length -) -{ - size_t dirlen = strlen(dirname); - // in C the cast is not needed, but a C++ compiler will complain for invalid conversion - char* name = (char*)malloc(dirlen + filename_length + 1); - if (!name) { - return NULL; - } - - name[0] = '\0'; - ethash_strncat(name, dirlen + filename_length + 1, dirname, dirlen); - ethash_strncat(name, dirlen + filename_length + 1, filename, filename_length); - return name; -} - - #ifdef __cplusplus } #endif diff --git a/io_posix.c b/io_posix.c index 5783ec272..c16521d5a 100644 --- a/io_posix.c +++ b/io_posix.c @@ -48,6 +48,31 @@ int ethash_fileno(FILE *f) return fileno(f); } +char* ethash_io_create_filename( + char const* dirname, + char const* filename, + size_t filename_length +) +{ + size_t dirlen = strlen(dirname); + size_t dest_size = dirlen + filename_length + 1; + if (dirname[dirlen] != '/') { + dest_size += 1; + } + char* name = malloc(dest_size); + if (!name) { + return NULL; + } + + name[0] = '\0'; + ethash_strncat(name, dest_size, dirname, dirlen); + if (dirname[dirlen] != '/') { + ethash_strncat(name, dest_size, "/", 1); + } + ethash_strncat(name, dest_size, filename, filename_length); + return name; +} + bool ethash_file_size(FILE* f, size_t* ret_size) { struct stat st; diff --git a/io_win32.c b/io_win32.c index 505f11e2b..d64362914 100644 --- a/io_win32.c +++ b/io_win32.c @@ -48,6 +48,31 @@ int ethash_fileno(FILE* f) return _fileno(f); } +char* ethash_io_create_filename( + char const* dirname, + char const* filename, + size_t filename_length +) +{ + size_t dirlen = strlen(dirname); + size_t dest_size = dirlen + filename_length + 1; + if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') { + dest_size += 1; + } + char* name = malloc(dest_size); + if (!name) { + return NULL; + } + + name[0] = '\0'; + ethash_strncat(name, dest_size, dirname, dirlen); + if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') { + ethash_strncat(name, dest_size, "\\", 1); + } + ethash_strncat(name, dest_size, filename, filename_length); + return name; +} + bool ethash_file_size(FILE* f, size_t* ret_size) { struct _stat st;