Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 #include <fcntl.h>
27 #if HAVE_IO_H
28 #include <io.h>
29 #endif
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include "os_support.h"
36 #include "url.h"
37 
38 
39 /* standard file protocol */
40 
41 typedef struct FileContext {
42  const AVClass *class;
43  int fd;
44  int trunc;
45 } FileContext;
46 
47 static const AVOption file_options[] = {
48  { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
49  { NULL }
50 };
51 
52 static const AVClass file_class = {
53  .class_name = "file",
54  .item_name = av_default_item_name,
55  .option = file_options,
56  .version = LIBAVUTIL_VERSION_INT,
57 };
58 
59 static int file_read(URLContext *h, unsigned char *buf, int size)
60 {
61  FileContext *c = h->priv_data;
62  int ret = read(c->fd, buf, size);
63  return (ret == -1) ? AVERROR(errno) : ret;
64 }
65 
66 static int file_write(URLContext *h, const unsigned char *buf, int size)
67 {
68  FileContext *c = h->priv_data;
69  int ret = write(c->fd, buf, size);
70  return (ret == -1) ? AVERROR(errno) : ret;
71 }
72 
74 {
75  FileContext *c = h->priv_data;
76  return c->fd;
77 }
78 
79 static int file_check(URLContext *h, int mask)
80 {
81  const char *filename = h->filename;
82  struct stat st;
83  int ret;
84 
85  av_strstart(filename, "file:", &filename);
86 
87  ret = stat(filename, &st);
88  if (ret < 0)
89  return AVERROR(errno);
90 
91  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
92  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
93 
94  return ret;
95 }
96 
97 #if CONFIG_FILE_PROTOCOL
98 
99 static int file_open(URLContext *h, const char *filename, int flags)
100 {
101  FileContext *c = h->priv_data;
102  int access;
103  int fd;
104 
105  av_strstart(filename, "file:", &filename);
106 
107  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
108  access = O_CREAT | O_RDWR;
109  if (c->trunc)
110  access |= O_TRUNC;
111  } else if (flags & AVIO_FLAG_WRITE) {
112  access = O_CREAT | O_WRONLY;
113  if (c->trunc)
114  access |= O_TRUNC;
115  } else {
116  access = O_RDONLY;
117  }
118 #ifdef O_BINARY
119  access |= O_BINARY;
120 #endif
121  fd = avpriv_open(filename, access, 0666);
122  if (fd == -1)
123  return AVERROR(errno);
124  c->fd = fd;
125  return 0;
126 }
127 
128 /* XXX: use llseek */
129 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
130 {
131  FileContext *c = h->priv_data;
132  int64_t ret;
133 
134  if (whence == AVSEEK_SIZE) {
135  struct stat st;
136 
137  ret = fstat(c->fd, &st);
138  return ret < 0 ? AVERROR(errno) : st.st_size;
139  }
140 
141  ret = lseek(c->fd, pos, whence);
142 
143  return ret < 0 ? AVERROR(errno) : ret;
144 }
145 
146 static int file_close(URLContext *h)
147 {
148  FileContext *c = h->priv_data;
149  return close(c->fd);
150 }
151 
152 URLProtocol ff_file_protocol = {
153  .name = "file",
154  .url_open = file_open,
155  .url_read = file_read,
156  .url_write = file_write,
157  .url_seek = file_seek,
158  .url_close = file_close,
159  .url_get_file_handle = file_get_handle,
160  .url_check = file_check,
161  .priv_data_size = sizeof(FileContext),
162  .priv_data_class = &file_class,
163 };
164 
165 #endif /* CONFIG_FILE_PROTOCOL */
166 
167 #if CONFIG_PIPE_PROTOCOL
168 
169 static int pipe_open(URLContext *h, const char *filename, int flags)
170 {
171  FileContext *c = h->priv_data;
172  int fd;
173  char *final;
174  av_strstart(filename, "pipe:", &filename);
175 
176  fd = strtol(filename, &final, 10);
177  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
178  if (flags & AVIO_FLAG_WRITE) {
179  fd = 1;
180  } else {
181  fd = 0;
182  }
183  }
184 #if HAVE_SETMODE
185  setmode(fd, O_BINARY);
186 #endif
187  c->fd = fd;
188  h->is_streamed = 1;
189  return 0;
190 }
191 
192 URLProtocol ff_pipe_protocol = {
193  .name = "pipe",
194  .url_open = pipe_open,
195  .url_read = file_read,
196  .url_write = file_write,
197  .url_get_file_handle = file_get_handle,
198  .url_check = file_check,
199  .priv_data_size = sizeof(FileContext),
200 };
201 
202 #endif /* CONFIG_PIPE_PROTOCOL */
static int file_read(URLContext *h, unsigned char *buf, int size)
Definition: file.c:59
int size
AVOption.
Definition: opt.h:234
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:48
#define AVIO_FLAG_READ
read-only
Definition: avio.h:294
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:295
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:38
AVOptions.
miscellaneous OS support macros and functions.
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:71
static int flags
Definition: log.c:44
static int file_check(URLContext *h, int mask)
Definition: file.c:79
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
int trunc
Definition: file.c:44
static const uint16_t mask[17]
Definition: lzw.c:38
#define AVERROR(e)
Definition: error.h:43
common internal API header
static av_always_inline av_const double trunc(double x)
Definition: libm.h:165
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:52
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
Definition: url.h:41
Describe the class of an AVClass context structure.
Definition: log.h:33
void * priv_data
Definition: url.h:44
static const AVClass file_class
Definition: file.c:52
static const AVOption file_options[]
Definition: file.c:47
const char * name
Definition: url.h:54
int fd
Definition: file.c:43
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:32
Main libavformat public API header.
static int file_get_handle(URLContext *h)
Definition: file.c:73
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:190
unbuffered private I/O API
static int file_write(URLContext *h, const unsigned char *buf, int size)
Definition: file.c:66