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.

196 lines
4.8KB

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