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.

219 lines
5.4KB

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