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.

169 lines
4.1KB

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