file.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 HAVE_MMAP
  81. ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  82. if (ptr == MAP_FAILED) {
  83. err = AVERROR(errno);
  84. av_strerror(err, errbuf, sizeof(errbuf));
  85. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
  86. close(fd);
  87. return err;
  88. }
  89. *bufptr = ptr;
  90. #elif HAVE_MAPVIEWOFFILE
  91. {
  92. HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
  93. mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
  94. if (!mh) {
  95. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
  96. close(fd);
  97. return -1;
  98. }
  99. ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
  100. CloseHandle(mh);
  101. if (!ptr) {
  102. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
  103. close(fd);
  104. return -1;
  105. }
  106. *bufptr = ptr;
  107. }
  108. #else
  109. *bufptr = av_malloc(*size);
  110. if (!*bufptr) {
  111. av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
  112. close(fd);
  113. return AVERROR(ENOMEM);
  114. }
  115. read(fd, *bufptr, *size);
  116. #endif
  117. close(fd);
  118. return 0;
  119. }
  120. void av_file_unmap(uint8_t *bufptr, size_t size)
  121. {
  122. #if HAVE_MMAP
  123. munmap(bufptr, size);
  124. #elif HAVE_MAPVIEWOFFILE
  125. UnmapViewOfFile(bufptr);
  126. #else
  127. av_free(bufptr);
  128. #endif
  129. }
  130. int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx) {
  131. return avpriv_tempfile(prefix, filename, log_offset, log_ctx);
  132. }