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.

174 lines
4.3KB

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