file.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Buffered file io for ffmpeg system
  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/time.h>
  29. #include <stdlib.h>
  30. #include "os_support.h"
  31. /* standard file protocol */
  32. static int file_open(URLContext *h, const char *filename, int flags)
  33. {
  34. int access;
  35. int fd;
  36. av_strstart(filename, "file:", &filename);
  37. if (flags & URL_RDWR) {
  38. access = O_CREAT | O_TRUNC | O_RDWR;
  39. } else if (flags & URL_WRONLY) {
  40. access = O_CREAT | O_TRUNC | O_WRONLY;
  41. } else {
  42. access = O_RDONLY;
  43. }
  44. #ifdef O_BINARY
  45. access |= O_BINARY;
  46. #endif
  47. fd = open(filename, access, 0666);
  48. if (fd < 0)
  49. return AVERROR(ENOENT);
  50. h->priv_data = (void *)(size_t)fd;
  51. return 0;
  52. }
  53. static int file_read(URLContext *h, unsigned char *buf, int size)
  54. {
  55. int fd = (size_t)h->priv_data;
  56. return read(fd, buf, size);
  57. }
  58. static int file_write(URLContext *h, unsigned char *buf, int size)
  59. {
  60. int fd = (size_t)h->priv_data;
  61. return write(fd, buf, size);
  62. }
  63. /* XXX: use llseek */
  64. static int64_t file_seek(URLContext *h, int64_t pos, int whence)
  65. {
  66. int fd = (size_t)h->priv_data;
  67. return lseek(fd, pos, whence);
  68. }
  69. static int file_close(URLContext *h)
  70. {
  71. int fd = (size_t)h->priv_data;
  72. return close(fd);
  73. }
  74. URLProtocol file_protocol = {
  75. "file",
  76. file_open,
  77. file_read,
  78. file_write,
  79. file_seek,
  80. file_close,
  81. };
  82. /* pipe protocol */
  83. static int pipe_open(URLContext *h, const char *filename, int flags)
  84. {
  85. int fd;
  86. char *final;
  87. av_strstart(filename, "pipe:", &filename);
  88. fd = strtol(filename, &final, 10);
  89. if((filename == final) || *final ) {/* No digits found, or something like 10ab */
  90. if (flags & URL_WRONLY) {
  91. fd = 1;
  92. } else {
  93. fd = 0;
  94. }
  95. }
  96. #if HAVE_SETMODE
  97. setmode(fd, O_BINARY);
  98. #endif
  99. h->priv_data = (void *)(size_t)fd;
  100. h->is_streamed = 1;
  101. return 0;
  102. }
  103. URLProtocol pipe_protocol = {
  104. "pipe",
  105. pipe_open,
  106. file_read,
  107. file_write,
  108. };