libavformat/file.c
Go to the documentation of this file.
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/opt.h"
24 #include "avformat.h"
25 #include <fcntl.h>
26 #if HAVE_IO_H
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <sys/stat.h>
33 #include <stdlib.h>
34 #include "os_support.h"
35 #include "url.h"
36 
37 /* Some systems may not have S_ISFIFO */
38 #ifndef S_ISFIFO
39 # ifdef S_IFIFO
40 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
41 # else
42 # define S_ISFIFO(m) 0
43 # endif
44 #endif
45 
46 /* standard file protocol */
47 
48 typedef struct FileContext {
49  const AVClass *class;
50  int fd;
51  int trunc;
52 } FileContext;
53 
54 static const AVOption file_options[] = {
55  { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
56  { NULL }
57 };
58 
59 static const AVClass file_class = {
60  .class_name = "file",
61  .item_name = av_default_item_name,
62  .option = file_options,
63  .version = LIBAVUTIL_VERSION_INT,
64 };
65 
66 static int file_read(URLContext *h, unsigned char *buf, int size)
67 {
68  FileContext *c = h->priv_data;
69  int r = read(c->fd, buf, size);
70  return (-1 == r)?AVERROR(errno):r;
71 }
72 
73 static int file_write(URLContext *h, const unsigned char *buf, int size)
74 {
75  FileContext *c = h->priv_data;
76  int r = write(c->fd, buf, size);
77  return (-1 == r)?AVERROR(errno):r;
78 }
79 
81 {
82  FileContext *c = h->priv_data;
83  return c->fd;
84 }
85 
86 static int file_check(URLContext *h, int mask)
87 {
88 #if HAVE_ACCESS && defined(R_OK)
89  int ret = 0;
90  if (access(h->filename, F_OK) < 0)
91  return AVERROR(errno);
92  if (mask&AVIO_FLAG_READ)
93  if (access(h->filename, R_OK) >= 0)
94  ret |= AVIO_FLAG_READ;
95  if (mask&AVIO_FLAG_WRITE)
96  if (access(h->filename, W_OK) >= 0)
97  ret |= AVIO_FLAG_WRITE;
98 #else
99  struct stat st;
100  int ret = stat(h->filename, &st);
101  if (ret < 0)
102  return AVERROR(errno);
103 
104  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
105  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
106 #endif
107  return ret;
108 }
109 
110 #if CONFIG_FILE_PROTOCOL
111 
112 static int file_open(URLContext *h, const char *filename, int flags)
113 {
114  FileContext *c = h->priv_data;
115  int access;
116  int fd;
117  struct stat st;
118 
119  av_strstart(filename, "file:", &filename);
120 
121  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
122  access = O_CREAT | O_RDWR;
123  if (c->trunc)
124  access |= O_TRUNC;
125  } else if (flags & AVIO_FLAG_WRITE) {
126  access = O_CREAT | O_WRONLY;
127  if (c->trunc)
128  access |= O_TRUNC;
129  } else {
130  access = O_RDONLY;
131  }
132 #ifdef O_BINARY
133  access |= O_BINARY;
134 #endif
135  fd = open(filename, access, 0666);
136  if (fd == -1)
137  return AVERROR(errno);
138  c->fd = fd;
139 
140  h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
141 
142  return 0;
143 }
144 
145 /* XXX: use llseek */
146 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
147 {
148  FileContext *c = h->priv_data;
149  int64_t ret;
150 
151  if (whence == AVSEEK_SIZE) {
152  struct stat st;
153  ret = fstat(c->fd, &st);
154  return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
155  }
156 
157  ret = lseek(c->fd, pos, whence);
158 
159  return ret < 0 ? AVERROR(errno) : ret;
160 }
161 
162 static int file_close(URLContext *h)
163 {
164  FileContext *c = h->priv_data;
165  return close(c->fd);
166 }
167 
168 URLProtocol ff_file_protocol = {
169  .name = "file",
170  .url_open = file_open,
171  .url_read = file_read,
172  .url_write = file_write,
173  .url_seek = file_seek,
174  .url_close = file_close,
175  .url_get_file_handle = file_get_handle,
176  .url_check = file_check,
177  .priv_data_size = sizeof(FileContext),
178  .priv_data_class = &file_class,
179 };
180 
181 #endif /* CONFIG_FILE_PROTOCOL */
182 
183 #if CONFIG_PIPE_PROTOCOL
184 
185 static int pipe_open(URLContext *h, const char *filename, int flags)
186 {
187  FileContext *c = h->priv_data;
188  int fd;
189  char *final;
190  av_strstart(filename, "pipe:", &filename);
191 
192  fd = strtol(filename, &final, 10);
193  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
194  if (flags & AVIO_FLAG_WRITE) {
195  fd = 1;
196  } else {
197  fd = 0;
198  }
199  }
200 #if HAVE_SETMODE
201  setmode(fd, O_BINARY);
202 #endif
203  c->fd = fd;
204  h->is_streamed = 1;
205  return 0;
206 }
207 
208 URLProtocol ff_pipe_protocol = {
209  .name = "pipe",
210  .url_open = pipe_open,
211  .url_read = file_read,
212  .url_write = file_write,
213  .url_get_file_handle = file_get_handle,
214  .url_check = file_check,
215  .priv_data_size = sizeof(FileContext),
216 };
217 
218 #endif /* CONFIG_PIPE_PROTOCOL */
static int file_read(URLContext *h, unsigned char *buf, int size)
AVOption.
Definition: opt.h:251
av_default_item_name
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:48
#define AVIO_FLAG_READ
read-only
Definition: avio.h:332
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:333
About Git write access
Definition: git-howto.txt:5
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
AVOptions.
miscellaneous OS support macros and functions.
static int file_check(URLContext *h, int mask)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:281
static const uint16_t mask[17]
Definition: lzw.c:37
#define S_ISFIFO(m)
const char * r
Definition: vf_curves.c:94
struct FileContext FileContext
int size
ret
Definition: avfilter.c:821
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define O_BINARY
NULL
Definition: eval.c:55
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
void * buf
Definition: avisynth_c.h:594
Definition: url.h:41
Describe the class of an AVClass context structure.
Definition: log.h:50
void * priv_data
Definition: url.h:44
static const AVClass file_class
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
static const AVOption file_options[]
const char * name
Definition: url.h:55
static int flags
Definition: cpu.c:23
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:33
Main libavformat public API header.
static int file_get_handle(URLContext *h)
static double c[64]
char * filename
specified URL
Definition: url.h:45
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition: avio.h:222
unbuffered private I/O API
static int file_write(URLContext *h, const unsigned char *buf, int size)