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.

209 lines
5.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. } FileContext;
  49. static const AVOption file_options[] = {
  50. { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  51. { NULL }
  52. };
  53. static const AVClass file_class = {
  54. .class_name = "file",
  55. .item_name = av_default_item_name,
  56. .option = file_options,
  57. .version = LIBAVUTIL_VERSION_INT,
  58. };
  59. static int file_read(URLContext *h, unsigned char *buf, int size)
  60. {
  61. FileContext *c = h->priv_data;
  62. int r = read(c->fd, buf, size);
  63. return (-1 == r)?AVERROR(errno):r;
  64. }
  65. static int file_write(URLContext *h, const unsigned char *buf, int size)
  66. {
  67. FileContext *c = h->priv_data;
  68. int r = write(c->fd, buf, size);
  69. return (-1 == r)?AVERROR(errno):r;
  70. }
  71. static int file_get_handle(URLContext *h)
  72. {
  73. FileContext *c = h->priv_data;
  74. return c->fd;
  75. }
  76. static int file_check(URLContext *h, int mask)
  77. {
  78. int ret = 0;
  79. if (access(h->filename, F_OK) < 0)
  80. return AVERROR(errno);
  81. if (mask&AVIO_FLAG_READ)
  82. if (access(h->filename, R_OK) >= 0)
  83. ret |= AVIO_FLAG_READ;
  84. if (mask&AVIO_FLAG_WRITE)
  85. if (access(h->filename, W_OK) >= 0)
  86. ret |= AVIO_FLAG_WRITE;
  87. return ret;
  88. }
  89. #if CONFIG_FILE_PROTOCOL
  90. static int file_open(URLContext *h, const char *filename, int flags)
  91. {
  92. FileContext *c = h->priv_data;
  93. int access;
  94. int fd;
  95. struct stat st;
  96. av_strstart(filename, "file:", &filename);
  97. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  98. access = O_CREAT | O_RDWR;
  99. if (c->trunc)
  100. access |= O_TRUNC;
  101. } else if (flags & AVIO_FLAG_WRITE) {
  102. access = O_CREAT | O_WRONLY;
  103. if (c->trunc)
  104. access |= O_TRUNC;
  105. } else {
  106. access = O_RDONLY;
  107. }
  108. #ifdef O_BINARY
  109. access |= O_BINARY;
  110. #endif
  111. fd = open(filename, access, 0666);
  112. if (fd == -1)
  113. return AVERROR(errno);
  114. c->fd = fd;
  115. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  116. return 0;
  117. }
  118. /* XXX: use llseek */
  119. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  120. {
  121. FileContext *c = h->priv_data;
  122. int64_t ret;
  123. if (whence == AVSEEK_SIZE) {
  124. struct stat st;
  125. ret = fstat(c->fd, &st);
  126. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  127. }
  128. ret = lseek(c->fd, pos, whence);
  129. return ret < 0 ? AVERROR(errno) : ret;
  130. }
  131. static int file_close(URLContext *h)
  132. {
  133. FileContext *c = h->priv_data;
  134. return close(c->fd);
  135. }
  136. URLProtocol ff_file_protocol = {
  137. .name = "file",
  138. .url_open = file_open,
  139. .url_read = file_read,
  140. .url_write = file_write,
  141. .url_seek = file_seek,
  142. .url_close = file_close,
  143. .url_get_file_handle = file_get_handle,
  144. .url_check = file_check,
  145. .priv_data_size = sizeof(FileContext),
  146. .priv_data_class = &file_class,
  147. };
  148. #endif /* CONFIG_FILE_PROTOCOL */
  149. #if CONFIG_PIPE_PROTOCOL
  150. static int pipe_open(URLContext *h, const char *filename, int flags)
  151. {
  152. FileContext *c = h->priv_data;
  153. int fd;
  154. char *final;
  155. av_strstart(filename, "pipe:", &filename);
  156. fd = strtol(filename, &final, 10);
  157. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  158. if (flags & AVIO_FLAG_WRITE) {
  159. fd = 1;
  160. } else {
  161. fd = 0;
  162. }
  163. }
  164. #if HAVE_SETMODE
  165. setmode(fd, O_BINARY);
  166. #endif
  167. c->fd = fd;
  168. h->is_streamed = 1;
  169. return 0;
  170. }
  171. URLProtocol ff_pipe_protocol = {
  172. .name = "pipe",
  173. .url_open = pipe_open,
  174. .url_read = file_read,
  175. .url_write = file_write,
  176. .url_get_file_handle = file_get_handle,
  177. .url_check = file_check,
  178. .priv_data_size = sizeof(FileContext),
  179. };
  180. #endif /* CONFIG_PIPE_PROTOCOL */