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.

238 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/opt.h"
  23. #include "avformat.h"
  24. #include <fcntl.h>
  25. #if HAVE_IO_H
  26. #include <io.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <sys/stat.h>
  32. #include <stdlib.h>
  33. #include "os_support.h"
  34. #include "url.h"
  35. /* Some systems may not have S_ISFIFO */
  36. #ifndef S_ISFIFO
  37. # ifdef S_IFIFO
  38. # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
  39. # else
  40. # define S_ISFIFO(m) 0
  41. # endif
  42. #endif
  43. /* standard file protocol */
  44. typedef struct FileContext {
  45. const AVClass *class;
  46. int fd;
  47. int trunc;
  48. int blocksize;
  49. } FileContext;
  50. static const AVOption file_options[] = {
  51. { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  52. { "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 },
  53. { NULL }
  54. };
  55. static const AVOption pipe_options[] = {
  56. { "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 },
  57. { NULL }
  58. };
  59. static const AVClass file_class = {
  60. .class_name = "file",
  61. .item_name = av_default_item_name,
  62. .option = file_options,
  63. .version = LIBAVUTIL_VERSION_INT,
  64. };
  65. static const AVClass pipe_class = {
  66. .class_name = "pipe",
  67. .item_name = av_default_item_name,
  68. .option = pipe_options,
  69. .version = LIBAVUTIL_VERSION_INT,
  70. };
  71. static int file_read(URLContext *h, unsigned char *buf, int size)
  72. {
  73. FileContext *c = h->priv_data;
  74. int r;
  75. size = FFMIN(size, c->blocksize);
  76. r = read(c->fd, buf, size);
  77. return (-1 == r)?AVERROR(errno):r;
  78. }
  79. static int file_write(URLContext *h, const unsigned char *buf, int size)
  80. {
  81. FileContext *c = h->priv_data;
  82. int r;
  83. size = FFMIN(size, c->blocksize);
  84. r = write(c->fd, buf, size);
  85. return (-1 == r)?AVERROR(errno):r;
  86. }
  87. static int file_get_handle(URLContext *h)
  88. {
  89. FileContext *c = h->priv_data;
  90. return c->fd;
  91. }
  92. static int file_check(URLContext *h, int mask)
  93. {
  94. #if HAVE_ACCESS && defined(R_OK)
  95. int ret = 0;
  96. if (access(h->filename, F_OK) < 0)
  97. return AVERROR(errno);
  98. if (mask&AVIO_FLAG_READ)
  99. if (access(h->filename, R_OK) >= 0)
  100. ret |= AVIO_FLAG_READ;
  101. if (mask&AVIO_FLAG_WRITE)
  102. if (access(h->filename, W_OK) >= 0)
  103. ret |= AVIO_FLAG_WRITE;
  104. #else
  105. struct stat st;
  106. int ret = stat(h->filename, &st);
  107. if (ret < 0)
  108. return AVERROR(errno);
  109. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  110. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  111. #endif
  112. return ret;
  113. }
  114. #if CONFIG_FILE_PROTOCOL
  115. static int file_open(URLContext *h, const char *filename, int flags)
  116. {
  117. FileContext *c = h->priv_data;
  118. int access;
  119. int fd;
  120. struct stat st;
  121. av_strstart(filename, "file:", &filename);
  122. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  123. access = O_CREAT | O_RDWR;
  124. if (c->trunc)
  125. access |= O_TRUNC;
  126. } else if (flags & AVIO_FLAG_WRITE) {
  127. access = O_CREAT | O_WRONLY;
  128. if (c->trunc)
  129. access |= O_TRUNC;
  130. } else {
  131. access = O_RDONLY;
  132. }
  133. #ifdef O_BINARY
  134. access |= O_BINARY;
  135. #endif
  136. fd = open(filename, access, 0666);
  137. if (fd == -1)
  138. return AVERROR(errno);
  139. c->fd = fd;
  140. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  141. return 0;
  142. }
  143. /* XXX: use llseek */
  144. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  145. {
  146. FileContext *c = h->priv_data;
  147. int64_t ret;
  148. if (whence == AVSEEK_SIZE) {
  149. struct stat st;
  150. ret = fstat(c->fd, &st);
  151. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  152. }
  153. ret = lseek(c->fd, pos, whence);
  154. return ret < 0 ? AVERROR(errno) : ret;
  155. }
  156. static int file_close(URLContext *h)
  157. {
  158. FileContext *c = h->priv_data;
  159. return close(c->fd);
  160. }
  161. URLProtocol ff_file_protocol = {
  162. .name = "file",
  163. .url_open = file_open,
  164. .url_read = file_read,
  165. .url_write = file_write,
  166. .url_seek = file_seek,
  167. .url_close = file_close,
  168. .url_get_file_handle = file_get_handle,
  169. .url_check = file_check,
  170. .priv_data_size = sizeof(FileContext),
  171. .priv_data_class = &file_class,
  172. };
  173. #endif /* CONFIG_FILE_PROTOCOL */
  174. #if CONFIG_PIPE_PROTOCOL
  175. static int pipe_open(URLContext *h, const char *filename, int flags)
  176. {
  177. FileContext *c = h->priv_data;
  178. int fd;
  179. char *final;
  180. av_strstart(filename, "pipe:", &filename);
  181. fd = strtol(filename, &final, 10);
  182. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  183. if (flags & AVIO_FLAG_WRITE) {
  184. fd = 1;
  185. } else {
  186. fd = 0;
  187. }
  188. }
  189. #if HAVE_SETMODE
  190. setmode(fd, O_BINARY);
  191. #endif
  192. c->fd = fd;
  193. h->is_streamed = 1;
  194. return 0;
  195. }
  196. URLProtocol ff_pipe_protocol = {
  197. .name = "pipe",
  198. .url_open = pipe_open,
  199. .url_read = file_read,
  200. .url_write = file_write,
  201. .url_get_file_handle = file_get_handle,
  202. .url_check = file_check,
  203. .priv_data_size = sizeof(FileContext),
  204. .priv_data_class = &pipe_class,
  205. };
  206. #endif /* CONFIG_PIPE_PROTOCOL */