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