io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. This file is part of ethash.
  3. ethash is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethash is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with ethash. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file io.c
  15. * @author Lefteris Karapetsas <lefteris@ethdev.com>
  16. * @date 2015
  17. */
  18. #include "io.h"
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. enum ethash_io_rc ethash_io_prepare(
  23. char const* dirname,
  24. ethash_h256_t const seedhash,
  25. FILE** output_file,
  26. uint64_t file_size,
  27. bool force_create
  28. )
  29. {
  30. char mutable_name[DAG_MUTABLE_NAME_MAX_SIZE];
  31. enum ethash_io_rc ret = ETHASH_IO_FAIL;
  32. // reset errno before io calls
  33. errno = 0;
  34. // assert directory exists
  35. if (!ethash_mkdir(dirname)) {
  36. ETHASH_CRITICAL("Could not create the ethash directory");
  37. goto end;
  38. }
  39. ethash_io_mutable_name(ETHASH_REVISION, &seedhash, mutable_name);
  40. char* tmpfile = ethash_io_create_filename(dirname, mutable_name, strlen(mutable_name));
  41. if (!tmpfile) {
  42. ETHASH_CRITICAL("Could not create the full DAG pathname");
  43. goto end;
  44. }
  45. FILE *f;
  46. if (!force_create) {
  47. // try to open the file
  48. f = ethash_fopen(tmpfile, "rb+");
  49. if (f) {
  50. size_t found_size;
  51. if (!ethash_file_size(f, &found_size)) {
  52. fclose(f);
  53. ETHASH_CRITICAL("Could not query size of DAG file: \"%s\"", tmpfile);
  54. goto free_memo;
  55. }
  56. if (file_size != found_size - ETHASH_DAG_MAGIC_NUM_SIZE) {
  57. fclose(f);
  58. ret = ETHASH_IO_MEMO_SIZE_MISMATCH;
  59. goto free_memo;
  60. }
  61. // compare the magic number, no need to care about endianess since it's local
  62. uint64_t magic_num;
  63. if (fread(&magic_num, ETHASH_DAG_MAGIC_NUM_SIZE, 1, f) != 1) {
  64. // I/O error
  65. fclose(f);
  66. ETHASH_CRITICAL("Could not read from DAG file: \"%s\"", tmpfile);
  67. ret = ETHASH_IO_MEMO_SIZE_MISMATCH;
  68. goto free_memo;
  69. }
  70. if (magic_num != ETHASH_DAG_MAGIC_NUM) {
  71. fclose(f);
  72. ret = ETHASH_IO_MEMO_SIZE_MISMATCH;
  73. goto free_memo;
  74. }
  75. ret = ETHASH_IO_MEMO_MATCH;
  76. goto set_file;
  77. }
  78. }
  79. // file does not exist, will need to be created
  80. f = ethash_fopen(tmpfile, "wb+");
  81. if (!f) {
  82. ETHASH_CRITICAL("Could not create DAG file: \"%s\"", tmpfile);
  83. goto free_memo;
  84. }
  85. // make sure it's of the proper size
  86. if (fseek(f, (long int)(file_size + ETHASH_DAG_MAGIC_NUM_SIZE - 1), SEEK_SET) != 0) {
  87. fclose(f);
  88. ETHASH_CRITICAL("Could not seek to the end of DAG file: \"%s\". Insufficient space?", tmpfile);
  89. goto free_memo;
  90. }
  91. if (fputc('\n', f) == EOF) {
  92. fclose(f);
  93. ETHASH_CRITICAL("Could not write in the end of DAG file: \"%s\". Insufficient space?", tmpfile);
  94. goto free_memo;
  95. }
  96. if (fflush(f) != 0) {
  97. fclose(f);
  98. ETHASH_CRITICAL("Could not flush at end of DAG file: \"%s\". Insufficient space?", tmpfile);
  99. goto free_memo;
  100. }
  101. ret = ETHASH_IO_MEMO_MISMATCH;
  102. goto set_file;
  103. ret = ETHASH_IO_MEMO_MATCH;
  104. set_file:
  105. *output_file = f;
  106. free_memo:
  107. free(tmpfile);
  108. end:
  109. return ret;
  110. }