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