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.

160 lines
3.9KB

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