annotate stream/stream_file.c @ 36583:322ffa4a22d2

coverity.sh: Avoid submitting broken builds to coverity.
author reimar
date Mon, 20 Jan 2014 22:32:41 +0000
parents 5c8339df2496
children cb25e73ac822
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30426
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
1 /*
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
2 * This file is part of MPlayer.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
3 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
5 * it under the terms of the GNU General Public License as published by
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
7 * (at your option) any later version.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
8 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
12 * GNU General Public License for more details.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
13 *
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
14 * You should have received a copy of the GNU General Public License along
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ce0122361a39 Add license header to all files missing it in the stream subdirectory.
diego
parents: 29920
diff changeset
17 */
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
18
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
19 #include "config.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
20
16748
ec02252fbaa6 we need stdio.h for SEEK_SET on mingw, patch by Zuxy <zuxy.meng at gmail.com>
faust3
parents: 15461
diff changeset
21 #include <stdio.h>
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
22 #include <sys/types.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
23 #include <sys/stat.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
24 #include <fcntl.h>
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
25 #include <unistd.h>
33759
3e37890b6261 Include io.h for setmode().
reimar
parents: 32794
diff changeset
26 #if HAVE_SETMODE
3e37890b6261 Include io.h for setmode().
reimar
parents: 32794
diff changeset
27 #include <io.h>
3e37890b6261 Include io.h for setmode().
reimar
parents: 32794
diff changeset
28 #endif
35868
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
29 #ifdef __MINGW32__
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
30 #include <windows.h>
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
31 #include <share.h>
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
32 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
33
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
34 #include "mp_msg.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
35 #include "stream.h"
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
36 #include "help_mp.h"
17012
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16750
diff changeset
37 #include "m_option.h"
6ff3379a0862 Unify include path handling, -I.. is in CFLAGS.
diego
parents: 16750
diff changeset
38 #include "m_struct.h"
36253
5c8339df2496 Check if path is too long
komh
parents: 35885
diff changeset
39 #include "osdep/osdep.h"
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
40
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
41 static struct stream_priv_s {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
42 char* filename;
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
43 char *filename2;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
44 } stream_priv_dflts = {
15452
de27bb6ff3ec make file:// prefix work
nicodvb
parents: 12921
diff changeset
45 NULL, NULL
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
46 };
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
47
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
48 #define ST_OFF(f) M_ST_OFF(struct stream_priv_s,f)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
49 /// URL definition
25242
371a40dcc1cc stream_opts arrays should be const
reimar
parents: 25211
diff changeset
50 static const m_option_t stream_opts_fields[] = {
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
51 {"string", ST_OFF(filename), CONF_TYPE_STRING, 0, 0 ,0, NULL},
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
52 {"filename", ST_OFF(filename2), CONF_TYPE_STRING, 0, 0 ,0, NULL},
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
53 { NULL, NULL, 0, 0, 0, 0, NULL }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
54 };
25691
68015115f63a stream_opts should be const
reimar
parents: 25242
diff changeset
55 static const struct m_struct_st stream_opts = {
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
56 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
57 sizeof(struct stream_priv_s),
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
58 &stream_priv_dflts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
59 stream_opts_fields
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 27727
diff changeset
60 };
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
61
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
62 static int fill_buffer(stream_t *s, char* buffer, int max_len){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
63 int r = read(s->fd,buffer,max_len);
35018
5f3501f7f4d9 Explicitly signal EOF when reaching the end of a file/pipe.
reimar
parents: 33759
diff changeset
64 // We are certain this is EOF, do not retry
5f3501f7f4d9 Explicitly signal EOF when reaching the end of a file/pipe.
reimar
parents: 33759
diff changeset
65 if (max_len && r == 0) s->eof = 1;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
66 return (r <= 0) ? -1 : r;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
67 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
68
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
69 static int write_buffer(stream_t *s, char* buffer, int len) {
32794
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
70 int r;
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
71 int wr = 0;
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
72 while (wr < len) {
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
73 r = write(s->fd,buffer,len);
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
74 if (r <= 0)
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
75 return -1;
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
76 wr += r;
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
77 buffer += r;
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
78 }
77d81e27a176 Fix stream_write_buffer to make sure all requested bytes are written
ranma
parents: 32529
diff changeset
79 return len;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
80 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
81
35885
3389262720da Fix previous commit, off_t must be replaced by int64_t
reimar
parents: 35881
diff changeset
82 static int seek(stream_t *s, int64_t newpos) {
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
83 s->pos = newpos;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
84 if(lseek(s->fd,s->pos,SEEK_SET)<0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
85 s->eof=1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
86 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
87 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
88 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
89 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
90
35885
3389262720da Fix previous commit, off_t must be replaced by int64_t
reimar
parents: 35881
diff changeset
91 static int seek_forward(stream_t *s, int64_t newpos) {
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
92 if(newpos<s->pos){
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
93 mp_msg(MSGT_STREAM,MSGL_INFO,"Cannot seek backward in linear streams!\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
94 return 0;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
95 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
96 while(s->pos<newpos){
12018
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
97 int len=s->fill_buffer(s,s->buffer,STREAM_BUFFER_SIZE);
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
98 if(len<=0){ s->eof=1; s->buf_pos=s->buf_len=0; break; } // EOF
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
99 s->buf_pos=0;
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
100 s->buf_len=len;
2c2fb4457982 Seek in HTTP streams and stdin seek fixes
rtognimp
parents: 10608
diff changeset
101 s->pos+=len;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
102 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
103 return 1;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
104 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
105
21658
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
106 static int control(stream_t *s, int cmd, void *arg) {
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
107 switch(cmd) {
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
108 case STREAM_CTRL_GET_SIZE: {
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
109 off_t size;
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
110
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
111 size = lseek(s->fd, 0, SEEK_END);
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
112 lseek(s->fd, s->pos, SEEK_SET);
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
113 if(size != (off_t)-1) {
35266
ceb148e1fe31 Change STREAM_CTRL_GET_SIZE argument type from off_t to
reimar
parents: 35018
diff changeset
114 *(uint64_t*)arg = size;
21658
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
115 return 1;
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
116 }
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
117 }
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
118 }
24257
d261f5109660 cosmetics: typo fix UNSUPORTED --> UNSUPPORTED
diego
parents: 22451
diff changeset
119 return STREAM_UNSUPPORTED;
21658
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
120 }
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
121
35868
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
122 #ifdef __MINGW32__
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
123 static int win32_open(const char *fname, int m, int omode)
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
124 {
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
125 int cnt;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
126 int fd = -1;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
127 wchar_t fname_w[MAX_PATH];
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
128 int WINAPI (*mb2wc)(UINT, DWORD, LPCSTR, int, LPWSTR, int) = NULL;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
129 HMODULE kernel32 = GetModuleHandle("Kernel32.dll");
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
130 if (!kernel32) goto fallback;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
131 mb2wc = GetProcAddress(kernel32, "MultiByteToWideChar");
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
132 if (!mb2wc) goto fallback;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
133 cnt = mb2wc(CP_UTF8, MB_ERR_INVALID_CHARS, fname, -1, fname_w, sizeof(fname_w) / sizeof(*fname_w));
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
134 if (cnt <= 0) goto fallback;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
135 fd = _wsopen(fname_w, m, SH_DENYNO, omode);
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
136 if (fd != -1 || (m & O_CREAT))
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
137 return fd;
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
138
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
139 fallback:
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
140 return _sopen(fname, m, SH_DENYNO, omode);
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
141 }
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
142 #endif
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
143
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
144 static int open_f(stream_t *stream,int mode, void* opts, int* file_format) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
145 int f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
146 mode_t m = 0;
35885
3389262720da Fix previous commit, off_t must be replaced by int64_t
reimar
parents: 35881
diff changeset
147 int64_t len;
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
148 unsigned char *filename;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
149 struct stream_priv_s* p = (struct stream_priv_s*)opts;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
150
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
151 if(mode == STREAM_READ)
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
152 m = O_RDONLY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
153 else if(mode == STREAM_WRITE)
22451
86f7b9cab33b truncate mencoder's output file if it exists, instead of overwriting just part of it.
lorenm
parents: 21705
diff changeset
154 m = O_RDWR|O_CREAT|O_TRUNC;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
155 else {
10608
2cae82f2ab02 Spelling police:
diego
parents: 9849
diff changeset
156 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] Unknown open mode %d\n",mode);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
157 m_struct_free(&stream_opts,opts);
24257
d261f5109660 cosmetics: typo fix UNSUPORTED --> UNSUPPORTED
diego
parents: 22451
diff changeset
158 return STREAM_UNSUPPORTED;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
159 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
160
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
161 if(p->filename)
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
162 filename = p->filename;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
163 else if(p->filename2)
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
164 filename = p->filename2;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
165 else
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
166 filename = NULL;
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
167 if(!filename) {
9849
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
168 mp_msg(MSGT_OPEN,MSGL_ERR, "[file] No filename\n");
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
169 m_struct_free(&stream_opts,opts);
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
170 return STREAM_ERROR;
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
171 }
5b649442fe72 Check that we really got a filename.
albeu
parents: 9827
diff changeset
172
30608
c05fbacce55f Replace platform preprocessor check by HAVE_DOS_PATHS.
komh
parents: 30426
diff changeset
173 #if HAVE_DOS_PATHS
26005
620976e4f865 Add support for DOS-style file:///x:/path paths.
diego
parents: 25691
diff changeset
174 // extract '/' from '/x:/path'
620976e4f865 Add support for DOS-style file:///x:/path paths.
diego
parents: 25691
diff changeset
175 if( filename[ 0 ] == '/' && filename[ 1 ] && filename[ 2 ] == ':' )
620976e4f865 Add support for DOS-style file:///x:/path paths.
diego
parents: 25691
diff changeset
176 filename++;
620976e4f865 Add support for DOS-style file:///x:/path paths.
diego
parents: 25691
diff changeset
177 #endif
620976e4f865 Add support for DOS-style file:///x:/path paths.
diego
parents: 25691
diff changeset
178
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
179 m |= O_BINARY;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
180
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
181 if(!strcmp(filename,"-")){
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
182 if(mode == STREAM_READ) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
183 // read from stdin
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
184 mp_msg(MSGT_OPEN,MSGL_INFO,MSGTR_ReadSTDIN);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
185 f=0; // 0=stdin
30787
e10ba171be06 Define HAVE_SETMODE conditionally, and use it in stream/stream_file.c instead
komh
parents: 30608
diff changeset
186 #if HAVE_SETMODE
e10ba171be06 Define HAVE_SETMODE conditionally, and use it in stream/stream_file.c instead
komh
parents: 30608
diff changeset
187 setmode(fileno(stdin),O_BINARY);
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
188 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
189 } else {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
190 mp_msg(MSGT_OPEN,MSGL_INFO,"Writing to stdout\n");
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
191 f=1;
30787
e10ba171be06 Define HAVE_SETMODE conditionally, and use it in stream/stream_file.c instead
komh
parents: 30608
diff changeset
192 #if HAVE_SETMODE
e10ba171be06 Define HAVE_SETMODE conditionally, and use it in stream/stream_file.c instead
komh
parents: 30608
diff changeset
193 setmode(fileno(stdout),O_BINARY);
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
194 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
195 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
196 } else {
21705
829b1f30a07f fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents: 21673
diff changeset
197 mode_t openmode = S_IRUSR|S_IWUSR;
35868
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
198 #ifdef __MINGW32__
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
199 f = win32_open(filename, m, openmode);
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
200 #else
21705
829b1f30a07f fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents: 21673
diff changeset
201 openmode |= S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
35868
3edaed3c1d60 Win32: support file names with characters outside system code-page
reimar
parents: 35266
diff changeset
202 f=open(filename,m, openmode);
21705
829b1f30a07f fix compilation on the most delicious variant of unix (mingw) that lacks S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
nicodvb
parents: 21673
diff changeset
203 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
204 if(f<0) {
15461
7c272bfba96f fixed file:// syntax using newly introduced -string- urlpart
nicodvb
parents: 15452
diff changeset
205 mp_msg(MSGT_OPEN,MSGL_ERR,MSGTR_FileNotFound,filename);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
206 m_struct_free(&stream_opts,opts);
9827
d179dbc45935 Little fix.
albeu
parents: 9794
diff changeset
207 return STREAM_ERROR;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
208 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
209 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
210
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
211 len=lseek(f,0,SEEK_END); lseek(f,0,SEEK_SET);
12921
1e42749917c8 mingw stdin fixes
faust3
parents: 12018
diff changeset
212 #ifdef __MINGW32__
31883
b14ca9b6b6d4 Simplify and document MinGW stdin hack.
reimar
parents: 30808
diff changeset
213 // seeks on stdin incorrectly succeed on MinGW
b14ca9b6b6d4 Simplify and document MinGW stdin hack.
reimar
parents: 30808
diff changeset
214 if(f==0)
b14ca9b6b6d4 Simplify and document MinGW stdin hack.
reimar
parents: 30808
diff changeset
215 len = -1;
b14ca9b6b6d4 Simplify and document MinGW stdin hack.
reimar
parents: 30808
diff changeset
216 #endif
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
217 if(len == -1) {
21656
32c50eb3ff18 in STREAM_WRITE mode open the stream with O_RDWR|O_CREAT, S_IRUSR|S_IWUSR and disable seek_forward for pipes
nicodvb
parents: 19271
diff changeset
218 if(mode == STREAM_READ) stream->seek = seek_forward;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
219 stream->type = STREAMTYPE_STREAM; // Must be move to STREAMTYPE_FILE
29920
4f740437ed2b Finally rename the STREAM_SEEK define to MP_STREAM_SEEK, there are just too many
reimar
parents: 29304
diff changeset
220 stream->flags |= MP_STREAM_SEEK_FW;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
221 } else if(len >= 0) {
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
222 stream->seek = seek;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
223 stream->end_pos = len;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
224 stream->type = STREAMTYPE_FILE;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
225 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
226
16750
0a31740dd5e6 Use PRI?64 defines as format strings for 64 bit variables.
reimar
parents: 16748
diff changeset
227 mp_msg(MSGT_OPEN,MSGL_V,"[file] File size is %"PRId64" bytes\n", (int64_t)len);
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
228
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
229 stream->fd = f;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
230 stream->fill_buffer = fill_buffer;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
231 stream->write_buffer = write_buffer;
21658
110286e8bbec implemented STREAM_CTRL_GET_SIZE
nicodvb
parents: 21656
diff changeset
232 stream->control = control;
32529
0624fa95a2aa Make the file protocol read up to 64 kB at once when the cache is used,
reimar
parents: 31883
diff changeset
233 stream->read_chunk = 64*1024;
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
234
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
235 m_struct_free(&stream_opts,opts);
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
236 return STREAM_OK;
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
237 }
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
238
25211
c1d17bd6683c Mark all stream_info_t as const
reimar
parents: 24257
diff changeset
239 const stream_info_t stream_info_file = {
9794
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
240 "File",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
241 "file",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
242 "Albeu",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
243 "based on the code from ??? (probably Arpi)",
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
244 open_f,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
245 { "file", "", NULL },
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
246 &stream_opts,
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
247 1 // Urls are an option string
f67d87b2d3c7 Stream modularization, the first step.
albeu
parents:
diff changeset
248 };