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