]> git.sesse.net Git - ffmpeg/blob - libavformat/file.c
Merge commit 'f154ef1ae5b03f288dd8c025dab1884b4cb20c1a'
[ffmpeg] / libavformat / file.c
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 "avformat.h"
24 #include <fcntl.h>
25 #if HAVE_SETMODE
26 #include <io.h>
27 #endif
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <stdlib.h>
31 #include "os_support.h"
32 #include "url.h"
33
34
35 /* standard file protocol */
36
37 static int file_read(URLContext *h, unsigned char *buf, int size)
38 {
39     int fd = (intptr_t) h->priv_data;
40     int r = read(fd, buf, size);
41     return (-1 == r)?AVERROR(errno):r;
42 }
43
44 static int file_write(URLContext *h, const unsigned char *buf, int size)
45 {
46     int fd = (intptr_t) h->priv_data;
47     int r = write(fd, buf, size);
48     return (-1 == r)?AVERROR(errno):r;
49 }
50
51 static int file_get_handle(URLContext *h)
52 {
53     return (intptr_t) h->priv_data;
54 }
55
56 static int file_check(URLContext *h, int mask)
57 {
58     struct stat st;
59     int ret = stat(h->filename, &st);
60     if (ret < 0)
61         return AVERROR(errno);
62
63     ret |= st.st_mode&S_IRUSR ? mask&AVIO_FLAG_READ  : 0;
64     ret |= st.st_mode&S_IWUSR ? mask&AVIO_FLAG_WRITE : 0;
65
66     return ret;
67 }
68
69 #if CONFIG_FILE_PROTOCOL
70
71 static int file_open(URLContext *h, const char *filename, int flags)
72 {
73     int access;
74     int fd;
75     struct stat st;
76
77     av_strstart(filename, "file:", &filename);
78
79     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
80         access = O_CREAT | O_TRUNC | O_RDWR;
81     } else if (flags & AVIO_FLAG_WRITE) {
82         access = O_CREAT | O_TRUNC | O_WRONLY;
83     } else {
84         access = O_RDONLY;
85     }
86 #ifdef O_BINARY
87     access |= O_BINARY;
88 #endif
89     fd = open(filename, access, 0666);
90     if (fd == -1)
91         return AVERROR(errno);
92     h->priv_data = (void *) (intptr_t) fd;
93
94     h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
95
96     return 0;
97 }
98
99 /* XXX: use llseek */
100 static int64_t file_seek(URLContext *h, int64_t pos, int whence)
101 {
102     int fd = (intptr_t) h->priv_data;
103     if (whence == AVSEEK_SIZE) {
104         struct stat st;
105         int ret = fstat(fd, &st);
106         return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
107     }
108     return lseek(fd, pos, whence);
109 }
110
111 static int file_close(URLContext *h)
112 {
113     int fd = (intptr_t) h->priv_data;
114     return close(fd);
115 }
116
117 URLProtocol ff_file_protocol = {
118     .name                = "file",
119     .url_open            = file_open,
120     .url_read            = file_read,
121     .url_write           = file_write,
122     .url_seek            = file_seek,
123     .url_close           = file_close,
124     .url_get_file_handle = file_get_handle,
125     .url_check           = file_check,
126 };
127
128 #endif /* CONFIG_FILE_PROTOCOL */
129
130 #if CONFIG_PIPE_PROTOCOL
131
132 static int pipe_open(URLContext *h, const char *filename, int flags)
133 {
134     int fd;
135     char *final;
136     av_strstart(filename, "pipe:", &filename);
137
138     fd = strtol(filename, &final, 10);
139     if((filename == final) || *final ) {/* No digits found, or something like 10ab */
140         if (flags & AVIO_FLAG_WRITE) {
141             fd = 1;
142         } else {
143             fd = 0;
144         }
145     }
146 #if HAVE_SETMODE
147     setmode(fd, O_BINARY);
148 #endif
149     h->priv_data = (void *) (intptr_t) fd;
150     h->is_streamed = 1;
151     return 0;
152 }
153
154 URLProtocol ff_pipe_protocol = {
155     .name                = "pipe",
156     .url_open            = pipe_open,
157     .url_read            = file_read,
158     .url_write           = file_write,
159     .url_get_file_handle = file_get_handle,
160     .url_check           = file_check,
161 };
162
163 #endif /* CONFIG_PIPE_PROTOCOL */