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>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
45 #elif defined( WIN32 ) && !defined( UNDER_CE )
53 static inline int dirfd (DIR *dir)
61 #include <vlc_strings.h>
70 typedef struct directory_t directory_t;
79 #ifndef HAVE_FDOPENDIR
92 char *psz_xspf_extension;
95 /*****************************************************************************
96 * Open: open the directory
97 *****************************************************************************/
98 int DirOpen( vlc_object_t *p_this )
100 access_t *p_access = (access_t*)p_this;
102 if( !p_access->psz_path )
105 DIR *handle = vlc_opendir (p_access->psz_path);
109 return DirInit (p_access, handle);
112 int DirInit (access_t *p_access, DIR *handle)
114 access_sys_t *p_sys = malloc (sizeof (*p_sys));
115 if (unlikely(p_sys == NULL))
119 if (!strcmp (p_access->psz_access, "fd"))
121 if (asprintf (&uri, "fd://%s", p_access->psz_path) == -1)
125 uri = make_URI (p_access->psz_path);
126 if (unlikely(uri == NULL))
129 p_access->p_sys = p_sys;
130 p_sys->current = NULL;
131 p_sys->handle = handle;
133 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
134 p_sys->i_item_count = 0;
135 p_sys->psz_xspf_extension = strdup( "" );
138 char *psz = var_InheritString (p_access, "recursive");
139 if (psz == NULL || !strcasecmp (psz, "none"))
140 p_sys->mode = MODE_NONE;
141 else if( !strcasecmp( psz, "collapse" ) )
142 p_sys->mode = MODE_COLLAPSE;
144 p_sys->mode = MODE_EXPAND;
147 access_InitFields(p_access);
148 p_access->pf_read = NULL;
149 p_access->pf_block = DirBlock;
150 p_access->pf_seek = NULL;
151 p_access->pf_control= DirControl;
152 free (p_access->psz_demux);
153 p_access->psz_demux = strdup ("xspf-open");
163 /*****************************************************************************
164 * Close: close the target
165 *****************************************************************************/
166 void DirClose( vlc_object_t * p_this )
168 access_t *p_access = (access_t*)p_this;
169 access_sys_t *p_sys = p_access->p_sys;
171 while (p_sys->current)
173 directory_t *current = p_sys->current;
175 p_sys->current = current->parent;
176 closedir (current->handle);
178 #ifndef HAVE_FDOPENDIR
179 free (current->path);
184 /* corner case: Block() not called ever */
185 if (p_sys->handle != NULL)
186 closedir (p_sys->handle);
189 free (p_sys->psz_xspf_extension);
190 free (p_sys->ignored_exts);
194 /* Detect directories that recurse into themselves. */
195 static bool has_inode_loop (const directory_t *dir)
198 dev_t dev = dir->st.st_dev;
199 ino_t inode = dir->st.st_ino;
201 while ((dir = dir->parent) != NULL)
202 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
206 # define fstat( fd, st ) (0)
212 block_t *DirBlock (access_t *p_access)
214 access_sys_t *p_sys = p_access->p_sys;
215 directory_t *current = p_sys->current;
217 if (p_access->info.b_eof)
221 { /* Startup: send the XSPF header */
222 static const char header[] =
223 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
224 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
226 block_t *block = block_Alloc (sizeof (header) - 1);
229 memcpy (block->p_buffer, header, sizeof (header) - 1);
231 /* "Open" the base directory */
232 current = malloc (sizeof (*current));
235 block_Release (block);
238 current->parent = NULL;
239 current->handle = p_sys->handle;
240 #ifndef HAVE_FDOPENDIR
241 current->path = strdup (p_access->psz_path);
243 current->uri = p_sys->uri;
244 if (fstat (dirfd (current->handle), ¤t->st))
247 block_Release (block);
251 p_sys->handle = NULL;
253 p_sys->current = current;
257 char *entry = vlc_readdir (current->handle);
259 { /* End of directory, go back to parent */
260 closedir (current->handle);
261 p_sys->current = current->parent;
263 #ifndef HAVE_FDOPENDIR
264 free (current->path);
268 if (p_sys->current == NULL)
269 { /* End of XSPF playlist */
271 int len = asprintf( &footer, " </trackList>\n" \
272 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
275 "</playlist>\n", p_sys->psz_xspf_extension );
276 if (unlikely(len == -1))
279 block_t *block = block_heap_Alloc (footer, footer, len);
280 if (unlikely(block == NULL))
282 p_access->info.b_eof = true;
287 /* This was the end of a "subnode" */
288 /* Write the ID to the extension */
289 char *old_xspf_extension = p_sys->psz_xspf_extension;
290 if (old_xspf_extension == NULL)
293 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
296 free( old_xspf_extension );
301 /* Skip current, parent and hidden directories */
307 /* Handle recursion */
308 if (p_sys->mode != MODE_COLLAPSE)
310 directory_t *sub = malloc (sizeof (*sub));
318 #ifdef HAVE_FDOPENDIR
319 int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
322 handle = fdopendir (fd);
329 if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1)
330 handle = vlc_opendir (sub->path);
336 sub->parent = current;
337 sub->handle = handle;
339 char *encoded = encode_URI_component (entry);
340 if ((encoded == NULL)
341 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
345 if ((p_sys->mode == MODE_NONE)
346 || fstat (dirfd (handle), &sub->st)
347 || has_inode_loop (sub)
348 || (sub->uri == NULL))
356 p_sys->current = sub;
358 /* Add node to xspf extension */
359 char *old_xspf_extension = p_sys->psz_xspf_extension;
360 if (old_xspf_extension == NULL)
366 char *title = convert_xml_special_chars (entry);
369 || asprintf (&p_sys->psz_xspf_extension, "%s"
370 " <vlc:node title=\"%s\">\n", old_xspf_extension,
377 free (old_xspf_extension);
384 /* Skip files with ignored extensions */
385 if (p_sys->ignored_exts != NULL)
387 const char *ext = strrchr (entry, '.');
390 size_t extlen = strlen (++ext);
391 for (const char *type = p_sys->ignored_exts, *end;
392 type[0]; type = end + 1)
394 end = strchr (type, ',');
396 end = type + strlen (type);
398 if (type + extlen == end
399 && !strncasecmp (ext, type, extlen))
411 char *encoded = encode_URI_component (entry);
415 int len = asprintf (&entry,
416 " <track><location>%s/%s</location>\n" \
417 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
418 " <vlc:id>%d</vlc:id>\n" \
421 current->uri, encoded, p_sys->i_item_count++);
426 /* Write the ID to the extension */
427 char *old_xspf_extension = p_sys->psz_xspf_extension;
428 if (old_xspf_extension == NULL)
431 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
432 old_xspf_extension, p_sys->i_item_count-1 );
435 free( old_xspf_extension );
437 block_t *block = block_heap_Alloc (entry, entry, len);
438 if (unlikely(block == NULL))
446 p_access->info.b_eof = true;
450 /*****************************************************************************
452 *****************************************************************************/
453 int DirControl( access_t *p_access, int i_query, va_list args )
458 case ACCESS_CAN_SEEK:
459 case ACCESS_CAN_FASTSEEK:
460 *va_arg( args, bool* ) = false;
463 case ACCESS_CAN_PAUSE:
464 case ACCESS_CAN_CONTROL_PACE:
465 *va_arg( args, bool* ) = true;
469 case ACCESS_GET_PTS_DELAY:
470 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
474 case ACCESS_SET_PAUSE_STATE:
475 case ACCESS_GET_TITLE_INFO:
476 case ACCESS_SET_TITLE:
477 case ACCESS_SET_SEEKPOINT:
478 case ACCESS_SET_PRIVATE_ID_STATE:
479 case ACCESS_GET_CONTENT_TYPE:
480 case ACCESS_GET_META:
484 msg_Warn( p_access, "unimplemented query in control" );