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.

244 lines
6.3KB

  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. int ret = 0;
  96. const char *filename = h->filename;
  97. av_strstart(filename, "file:", &filename);
  98. {
  99. #if HAVE_ACCESS && defined(R_OK)
  100. if (access(filename, F_OK) < 0)
  101. return AVERROR(errno);
  102. if (mask&AVIO_FLAG_READ)
  103. if (access(filename, R_OK) >= 0)
  104. ret |= AVIO_FLAG_READ;
  105. if (mask&AVIO_FLAG_WRITE)
  106. if (access(filename, W_OK) >= 0)
  107. ret |= AVIO_FLAG_WRITE;
  108. #else
  109. struct stat st;
  110. ret = stat(filename, &st);
  111. if (ret < 0)
  112. return AVERROR(errno);
  113. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  114. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  115. #endif
  116. }
  117. return ret;
  118. }
  119. #if CONFIG_FILE_PROTOCOL
  120. static int file_open(URLContext *h, const char *filename, int flags)
  121. {
  122. FileContext *c = h->priv_data;
  123. int access;
  124. int fd;
  125. struct stat st;
  126. av_strstart(filename, "file:", &filename);
  127. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  128. access = O_CREAT | O_RDWR;
  129. if (c->trunc)
  130. access |= O_TRUNC;
  131. } else if (flags & AVIO_FLAG_WRITE) {
  132. access = O_CREAT | O_WRONLY;
  133. if (c->trunc)
  134. access |= O_TRUNC;
  135. } else {
  136. access = O_RDONLY;
  137. }
  138. #ifdef O_BINARY
  139. access |= O_BINARY;
  140. #endif
  141. fd = avpriv_open(filename, access, 0666);
  142. if (fd == -1)
  143. return AVERROR(errno);
  144. c->fd = fd;
  145. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  146. return 0;
  147. }
  148. /* XXX: use llseek */
  149. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  150. {
  151. FileContext *c = h->priv_data;
  152. int64_t ret;
  153. if (whence == AVSEEK_SIZE) {
  154. struct stat st;
  155. ret = fstat(c->fd, &st);
  156. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  157. }
  158. ret = lseek(c->fd, pos, whence);
  159. return ret < 0 ? AVERROR(errno) : ret;
  160. }
  161. static int file_close(URLContext *h)
  162. {
  163. FileContext *c = h->priv_data;
  164. return close(c->fd);
  165. }
  166. URLProtocol ff_file_protocol = {
  167. .name = "file",
  168. .url_open = file_open,
  169. .url_read = file_read,
  170. .url_write = file_write,
  171. .url_seek = file_seek,
  172. .url_close = file_close,
  173. .url_get_file_handle = file_get_handle,
  174. .url_check = file_check,
  175. .priv_data_size = sizeof(FileContext),
  176. .priv_data_class = &file_class,
  177. };
  178. #endif /* CONFIG_FILE_PROTOCOL */
  179. #if CONFIG_PIPE_PROTOCOL
  180. static int pipe_open(URLContext *h, const char *filename, int flags)
  181. {
  182. FileContext *c = h->priv_data;
  183. int fd;
  184. char *final;
  185. av_strstart(filename, "pipe:", &filename);
  186. fd = strtol(filename, &final, 10);
  187. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  188. if (flags & AVIO_FLAG_WRITE) {
  189. fd = 1;
  190. } else {
  191. fd = 0;
  192. }
  193. }
  194. #if HAVE_SETMODE
  195. setmode(fd, O_BINARY);
  196. #endif
  197. c->fd = fd;
  198. h->is_streamed = 1;
  199. return 0;
  200. }
  201. URLProtocol ff_pipe_protocol = {
  202. .name = "pipe",
  203. .url_open = pipe_open,
  204. .url_read = file_read,
  205. .url_write = file_write,
  206. .url_get_file_handle = file_get_handle,
  207. .url_check = file_check,
  208. .priv_data_size = sizeof(FileContext),
  209. .priv_data_class = &pipe_class,
  210. };
  211. #endif /* CONFIG_PIPE_PROTOCOL */