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.

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