]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
Merge commit '97bf7c03b1338a867da52c159a2afecbdedcfa88'
[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 } FileContext;
53
54 static const AVOption file_options[] = {
55     { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
56     { NULL }
57 };
58
59 static const AVClass file_class = {
60     .class_name = "file",
61     .item_name  = av_default_item_name,
62     .option     = file_options,
63     .version    = LIBAVUTIL_VERSION_INT,
64 };
65
66 static int file_read(URLContext *h, unsigned char *buf, int size)
67 {
68     FileContext *c = h->priv_data;
69     int r = read(c->fd, buf, size);
70     return (-1 == r)?AVERROR(errno):r;
71 }
72
73 static int file_write(URLContext *h, const unsigned char *buf, int size)
74 {
75     FileContext *c = h->priv_data;
76     int r = write(c->fd, buf, size);
77     return (-1 == r)?AVERROR(errno):r;
78 }
79
80 static int file_get_handle(URLContext *h)
81 {
82     FileContext *c = h->priv_data;
83     return c->fd;
84 }
85
86 static int file_check(URLContext *h, int mask)
87 {
88     struct stat st;
89     int ret = stat(h->filename, &st);
90     if (ret < 0)
91         return AVERROR(errno);
92
93     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
94     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
95
96     return ret;
97 }
98
99 #if CONFIG_FILE_PROTOCOL
100
101 static int file_open(URLContext *h, const char *filename, int flags)
102 {
103     FileContext *c = h->priv_data;
104     int access;
105     int fd;
106     struct stat st;
107
108     av_strstart(filename, "file:", &filename);
109
110     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
111         access = O_CREAT | O_RDWR;
112         if (c->trunc)
113             access |= O_TRUNC;
114     } else if (flags & AVIO_FLAG_WRITE) {
115         access = O_CREAT | O_WRONLY;
116         if (c->trunc)
117             access |= O_TRUNC;
118     } else {
119         access = O_RDONLY;
120     }
121 #ifdef O_BINARY
122     access |= O_BINARY;
123 #endif
124     fd = open(filename, access, 0666);
125     if (fd == -1)
126         return AVERROR(errno);
127     c->fd = fd;
128
129     h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
130
131     return 0;
132 }
133
134 /* XXX: use llseek */
135 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
136 {
137     FileContext *c = h->priv_data;
138     int64_t ret;
139
140     if (whence == AVSEEK_SIZE) {
141         struct stat st;
142         ret = fstat(c->fd, &st);
143         return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
144     }
145
146     ret = lseek(c->fd, pos, whence);
147
148     return ret < 0 ? AVERROR(errno) : ret;
149 }
150
151 static int file_close(URLContext *h)
152 {
153     FileContext *c = h->priv_data;
154     return close(c->fd);
155 }
156
157 URLProtocol ff_file_protocol = {
158     .name                = "file",
159     .url_open            = file_open,
160     .url_read            = file_read,
161     .url_write           = file_write,
162     .url_seek            = file_seek,
163     .url_close           = file_close,
164     .url_get_file_handle = file_get_handle,
165     .url_check           = file_check,
166     .priv_data_size      = sizeof(FileContext),
167     .priv_data_class     = &file_class,
168 };
169
170 #endif /* CONFIG_FILE_PROTOCOL */
171
172 #if CONFIG_PIPE_PROTOCOL
173
174 static int pipe_open(URLContext *h, const char *filename, int flags)
175 {
176     FileContext *c = h->priv_data;
177     int fd;
178     char *final;
179     av_strstart(filename, "pipe:", &filename);
180
181     fd = strtol(filename, &final, 10);
182     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
183         if (flags & AVIO_FLAG_WRITE) {
184             fd = 1;
185         } else {
186             fd = 0;
187         }
188     }
189 #if HAVE_SETMODE
190     setmode(fd, O_BINARY);
191 #endif
192     c->fd = fd;
193     h->is_streamed = 1;
194     return 0;
195 }
196
197 URLProtocol ff_pipe_protocol = {
198     .name                = "pipe",
199     .url_open            = pipe_open,
200     .url_read            = file_read,
201     .url_write           = file_write,
202     .url_get_file_handle = file_get_handle,
203     .url_check           = file_check,
204     .priv_data_size      = sizeof(FileContext),
205 };
206
207 #endif /* CONFIG_PIPE_PROTOCOL */