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.

282 lines
7.2KB

  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/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. /* Some systems may not have S_ISFIFO */
  37. #ifndef S_ISFIFO
  38. # ifdef S_IFIFO
  39. # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
  40. # else
  41. # define S_ISFIFO(m) 0
  42. # endif
  43. #endif
  44. /* standard file protocol */
  45. typedef struct FileContext {
  46. const AVClass *class;
  47. int fd;
  48. int trunc;
  49. int blocksize;
  50. } FileContext;
  51. static const AVOption file_options[] = {
  52. { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
  53. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  54. { NULL }
  55. };
  56. static const AVOption pipe_options[] = {
  57. { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  58. { NULL }
  59. };
  60. static const AVClass file_class = {
  61. .class_name = "file",
  62. .item_name = av_default_item_name,
  63. .option = file_options,
  64. .version = LIBAVUTIL_VERSION_INT,
  65. };
  66. static const AVClass pipe_class = {
  67. .class_name = "pipe",
  68. .item_name = av_default_item_name,
  69. .option = pipe_options,
  70. .version = LIBAVUTIL_VERSION_INT,
  71. };
  72. static int file_read(URLContext *h, unsigned char *buf, int size)
  73. {
  74. FileContext *c = h->priv_data;
  75. int r;
  76. size = FFMIN(size, c->blocksize);
  77. r = read(c->fd, buf, size);
  78. return (-1 == r)?AVERROR(errno):r;
  79. }
  80. static int file_write(URLContext *h, const unsigned char *buf, int size)
  81. {
  82. FileContext *c = h->priv_data;
  83. int r;
  84. size = FFMIN(size, c->blocksize);
  85. r = write(c->fd, buf, size);
  86. return (-1 == r)?AVERROR(errno):r;
  87. }
  88. static int file_get_handle(URLContext *h)
  89. {
  90. FileContext *c = h->priv_data;
  91. return c->fd;
  92. }
  93. static int file_check(URLContext *h, int mask)
  94. {
  95. int ret = 0;
  96. const char *filename = h->filename;
  97. av_strstart(filename, "file:", &filename);
  98. {
  99. #if HAVE_ACCESS && defined(R_OK)
  100. if (access(filename, F_OK) < 0)
  101. return AVERROR(errno);
  102. if (mask&AVIO_FLAG_READ)
  103. if (access(filename, R_OK) >= 0)
  104. ret |= AVIO_FLAG_READ;
  105. if (mask&AVIO_FLAG_WRITE)
  106. if (access(filename, W_OK) >= 0)
  107. ret |= AVIO_FLAG_WRITE;
  108. #else
  109. struct stat st;
  110. ret = stat(filename, &st);
  111. if (ret < 0)
  112. return AVERROR(errno);
  113. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  114. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  115. #endif
  116. }
  117. return ret;
  118. }
  119. static int file_delete(URLContext *h)
  120. {
  121. #if HAVE_UNISTD_H
  122. int ret;
  123. const char *filename = h->filename;
  124. av_strstart(filename, "file:", &filename);
  125. ret = rmdir(filename);
  126. if (ret < 0 && errno == ENOTDIR)
  127. ret = unlink(filename);
  128. if (ret < 0)
  129. return AVERROR(errno);
  130. return ret;
  131. #else
  132. return AVERROR(ENOSYS);
  133. #endif /* HAVE_UNISTD_H */
  134. }
  135. static int file_move(URLContext *h_src, URLContext *h_dst)
  136. {
  137. #if HAVE_UNISTD_H
  138. const char *filename_src = h_src->filename;
  139. const char *filename_dst = h_dst->filename;
  140. av_strstart(filename_src, "file:", &filename_src);
  141. av_strstart(filename_dst, "file:", &filename_src);
  142. if (rename(filename_src, filename_dst) < 0)
  143. return AVERROR(errno);
  144. return 0;
  145. #else
  146. return AVERROR(ENOSYS);
  147. #endif /* HAVE_UNISTD_H */
  148. }
  149. #if CONFIG_FILE_PROTOCOL
  150. static int file_open(URLContext *h, const char *filename, int flags)
  151. {
  152. FileContext *c = h->priv_data;
  153. int access;
  154. int fd;
  155. struct stat st;
  156. av_strstart(filename, "file:", &filename);
  157. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  158. access = O_CREAT | O_RDWR;
  159. if (c->trunc)
  160. access |= O_TRUNC;
  161. } else if (flags & AVIO_FLAG_WRITE) {
  162. access = O_CREAT | O_WRONLY;
  163. if (c->trunc)
  164. access |= O_TRUNC;
  165. } else {
  166. access = O_RDONLY;
  167. }
  168. #ifdef O_BINARY
  169. access |= O_BINARY;
  170. #endif
  171. fd = avpriv_open(filename, access, 0666);
  172. if (fd == -1)
  173. return AVERROR(errno);
  174. c->fd = fd;
  175. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  176. return 0;
  177. }
  178. /* XXX: use llseek */
  179. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  180. {
  181. FileContext *c = h->priv_data;
  182. int64_t ret;
  183. if (whence == AVSEEK_SIZE) {
  184. struct stat st;
  185. ret = fstat(c->fd, &st);
  186. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  187. }
  188. ret = lseek(c->fd, pos, whence);
  189. return ret < 0 ? AVERROR(errno) : ret;
  190. }
  191. static int file_close(URLContext *h)
  192. {
  193. FileContext *c = h->priv_data;
  194. return close(c->fd);
  195. }
  196. URLProtocol ff_file_protocol = {
  197. .name = "file",
  198. .url_open = file_open,
  199. .url_read = file_read,
  200. .url_write = file_write,
  201. .url_seek = file_seek,
  202. .url_close = file_close,
  203. .url_get_file_handle = file_get_handle,
  204. .url_check = file_check,
  205. .url_delete = file_delete,
  206. .url_move = file_move,
  207. .priv_data_size = sizeof(FileContext),
  208. .priv_data_class = &file_class,
  209. };
  210. #endif /* CONFIG_FILE_PROTOCOL */
  211. #if CONFIG_PIPE_PROTOCOL
  212. static int pipe_open(URLContext *h, const char *filename, int flags)
  213. {
  214. FileContext *c = h->priv_data;
  215. int fd;
  216. char *final;
  217. av_strstart(filename, "pipe:", &filename);
  218. fd = strtol(filename, &final, 10);
  219. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  220. if (flags & AVIO_FLAG_WRITE) {
  221. fd = 1;
  222. } else {
  223. fd = 0;
  224. }
  225. }
  226. #if HAVE_SETMODE
  227. setmode(fd, O_BINARY);
  228. #endif
  229. c->fd = fd;
  230. h->is_streamed = 1;
  231. return 0;
  232. }
  233. URLProtocol ff_pipe_protocol = {
  234. .name = "pipe",
  235. .url_open = pipe_open,
  236. .url_read = file_read,
  237. .url_write = file_write,
  238. .url_get_file_handle = file_get_handle,
  239. .url_check = file_check,
  240. .priv_data_size = sizeof(FileContext),
  241. .priv_data_class = &pipe_class,
  242. };
  243. #endif /* CONFIG_PIPE_PROTOCOL */