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.

207 lines
5.2KB

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