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.

202 lines
5.1KB

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