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  struct stat st;
82  int ret = stat(h->filename, &st);
83  if (ret < 0)
84  return AVERROR(errno);
85 
86  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
87  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
88 
89  return ret;
90 }
91 
92 #if CONFIG_FILE_PROTOCOL
93 
94 static int file_open(URLContext *h, const char *filename, int flags)
95 {
96  FileContext *c = h->priv_data;
97  int access;
98  int fd;
99 
100  av_strstart(filename, "file:", &filename);
101 
102  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
103  access = O_CREAT | O_RDWR;
104  if (c->trunc)
105  access |= O_TRUNC;
106  } else if (flags & AVIO_FLAG_WRITE) {
107  access = O_CREAT | O_WRONLY;
108  if (c->trunc)
109  access |= O_TRUNC;
110  } else {
111  access = O_RDONLY;
112  }
113 #ifdef O_BINARY
114  access |= O_BINARY;
115 #endif
116  fd = avpriv_open(filename, access, 0666);
117  if (fd == -1)
118  return AVERROR(errno);
119  c->fd = fd;
120  return 0;
121 }
122 
123 /* XXX: use llseek */
124 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
125 {
126  FileContext *c = h->priv_data;
127  int64_t ret;
128 
129  if (whence == AVSEEK_SIZE) {
130  struct stat st;
131 
132  ret = fstat(c->fd, &st);
133  return ret < 0 ? AVERROR(errno) : st.st_size;
134  }
135 
136  ret = lseek(c->fd, pos, whence);
137 
138  return ret < 0 ? AVERROR(errno) : ret;
139 }
140 
141 static int file_close(URLContext *h)
142 {
143  FileContext *c = h->priv_data;
144  return close(c->fd);
145 }
146 
147 URLProtocol ff_file_protocol = {
148  .name = "file",
149  .url_open = file_open,
150  .url_read = file_read,
151  .url_write = file_write,
152  .url_seek = file_seek,
153  .url_close = file_close,
154  .url_get_file_handle = file_get_handle,
155  .url_check = file_check,
156  .priv_data_size = sizeof(FileContext),
157  .priv_data_class = &file_class,
158 };
159 
160 #endif /* CONFIG_FILE_PROTOCOL */
161 
162 #if CONFIG_PIPE_PROTOCOL
163 
164 static int pipe_open(URLContext *h, const char *filename, int flags)
165 {
166  FileContext *c = h->priv_data;
167  int fd;
168  char *final;
169  av_strstart(filename, "pipe:", &filename);
170 
171  fd = strtol(filename, &final, 10);
172  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
173  if (flags & AVIO_FLAG_WRITE) {
174  fd = 1;
175  } else {
176  fd = 0;
177  }
178  }
179 #if HAVE_SETMODE
180  setmode(fd, O_BINARY);
181 #endif
182  c->fd = fd;
183  h->is_streamed = 1;
184  return 0;
185 }
186 
187 URLProtocol ff_pipe_protocol = {
188  .name = "pipe",
189  .url_open = pipe_open,
190  .url_read = file_read,
191  .url_write = file_write,
192  .url_get_file_handle = file_get_handle,
193  .url_check = file_check,
194  .priv_data_size = sizeof(FileContext),
195 };
196 
197 #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:292
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
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