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