file.c 5.2 KB

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