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 )
103 DIR *handle = utf8_opendir (p_access->psz_path);
107 return DirInit (p_access, handle);
110 int DirInit (access_t *p_access, DIR *handle)
112 access_sys_t *p_sys = malloc (sizeof (*p_sys));
119 p_access->p_sys = p_sys;
120 p_sys->current = NULL;
121 p_sys->handle = handle;
122 p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
123 p_sys->i_item_count = 0;
124 p_sys->psz_xspf_extension = strdup( "" );
127 char *psz = var_CreateGetString( p_access, "recursive" );
128 if( *psz == '\0' || !strcasecmp( psz, "none" ) )
129 p_sys->mode = MODE_NONE;
130 else if( !strcasecmp( psz, "collapse" ) )
131 p_sys->mode = MODE_COLLAPSE;
133 p_sys->mode = MODE_EXPAND;
136 access_InitFields(p_access);
137 p_access->pf_read = NULL;
138 p_access->pf_block = DirBlock;
139 p_access->pf_seek = NULL;
140 p_access->pf_control= DirControl;
141 free (p_access->psz_demux);
142 p_access->psz_demux = strdup ("xspf-open");
147 /*****************************************************************************
148 * Close: close the target
149 *****************************************************************************/
150 void DirClose( vlc_object_t * p_this )
152 access_t *p_access = (access_t*)p_this;
153 access_sys_t *p_sys = p_access->p_sys;
155 while (p_sys->current)
157 directory_t *current = p_sys->current;
159 p_sys->current = current->parent;
160 closedir (current->handle);
164 if (p_sys->handle != NULL)
165 closedir (p_sys->handle); /* corner case,:Block() not called ever */
166 free (p_sys->psz_xspf_extension);
167 free (p_sys->ignored_exts);
171 /* Detect directories that recurse into themselves. */
172 static bool has_inode_loop (const directory_t *dir)
175 dev_t dev = dir->st.st_dev;
176 ino_t inode = dir->st.st_ino;
178 while ((dir = dir->parent) != NULL)
179 if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
183 # define fstat( fd, st ) (0)
189 block_t *DirBlock (access_t *p_access)
191 access_sys_t *p_sys = p_access->p_sys;
192 directory_t *current = p_sys->current;
194 if (p_access->info.b_eof)
198 { /* Startup: send the XSPF header */
199 static const char header[] =
200 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
201 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
203 block_t *block = block_Alloc (sizeof (header) - 1);
206 memcpy (block->p_buffer, header, sizeof (header) - 1);
208 /* "Open" the base directory */
209 current = malloc (sizeof (*current) + strlen (p_access->psz_path));
212 block_Release (block);
215 current->parent = NULL;
216 current->handle = p_sys->handle;
217 strcpy (current->path, p_access->psz_path);
218 current->uri = make_URI (current->path);
219 if ((current->uri == NULL)
220 || fstat (dirfd (current->handle), ¤t->st))
224 block_Release (block);
228 p_sys->handle = NULL;
229 p_sys->current = current;
233 char *entry = utf8_readdir (current->handle);
235 { /* End of directory, go back to parent */
236 closedir (current->handle);
237 p_sys->current = current->parent;
241 if (p_sys->current == NULL)
242 { /* End of XSPF playlist */
244 int len = asprintf( &footer, " </trackList>\n" \
245 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
248 "</playlist>\n", p_sys->psz_xspf_extension );
252 block_t *block = block_Alloc ( len );
255 memcpy (block->p_buffer, footer, len);
257 p_access->info.b_eof = true;
262 /* This was the end of a "subnode" */
263 /* Write the ID to the extension */
264 char *old_xspf_extension = p_sys->psz_xspf_extension;
265 if (old_xspf_extension == NULL)
268 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s </vlc:node>\n", old_xspf_extension );
271 free( old_xspf_extension );
276 /* Skip current, parent and hidden directories */
282 /* Handle recursion */
283 if (p_sys->mode != MODE_COLLAPSE)
285 directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
292 sprintf (sub->path, "%s/%s", current->path, entry);
294 DIR *handle = utf8_opendir (sub->path);
297 sub->parent = current;
298 sub->handle = handle;
300 char *encoded = encode_URI_component (entry);
301 if ((encoded == NULL)
302 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
306 if ((p_sys->mode == MODE_NONE)
307 || fstat (dirfd (handle), &sub->st)
308 || has_inode_loop (sub)
309 || (sub->uri == NULL))
317 p_sys->current = sub;
319 /* Add node to xspf extension */
320 char *old_xspf_extension = p_sys->psz_xspf_extension;
321 if (old_xspf_extension == NULL)
327 char *title = convert_xml_special_chars (entry);
330 || asprintf (&p_sys->psz_xspf_extension, "%s"
331 " <vlc:node title=\"%s\">\n", old_xspf_extension,
338 free (old_xspf_extension);
345 /* Skip files with ignored extensions */
346 if (p_sys->ignored_exts != NULL)
348 const char *ext = strrchr (entry, '.');
351 size_t extlen = strlen (++ext);
352 for (const char *type = p_sys->ignored_exts, *end;
353 type[0]; type = end + 1)
355 end = strchr (type, ',');
357 end = type + strlen (type);
359 if (type + extlen == end
360 && !strncasecmp (ext, type, extlen))
372 char *encoded = encode_URI_component (entry);
376 int len = asprintf (&entry,
377 " <track><location>%s/%s</location>\n" \
378 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
379 " <vlc:id>%d</vlc:id>\n" \
382 current->uri, encoded, p_sys->i_item_count++);
387 /* Write the ID to the extension */
388 char *old_xspf_extension = p_sys->psz_xspf_extension;
389 if (old_xspf_extension == NULL)
392 int len2 = asprintf( &p_sys->psz_xspf_extension, "%s <vlc:item tid=\"%i\" />\n",
393 old_xspf_extension, p_sys->i_item_count-1 );
396 free( old_xspf_extension );
398 /* TODO: new block allocator for malloc()ated data */
399 block_t *block = block_Alloc (len);
405 memcpy (block->p_buffer, entry, len);
410 p_access->info.b_eof = true;
414 /*****************************************************************************
416 *****************************************************************************/
417 int DirControl( access_t *p_access, int i_query, va_list args )
422 case ACCESS_CAN_SEEK:
423 case ACCESS_CAN_FASTSEEK:
424 *va_arg( args, bool* ) = false;
427 case ACCESS_CAN_PAUSE:
428 case ACCESS_CAN_CONTROL_PACE:
429 *va_arg( args, bool* ) = true;
433 case ACCESS_GET_PTS_DELAY:
434 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
438 case ACCESS_SET_PAUSE_STATE:
439 case ACCESS_GET_TITLE_INFO:
440 case ACCESS_SET_TITLE:
441 case ACCESS_SET_SEEKPOINT:
442 case ACCESS_SET_PRIVATE_ID_STATE:
443 case ACCESS_GET_CONTENT_TYPE:
444 case ACCESS_GET_META:
448 msg_Warn( p_access, "unimplemented query in control" );