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.

239 lines
6.2KB

  1. /*
  2. * buffered file I/O
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avstring.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "avformat.h"
  25. #include <fcntl.h>
  26. #if HAVE_IO_H
  27. #include <io.h>
  28. #endif
  29. #if HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <sys/stat.h>
  33. #include <stdlib.h>
  34. #include "os_support.h"
  35. #include "url.h"
  36. /* Some systems may not have S_ISFIFO */
  37. #ifndef S_ISFIFO
  38. # ifdef S_IFIFO
  39. # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
  40. # else
  41. # define S_ISFIFO(m) 0
  42. # endif
  43. #endif
  44. /* standard file protocol */
  45. typedef struct FileContext {
  46. const AVClass *class;
  47. int fd;
  48. int trunc;
  49. int blocksize;
  50. } FileContext;
  51. static const AVOption file_options[] = {
  52. { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  53. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  54. { NULL }
  55. };
  56. static const AVOption pipe_options[] = {
  57. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  58. { NULL }
  59. };
  60. static const AVClass file_class = {
  61. .class_name = "file",
  62. .item_name = av_default_item_name,
  63. .option = file_options,
  64. .version = LIBAVUTIL_VERSION_INT,
  65. };
  66. static const AVClass pipe_class = {
  67. .class_name = "pipe",
  68. .item_name = av_default_item_name,
  69. .option = pipe_options,
  70. .version = LIBAVUTIL_VERSION_INT,
  71. };
  72. static int file_read(URLContext *h, unsigned char *buf, int size)
  73. {
  74. FileContext *c = h->priv_data;
  75. int r;
  76. size = FFMIN(size, c->blocksize);
  77. r = read(c->fd, buf, size);
  78. return (-1 == r)?AVERROR(errno):r;
  79. }
  80. static int file_write(URLContext *h, const unsigned char *buf, int size)
  81. {
  82. FileContext *c = h->priv_data;
  83. int r;
  84. size = FFMIN(size, c->blocksize);
  85. r = write(c->fd, buf, size);
  86. return (-1 == r)?AVERROR(errno):r;
  87. }
  88. static int file_get_handle(URLContext *h)
  89. {
  90. FileContext *c = h->priv_data;
  91. return c->fd;
  92. }
  93. static int file_check(URLContext *h, int mask)
  94. {
  95. #if HAVE_ACCESS && defined(R_OK)
  96. int ret = 0;
  97. if (access(h->filename, F_OK) < 0)
  98. return AVERROR(errno);
  99. if (mask&AVIO_FLAG_READ)
  100. if (access(h->filename, R_OK) >= 0)
  101. ret |= AVIO_FLAG_READ;
  102. if (mask&AVIO_FLAG_WRITE)
  103. if (access(h->filename, W_OK) >= 0)
  104. ret |= AVIO_FLAG_WRITE;
  105. #else
  106. struct stat st;
  107. int ret = stat(h->filename, &st);
  108. if (ret < 0)
  109. return AVERROR(errno);
  110. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  111. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  112. #endif
  113. return ret;
  114. }
  115. #if CONFIG_FILE_PROTOCOL
  116. static int file_open(URLContext *h, const char *filename, int flags)
  117. {
  118. FileContext *c = h->priv_data;
  119. int access;
  120. int fd;
  121. struct stat st;
  122. av_strstart(filename, "file:", &filename);
  123. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  124. access = O_CREAT | O_RDWR;
  125. if (c->trunc)
  126. access |= O_TRUNC;
  127. } else if (flags & AVIO_FLAG_WRITE) {
  128. access = O_CREAT | O_WRONLY;
  129. if (c->trunc)
  130. access |= O_TRUNC;
  131. } else {
  132. access = O_RDONLY;
  133. }
  134. #ifdef O_BINARY
  135. access |= O_BINARY;
  136. #endif
  137. fd = avpriv_open(filename, access, 0666);
  138. if (fd == -1)
  139. return AVERROR(errno);
  140. c->fd = fd;
  141. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  142. return 0;
  143. }
  144. /* XXX: use llseek */
  145. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  146. {
  147. FileContext *c = h->priv_data;
  148. int64_t ret;
  149. if (whence == AVSEEK_SIZE) {
  150. struct stat st;
  151. ret = fstat(c->fd, &st);
  152. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  153. }
  154. ret = lseek(c->fd, pos, whence);
  155. return ret < 0 ? AVERROR(errno) : ret;
  156. }
  157. static int file_close(URLContext *h)
  158. {
  159. FileContext *c = h->priv_data;
  160. return close(c->fd);
  161. }
  162. URLProtocol ff_file_protocol = {
  163. .name = "file",
  164. .url_open = file_open,
  165. .url_read = file_read,
  166. .url_write = file_write,
  167. .url_seek = file_seek,
  168. .url_close = file_close,
  169. .url_get_file_handle = file_get_handle,
  170. .url_check = file_check,
  171. .priv_data_size = sizeof(FileContext),
  172. .priv_data_class = &file_class,
  173. };
  174. #endif /* CONFIG_FILE_PROTOCOL */
  175. #if CONFIG_PIPE_PROTOCOL
  176. static int pipe_open(URLContext *h, const char *filename, int flags)
  177. {
  178. FileContext *c = h->priv_data;
  179. int fd;
  180. char *final;
  181. av_strstart(filename, "pipe:", &filename);
  182. fd = strtol(filename, &final, 10);
  183. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  184. if (flags & AVIO_FLAG_WRITE) {
  185. fd = 1;
  186. } else {
  187. fd = 0;
  188. }
  189. }
  190. #if HAVE_SETMODE
  191. setmode(fd, O_BINARY);
  192. #endif
  193. c->fd = fd;
  194. h->is_streamed = 1;
  195. return 0;
  196. }
  197. URLProtocol ff_pipe_protocol = {
  198. .name = "pipe",
  199. .url_open = pipe_open,
  200. .url_read = file_read,
  201. .url_write = file_write,
  202. .url_get_file_handle = file_get_handle,
  203. .url_check = file_check,
  204. .priv_data_size = sizeof(FileContext),
  205. .priv_data_class = &pipe_class,
  206. };
  207. #endif /* CONFIG_PIPE_PROTOCOL */