file.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 {
  37. const AVClass *class;
  38. int log_offset;
  39. void *log_ctx;
  40. } FileLogContext;
  41. static const AVClass file_log_ctx_class = {
  42. "FILE", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT,
  43. offsetof(FileLogContext, log_offset), offsetof(FileLogContext, log_ctx)
  44. };
  45. int av_file_map(const char *filename, uint8_t **bufptr, size_t *size,
  46. int log_offset, void *log_ctx)
  47. {
  48. FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
  49. int err, fd = avpriv_open(filename, O_RDONLY);
  50. struct stat st;
  51. av_unused void *ptr;
  52. off_t off_size;
  53. char errbuf[128];
  54. *bufptr = NULL;
  55. if (fd < 0) {
  56. err = AVERROR(errno);
  57. av_strerror(err, errbuf, sizeof(errbuf));
  58. av_log(&file_log_ctx, AV_LOG_ERROR, "Cannot read file '%s': %s\n", filename, errbuf);
  59. return err;
  60. }
  61. if (fstat(fd, &st) < 0) {
  62. err = AVERROR(errno);
  63. av_strerror(err, errbuf, sizeof(errbuf));
  64. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in fstat(): %s\n", errbuf);
  65. close(fd);
  66. return err;
  67. }
  68. off_size = st.st_size;
  69. if (off_size > SIZE_MAX) {
  70. av_log(&file_log_ctx, AV_LOG_ERROR,
  71. "File size for file '%s' is too big\n", filename);
  72. close(fd);
  73. return AVERROR(EINVAL);
  74. }
  75. *size = off_size;
  76. #if HAVE_MMAP
  77. ptr = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
  78. if (ptr == MAP_FAILED) {
  79. err = AVERROR(errno);
  80. av_strerror(err, errbuf, sizeof(errbuf));
  81. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in mmap(): %s\n", errbuf);
  82. close(fd);
  83. return err;
  84. }
  85. *bufptr = ptr;
  86. #elif HAVE_MAPVIEWOFFILE
  87. {
  88. HANDLE mh, fh = (HANDLE)_get_osfhandle(fd);
  89. mh = CreateFileMapping(fh, NULL, PAGE_READONLY, 0, 0, NULL);
  90. if (!mh) {
  91. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in CreateFileMapping()\n");
  92. close(fd);
  93. return -1;
  94. }
  95. ptr = MapViewOfFile(mh, FILE_MAP_READ, 0, 0, *size);
  96. CloseHandle(mh);
  97. if (!ptr) {
  98. av_log(&file_log_ctx, AV_LOG_ERROR, "Error occurred in MapViewOfFile()\n");
  99. close(fd);
  100. return -1;
  101. }
  102. *bufptr = ptr;
  103. }
  104. #else
  105. *bufptr = av_malloc(*size);
  106. if (!*bufptr) {
  107. av_log(&file_log_ctx, AV_LOG_ERROR, "Memory allocation error occurred\n");
  108. close(fd);
  109. return AVERROR(ENOMEM);
  110. }
  111. read(fd, *bufptr, *size);
  112. #endif
  113. close(fd);
  114. return 0;
  115. }
  116. void av_file_unmap(uint8_t *bufptr, size_t size)
  117. {
  118. #if HAVE_MMAP
  119. munmap(bufptr, size);
  120. #elif HAVE_MAPVIEWOFFILE
  121. UnmapViewOfFile(bufptr);
  122. #else
  123. av_free(bufptr);
  124. #endif
  125. }
  126. int av_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
  127. {
  128. FileLogContext file_log_ctx = { &file_log_ctx_class, log_offset, log_ctx };
  129. int fd = -1;
  130. #if !HAVE_MKSTEMP
  131. void *ptr= tempnam(NULL, prefix);
  132. if(!ptr)
  133. ptr= tempnam(".", prefix);
  134. *filename = av_strdup(ptr);
  135. #undef free
  136. free(ptr);
  137. #else
  138. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  139. *filename = av_malloc(len);
  140. #endif
  141. /* -----common section-----*/
  142. if (!*filename) {
  143. av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  144. return AVERROR(ENOMEM);
  145. }
  146. #if !HAVE_MKSTEMP
  147. # ifndef O_BINARY
  148. # define O_BINARY 0
  149. # endif
  150. # ifndef O_EXCL
  151. # define O_EXCL 0
  152. # endif
  153. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT | O_EXCL, 0600);
  154. #else
  155. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  156. fd = mkstemp(*filename);
  157. #ifdef _WIN32
  158. if (fd < 0) {
  159. snprintf(*filename, len, "./%sXXXXXX", prefix);
  160. fd = mkstemp(*filename);
  161. }
  162. #endif
  163. #endif
  164. /* -----common section-----*/
  165. if (fd < 0) {
  166. int err = AVERROR(errno);
  167. av_log(&file_log_ctx, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  168. av_freep(filename);
  169. return err;
  170. }
  171. return fd; /* success */
  172. }
  173. #ifdef TEST
  174. #undef printf
  175. int main(void)
  176. {
  177. uint8_t *buf;
  178. size_t size;
  179. if (av_file_map("file.c", &buf, &size, 0, NULL) < 0)
  180. return 1;
  181. buf[0] = 's';
  182. printf("%s", buf);
  183. av_file_unmap(buf, size);
  184. return 0;
  185. }
  186. #endif