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.

137 lines
3.2KB

  1. /*
  2. * Buffered file io for ffmpeg system
  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 "avformat.h"
  23. #include <fcntl.h>
  24. #if HAVE_SETMODE
  25. #include <io.h>
  26. #endif
  27. #include <unistd.h>
  28. #include <sys/stat.h>
  29. #include <sys/time.h>
  30. #include <stdlib.h>
  31. #include "os_support.h"
  32. /* standard file protocol */
  33. static int file_open(URLContext *h, const char *filename, int flags)
  34. {
  35. int access;
  36. int fd;
  37. av_strstart(filename, "file:", &filename);
  38. if (flags & URL_RDWR) {
  39. access = O_CREAT | O_TRUNC | O_RDWR;
  40. } else if (flags & URL_WRONLY) {
  41. access = O_CREAT | O_TRUNC | O_WRONLY;
  42. } else {
  43. access = O_RDONLY;
  44. }
  45. #ifdef O_BINARY
  46. access |= O_BINARY;
  47. #endif
  48. fd = open(filename, access, 0666);
  49. if (fd == -1)
  50. return AVERROR(errno);
  51. h->priv_data = (void *) (intptr_t) fd;
  52. return 0;
  53. }
  54. static int file_read(URLContext *h, unsigned char *buf, int size)
  55. {
  56. int fd = (intptr_t) h->priv_data;
  57. return read(fd, buf, size);
  58. }
  59. static int file_write(URLContext *h, const unsigned char *buf, int size)
  60. {
  61. int fd = (intptr_t) h->priv_data;
  62. return write(fd, buf, size);
  63. }
  64. /* XXX: use llseek */
  65. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  66. {
  67. int fd = (intptr_t) h->priv_data;
  68. if (whence == AVSEEK_SIZE) {
  69. struct stat st;
  70. int ret = fstat(fd, &st);
  71. return ret < 0 ? AVERROR(errno) : st.st_size;
  72. }
  73. return lseek(fd, pos, whence);
  74. }
  75. static int file_close(URLContext *h)
  76. {
  77. int fd = (intptr_t) h->priv_data;
  78. return close(fd);
  79. }
  80. static int file_get_handle(URLContext *h)
  81. {
  82. return (intptr_t) h->priv_data;
  83. }
  84. URLProtocol file_protocol = {
  85. "file",
  86. file_open,
  87. file_read,
  88. file_write,
  89. file_seek,
  90. file_close,
  91. .url_get_file_handle = file_get_handle,
  92. };
  93. /* pipe protocol */
  94. static int pipe_open(URLContext *h, const char *filename, int flags)
  95. {
  96. int fd;
  97. char *final;
  98. av_strstart(filename, "pipe:", &filename);
  99. fd = strtol(filename, &final, 10);
  100. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  101. if (flags & URL_WRONLY) {
  102. fd = 1;
  103. } else {
  104. fd = 0;
  105. }
  106. }
  107. #if HAVE_SETMODE
  108. setmode(fd, O_BINARY);
  109. #endif
  110. h->priv_data = (void *) (intptr_t) fd;
  111. h->is_streamed = 1;
  112. return 0;
  113. }
  114. URLProtocol pipe_protocol = {
  115. "pipe",
  116. pipe_open,
  117. file_read,
  118. file_write,
  119. .url_get_file_handle = file_get_handle,
  120. };