1 /*
2 * buffered file I/O
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
26 #include <fcntl.h>
27 #if HAVE_IO_H
28 #include <io.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/stat.h>
34 #include <stdlib.h>
37
38 /* Some systems may not have S_ISFIFO */
39 #ifndef S_ISFIFO
40 # ifdef S_IFIFO
41 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
42 # else
43 # define S_ISFIFO(m) 0
44 # endif
45 #endif
46
47 /* standard file protocol */
48
55
59 { NULL }
60 };
61
64 { NULL }
65 };
66
72 };
73
79 };
80
82 {
86 r = read(c->
fd, buf, size);
88 }
89
91 {
95 r = write(c->
fd, buf, size);
97 }
98
100 {
103 }
104
106 {
107 #if HAVE_ACCESS && defined(R_OK)
117 #else
118 struct stat st;
120 if (ret < 0)
122
123 ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
124 ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
125 #endif
127 }
128
129 #if CONFIG_FILE_PROTOCOL
130
132 {
134 int access;
135 int fd;
136 struct stat st;
137
139
141 access = O_CREAT | O_RDWR;
143 access |= O_TRUNC;
145 access = O_CREAT | O_WRONLY;
147 access |= O_TRUNC;
148 } else {
149 access = O_RDONLY;
150 }
151 #ifdef O_BINARY
153 #endif
155 if (fd == -1)
158
160
161 return 0;
162 }
163
164 /* XXX: use llseek */
165 static int64_t file_seek(
URLContext *h, int64_t pos,
int whence)
166 {
169
171 struct stat st;
172 ret = fstat(c->
fd, &st);
173 return ret < 0 ?
AVERROR(errno) : (
S_ISFIFO(st.st_mode) ? 0 : st.st_size);
174 }
175
176 ret = lseek(c->
fd, pos, whence);
177
178 return ret < 0 ?
AVERROR(errno) : ret;
179 }
180
182 {
185 }
186
189 .url_open = file_open,
192 .url_seek = file_seek,
193 .url_close = file_close,
197 .priv_data_class = &file_class,
198 };
199
200 #endif /* CONFIG_FILE_PROTOCOL */
201
202 #if CONFIG_PIPE_PROTOCOL
203
204 static int pipe_open(
URLContext *h,
const char *filename,
int flags)
205 {
207 int fd;
208 char *final;
210
211 fd = strtol(filename, &final, 10);
212 if((filename == final) || *final ) {/* No digits found, or something like 10ab */
213 if (flags & AVIO_FLAG_WRITE) {
214 fd = 1;
215 } else {
216 fd = 0;
217 }
218 }
219 #if HAVE_SETMODE
220 setmode(fd, O_BINARY);
221 #endif
224 return 0;
225 }
226
229 .url_open = pipe_open,
235 .priv_data_class = &pipe_class,
236 };
237
238 #endif /* CONFIG_PIPE_PROTOCOL */