]> git.sesse.net Git - vlc/blob - modules/access_filter/dump.c
input options whitelisting, step 2 (refs #1371)
[vlc] / modules / access_filter / dump.c
1 /*****************************************************************************
2  * dump.c
3  *****************************************************************************
4  * Copyright © 2006 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Author: Rémi Denis-Courmont <rem # videolan,org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc/vlc.h>
25
26 #include <assert.h>
27 #include <time.h>
28 #include <errno.h>
29
30 #include <vlc_access.h>
31
32 #include <vlc_charset.h>
33 #include "vlc_keys.h"
34
35 #define DEFAULT_MARGIN 32 // megabytes
36
37 #define FORCE_TEXT N_("Force use of dump module")
38 #define FORCE_LONGTEXT N_("Activate the dump module " \
39                           "even for media with fast seeking.")
40
41 #define MARGIN_TEXT N_("Maximum size of temporary file (Mb)")
42 #define MARGIN_LONGTEXT N_("The dump module will abort dumping of the media " \
43                            "if more than this much megabyte were performed.")
44
45 static int  Open (vlc_object_t *);
46 static void Close (vlc_object_t *);
47
48 vlc_module_begin ();
49     set_shortname (_("Dump"));
50     set_description (_("Dump"));
51     set_category (CAT_INPUT);
52     set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
53     set_capability ("access_filter", 0);
54     add_shortcut ("dump");
55     set_callbacks (Open, Close);
56
57     add_bool ("dump-force", VLC_FALSE, NULL, FORCE_TEXT,
58               FORCE_LONGTEXT, VLC_FALSE);
59         change_safe();
60     add_integer ("dump-margin", DEFAULT_MARGIN, NULL, MARGIN_TEXT,
61                  MARGIN_LONGTEXT, VLC_FALSE);
62         change_safe();
63
64 vlc_module_end();
65
66 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
67 static block_t *Block (access_t *access);
68 static int Seek (access_t *access, int64_t offset);
69 static int Control (access_t *access, int cmd, va_list ap);
70
71 static void Trigger (access_t *access);
72 static int KeyHandler (vlc_object_t *obj, char const *varname,
73                        vlc_value_t oldval, vlc_value_t newval, void *data);
74
75 struct access_sys_t
76 {
77     FILE *stream;
78     int64_t tmp_max;
79     int64_t dumpsize;
80 };
81
82 /**
83  * Open()
84  */
85 static int Open (vlc_object_t *obj)
86 {
87     access_t *access = (access_t*)obj;
88     access_t *src = access->p_source;
89
90     if (!var_CreateGetBool (access, "dump-force"))
91     {
92         vlc_bool_t b;
93         if ((access2_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
94         {
95             msg_Dbg (obj, "dump filter useless");
96             return VLC_EGENERIC;
97         }
98     }
99
100     if (src->pf_read != NULL)
101         access->pf_read = Read;
102     else
103         access->pf_block = Block;
104     if (src->pf_seek != NULL)
105         access->pf_seek = Seek;
106
107     access->pf_control = Control;
108     access->info = src->info;
109
110     access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
111     if (p_sys == NULL)
112         return VLC_ENOMEM;
113     memset (p_sys, 0, sizeof (*p_sys));
114
115     if ((p_sys->stream = tmpfile ()) == NULL)
116     {
117         msg_Err (access, "cannot create temporary file: %m");
118         free (p_sys);
119         return VLC_EGENERIC;
120     }
121     p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20;
122
123     var_AddCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
124
125     return VLC_SUCCESS;
126 }
127
128
129 /**
130  * Close()
131  */
132 static void Close (vlc_object_t *obj)
133 {
134     access_t *access = (access_t *)obj;
135     access_sys_t *p_sys = access->p_sys;
136
137     var_DelCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
138
139     if (p_sys->stream != NULL)
140         fclose (p_sys->stream);
141     free (p_sys);
142 }
143
144
145 static void Dump (access_t *access, const uint8_t *buffer, size_t len)
146 {
147     access_sys_t *p_sys = access->p_sys;
148     FILE *stream = p_sys->stream;
149
150     if ((stream == NULL) /* not dumping */
151      || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
152         return;
153
154     size_t needed = access->info.i_pos - p_sys->dumpsize;
155     if (len < needed)
156         return; /* gap between data and dump offset (seek too far ahead?) */
157
158     buffer += len - needed;
159     len = needed;
160
161     if (len == 0)
162         return; /* no useful data */
163
164     if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
165     {
166         msg_Dbg (access, "too much data - dump will not work");
167         goto error;
168     }
169
170     assert (len > 0);
171     if (fwrite (buffer, len, 1, stream) != 1)
172     {
173         msg_Err (access, "cannot write to file: %m");
174         goto error;
175     }
176
177     p_sys->dumpsize += len;
178     return;
179
180 error:
181     fclose (stream);
182     p_sys->stream = NULL;
183 }
184
185
186 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
187 {
188     access_t *src = access->p_source;
189
190     src->info.i_update = access->info.i_update;
191     len = src->pf_read (src, buffer, len);
192     access->info = src->info;
193
194     Dump (access, buffer, len);
195
196     return len;
197 }
198
199
200 static block_t *Block (access_t *access)
201 {
202     access_t *src = access->p_source;
203     block_t *block;
204
205     src->info.i_update = access->info.i_update;
206     block = src->pf_block (src);
207     access->info = src->info;
208
209     if ((block == NULL) || (block->i_buffer <= 0))
210         return block;
211
212     Dump (access, block->p_buffer, block->i_buffer);
213
214     return block;
215 }
216
217
218 static int Control (access_t *access, int cmd, va_list ap)
219 {
220     access_t *src = access->p_source;
221
222     return src->pf_control (src, cmd, ap);
223 }
224
225
226 static int Seek (access_t *access, int64_t offset)
227 {
228     access_t *src = access->p_source;
229     access_sys_t *p_sys = access->p_sys;
230
231     if (p_sys->tmp_max == -1)
232     {
233         msg_Err (access, "cannot seek while dumping!");
234         return VLC_EGENERIC;
235     }
236
237     if (p_sys->stream != NULL)
238         msg_Dbg (access, "seeking - dump might not work");
239
240     src->info.i_update = access->info.i_update;
241     int ret = src->pf_seek (src, offset);
242     access->info = src->info;
243     return ret;
244 }
245
246
247 #ifndef HAVE_LOCALTIME_R
248 static inline struct tm *localtime_r (const time_t *now, struct tm *res)
249 {
250     struct tm *unsafe = localtime (now);
251     /*
252      * This is not thread-safe. Blame your C library.
253      * On Win32 there SHOULD be _localtime_s instead, but of course
254      * Cygwin and Mingw32 don't know about it. You're on your own if you
255      * use this platform.
256      */
257     if (unsafe == NULL)
258         return NULL;
259
260     memcpy (res, unsafe, sizeof (*res));
261     return res;
262 }
263 #endif
264
265
266 static void Trigger (access_t *access)
267 {
268     access_sys_t *p_sys = access->p_sys;
269
270     if (p_sys->stream == NULL)
271         return; // too late
272
273     if (p_sys->tmp_max == -1)
274         return; // already triggered - should we stop? FIXME
275
276     time_t now;
277     time (&now);
278
279     struct tm t;
280     if (localtime_r (&now, &t) == NULL)
281         return; // No time, eh? Well, I'd rather not run on your computer.
282
283     if (t.tm_year > 999999999)
284         // Humanity is about 300 times older than when this was written,
285         // and there is an off-by-one in the following sprintf().
286         return;
287
288     const char *home = access->p_libvlc->psz_homedir;
289
290     /* Hmm what about the extension?? */
291     char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
292     sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
293              t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
294
295     msg_Info (access, "dumping media to \"%s\"...", filename);
296
297     FILE *newstream = utf8_fopen (filename, "wb");
298     if (newstream == NULL)
299     {
300         msg_Err (access, "cannot create dump file \"%s\": %m", filename);
301         return;
302     }
303
304     /* This might cause excessive hard drive work :( */
305     FILE *oldstream = p_sys->stream;
306     rewind (oldstream);
307
308     for (;;)
309     {
310         char buf[16384];
311         size_t len = fread (buf, 1, sizeof (buf), oldstream);
312         if (len == 0)
313         {
314             if (ferror (oldstream))
315             {
316                 msg_Err (access, "cannot read temporary file: %m");
317                 break;
318             }
319
320             /* Done with temporary file */
321             fclose (oldstream);
322             p_sys->stream = newstream;
323             p_sys->tmp_max = -1;
324             return;
325         }
326
327         if (fwrite (buf, len, 1, newstream) != 1)
328         {
329             msg_Err (access, "cannot write dump file: %m");
330             break;
331         }
332     }
333
334     /* Failed to copy temporary file */
335     fseek (oldstream, 0, SEEK_END);
336     fclose (newstream);
337     return;
338 }
339
340
341 static int KeyHandler (vlc_object_t *obj, char const *varname,
342                        vlc_value_t oldval, vlc_value_t newval, void *data)
343 {
344     access_t *access = data;
345
346     (void)oldval;
347     (void)obj;
348
349     for (struct hotkey *key = access->p_libvlc->p_hotkeys;
350          key->psz_action != NULL; key++)
351     {
352         if (key->i_key == newval.i_int)
353         {
354             if (key->i_action == ACTIONID_DUMP)
355                 Trigger ((access_t *)data);
356             break;
357         }
358     }
359
360     return VLC_SUCCESS;
361 }