file.c 5.2 KB

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