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.

166 lines
4.1KB

  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 "avformat.h"
  23. #include <fcntl.h>
  24. #if HAVE_IO_H
  25. #include <io.h>
  26. #endif
  27. #if HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #include <sys/stat.h>
  31. #include <stdlib.h>
  32. #include "os_support.h"
  33. #include "url.h"
  34. /* standard file protocol */
  35. static int file_read(URLContext *h, unsigned char *buf, int size)
  36. {
  37. int fd = (intptr_t) h->priv_data;
  38. int r = read(fd, buf, size);
  39. return (-1 == r)?AVERROR(errno):r;
  40. }
  41. static int file_write(URLContext *h, const unsigned char *buf, int size)
  42. {
  43. int fd = (intptr_t) h->priv_data;
  44. int r = write(fd, buf, size);
  45. return (-1 == r)?AVERROR(errno):r;
  46. }
  47. static int file_get_handle(URLContext *h)
  48. {
  49. return (intptr_t) h->priv_data;
  50. }
  51. static int file_check(URLContext *h, int mask)
  52. {
  53. struct stat st;
  54. int ret = stat(h->filename, &st);
  55. if (ret < 0)
  56. return AVERROR(errno);
  57. ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
  58. ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
  59. return ret;
  60. }
  61. #if CONFIG_FILE_PROTOCOL
  62. static int file_open(URLContext *h, const char *filename, int flags)
  63. {
  64. int access;
  65. int fd;
  66. struct stat st;
  67. av_strstart(filename, "file:", &filename);
  68. if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
  69. access = O_CREAT | O_TRUNC | O_RDWR;
  70. } else if (flags & AVIO_FLAG_WRITE) {
  71. access = O_CREAT | O_TRUNC | O_WRONLY;
  72. } else {
  73. access = O_RDONLY;
  74. }
  75. #ifdef O_BINARY
  76. access |= O_BINARY;
  77. #endif
  78. fd = open(filename, access, 0666);
  79. if (fd == -1)
  80. return AVERROR(errno);
  81. h->priv_data = (void *) (intptr_t) fd;
  82. h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
  83. return 0;
  84. }
  85. /* XXX: use llseek */
  86. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  87. {
  88. int fd = (intptr_t) h->priv_data;
  89. if (whence == AVSEEK_SIZE) {
  90. struct stat st;
  91. int ret = fstat(fd, &st);
  92. return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
  93. }
  94. return lseek(fd, pos, whence);
  95. }
  96. static int file_close(URLContext *h)
  97. {
  98. int fd = (intptr_t) h->priv_data;
  99. return close(fd);
  100. }
  101. URLProtocol ff_file_protocol = {
  102. .name = "file",
  103. .url_open = file_open,
  104. .url_read = file_read,
  105. .url_write = file_write,
  106. .url_seek = file_seek,
  107. .url_close = file_close,
  108. .url_get_file_handle = file_get_handle,
  109. .url_check = file_check,
  110. };
  111. #endif /* CONFIG_FILE_PROTOCOL */
  112. #if CONFIG_PIPE_PROTOCOL
  113. static int pipe_open(URLContext *h, const char *filename, int flags)
  114. {
  115. int fd;
  116. char *final;
  117. av_strstart(filename, "pipe:", &filename);
  118. fd = strtol(filename, &final, 10);
  119. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  120. if (flags & AVIO_FLAG_WRITE) {
  121. fd = 1;
  122. } else {
  123. fd = 0;
  124. }
  125. }
  126. #if HAVE_SETMODE
  127. setmode(fd, O_BINARY);
  128. #endif
  129. h->priv_data = (void *) (intptr_t) fd;
  130. h->is_streamed = 1;
  131. return 0;
  132. }
  133. URLProtocol ff_pipe_protocol = {
  134. .name = "pipe",
  135. .url_open = pipe_open,
  136. .url_read = file_read,
  137. .url_write = file_write,
  138. .url_get_file_handle = file_get_handle,
  139. .url_check = file_check,
  140. };
  141. #endif /* CONFIG_PIPE_PROTOCOL */