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  int follow;
46 } FileContext;
47 
48 static const AVOption file_options[] = {
49  { "truncate", "Truncate existing files on write", offsetof(FileContext, trunc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
50  { "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 },
51  { NULL }
52 };
53 
54 static const AVClass file_class = {
55  .class_name = "file",
56  .item_name = av_default_item_name,
57  .option = file_options,
58  .version = LIBAVUTIL_VERSION_INT,
59 };
60 
61 static int file_read(URLContext *h, unsigned char *buf, int size)
62 {
63  FileContext *c = h->priv_data;
64  int ret = read(c->fd, buf, size);
65  if (ret == 0 && c->follow)
66  return AVERROR(EAGAIN);
67  return (ret == -1) ? AVERROR(errno) : ret;
68 }
69 
70 static int file_write(URLContext *h, const unsigned char *buf, int size)
71 {
72  FileContext *c = h->priv_data;
73  int ret = write(c->fd, buf, size);
74  return (ret == -1) ? AVERROR(errno) : ret;
75 }
76 
78 {
79  FileContext *c = h->priv_data;
80  return c->fd;
81 }
82 
83 static int file_check(URLContext *h, int mask)
84 {
85  const char *filename = h->filename;
86  struct stat st;
87  int ret;
88 
89  av_strstart(filename, "file:", &filename);
90 
91  ret = stat(filename, &st);
92  if (ret < 0)
93  return AVERROR(errno);
94 
95  ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ : 0;
96  ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
97 
98  return ret;
99 }
100 
101 #if CONFIG_FILE_PROTOCOL
102 
103 static int file_open(URLContext *h, const char *filename, int flags)
104 {
105  FileContext *c = h->priv_data;
106  int access;
107  int fd;
108 
109  av_strstart(filename, "file:", &filename);
110 
111  if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
112  access = O_CREAT | O_RDWR;
113  if (c->trunc)
114  access |= O_TRUNC;
115  } else if (flags & AVIO_FLAG_WRITE) {
116  access = O_CREAT | O_WRONLY;
117  if (c->trunc)
118  access |= O_TRUNC;
119  } else {
120  access = O_RDONLY;
121  }
122 #ifdef O_BINARY
123  access |= O_BINARY;
124 #endif
125  fd = avpriv_open(filename, access, 0666);
126  if (fd == -1)
127  return AVERROR(errno);
128  c->fd = fd;
129  return 0;
130 }
131 
132 /* XXX: use llseek */
133 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
134 {
135  FileContext *c = h->priv_data;
136  int64_t ret;
137 
138  if (whence == AVSEEK_SIZE) {
139  struct stat st;
140 
141  ret = fstat(c->fd, &st);
142  return ret < 0 ? AVERROR(errno) : st.st_size;
143  }
144 
145  ret = lseek(c->fd, pos, whence);
146 
147  return ret < 0 ? AVERROR(errno) : ret;
148 }
149 
150 static int file_close(URLContext *h)
151 {
152  FileContext *c = h->priv_data;
153  return close(c->fd);
154 }
155 
157  .name = "file",
158  .url_open = file_open,
159  .url_read = file_read,
160  .url_write = file_write,
161  .url_seek = file_seek,
162  .url_close = file_close,
163  .url_get_file_handle = file_get_handle,
164  .url_check = file_check,
165  .priv_data_size = sizeof(FileContext),
166  .priv_data_class = &file_class,
167 };
168 
169 #endif /* CONFIG_FILE_PROTOCOL */
170 
171 #if CONFIG_PIPE_PROTOCOL
172 
173 static int pipe_open(URLContext *h, const char *filename, int flags)
174 {
175  FileContext *c = h->priv_data;
176  int fd;
177  char *final;
178  av_strstart(filename, "pipe:", &filename);
179 
180  fd = strtol(filename, &final, 10);
181  if((filename == final) || *final ) {/* No digits found, or something like 10ab */
182  if (flags & AVIO_FLAG_WRITE) {
183  fd = 1;
184  } else {
185  fd = 0;
186  }
187  }
188 #if HAVE_SETMODE
189  setmode(fd, O_BINARY);
190 #endif
191  c->fd = fd;
192  h->is_streamed = 1;
193  return 0;
194 }
195 
197  .name = "pipe",
198  .url_open = pipe_open,
199  .url_read = file_read,
200  .url_write = file_write,
201  .url_get_file_handle = file_get_handle,
202  .url_check = file_check,
203  .priv_data_size = sizeof(FileContext),
204 };
205 
206 #endif /* CONFIG_PIPE_PROTOCOL */
static int file_read(URLContext *h, unsigned char *buf, int size)
Definition: file.c:61
int size
AVOption.
Definition: opt.h:234
int is_streamed
true if streamed (no seek possible), default = false
Definition: url.h:49
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:369
int follow
Definition: file.c:45
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:39
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:66
static int flags
Definition: log.c:50
static int file_check(URLContext *h, int mask)
Definition: file.c:83
#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
const URLProtocol ff_pipe_protocol
const URLProtocol ff_file_protocol
common internal API header
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
av_default_item_name
Definition: dnxhdenc.c:55
Definition: url.h:38
Describe the class of an AVClass context structure.
Definition: log.h:34
void * priv_data
Definition: url.h:45
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:265
static const AVClass file_class
Definition: file.c:54
static const AVOption file_options[]
Definition: file.c:48
const char * name
Definition: url.h:56
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:77
char * filename
specified URL
Definition: url.h:46
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:266
unbuffered private I/O API
static int file_write(URLContext *h, const unsigned char *buf, int size)
Definition: file.c:70