• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config_components.h"
23 
24 #include "libavutil/avstring.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/opt.h"
27 #include "avformat.h"
28 #if HAVE_DIRENT_H
29 #include <dirent.h>
30 #endif
31 #include <fcntl.h>
32 #if HAVE_IO_H
33 #include <io.h>
34 #endif
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <sys/stat.h>
39 #include <stdlib.h>
40 #include "os_support.h"
41 #include "url.h"
42 
43 /* Some systems may not have S_ISFIFO */
44 #ifndef S_ISFIFO
45 #  ifdef S_IFIFO
46 #    define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
47 #  else
48 #    define S_ISFIFO(m) 0
49 #  endif
50 #endif
51 
52 /* Not available in POSIX.1-1996 */
53 #ifndef S_ISLNK
54 #  ifdef S_IFLNK
55 #    define S_ISLNK(m) (((m) & S_IFLNK) == S_IFLNK)
56 #  else
57 #    define S_ISLNK(m) 0
58 #  endif
59 #endif
60 
61 /* Not available in POSIX.1-1996 */
62 #ifndef S_ISSOCK
63 #  ifdef S_IFSOCK
64 #    define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
65 #  else
66 #    define S_ISSOCK(m) 0
67 #  endif
68 #endif
69 
70 /* standard file protocol */
71 
72 typedef struct FileContext {
73     const AVClass *class;
74     int fd;
75     int trunc;
76     int blocksize;
77     int follow;
78     int seekable;
79 #if HAVE_DIRENT_H
80     DIR *dir;
81 #endif
82 } FileContext;
83 
84 static const AVOption file_options[] = {
85     { "truncate", "truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
86     { "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 },
87     { "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 },
88     { "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 },
89     { NULL }
90 };
91 
92 static const AVOption pipe_options[] = {
93     { "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 },
94     { NULL }
95 };
96 
97 static const AVClass file_class = {
98     .class_name = "file",
99     .item_name  = av_default_item_name,
100     .option     = file_options,
101     .version    = LIBAVUTIL_VERSION_INT,
102 };
103 
104 static const AVClass pipe_class = {
105     .class_name = "pipe",
106     .item_name  = av_default_item_name,
107     .option     = pipe_options,
108     .version    = LIBAVUTIL_VERSION_INT,
109 };
110 
file_read(URLContext * h,unsigned char * buf,int size)111 static int file_read(URLContext *h, unsigned char *buf, int size)
112 {
113     FileContext *c = h->priv_data;
114     int ret;
115     size = FFMIN(size, c->blocksize);
116     ret = read(c->fd, buf, size);
117     if (ret == 0 && c->follow)
118         return AVERROR(EAGAIN);
119     if (ret == 0)
120         return AVERROR_EOF;
121     return (ret == -1) ? AVERROR(errno) : ret;
122 }
123 
file_write(URLContext * h,const unsigned char * buf,int size)124 static int file_write(URLContext *h, const unsigned char *buf, int size)
125 {
126     FileContext *c = h->priv_data;
127     int ret;
128     size = FFMIN(size, c->blocksize);
129     ret = write(c->fd, buf, size);
130     return (ret == -1) ? AVERROR(errno) : ret;
131 }
132 
file_get_handle(URLContext * h)133 static int file_get_handle(URLContext *h)
134 {
135     FileContext *c = h->priv_data;
136     return c->fd;
137 }
138 
file_check(URLContext * h,int mask)139 static int file_check(URLContext *h, int mask)
140 {
141     int ret = 0;
142     const char *filename = h->filename;
143     av_strstart(filename, "file:", &filename);
144 
145     {
146 #if HAVE_ACCESS && defined(R_OK)
147     if (access(filename, F_OK) < 0)
148         return AVERROR(errno);
149     if (mask&AVIO_FLAG_READ)
150         if (access(filename, R_OK) >= 0)
151             ret |= AVIO_FLAG_READ;
152     if (mask&AVIO_FLAG_WRITE)
153         if (access(filename, W_OK) >= 0)
154             ret |= AVIO_FLAG_WRITE;
155 #else
156     struct stat st;
157     ret = stat(filename, &st);
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 #if CONFIG_FILE_PROTOCOL
169 
file_delete(URLContext * h)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 
file_move(URLContext * h_src,URLContext * h_dst)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 
file_open(URLContext * h,const char * filename,int flags)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     if (c->seekable >= 0)
242         h->is_streamed = !c->seekable;
243 
244     return 0;
245 }
246 
247 /* XXX: use llseek */
file_seek(URLContext * h,int64_t pos,int whence)248 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
249 {
250     FileContext *c = h->priv_data;
251     int64_t ret;
252 
253     if (whence == AVSEEK_SIZE) {
254         struct stat st;
255         ret = fstat(c->fd, &st);
256         return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
257     }
258 
259     ret = lseek(c->fd, pos, whence);
260 
261     return ret < 0 ? AVERROR(errno) : ret;
262 }
263 
file_close(URLContext * h)264 static int file_close(URLContext *h)
265 {
266     FileContext *c = h->priv_data;
267     int ret = close(c->fd);
268     return (ret == -1) ? AVERROR(errno) : 0;
269 }
270 
file_open_dir(URLContext * h)271 static int file_open_dir(URLContext *h)
272 {
273 #if HAVE_LSTAT
274     FileContext *c = h->priv_data;
275 
276     c->dir = opendir(h->filename);
277     if (!c->dir)
278         return AVERROR(errno);
279 
280     return 0;
281 #else
282     return AVERROR(ENOSYS);
283 #endif /* HAVE_LSTAT */
284 }
285 
file_read_dir(URLContext * h,AVIODirEntry ** next)286 static int file_read_dir(URLContext *h, AVIODirEntry **next)
287 {
288 #if HAVE_LSTAT
289     FileContext *c = h->priv_data;
290     struct dirent *dir;
291     char *fullpath = NULL;
292 
293     *next = ff_alloc_dir_entry();
294     if (!*next)
295         return AVERROR(ENOMEM);
296     do {
297         errno = 0;
298         dir = readdir(c->dir);
299         if (!dir) {
300             av_freep(next);
301             return AVERROR(errno);
302         }
303     } while (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."));
304 
305     fullpath = av_append_path_component(h->filename, dir->d_name);
306     if (fullpath) {
307         struct stat st;
308         if (!lstat(fullpath, &st)) {
309             if (S_ISDIR(st.st_mode))
310                 (*next)->type = AVIO_ENTRY_DIRECTORY;
311             else if (S_ISFIFO(st.st_mode))
312                 (*next)->type = AVIO_ENTRY_NAMED_PIPE;
313             else if (S_ISCHR(st.st_mode))
314                 (*next)->type = AVIO_ENTRY_CHARACTER_DEVICE;
315             else if (S_ISBLK(st.st_mode))
316                 (*next)->type = AVIO_ENTRY_BLOCK_DEVICE;
317             else if (S_ISLNK(st.st_mode))
318                 (*next)->type = AVIO_ENTRY_SYMBOLIC_LINK;
319             else if (S_ISSOCK(st.st_mode))
320                 (*next)->type = AVIO_ENTRY_SOCKET;
321             else if (S_ISREG(st.st_mode))
322                 (*next)->type = AVIO_ENTRY_FILE;
323             else
324                 (*next)->type = AVIO_ENTRY_UNKNOWN;
325 
326             (*next)->group_id = st.st_gid;
327             (*next)->user_id = st.st_uid;
328             (*next)->size = st.st_size;
329             (*next)->filemode = st.st_mode & 0777;
330             (*next)->modification_timestamp = INT64_C(1000000) * st.st_mtime;
331             (*next)->access_timestamp =  INT64_C(1000000) * st.st_atime;
332             (*next)->status_change_timestamp = INT64_C(1000000) * st.st_ctime;
333         }
334         av_free(fullpath);
335     }
336 
337     (*next)->name = av_strdup(dir->d_name);
338     return 0;
339 #else
340     return AVERROR(ENOSYS);
341 #endif /* HAVE_LSTAT */
342 }
343 
file_close_dir(URLContext * h)344 static int file_close_dir(URLContext *h)
345 {
346 #if HAVE_LSTAT
347     FileContext *c = h->priv_data;
348     closedir(c->dir);
349     return 0;
350 #else
351     return AVERROR(ENOSYS);
352 #endif /* HAVE_LSTAT */
353 }
354 
355 const URLProtocol ff_file_protocol = {
356     .name                = "file",
357     .url_open            = file_open,
358     .url_read            = file_read,
359     .url_write           = file_write,
360     .url_seek            = file_seek,
361     .url_close           = file_close,
362     .url_get_file_handle = file_get_handle,
363     .url_check           = file_check,
364     .url_delete          = file_delete,
365     .url_move            = file_move,
366     .priv_data_size      = sizeof(FileContext),
367     .priv_data_class     = &file_class,
368     .url_open_dir        = file_open_dir,
369     .url_read_dir        = file_read_dir,
370     .url_close_dir       = file_close_dir,
371     .default_whitelist   = "file,crypto,data"
372 };
373 
374 #endif /* CONFIG_FILE_PROTOCOL */
375 
376 #if CONFIG_PIPE_PROTOCOL
377 
pipe_open(URLContext * h,const char * filename,int flags)378 static int pipe_open(URLContext *h, const char *filename, int flags)
379 {
380     FileContext *c = h->priv_data;
381     int fd;
382     char *final;
383     av_strstart(filename, "pipe:", &filename);
384 
385     fd = strtol(filename, &final, 10);
386     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
387         if (flags & AVIO_FLAG_WRITE) {
388             fd = 1;
389         } else {
390             fd = 0;
391         }
392     }
393 #if HAVE_SETMODE
394     setmode(fd, O_BINARY);
395 #endif
396     c->fd = fd;
397     h->is_streamed = 1;
398     return 0;
399 }
400 
401 const URLProtocol ff_pipe_protocol = {
402     .name                = "pipe",
403     .url_open            = pipe_open,
404     .url_read            = file_read,
405     .url_write           = file_write,
406     .url_get_file_handle = file_get_handle,
407     .url_check           = file_check,
408     .priv_data_size      = sizeof(FileContext),
409     .priv_data_class     = &pipe_class,
410     .default_whitelist   = "crypto,data"
411 };
412 
413 #endif /* CONFIG_PIPE_PROTOCOL */
414