bh_read_file.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "bh_read_file.h"
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #if defined(_WIN32) || defined(_WIN32_)
  5. #include <io.h>
  6. #else
  7. #include <unistd.h>
  8. #endif
  9. #if defined(_WIN32) || defined(_WIN32_)
  10. char *
  11. bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
  12. {
  13. char *buffer;
  14. int file;
  15. uint32 file_size, buf_size, read_size;
  16. struct stat stat_buf;
  17. if (!filename || !ret_size) {
  18. printf("Read file to buffer failed: invalid filename or ret size.\n");
  19. return NULL;
  20. }
  21. if (_sopen_s(&file, filename, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0)) {
  22. printf("Read file to buffer failed: open file %s failed.\n", filename);
  23. return NULL;
  24. }
  25. if (fstat(file, &stat_buf) != 0) {
  26. printf("Read file to buffer failed: fstat file %s failed.\n", filename);
  27. _close(file);
  28. return NULL;
  29. }
  30. file_size = (uint32)stat_buf.st_size;
  31. /* At lease alloc 1 byte to avoid malloc failed */
  32. buf_size = file_size > 0 ? file_size : 1;
  33. if (!(buffer = (char *)BH_MALLOC(buf_size))) {
  34. printf("Read file to buffer failed: alloc memory failed.\n");
  35. _close(file);
  36. return NULL;
  37. }
  38. #if WASM_ENABLE_MEMORY_TRACING != 0
  39. printf("Read file, total size: %u\n", file_size);
  40. #endif
  41. read_size = _read(file, buffer, file_size);
  42. _close(file);
  43. if (read_size < file_size) {
  44. printf("Read file to buffer failed: read file content failed.\n");
  45. BH_FREE(buffer);
  46. return NULL;
  47. }
  48. *ret_size = file_size;
  49. return buffer;
  50. }
  51. #else /* else of defined(_WIN32) || defined(_WIN32_) */
  52. char *
  53. bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
  54. {
  55. char *buffer;
  56. int file;
  57. uint32 file_size, buf_size, read_size;
  58. struct stat stat_buf;
  59. if (!filename || !ret_size) {
  60. printf("Read file to buffer failed: invalid filename or ret size.\n");
  61. return NULL;
  62. }
  63. if ((file = open(filename, O_RDONLY, 0)) == -1) {
  64. printf("Read file to buffer failed: open file %s failed.\n", filename);
  65. return NULL;
  66. }
  67. if (fstat(file, &stat_buf) != 0) {
  68. printf("Read file to buffer failed: fstat file %s failed.\n", filename);
  69. close(file);
  70. return NULL;
  71. }
  72. file_size = (uint32)stat_buf.st_size;
  73. /* At lease alloc 1 byte to avoid malloc failed */
  74. buf_size = file_size > 0 ? file_size : 1;
  75. if (!(buffer = BH_MALLOC(buf_size))) {
  76. printf("Read file to buffer failed: alloc memory failed.\n");
  77. close(file);
  78. return NULL;
  79. }
  80. #if WASM_ENABLE_MEMORY_TRACING != 0
  81. printf("Read file, total size: %u\n", file_size);
  82. #endif
  83. read_size = (uint32)read(file, buffer, file_size);
  84. close(file);
  85. if (read_size < file_size) {
  86. printf("Read file to buffer failed: read file content failed.\n");
  87. BH_FREE(buffer);
  88. return NULL;
  89. }
  90. *ret_size = file_size;
  91. return buffer;
  92. }
  93. #endif /* end of defined(_WIN32) || defined(_WIN32_) */