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.

189 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. if (whence == AVSEEK_SIZE) {
  108. struct stat st;
  109. int ret = fstat(c->fd, &st);
  110. return ret < 0 ? AVERROR(errno) : st.st_size;
  111. }
  112. return lseek(c->fd, pos, whence);
  113. }
  114. static int file_close(URLContext *h)
  115. {
  116. FileContext *c = h->priv_data;
  117. return close(c->fd);
  118. }
  119. URLProtocol ff_file_protocol = {
  120. .name = "file",
  121. .url_open = file_open,
  122. .url_read = file_read,
  123. .url_write = file_write,
  124. .url_seek = file_seek,
  125. .url_close = file_close,
  126. .url_get_file_handle = file_get_handle,
  127. .url_check = file_check,
  128. .priv_data_size = sizeof(FileContext),
  129. .priv_data_class = &file_class,
  130. };
  131. #endif /* CONFIG_FILE_PROTOCOL */
  132. #if CONFIG_PIPE_PROTOCOL
  133. static int pipe_open(URLContext *h, const char *filename, int flags)
  134. {
  135. FileContext *c = h->priv_data;
  136. int fd;
  137. char *final;
  138. av_strstart(filename, "pipe:", &filename);
  139. fd = strtol(filename, &final, 10);
  140. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  141. if (flags & AVIO_FLAG_WRITE) {
  142. fd = 1;
  143. } else {
  144. fd = 0;
  145. }
  146. }
  147. #if HAVE_SETMODE
  148. setmode(fd, O_BINARY);
  149. #endif
  150. c->fd = fd;
  151. h->is_streamed = 1;
  152. return 0;
  153. }
  154. URLProtocol ff_pipe_protocol = {
  155. .name = "pipe",
  156. .url_open = pipe_open,
  157. .url_read = file_read,
  158. .url_write = file_write,
  159. .url_get_file_handle = file_get_handle,
  160. .url_check = file_check,
  161. .priv_data_size = sizeof(FileContext),
  162. };
  163. #endif /* CONFIG_PIPE_PROTOCOL */