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>
44 #elif defined( WIN32 ) && !defined( UNDER_CE )
52 static inline int dirfd (DIR *dir)
60 #include <vlc_strings.h>
69 typedef struct directory_t directory_t;
88 char *psz_xspf_extension;
91 /*****************************************************************************
92 * Open: open the directory
93 *****************************************************************************/
94 int DirOpen( vlc_object_t *p_this )
96 access_t *p_access = (access_t*)p_this;
98 if( !p_access->psz_path )
101 DIR *handle = vlc_opendir (p_access->psz_path);
105 return DirInit (p_access, handle);
108 int DirInit (access_t *p_access, DIR *handle)
110 access_sys_t *p_sys = malloc (sizeof (*p_sys));
117 p_access->p_sys = p_sys;
118 p_sys->current = NULL;
119 p_sys->handle = handle;
120 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
121 p_sys->i_item_count = 0;
122 p_sys->psz_xspf_extension = strdup( "" );
125 char *psz = var_CreateGetString( p_access, "recursive" );
126 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
127 p_sys->mode = MODE_NONE;
128 else if( !strcasecmp( psz, "collapse" ) )
129 p_sys->mode = MODE_COLLAPSE;
131 p_sys->mode = MODE_EXPAND;
134 access_InitFields(p_access);
135 p_access->pf_read = NULL;
136 p_access->pf_block = DirBlock;
137 p_access->pf_seek = NULL;
138 p_access->pf_control= DirControl;
139 free (p_access->psz_demux);
140 p_access->psz_demux = strdup ("xspf-open");
145 /*****************************************************************************
146 * Close: close the target
147 *****************************************************************************/
148 void DirClose( vlc_object_t * p_this )
150 access_t *p_access = (access_t*)p_this;
151 access_sys_t *p_sys = p_access->p_sys;
153 while (p_sys->current)
155 directory_t *current = p_sys->current;
157 p_sys->current = current->parent;
158 closedir (current->handle);
162 if (p_sys->handle != NULL)
163 closedir (p_sys->handle); /* corner case,:Block() not called ever */
164 free (p_sys->psz_xspf_extension);
165 free (p_sys->ignored_exts);
169 /* Detect directories that recurse into themselves. */
170 static bool has_inode_loop (const directory_t *dir)
173 dev_t dev = dir->st.st_dev;
174 ino_t inode = dir->st.st_ino;
176 while ((dir = dir->parent) != NULL)
177 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
181 # define fstat( fd, st ) (0)
187 block_t *DirBlock (access_t *p_access)
189 access_sys_t *p_sys = p_access->p_sys;
190 directory_t *current = p_sys->current;
192 if (p_access->info.b_eof)
196 { /* Startup: send the XSPF header */
197 static const char header[] =
198 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
199 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
201 block_t *block = block_Alloc (sizeof (header) - 1);
204 memcpy (block->p_buffer, header, sizeof (header) - 1);
206 /* "Open" the base directory */
207 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
210 block_Release (block);
213 current->parent = NULL;
214 current->handle = p_sys->handle;
215 strcpy (current->path, p_access->psz_path);
216 current->uri = make_URI (current->path);
217 if ((current->uri == NULL)
218 || fstat (dirfd (current->handle), ¤t->st))
222 block_Release (block);
226 p_sys->handle = NULL;
227 p_sys->current = current;
231 char *entry = vlc_readdir (current->handle);
233 { /* End of directory, go back to parent */
234 closedir (current->handle);
235 p_sys->current = current->parent;
239 if (p_sys->current == NULL)
240 { /* End of XSPF playlist */
242 int len = asprintf( &footer, " </trackList>\n" \
243 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
246 "</playlist>\n", p_sys->psz_xspf_extension );
250 block_t *block = block_Alloc ( len );
253 memcpy (block->p_buffer, footer, len);
255 p_access->info.b_eof = true;
260 /* This was the end of a "subnode" */
261 /* Write the ID to the extension */
262 char *old_xspf_extension = p_sys->psz_xspf_extension;
263 if (old_xspf_extension == NULL)
266 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
269 free( old_xspf_extension );
274 /* Skip current, parent and hidden directories */
280 /* Handle recursion */
281 if (p_sys->mode != MODE_COLLAPSE)
283 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
290 sprintf (sub->path, "%s/%s", current->path, entry);
292 DIR *handle = vlc_opendir (sub->path);
295 sub->parent = current;
296 sub->handle = handle;
298 char *encoded = encode_URI_component (entry);
299 if ((encoded == NULL)
300 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
304 if ((p_sys->mode == MODE_NONE)
305 || fstat (dirfd (handle), &sub->st)
306 || has_inode_loop (sub)
307 || (sub->uri == NULL))
315 p_sys->current = sub;
317 /* Add node to xspf extension */
318 char *old_xspf_extension = p_sys->psz_xspf_extension;
319 if (old_xspf_extension == NULL)
325 char *title = convert_xml_special_chars (entry);
328 || asprintf (&p_sys->psz_xspf_extension, "%s"
329 " <vlc:node title=\"%s\">\n", old_xspf_extension,
336 free (old_xspf_extension);
343 /* Skip files with ignored extensions */
344 if (p_sys->ignored_exts != NULL)
346 const char *ext = strrchr (entry, '.');
349 size_t extlen = strlen (++ext);
350 for (const char *type = p_sys->ignored_exts, *end;
351 type[0]; type = end + 1)
353 end = strchr (type, ',');
355 end = type + strlen (type);
357 if (type + extlen == end
358 && !strncasecmp (ext, type, extlen))
370 char *encoded = encode_URI_component (entry);
374 int len = asprintf (&entry,
375 " <track><location>%s/%s</location>\n" \
376 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
377 " <vlc:id>%d</vlc:id>\n" \
380 current->uri, encoded, p_sys->i_item_count++);
385 /* Write the ID to the extension */
386 char *old_xspf_extension = p_sys->psz_xspf_extension;
387 if (old_xspf_extension == NULL)
390 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
391 old_xspf_extension, p_sys->i_item_count-1 );
394 free( old_xspf_extension );
396 /* TODO: new block allocator for malloc()ated data */
397 block_t *block = block_Alloc (len);
403 memcpy (block->p_buffer, entry, len);
408 p_access->info.b_eof = true;
412 /*****************************************************************************
414 *****************************************************************************/
415 int DirControl( access_t *p_access, int i_query, va_list args )
420 case ACCESS_CAN_SEEK:
421 case ACCESS_CAN_FASTSEEK:
422 *va_arg( args, bool* ) = false;
425 case ACCESS_CAN_PAUSE:
426 case ACCESS_CAN_CONTROL_PACE:
427 *va_arg( args, bool* ) = true;
431 case ACCESS_GET_PTS_DELAY:
432 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
436 case ACCESS_SET_PAUSE_STATE:
437 case ACCESS_GET_TITLE_INFO:
438 case ACCESS_SET_TITLE:
439 case ACCESS_SET_SEEKPOINT:
440 case ACCESS_SET_PRIVATE_ID_STATE:
441 case ACCESS_GET_CONTENT_TYPE:
442 case ACCESS_GET_META:
446 msg_Warn( p_access, "unimplemented query in control" );