file.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "file.h"
  20. #include "internal.h"
  21. #include "log.h"
  22. #include "mem.h"
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #if HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #if HAVE_IO_H
  29. #include <io.h>
  30. #endif
  31. #if HAVE_MMAP
  32. #include <sys/mman.h>
  33. #elif HAVE_MAPVIEWOFFILE
  34. #include <windows.h>
  35. #endif
  36. typedef struct FileLogContext {
  37. const AVClass *class;
  38. int log_offset;
  39. void *log_ctx;
  40. } FileLogContext;
  41. static const AVClass file_log_ctx_class = {
  42. .class_name = "FILE",
  43. .item_name = av_default_item_name,
  44. .option = NULL,
  45. .version = LIBAVUTIL_VERSION_INT,
  46. .log_level_offset_offset = offsetof(FileLogContext, log_offset),
  47. .parent_log_context_offset = offsetof(FileLogContext, log_ctx),
  48. };
  49. int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
  50. int log_offset, void *log_ctx)
  51. {
  52. FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
  53. int err, fd = avpriv_open(filename, O_RDONLY);
  54. struct stat st;
  55. av_unused void *ptr;
  56. off_t off_size;
  57. char errbuf[128];
  58. *bufptr = NULL;
  59. if (fd < 0) {
  60. err = AVERROR(errno);
  61. av_strerror(err, errbuf, sizeof(errbuf));
  62. av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
  63. return err;
  64. }
  65. if (fstat(fd, &st) < 0) {
  66. err = AVERROR(errno);
  67. av_strerror(err, errbuf, sizeof(errbuf));
  68. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
  69. close(fd);
  70. return err;
  71. }
  72. off_size = st.st_size;
  73. if (off_size > SIZE_MAX) {
  74. av_log(&file_log_ctx, AV_LOG_ERROR,
  75. "File size for file '%s' is too big\n", filename);
  76. close(fd);
  77. return AVERROR(EINVAL);
  78. }
  79. *size = off_size;
  80. if (!*size) {
  81. *bufptr = NULL;
  82. goto out;
  83. }
  84. #if HAVE_MMAP
  85. ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  86. if (ptr == MAP_FAILED) {
  87. err = AVERROR(errno);
  88. av_strerror(err, errbuf, sizeof(errbuf));
  89. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
  90. close(fd);
  91. return err;
  92. }
  93. *bufptr = ptr;
  94. #elif HAVE_MAPVIEWOFFILE
  95. {
  96. HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
  97. mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
  98. if (!mh) {
  99. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
  100. close(fd);
  101. return -1;
  102. }
  103. ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
  104. CloseHandle(mh);
  105. if (!ptr) {
  106. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
  107. close(fd);
  108. return -1;
  109. }
  110. *bufptr = ptr;
  111. }
  112. #else
  113. *bufptr = av_malloc(*size);
  114. if (!*bufptr) {
  115. av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
  116. close(fd);
  117. return AVERROR(ENOMEM);
  118. }
  119. read(fd, *bufptr, *size);
  120. #endif
  121. out:
  122. close(fd);
  123. return 0;
  124. }
  125. void av_file_unmap(uint8_t *bufptr, size_t size)
  126. {
  127. if (!size)
  128. return;
  129. #if HAVE_MMAP
  130. munmap(bufptr, size);
  131. #elif HAVE_MAPVIEWOFFILE
  132. UnmapViewOfFile(bufptr);
  133. #else
  134. av_free(bufptr);
  135. #endif
  136. }
  137. int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
  138. return avpriv_tempfile(prefix, filename, log_offset, log_ctx);
  139. }