]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
Merge commit 'f542dedf72091af8e6f32a12bd64289c58857c21'
[ffmpeg] / libavformat / file.c
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
22 #include "libavutil/avstring.h"
23 #include "libavutil/opt.h"
24 #include "avformat.h"
25 #include <fcntl.h>
26 #if HAVE_IO_H
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <sys/stat.h>
33 #include <stdlib.h>
34 #include "os_support.h"
35 #include "url.h"
36
37 /* Some systems may not have S_ISFIFO */
38 #ifndef S_ISFIFO
39 #  ifdef S_IFIFO
40 #    define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
41 #  else
42 #    define S_ISFIFO(m) 0
43 #  endif
44 #endif
45
46 /* standard file protocol */
47
48 typedef struct FileContext {
49     const AVClass *class;
50     int fd;
51     int trunc;
52     int blocksize;
53 } FileContext;
54
55 static const AVOption file_options[] = {
56     { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
57     { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
58     { NULL }
59 };
60
61 static const AVOption pipe_options[] = {
62     { "blocksize", "set I/O operation maximum block size", offsetof(FileContext, blocksize), AV_OPT_TYPE_INT, { .i64 = INT_MAX }, 1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
63     { NULL }
64 };
65
66 static const AVClass file_class = {
67     .class_name = "file",
68     .item_name  = av_default_item_name,
69     .option     = file_options,
70     .version    = LIBAVUTIL_VERSION_INT,
71 };
72
73 static const AVClass pipe_class = {
74     .class_name = "pipe",
75     .item_name  = av_default_item_name,
76     .option     = pipe_options,
77     .version    = LIBAVUTIL_VERSION_INT,
78 };
79
80 static int file_read(URLContext *h, unsigned char *buf, int size)
81 {
82     FileContext *c = h->priv_data;
83     int r;
84     size = FFMIN(size, c->blocksize);
85     r = read(c->fd, buf, size);
86     return (-1 == r)?AVERROR(errno):r;
87 }
88
89 static int file_write(URLContext *h, const unsigned char *buf, int size)
90 {
91     FileContext *c = h->priv_data;
92     int r;
93     size = FFMIN(size, c->blocksize);
94     r = write(c->fd, buf, size);
95     return (-1 == r)?AVERROR(errno):r;
96 }
97
98 static int file_get_handle(URLContext *h)
99 {
100     FileContext *c = h->priv_data;
101     return c->fd;
102 }
103
104 static int file_check(URLContext *h, int mask)
105 {
106 #if HAVE_ACCESS && defined(R_OK)
107     int ret = 0;
108     if (access(h->filename, F_OK) < 0)
109         return AVERROR(errno);
110     if (mask&AVIO_FLAG_READ)
111         if (access(h->filename, R_OK) >= 0)
112             ret |= AVIO_FLAG_READ;
113     if (mask&AVIO_FLAG_WRITE)
114         if (access(h->filename, W_OK) >= 0)
115             ret |= AVIO_FLAG_WRITE;
116 #else
117     struct stat st;
118     int ret = stat(h->filename, &st);
119     if (ret < 0)
120         return AVERROR(errno);
121
122     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
123     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
124 #endif
125     return ret;
126 }
127
128 #if CONFIG_FILE_PROTOCOL
129
130 static int file_open(URLContext *h, const char *filename, int flags)
131 {
132     FileContext *c = h->priv_data;
133     int access;
134     int fd;
135     struct stat st;
136
137     av_strstart(filename, "file:", &filename);
138
139     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
140         access = O_CREAT | O_RDWR;
141         if (c->trunc)
142             access |= O_TRUNC;
143     } else if (flags & AVIO_FLAG_WRITE) {
144         access = O_CREAT | O_WRONLY;
145         if (c->trunc)
146             access |= O_TRUNC;
147     } else {
148         access = O_RDONLY;
149     }
150 #ifdef O_BINARY
151     access |= O_BINARY;
152 #endif
153     fd = open(filename, access, 0666);
154     if (fd == -1)
155         return AVERROR(errno);
156     c->fd = fd;
157
158     h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
159
160     return 0;
161 }
162
163 /* XXX: use llseek */
164 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
165 {
166     FileContext *c = h->priv_data;
167     int64_t ret;
168
169     if (whence == AVSEEK_SIZE) {
170         struct stat st;
171         ret = fstat(c->fd, &st);
172         return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
173     }
174
175     ret = lseek(c->fd, pos, whence);
176
177     return ret < 0 ? AVERROR(errno) : ret;
178 }
179
180 static int file_close(URLContext *h)
181 {
182     FileContext *c = h->priv_data;
183     return close(c->fd);
184 }
185
186 URLProtocol ff_file_protocol = {
187     .name                = "file",
188     .url_open            = file_open,
189     .url_read            = file_read,
190     .url_write           = file_write,
191     .url_seek            = file_seek,
192     .url_close           = file_close,
193     .url_get_file_handle = file_get_handle,
194     .url_check           = file_check,
195     .priv_data_size      = sizeof(FileContext),
196     .priv_data_class     = &file_class,
197 };
198
199 #endif /* CONFIG_FILE_PROTOCOL */
200
201 #if CONFIG_PIPE_PROTOCOL
202
203 static int pipe_open(URLContext *h, const char *filename, int flags)
204 {
205     FileContext *c = h->priv_data;
206     int fd;
207     char *final;
208     av_strstart(filename, "pipe:", &filename);
209
210     fd = strtol(filename, &final, 10);
211     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
212         if (flags & AVIO_FLAG_WRITE) {
213             fd = 1;
214         } else {
215             fd = 0;
216         }
217     }
218 #if HAVE_SETMODE
219     setmode(fd, O_BINARY);
220 #endif
221     c->fd = fd;
222     h->is_streamed = 1;
223     return 0;
224 }
225
226 URLProtocol ff_pipe_protocol = {
227     .name                = "pipe",
228     .url_open            = pipe_open,
229     .url_read            = file_read,
230     .url_write           = file_write,
231     .url_get_file_handle = file_get_handle,
232     .url_check           = file_check,
233     .priv_data_size      = sizeof(FileContext),
234     .priv_data_class     = &pipe_class,
235 };
236
237 #endif /* CONFIG_PIPE_PROTOCOL */