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.

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