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.

195 lines
4.7KB

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