file.c 2.8 KB

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