file.c 5.1 KB

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