Merge branch 'master' into ordered_chapters
[FFMpeg-mirror/ordered_chapters.git] / libavformat / file.c
blob07c5634d8a230c6b3879c1d4493bdbcd8655975c
1 /*
2 * Buffered file io for ffmpeg system
3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include <dirent.h>
25 #include <fcntl.h>
26 #if HAVE_SETMODE
27 #include <io.h>
28 #endif
29 #include <unistd.h>
30 #include <sys/time.h>
31 #include <stdlib.h>
32 #include "os_support.h"
35 /* standard file protocol */
37 static int file_opendir(URLContext *h, const char *path, int flags)
39 DIR *dir;
41 if(!(dir = opendir(path)))
42 return -1;
43 h->priv_data = dir;
44 return 0;
47 static int file_readdir(URLContext *h, char *filename, int size)
49 DIR *dir = h->priv_data;
50 struct dirent *file = readdir(dir);
52 if(!file)
53 return -1;
54 if(strlen(h->filename)+strlen(file->d_name) + 2 > size)
55 return -1;
56 strcpy(filename, h->filename);
57 av_strlcat(filename, "/", size);
58 av_strlcat(filename, file->d_name, size);
59 return 0;
62 static int file_closedir(URLContext *h)
64 DIR *dir = h->priv_data;
65 closedir(dir);
66 return 0;
69 static int file_open(URLContext *h, const char *filename, int flags)
71 int access;
72 int fd;
74 av_strstart(filename, "file:", &filename);
76 if (flags & URL_DIR)
77 return file_opendir(h, filename, flags);
78 if (flags & URL_RDWR) {
79 access = O_CREAT | O_TRUNC | O_RDWR;
80 } else if (flags & URL_WRONLY) {
81 access = O_CREAT | O_TRUNC | O_WRONLY;
82 } else {
83 access = O_RDONLY;
85 #ifdef O_BINARY
86 access |= O_BINARY;
87 #endif
88 fd = open(filename, access, 0666);
89 if (fd < 0)
90 return AVERROR(ENOENT);
91 h->priv_data = (void *)(size_t)fd;
92 return 0;
95 static int file_read(URLContext *h, unsigned char *buf, int size)
97 int fd = (size_t)h->priv_data;
98 if(h->flags&URL_DIR)
99 return file_readdir(h, buf, size);
100 return read(fd, buf, size);
103 static int file_write(URLContext *h, unsigned char *buf, int size)
105 int fd = (size_t)h->priv_data;
106 if(h->flags&URL_DIR)
107 return AVERROR(EIO);
108 return write(fd, buf, size);
111 /* XXX: use llseek */
112 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
114 int fd = (size_t)h->priv_data;
115 if(h->flags&URL_DIR)
116 return AVERROR(EPIPE);
117 return lseek(fd, pos, whence);
120 static int file_close(URLContext *h)
122 int fd = (size_t)h->priv_data;
123 if(h->flags&URL_DIR)
124 return file_closedir(h);
125 return close(fd);
128 URLProtocol file_protocol = {
129 "file",
130 file_open,
131 file_read,
132 file_write,
133 file_seek,
134 file_close,
137 /* pipe protocol */
139 static int pipe_open(URLContext *h, const char *filename, int flags)
141 int fd;
142 char *final;
143 av_strstart(filename, "pipe:", &filename);
145 fd = strtol(filename, &final, 10);
146 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
147 if (flags & URL_WRONLY) {
148 fd = 1;
149 } else {
150 fd = 0;
153 #if HAVE_SETMODE
154 setmode(fd, O_BINARY);
155 #endif
156 h->priv_data = (void *)(size_t)fd;
157 h->is_streamed = 1;
158 return 0;
161 URLProtocol pipe_protocol = {
162 "pipe",
163 pipe_open,
164 file_read,
165 file_write,