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 #ifdef HAVE_SYS_TYPES_H
38 # include <sys/types.h>
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
46 #elif defined( WIN32 ) && !defined( UNDER_CE )
54 static inline int dirfd (DIR *dir)
60 #include <vlc_charset.h>
62 #include <vlc_strings.h>
71 typedef struct directory_t directory_t;
90 char *psz_xspf_extension;
93 /*****************************************************************************
94 * Open: open the directory
95 *****************************************************************************/
96 int DirOpen( vlc_object_t *p_this )
98 access_t *p_access = (access_t*)p_this;
100 if( !p_access->psz_path )
104 if (strcmp (p_access->psz_path, "-"))
105 handle = utf8_opendir (p_access->psz_path);
108 #if 0 /* This won't work yet, it generates paths like "-/music.ogg".
109 * We'd need to use openat() here and in the file access... */
111 handle = fdopendir (fd);
122 return DirInit (p_access, handle);
125 int DirInit (access_t *p_access, DIR *handle)
127 access_sys_t *p_sys = malloc (sizeof (*p_sys));
134 p_access->p_sys = p_sys;
135 p_sys->current = NULL;
136 p_sys->handle = handle;
137 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
138 p_sys->i_item_count = 0;
139 p_sys->psz_xspf_extension = strdup( "" );
142 char *psz = var_CreateGetString( p_access, "recursive" );
143 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
144 p_sys->mode = MODE_NONE;
145 else if( !strcasecmp( psz, "collapse" ) )
146 p_sys->mode = MODE_COLLAPSE;
148 p_sys->mode = MODE_EXPAND;
151 access_InitFields(p_access);
152 p_access->pf_read = NULL;
153 p_access->pf_block = DirBlock;
154 p_access->pf_seek = NULL;
155 p_access->pf_control= DirControl;
156 free (p_access->psz_demux);
157 p_access->psz_demux = strdup ("xspf-open");
162 /*****************************************************************************
163 * Close: close the target
164 *****************************************************************************/
165 void DirClose( vlc_object_t * p_this )
167 access_t *p_access = (access_t*)p_this;
168 access_sys_t *p_sys = p_access->p_sys;
170 while (p_sys->current)
172 directory_t *current = p_sys->current;
174 p_sys->current = current->parent;
175 closedir (current->handle);
179 if (p_sys->handle != NULL)
180 closedir (p_sys->handle); /* corner case,:Block() not called ever */
181 free (p_sys->psz_xspf_extension);
182 free (p_sys->ignored_exts);
186 /* Detect directories that recurse into themselves. */
187 static bool has_inode_loop (const directory_t *dir)
190 dev_t dev = dir->st.st_dev;
191 ino_t inode = dir->st.st_ino;
193 while ((dir = dir->parent) != NULL)
194 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
198 # define fstat( fd, st ) (0)
204 block_t *DirBlock (access_t *p_access)
206 access_sys_t *p_sys = p_access->p_sys;
207 directory_t *current = p_sys->current;
209 if (p_access->info.b_eof)
213 { /* Startup: send the XSPF header */
214 static const char header[] =
215 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
216 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
218 block_t *block = block_Alloc (sizeof (header) - 1);
221 memcpy (block->p_buffer, header, sizeof (header) - 1);
223 /* "Open" the base directory */
224 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
227 block_Release (block);
230 current->parent = NULL;
231 current->handle = p_sys->handle;
232 strcpy (current->path, p_access->psz_path);
233 current->uri = make_URI (current->path);
234 if ((current->uri == NULL)
235 || fstat (dirfd (current->handle), ¤t->st))
239 block_Release (block);
243 p_sys->handle = NULL;
244 p_sys->current = current;
248 char *entry = utf8_readdir (current->handle);
250 { /* End of directory, go back to parent */
251 closedir (current->handle);
252 p_sys->current = current->parent;
256 if (p_sys->current == NULL)
257 { /* End of XSPF playlist */
259 int len = asprintf( &footer, " </trackList>\n" \
260 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
263 "</playlist>\n", p_sys->psz_xspf_extension );
267 block_t *block = block_Alloc ( len );
270 memcpy (block->p_buffer, footer, len);
272 p_access->info.b_eof = true;
277 /* This was the end of a "subnode" */
278 /* Write the ID to the extension */
279 char *old_xspf_extension = p_sys->psz_xspf_extension;
280 if (old_xspf_extension == NULL)
283 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
286 free( old_xspf_extension );
291 /* Skip current, parent and hidden directories */
297 /* Handle recursion */
298 if (p_sys->mode != MODE_COLLAPSE)
300 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
307 sprintf (sub->path, "%s/%s", current->path, entry);
309 DIR *handle = utf8_opendir (sub->path);
312 sub->parent = current;
313 sub->handle = handle;
315 char *encoded = encode_URI_component (entry);
316 if ((encoded == NULL)
317 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
321 if ((p_sys->mode == MODE_NONE)
322 || fstat (dirfd (handle), &sub->st)
323 || has_inode_loop (sub)
324 || (sub->uri == NULL))
332 p_sys->current = sub;
334 /* Add node to xspf extension */
335 char *old_xspf_extension = p_sys->psz_xspf_extension;
336 if (old_xspf_extension == NULL)
342 char *title = convert_xml_special_chars (entry);
345 || asprintf (&p_sys->psz_xspf_extension, "%s"
346 " <vlc:node title=\"%s\">\n", old_xspf_extension,
353 free (old_xspf_extension);
360 /* Skip files with ignored extensions */
361 if (p_sys->ignored_exts != NULL)
363 const char *ext = strrchr (entry, '.');
366 size_t extlen = strlen (++ext);
367 for (const char *type = p_sys->ignored_exts, *end;
368 type[0]; type = end + 1)
370 end = strchr (type, ',');
372 end = type + strlen (type);
374 if (type + extlen == end
375 && !strncasecmp (ext, type, extlen))
387 char *encoded = encode_URI_component (entry);
391 int len = asprintf (&entry,
392 " <track><location>%s/%s</location>\n" \
393 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
394 " <vlc:id>%d</vlc:id>\n" \
397 current->uri, encoded, p_sys->i_item_count++);
402 /* Write the ID to the extension */
403 char *old_xspf_extension = p_sys->psz_xspf_extension;
404 if (old_xspf_extension == NULL)
407 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
408 old_xspf_extension, p_sys->i_item_count-1 );
411 free( old_xspf_extension );
413 /* TODO: new block allocator for malloc()ated data */
414 block_t *block = block_Alloc (len);
420 memcpy (block->p_buffer, entry, len);
425 p_access->info.b_eof = true;
429 /*****************************************************************************
431 *****************************************************************************/
432 int DirControl( access_t *p_access, int i_query, va_list args )
437 case ACCESS_CAN_SEEK:
438 case ACCESS_CAN_FASTSEEK:
439 *va_arg( args, bool* ) = false;
442 case ACCESS_CAN_PAUSE:
443 case ACCESS_CAN_CONTROL_PACE:
444 *va_arg( args, bool* ) = true;
448 case ACCESS_GET_PTS_DELAY:
449 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
453 case ACCESS_SET_PAUSE_STATE:
454 case ACCESS_GET_TITLE_INFO:
455 case ACCESS_SET_TITLE:
456 case ACCESS_SET_SEEKPOINT:
457 case ACCESS_SET_PRIVATE_ID_STATE:
458 case ACCESS_GET_CONTENT_TYPE:
459 case ACCESS_GET_META:
463 msg_Warn( p_access, "unimplemented query in control" );