You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
5.7KB

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