1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2008 the VideoLAN team
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <vlc_access.h>
37 #include <sys/types.h>
42 #elif defined( WIN32 ) && !defined( UNDER_CE )
48 #include <vlc_strings.h>
49 #include <vlc_charset.h>
58 typedef struct directory_t directory_t;
82 int (*compar)(const char **a, const char **b);
85 /* Select non-hidden files only */
86 static int visible (const char *name)
88 return name[0] != '.';
91 static int collate (const char **a, const char **b)
94 return strcoll (*a, *b);
96 return strcmp (*a, *b);
100 static int version (const char **a, const char **b)
102 return strverscmp (*a, *b);
105 /*****************************************************************************
106 * Open: open the directory
107 *****************************************************************************/
108 int DirOpen( vlc_object_t *p_this )
110 access_t *p_access = (access_t*)p_this;
112 if( !p_access->psz_filepath )
115 DIR *handle = vlc_opendir (p_access->psz_filepath);
119 return DirInit (p_access, handle);
122 int DirInit (access_t *p_access, DIR *handle)
124 access_sys_t *p_sys = malloc (sizeof (*p_sys));
125 if (unlikely(p_sys == NULL))
129 if (!strcmp (p_access->psz_access, "fd"))
131 if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
135 uri = make_URI (p_access->psz_filepath, "file");
136 if (unlikely(uri == NULL))
139 /* "Open" the base directory */
140 directory_t *root = malloc (sizeof (*root));
141 if (unlikely(root == NULL))
147 char *psz_sort = var_InheritString (p_access, "directory-sort");
149 p_sys->compar = collate;
150 else if (!strcasecmp (psz_sort, "version"))
151 p_sys->compar = version;
152 else if (!strcasecmp (psz_sort, "none"))
153 p_sys->compar = NULL;
155 p_sys->compar = collate;
159 root->handle = handle;
161 root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
167 if (fstat (dirfd (handle), &st))
173 root->device = st.st_dev;
174 root->inode = st.st_ino;
176 root->path = strdup (p_access->psz_filepath);
179 p_access->p_sys = p_sys;
180 p_sys->current = root;
181 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
182 p_sys->header = true;
183 p_sys->i_item_count = 0;
184 p_sys->xspf_ext = strdup ("");
187 char *psz = var_InheritString (p_access, "recursive");
188 if (psz == NULL || !strcasecmp (psz, "none"))
189 p_sys->mode = MODE_NONE;
190 else if( !strcasecmp( psz, "collapse" ) )
191 p_sys->mode = MODE_COLLAPSE;
193 p_sys->mode = MODE_EXPAND;
196 access_InitFields(p_access);
197 p_access->pf_read = NULL;
198 p_access->pf_block = DirBlock;
199 p_access->pf_seek = NULL;
200 p_access->pf_control= DirControl;
201 free (p_access->psz_demux);
202 p_access->psz_demux = strdup ("xspf-open");
212 /*****************************************************************************
213 * Close: close the target
214 *****************************************************************************/
215 void DirClose( vlc_object_t * p_this )
217 access_t *p_access = (access_t*)p_this;
218 access_sys_t *p_sys = p_access->p_sys;
220 while (p_sys->current)
222 directory_t *current = p_sys->current;
224 p_sys->current = current->parent;
225 closedir (current->handle);
227 while (current->i < current->filec)
228 free (current->filev[current->i++]);
229 free (current->filev);
231 free (current->path);
236 free (p_sys->xspf_ext);
237 free (p_sys->ignored_exts);
242 /* Detect directories that recurse into themselves. */
243 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
247 if ((dir->device == dev) && (dir->inode == inode))
255 block_t *DirBlock (access_t *p_access)
257 access_sys_t *p_sys = p_access->p_sys;
258 directory_t *current = p_sys->current;
260 if (p_access->info.b_eof)
264 { /* Startup: send the XSPF header */
265 static const char header[] =
266 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
267 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
269 block_t *block = block_Alloc (sizeof (header) - 1);
272 memcpy (block->p_buffer, header, sizeof (header) - 1);
273 p_sys->header = false;
277 if (current->i >= current->filec)
278 { /* End of directory, go back to parent */
279 closedir (current->handle);
280 p_sys->current = current->parent;
282 free (current->filev);
284 free (current->path);
288 if (p_sys->current == NULL)
289 { /* End of XSPF playlist */
291 int len = asprintf (&footer, " </trackList>\n"
292 " <extension application=\"http://www.videolan.org/"
293 "vlc/playlist/0\">\n"
296 "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
297 if (unlikely(len == -1))
300 block_t *block = block_heap_Alloc (footer, footer, len);
301 if (unlikely(block == NULL))
303 p_access->info.b_eof = true;
308 /* This was the end of a "subnode" */
309 /* Write the ID to the extension */
310 char *old_xspf_ext = p_sys->xspf_ext;
311 if (old_xspf_ext != NULL
312 && asprintf (&p_sys->xspf_ext, "%s </vlc:node>\n",
313 old_xspf_ext ? old_xspf_ext : "") == -1)
314 p_sys->xspf_ext = NULL;
320 char *entry = current->filev[current->i++];
322 /* Handle recursion */
323 if (p_sys->mode != MODE_COLLAPSE)
327 int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
329 goto skip; /* File cannot be opened... forget it */
335 goto skip; /* cannot stat?! */
337 if (!S_ISDIR (st.st_mode))
342 if (p_sys->mode == MODE_NONE
343 || has_inode_loop (current, st.st_dev, st.st_ino)
344 || (handle = fdopendir (fd)) == NULL)
351 if (asprintf (&path, "%s/%s", current->path, entry) == -1)
353 if ((handle = vlc_opendir (path)) == NULL)
355 if (p_sys->mode == MODE_NONE)
358 directory_t *sub = malloc (sizeof (*sub));
359 if (unlikely(sub == NULL))
367 sub->parent = current;
368 sub->handle = handle;
369 sub->filec = vlc_loaddir (handle, &sub->filev, visible, p_sys->compar);
374 sub->device = st.st_dev;
375 sub->inode = st.st_ino;
379 p_sys->current = sub;
381 char *encoded = encode_URI_component (entry);
383 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
386 if (unlikely(sub->uri == NULL))
392 /* Add node to XSPF extension */
393 char *old_xspf_ext = p_sys->xspf_ext;
395 char *title = convert_xml_special_chars (entry);
396 if (old_xspf_ext != NULL
397 && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
398 old_xspf_ext, title ? title : "?") == -1)
399 p_sys->xspf_ext = NULL;
406 /* Skip files with ignored extensions */
407 if (p_sys->ignored_exts != NULL)
409 const char *ext = strrchr (entry, '.');
412 size_t extlen = strlen (++ext);
413 for (const char *type = p_sys->ignored_exts, *end;
414 type[0]; type = end + 1)
416 end = strchr (type, ',');
418 end = type + strlen (type);
420 if (type + extlen == end
421 && !strncasecmp (ext, type, extlen))
433 char *encoded = encode_URI_component (entry);
437 int len = asprintf (&entry,
438 " <track><location>%s/%s</location>\n" \
439 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
440 " <vlc:id>%d</vlc:id>\n" \
443 current->uri, encoded, p_sys->i_item_count++);
448 /* Write the ID to the extension */
449 char *old_xspf_ext = p_sys->xspf_ext;
450 if (old_xspf_ext != NULL
451 && asprintf (&p_sys->xspf_ext, "%s <vlc:item tid=\"%i\" />\n",
452 old_xspf_ext, p_sys->i_item_count - 1) == -1)
453 p_sys->xspf_ext = NULL;
456 block_t *block = block_heap_Alloc (entry, entry, len);
457 if (unlikely(block == NULL))
465 p_access->info.b_eof = true;
473 /*****************************************************************************
475 *****************************************************************************/
476 int DirControl( access_t *p_access, int i_query, va_list args )
481 case ACCESS_CAN_SEEK:
482 case ACCESS_CAN_FASTSEEK:
483 *va_arg( args, bool* ) = false;
486 case ACCESS_CAN_PAUSE:
487 case ACCESS_CAN_CONTROL_PACE:
488 *va_arg( args, bool* ) = true;
492 case ACCESS_GET_PTS_DELAY:
493 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
497 case ACCESS_SET_PAUSE_STATE:
498 case ACCESS_GET_TITLE_INFO:
499 case ACCESS_SET_TITLE:
500 case ACCESS_SET_SEEKPOINT:
501 case ACCESS_SET_PRIVATE_ID_STATE:
502 case ACCESS_GET_CONTENT_TYPE:
503 case ACCESS_GET_META:
507 msg_Warn( p_access, "unimplemented query in control" );