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.

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