file.c 5.3 KB

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