]> git.sesse.net Git - vlc/blob - modules/access/directory.c
XCB/screen: fix config item descriptions
[vlc] / modules / access / directory.c
1 /*****************************************************************************
2  * directory.c: expands a directory (directory: access plug-in)
3  *****************************************************************************
4  * Copyright (C) 2002-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8  *          RĂ©mi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include "fs.h"
35 #include <vlc_access.h>
36
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 #   include <sys/stat.h>
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #   include <fcntl.h>
45 #elif defined( WIN32 ) && !defined( UNDER_CE )
46 #   include <io.h>
47 #endif
48
49 #ifdef __sun__
50 static inline int dirfd (DIR *dir)
51 {
52     return dir->dd_fd;
53 }
54 #endif
55
56 #include <vlc_fs.h>
57 #include <vlc_url.h>
58 #include <vlc_strings.h>
59
60 enum
61 {
62     MODE_NONE,
63     MODE_COLLAPSE,
64     MODE_EXPAND,
65 };
66
67 typedef struct directory_t directory_t;
68 struct directory_t
69 {
70     directory_t *parent;
71     DIR         *handle;
72     char        *uri;
73     char       **filev;
74     int          filec, i;
75 #ifdef HAVE_OPENAT
76     dev_t        device;
77     ino_t        inode;
78 #else
79     char         *path;
80 #endif
81 };
82
83 struct access_sys_t
84 {
85     directory_t *current;
86     char *ignored_exts;
87     char mode;
88     bool header;
89     int i_item_count;
90     char *xspf_ext;
91 };
92
93 /* Select non-hidden files only */
94 static int visible (const char *name)
95 {
96     return name[0] != '.';
97 }
98
99 static int collate (const char **a, const char **b)
100 {
101 #ifdef HAVE_STRCOLL
102     return strcoll (*a, *b);
103 #else
104     return strcmp  (*a, *b);
105 #endif
106 }
107
108 /*****************************************************************************
109  * Open: open the directory
110  *****************************************************************************/
111 int DirOpen( vlc_object_t *p_this )
112 {
113     access_t *p_access = (access_t*)p_this;
114
115     if( !p_access->psz_filepath )
116         return VLC_EGENERIC;
117
118     DIR *handle = vlc_opendir (p_access->psz_filepath);
119     if (handle == NULL)
120         return VLC_EGENERIC;
121
122     return DirInit (p_access, handle);
123 }
124
125 int DirInit (access_t *p_access, DIR *handle)
126 {
127     access_sys_t *p_sys = malloc (sizeof (*p_sys));
128     if (unlikely(p_sys == NULL))
129         goto error;
130
131     char *uri;
132     if (!strcmp (p_access->psz_access, "fd"))
133     {
134         if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
135             uri = NULL;
136     }
137     else
138         uri = make_URI (p_access->psz_filepath, "file");
139     if (unlikely(uri == NULL))
140         goto error;
141
142     /* "Open" the base directory */
143     directory_t *root = malloc (sizeof (*root));
144     if (unlikely(root == NULL))
145     {
146         free (uri);
147         goto error;
148     }
149     root->parent = NULL;
150     root->handle = handle;
151     root->uri = uri;
152     root->filec = vlc_loaddir (handle, &root->filev, visible, collate);
153     if (root->filec < 0)
154         root->filev = NULL;
155     root->i = 0;
156 #ifdef HAVE_OPENAT
157     struct stat st;
158     if (fstat (dirfd (handle), &st))
159     {
160         free (root);
161         free (uri);
162         goto error;
163     }
164     root->device = st.st_dev;
165     root->inode = st.st_ino;
166 #else
167     root->path = strdup (p_access->psz_filepath);
168 #endif
169
170     p_access->p_sys = p_sys;
171     p_sys->current = root;
172     p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
173     p_sys->header = true;
174     p_sys->i_item_count = 0;
175     p_sys->xspf_ext = strdup ("");
176
177     /* Handle mode */
178     char *psz = var_InheritString (p_access, "recursive");
179     if (psz == NULL || !strcasecmp (psz, "none"))
180         p_sys->mode = MODE_NONE;
181     else if( !strcasecmp( psz, "collapse" )  )
182         p_sys->mode = MODE_COLLAPSE;
183     else
184         p_sys->mode = MODE_EXPAND;
185     free( psz );
186
187     access_InitFields(p_access);
188     p_access->pf_read  = NULL;
189     p_access->pf_block = DirBlock;
190     p_access->pf_seek  = NULL;
191     p_access->pf_control= DirControl;
192     free (p_access->psz_demux);
193     p_access->psz_demux = strdup ("xspf-open");
194
195     return VLC_SUCCESS;
196
197 error:
198     closedir (handle);
199     free (p_sys);
200     return VLC_EGENERIC;
201 }
202
203 /*****************************************************************************
204  * Close: close the target
205  *****************************************************************************/
206 void DirClose( vlc_object_t * p_this )
207 {
208     access_t *p_access = (access_t*)p_this;
209     access_sys_t *p_sys = p_access->p_sys;
210
211     while (p_sys->current)
212     {
213         directory_t *current = p_sys->current;
214
215         p_sys->current = current->parent;
216         closedir (current->handle);
217         free (current->uri);
218         while (current->i < current->filec)
219             free (current->filev[current->i++]);
220         free (current->filev);
221 #ifndef HAVE_OPENAT
222         free (current->path);
223 #endif
224         free (current);
225     }
226
227     free (p_sys->xspf_ext);
228     free (p_sys->ignored_exts);
229     free (p_sys);
230 }
231
232 #ifdef HAVE_OPENAT
233 /* Detect directories that recurse into themselves. */
234 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
235 {
236     while (dir != NULL)
237     {
238         if ((dir->device == dev) && (dir->inode == inode))
239             return true;
240         dir = dir->parent;
241     }
242     return false;
243 }
244 #endif
245
246 block_t *DirBlock (access_t *p_access)
247 {
248     access_sys_t *p_sys = p_access->p_sys;
249     directory_t *current = p_sys->current;
250
251     if (p_access->info.b_eof)
252         return NULL;
253
254     if (p_sys->header)
255     {   /* Startup: send the XSPF header */
256         static const char header[] =
257             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
258             "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
259             " <trackList>\n";
260         block_t *block = block_Alloc (sizeof (header) - 1);
261         if (!block)
262             goto fatal;
263         memcpy (block->p_buffer, header, sizeof (header) - 1);
264         p_sys->header = false;
265         return block;
266     }
267
268     if (current->i >= current->filec)
269     {   /* End of directory, go back to parent */
270         closedir (current->handle);
271         p_sys->current = current->parent;
272         free (current->uri);
273         free (current->filev);
274 #ifndef HAVE_OPENAT
275         free (current->path);
276 #endif
277         free (current);
278
279         if (p_sys->current == NULL)
280         {   /* End of XSPF playlist */
281             char *footer;
282             int len = asprintf (&footer, " </trackList>\n"
283                 " <extension application=\"http://www.videolan.org/"
284                                              "vlc/playlist/0\">\n"
285                 "%s"
286                 " </extension>\n"
287                 "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
288             if (unlikely(len == -1))
289                 goto fatal;
290
291             block_t *block = block_heap_Alloc (footer, footer, len);
292             if (unlikely(block == NULL))
293                 free (footer);
294             p_access->info.b_eof = true;
295             return block;
296         }
297         else
298         {
299             /* This was the end of a "subnode" */
300             /* Write the ID to the extension */
301             char *old_xspf_ext = p_sys->xspf_ext;
302             if (old_xspf_ext != NULL
303              && asprintf (&p_sys->xspf_ext, "%s  </vlc:node>\n",
304                           old_xspf_ext ? old_xspf_ext : "") == -1)
305                 p_sys->xspf_ext = NULL;
306             free (old_xspf_ext);
307         }
308         return NULL;
309     }
310
311     char *entry = current->filev[current->i++];
312
313     /* Handle recursion */
314     if (p_sys->mode != MODE_COLLAPSE)
315     {
316         DIR *handle;
317 #ifdef HAVE_OPENAT
318         int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
319         if (fd == -1)
320             goto skip; /* File cannot be opened... forget it */
321
322         struct stat st;
323         if (fstat (fd, &st))
324         {
325             close (fd);
326             goto skip; /* cannot stat?! */
327         }
328         if (!S_ISDIR (st.st_mode))
329         {
330             close (fd);
331             goto notdir;
332         }
333         if (p_sys->mode == MODE_NONE
334          || has_inode_loop (current, st.st_dev, st.st_ino)
335          || (handle = fdopendir (fd)) == NULL)
336         {
337             close (fd);
338             goto skip;
339         }
340 #else
341         char *path;
342         if (asprintf (&path, "%s/%s", current->path, entry) == -1)
343             goto skip;
344         if ((handle = vlc_opendir (path)) == NULL)
345             goto notdir;
346         if (p_sys->mode == MODE_NONE)
347             goto skip;
348 #endif
349         directory_t *sub = malloc (sizeof (*sub));
350         if (unlikely(sub == NULL))
351         {
352             closedir (handle);
353 #ifndef HAVE_OPENAT
354             free (path);
355 #endif
356             goto skip;
357         }
358         sub->parent = current;
359         sub->handle = handle;
360         sub->filec = vlc_loaddir (handle, &sub->filev, visible, collate);
361         if (sub->filec < 0)
362             sub->filev = NULL;
363         sub->i = 0;
364 #ifdef HAVE_OPENAT
365         sub->device = st.st_dev;
366         sub->inode = st.st_ino;
367 #else
368         sub->path = path;
369 #endif
370         p_sys->current = sub;
371
372         char *encoded = encode_URI_component (entry);
373         if (encoded == NULL
374          || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
375              sub->uri = NULL;
376         free (encoded);
377         if (unlikely(sub->uri == NULL))
378         {
379             free (entry);
380             goto fatal;
381         }
382
383         /* Add node to XSPF extension */
384         char *old_xspf_ext = p_sys->xspf_ext;
385         char *title = convert_xml_special_chars (entry);
386         if (old_xspf_ext != NULL && title != NULL
387          && asprintf (&p_sys->xspf_ext, "%s  <vlc:node title=\"%s\">\n",
388                       old_xspf_ext, title) == -1)
389             p_sys->xspf_ext = NULL;
390         free (old_xspf_ext);
391         free (title);
392         goto skip;
393     }
394
395 notdir:
396     /* Skip files with ignored extensions */
397     if (p_sys->ignored_exts != NULL)
398     {
399         const char *ext = strrchr (entry, '.');
400         if (ext != NULL)
401         {
402             size_t extlen = strlen (++ext);
403             for (const char *type = p_sys->ignored_exts, *end;
404                  type[0]; type = end + 1)
405             {
406                 end = strchr (type, ',');
407                 if (end == NULL)
408                     end = type + strlen (type);
409
410                 if (type + extlen == end
411                  && !strncasecmp (ext, type, extlen))
412                 {
413                     free (entry);
414                     return NULL;
415                 }
416
417                 if (*end == '\0')
418                     break;
419             }
420         }
421     }
422
423     char *encoded = encode_URI_component (entry);
424     free (entry);
425     if (encoded == NULL)
426         goto fatal;
427     int len = asprintf (&entry,
428                         "  <track><location>%s/%s</location>\n" \
429                         "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
430                         "    <vlc:id>%d</vlc:id>\n" \
431                         "   </extension>\n" \
432                         "  </track>\n",
433                         current->uri, encoded, p_sys->i_item_count++);
434     free (encoded);
435     if (len == -1)
436         goto fatal;
437
438     /* Write the ID to the extension */
439     char *old_xspf_ext = p_sys->xspf_ext;
440     if (old_xspf_ext != NULL
441      && asprintf (&p_sys->xspf_ext, "%s   <vlc:item tid=\"%i\" />\n",
442                   old_xspf_ext, p_sys->i_item_count - 1) == -1)
443         p_sys->xspf_ext = NULL;
444     free (old_xspf_ext);
445
446     block_t *block = block_heap_Alloc (entry, entry, len);
447     if (unlikely(block == NULL))
448     {
449         free (entry);
450         goto fatal;
451     }
452     return block;
453
454 fatal:
455     p_access->info.b_eof = true;
456     return NULL;
457
458 skip:
459     free (entry);
460     return NULL;
461 }
462
463 /*****************************************************************************
464  * Control:
465  *****************************************************************************/
466 int DirControl( access_t *p_access, int i_query, va_list args )
467 {
468     switch( i_query )
469     {
470         /* */
471         case ACCESS_CAN_SEEK:
472         case ACCESS_CAN_FASTSEEK:
473             *va_arg( args, bool* ) = false;
474             break;
475
476         case ACCESS_CAN_PAUSE:
477         case ACCESS_CAN_CONTROL_PACE:
478             *va_arg( args, bool* ) = true;
479             break;
480
481         /* */
482         case ACCESS_GET_PTS_DELAY:
483             *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
484             break;
485
486         /* */
487         case ACCESS_SET_PAUSE_STATE:
488         case ACCESS_GET_TITLE_INFO:
489         case ACCESS_SET_TITLE:
490         case ACCESS_SET_SEEKPOINT:
491         case ACCESS_SET_PRIVATE_ID_STATE:
492         case ACCESS_GET_CONTENT_TYPE:
493         case ACCESS_GET_META:
494             return VLC_EGENERIC;
495
496         default:
497             msg_Warn( p_access, "unimplemented query in control" );
498             return VLC_EGENERIC;
499     }
500     return VLC_SUCCESS;
501 }