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.

198 lines
4.9KB

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