]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
Merge commit '01f0e6a0c9270f1d5bef08459a6f167cf55e0596'
[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/internal.h"
24 #include "libavutil/opt.h"
25 #include "avformat.h"
26 #if HAVE_DIRENT_H
27 #include <dirent.h>
28 #endif
29 #include <fcntl.h>
30 #if HAVE_IO_H
31 #include <io.h>
32 #endif
33 #if HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <sys/stat.h>
37 #include <stdlib.h>
38 #include "os_support.h"
39 #include "url.h"
40
41 /* Some systems may not have S_ISFIFO */
42 #ifndef S_ISFIFO
43 #  ifdef S_IFIFO
44 #    define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
45 #  else
46 #    define S_ISFIFO(m) 0
47 #  endif
48 #endif
49
50 /* Not available in POSIX.1-1996 */
51 #ifndef S_ISLNK
52 #  ifdef S_IFLNK
53 #    define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
54 #  else
55 #    define S_ISLNK(m) 0
56 #  endif
57 #endif
58
59 /* Not available in POSIX.1-1996 */
60 #ifndef S_ISSOCK
61 #  ifdef S_IFSOCK
62 #    define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
63 #  else
64 #    define S_ISSOCK(m) 0
65 #  endif
66 #endif
67
68 /* standard file protocol */
69
70 typedef struct FileContext {
71     const AVClass *class;
72     int fd;
73     int trunc;
74     int blocksize;
75 #if HAVE_DIRENT_H
76     DIR *dir;
77 #endif
78 } FileContext;
79
80 static const AVOption file_options[] = {
81     { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
82     { "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 },
83     { NULL }
84 };
85
86 static const AVOption pipe_options[] = {
87     { "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 },
88     { NULL }
89 };
90
91 static const AVClass file_class = {
92     .class_name = "file",
93     .item_name  = av_default_item_name,
94     .option     = file_options,
95     .version    = LIBAVUTIL_VERSION_INT,
96 };
97
98 static const AVClass pipe_class = {
99     .class_name = "pipe",
100     .item_name  = av_default_item_name,
101     .option     = pipe_options,
102     .version    = LIBAVUTIL_VERSION_INT,
103 };
104
105 static int file_read(URLContext *h, unsigned char *buf, int size)
106 {
107     FileContext *c = h->priv_data;
108     int ret;
109     size = FFMIN(size, c->blocksize);
110     ret = read(c->fd, buf, size);
111     return (ret == -1) ? AVERROR(errno) : ret;
112 }
113
114 static int file_write(URLContext *h, const unsigned char *buf, int size)
115 {
116     FileContext *c = h->priv_data;
117     int ret;
118     size = FFMIN(size, c->blocksize);
119     ret = write(c->fd, buf, size);
120     return (ret == -1) ? AVERROR(errno) : ret;
121 }
122
123 static int file_get_handle(URLContext *h)
124 {
125     FileContext *c = h->priv_data;
126     return c->fd;
127 }
128
129 static int file_check(URLContext *h, int mask)
130 {
131     int ret = 0;
132     const char *filename = h->filename;
133     av_strstart(filename, "file:", &filename);
134
135     {
136 #if HAVE_ACCESS && defined(R_OK)
137     if (access(filename, F_OK) < 0)
138         return AVERROR(errno);
139     if (mask&AVIO_FLAG_READ)
140         if (access(filename, R_OK) >= 0)
141             ret |= AVIO_FLAG_READ;
142     if (mask&AVIO_FLAG_WRITE)
143         if (access(filename, W_OK) >= 0)
144             ret |= AVIO_FLAG_WRITE;
145 #else
146     struct stat st;
147     ret = stat(filename, &st);
148     if (ret < 0)
149         return AVERROR(errno);
150
151     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
152     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
153 #endif
154     }
155     return ret;
156 }
157
158 static int file_delete(URLContext *h)
159 {
160 #if HAVE_UNISTD_H
161     int ret;
162     const char *filename = h->filename;
163     av_strstart(filename, "file:", &filename);
164
165     ret = rmdir(filename);
166     if (ret < 0 && errno == ENOTDIR)
167         ret = unlink(filename);
168     if (ret < 0)
169         return AVERROR(errno);
170
171     return ret;
172 #else
173     return AVERROR(ENOSYS);
174 #endif /* HAVE_UNISTD_H */
175 }
176
177 static int file_move(URLContext *h_src, URLContext *h_dst)
178 {
179     const char *filename_src = h_src->filename;
180     const char *filename_dst = h_dst->filename;
181     av_strstart(filename_src, "file:", &filename_src);
182     av_strstart(filename_dst, "file:", &filename_dst);
183
184     if (rename(filename_src, filename_dst) < 0)
185         return AVERROR(errno);
186
187     return 0;
188 }
189
190 #if CONFIG_FILE_PROTOCOL
191
192 static int file_open(URLContext *h, const char *filename, int flags)
193 {
194     FileContext *c = h->priv_data;
195     int access;
196     int fd;
197     struct stat st;
198
199     av_strstart(filename, "file:", &filename);
200
201     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
202         access = O_CREAT | O_RDWR;
203         if (c->trunc)
204             access |= O_TRUNC;
205     } else if (flags & AVIO_FLAG_WRITE) {
206         access = O_CREAT | O_WRONLY;
207         if (c->trunc)
208             access |= O_TRUNC;
209     } else {
210         access = O_RDONLY;
211     }
212 #ifdef O_BINARY
213     access |= O_BINARY;
214 #endif
215     fd = avpriv_open(filename, access, 0666);
216     if (fd == -1)
217         return AVERROR(errno);
218     c->fd = fd;
219
220     h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
221
222     return 0;
223 }
224
225 /* XXX: use llseek */
226 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
227 {
228     FileContext *c = h->priv_data;
229     int64_t ret;
230
231     if (whence == AVSEEK_SIZE) {
232         struct stat st;
233         ret = fstat(c->fd, &st);
234         return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
235     }
236
237     ret = lseek(c->fd, pos, whence);
238
239     return ret < 0 ? AVERROR(errno) : ret;
240 }
241
242 static int file_close(URLContext *h)
243 {
244     FileContext *c = h->priv_data;
245     return close(c->fd);
246 }
247
248 static int file_open_dir(URLContext *h)
249 {
250 #if HAVE_LSTAT
251     FileContext *c = h->priv_data;
252
253     c->dir = opendir(h->filename);
254     if (!c->dir)
255         return AVERROR(errno);
256
257     return 0;
258 #else
259     return AVERROR(ENOSYS);
260 #endif /* HAVE_LSTAT */
261 }
262
263 static int file_read_dir(URLContext *h, AVIODirEntry **next)
264 {
265 #if HAVE_LSTAT
266     FileContext *c = h->priv_data;
267     struct dirent *dir;
268     char *fullpath = NULL;
269
270     *next = ff_alloc_dir_entry();
271     if (!*next)
272         return AVERROR(ENOMEM);
273     do {
274         errno = 0;
275         dir = readdir(c->dir);
276         if (!dir) {
277             av_freep(next);
278             return AVERROR(errno);
279         }
280     } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
281
282     fullpath = av_append_path_component(h->filename, dir->d_name);
283     if (fullpath) {
284         struct stat st;
285         if (!lstat(fullpath, &st)) {
286             if (S_ISDIR(st.st_mode))
287                 (*next)->type = AVIO_ENTRY_DIRECTORY;
288             else if (S_ISFIFO(st.st_mode))
289                 (*next)->type = AVIO_ENTRY_NAMED_PIPE;
290             else if (S_ISCHR(st.st_mode))
291                 (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
292             else if (S_ISBLK(st.st_mode))
293                 (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
294             else if (S_ISLNK(st.st_mode))
295                 (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
296             else if (S_ISSOCK(st.st_mode))
297                 (*next)->type = AVIO_ENTRY_SOCKET;
298             else if (S_ISREG(st.st_mode))
299                 (*next)->type = AVIO_ENTRY_FILE;
300             else
301                 (*next)->type = AVIO_ENTRY_UNKNOWN;
302
303             (*next)->group_id = st.st_gid;
304             (*next)->user_id = st.st_uid;
305             (*next)->size = st.st_size;
306             (*next)->filemode = st.st_mode & 0777;
307             (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
308             (*next)->access_timestamp =  INT64_C(1000000) * st.st_atime;
309             (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
310         }
311         av_free(fullpath);
312     }
313
314     (*next)->name = av_strdup(dir->d_name);
315     return 0;
316 #else
317     return AVERROR(ENOSYS);
318 #endif /* HAVE_LSTAT */
319 }
320
321 static int file_close_dir(URLContext *h)
322 {
323 #if HAVE_LSTAT
324     FileContext *c = h->priv_data;
325     closedir(c->dir);
326     return 0;
327 #else
328     return AVERROR(ENOSYS);
329 #endif /* HAVE_LSTAT */
330 }
331
332 const URLProtocol ff_file_protocol = {
333     .name                = "file",
334     .url_open            = file_open,
335     .url_read            = file_read,
336     .url_write           = file_write,
337     .url_seek            = file_seek,
338     .url_close           = file_close,
339     .url_get_file_handle = file_get_handle,
340     .url_check           = file_check,
341     .url_delete          = file_delete,
342     .url_move            = file_move,
343     .priv_data_size      = sizeof(FileContext),
344     .priv_data_class     = &file_class,
345     .url_open_dir        = file_open_dir,
346     .url_read_dir        = file_read_dir,
347     .url_close_dir       = file_close_dir,
348     .default_whitelist   = "file,crypto"
349 };
350
351 #endif /* CONFIG_FILE_PROTOCOL */
352
353 #if CONFIG_PIPE_PROTOCOL
354
355 static int pipe_open(URLContext *h, const char *filename, int flags)
356 {
357     FileContext *c = h->priv_data;
358     int fd;
359     char *final;
360     av_strstart(filename, "pipe:", &filename);
361
362     fd = strtol(filename, &final, 10);
363     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
364         if (flags & AVIO_FLAG_WRITE) {
365             fd = 1;
366         } else {
367             fd = 0;
368         }
369     }
370 #if HAVE_SETMODE
371     setmode(fd, O_BINARY);
372 #endif
373     c->fd = fd;
374     h->is_streamed = 1;
375     return 0;
376 }
377
378 const URLProtocol ff_pipe_protocol = {
379     .name                = "pipe",
380     .url_open            = pipe_open,
381     .url_read            = file_read,
382     .url_write           = file_write,
383     .url_get_file_handle = file_get_handle,
384     .url_check           = file_check,
385     .priv_data_size      = sizeof(FileContext),
386     .priv_data_class     = &pipe_class,
387     .default_whitelist   = "crypto"
388 };
389
390 #endif /* CONFIG_PIPE_PROTOCOL */