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 )
50 static inline int dirfd (DIR *dir)
58 #include <vlc_strings.h>
67 typedef struct directory_t directory_t;
93 /* Select non-hidden files only */
94 static int visible (const char *name)
96 return name[0] != '.';
99 static int collate (const char **a, const char **b)
102 return strcoll (*a, *b);
104 return strcmp (*a, *b);
108 /*****************************************************************************
109 * Open: open the directory
110 *****************************************************************************/
111 int DirOpen( vlc_object_t *p_this )
113 access_t *p_access = (access_t*)p_this;
115 if( !p_access->psz_filepath )
118 DIR *handle = vlc_opendir (p_access->psz_filepath);
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));
128 if (unlikely(p_sys == NULL))
132 if (!strcmp (p_access->psz_access, "fd"))
134 if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
138 uri = make_URI (p_access->psz_filepath, "file");
139 if (unlikely(uri == NULL))
142 /* "Open" the base directory */
143 directory_t *root = malloc (sizeof (*root));
144 if (unlikely(root == NULL))
150 root->handle = handle;
152 root->filec = vlc_loaddir (handle, &root->filev, visible, collate);
158 if (fstat (dirfd (handle), &st))
164 root->device = st.st_dev;
165 root->inode = st.st_ino;
167 root->path = strdup (p_access->psz_filepath);
170 p_access->p_sys = p_sys;
171 p_sys->current = root;
172 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
173 p_sys->header = true;
174 p_sys->i_item_count = 0;
175 p_sys->xspf_ext = strdup ("");
178 char *psz = var_InheritString (p_access, "recursive");
179 if (psz == NULL || !strcasecmp (psz, "none"))
180 p_sys->mode = MODE_NONE;
181 else if( !strcasecmp( psz, "collapse" ) )
182 p_sys->mode = MODE_COLLAPSE;
184 p_sys->mode = MODE_EXPAND;
187 access_InitFields(p_access);
188 p_access->pf_read = NULL;
189 p_access->pf_block = DirBlock;
190 p_access->pf_seek = NULL;
191 p_access->pf_control= DirControl;
192 free (p_access->psz_demux);
193 p_access->psz_demux = strdup ("xspf-open");
203 /*****************************************************************************
204 * Close: close the target
205 *****************************************************************************/
206 void DirClose( vlc_object_t * p_this )
208 access_t *p_access = (access_t*)p_this;
209 access_sys_t *p_sys = p_access->p_sys;
211 while (p_sys->current)
213 directory_t *current = p_sys->current;
215 p_sys->current = current->parent;
216 closedir (current->handle);
218 while (current->i < current->filec)
219 free (current->filev[current->i++]);
220 free (current->filev);
222 free (current->path);
227 free (p_sys->xspf_ext);
228 free (p_sys->ignored_exts);
233 /* Detect directories that recurse into themselves. */
234 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
238 if ((dir->device == dev) && (dir->inode == inode))
246 block_t *DirBlock (access_t *p_access)
248 access_sys_t *p_sys = p_access->p_sys;
249 directory_t *current = p_sys->current;
251 if (p_access->info.b_eof)
255 { /* Startup: send the XSPF header */
256 static const char header[] =
257 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
258 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
260 block_t *block = block_Alloc (sizeof (header) - 1);
263 memcpy (block->p_buffer, header, sizeof (header) - 1);
264 p_sys->header = false;
268 if (current->i >= current->filec)
269 { /* End of directory, go back to parent */
270 closedir (current->handle);
271 p_sys->current = current->parent;
273 free (current->filev);
275 free (current->path);
279 if (p_sys->current == NULL)
280 { /* End of XSPF playlist */
282 int len = asprintf (&footer, " </trackList>\n"
283 " <extension application=\"http://www.videolan.org/"
284 "vlc/playlist/0\">\n"
287 "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
288 if (unlikely(len == -1))
291 block_t *block = block_heap_Alloc (footer, footer, len);
292 if (unlikely(block == NULL))
294 p_access->info.b_eof = true;
299 /* This was the end of a "subnode" */
300 /* Write the ID to the extension */
301 char *old_xspf_ext = p_sys->xspf_ext;
302 if (old_xspf_ext != NULL
303 && asprintf (&p_sys->xspf_ext, "%s </vlc:node>\n",
304 old_xspf_ext ? old_xspf_ext : "") == -1)
305 p_sys->xspf_ext = NULL;
311 char *entry = current->filev[current->i++];
313 /* Handle recursion */
314 if (p_sys->mode != MODE_COLLAPSE)
318 int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
320 goto skip; /* File cannot be opened... forget it */
326 goto skip; /* cannot stat?! */
328 if (!S_ISDIR (st.st_mode))
333 if (p_sys->mode == MODE_NONE
334 || has_inode_loop (current, st.st_dev, st.st_ino)
335 || (handle = fdopendir (fd)) == NULL)
342 if (asprintf (&path, "%s/%s", current->path, entry) == -1)
344 if ((handle = vlc_opendir (path)) == NULL)
346 if (p_sys->mode == MODE_NONE)
349 directory_t *sub = malloc (sizeof (*sub));
350 if (unlikely(sub == NULL))
358 sub->parent = current;
359 sub->handle = handle;
360 sub->filec = vlc_loaddir (handle, &sub->filev, visible, collate);
365 sub->device = st.st_dev;
366 sub->inode = st.st_ino;
370 p_sys->current = sub;
372 char *encoded = encode_URI_component (entry);
374 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
377 if (unlikely(sub->uri == NULL))
383 /* Add node to XSPF extension */
384 char *old_xspf_ext = p_sys->xspf_ext;
385 char *title = convert_xml_special_chars (entry);
386 if (old_xspf_ext != NULL && title != NULL
387 && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
388 old_xspf_ext, title) == -1)
389 p_sys->xspf_ext = NULL;
396 /* Skip files with ignored extensions */
397 if (p_sys->ignored_exts != NULL)
399 const char *ext = strrchr (entry, '.');
402 size_t extlen = strlen (++ext);
403 for (const char *type = p_sys->ignored_exts, *end;
404 type[0]; type = end + 1)
406 end = strchr (type, ',');
408 end = type + strlen (type);
410 if (type + extlen == end
411 && !strncasecmp (ext, type, extlen))
423 char *encoded = encode_URI_component (entry);
427 int len = asprintf (&entry,
428 " <track><location>%s/%s</location>\n" \
429 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
430 " <vlc:id>%d</vlc:id>\n" \
433 current->uri, encoded, p_sys->i_item_count++);
438 /* Write the ID to the extension */
439 char *old_xspf_ext = p_sys->xspf_ext;
440 if (old_xspf_ext != NULL
441 && asprintf (&p_sys->xspf_ext, "%s <vlc:item tid=\"%i\" />\n",
442 old_xspf_ext, p_sys->i_item_count - 1) == -1)
443 p_sys->xspf_ext = NULL;
446 block_t *block = block_heap_Alloc (entry, entry, len);
447 if (unlikely(block == NULL))
455 p_access->info.b_eof = true;
463 /*****************************************************************************
465 *****************************************************************************/
466 int DirControl( access_t *p_access, int i_query, va_list args )
471 case ACCESS_CAN_SEEK:
472 case ACCESS_CAN_FASTSEEK:
473 *va_arg( args, bool* ) = false;
476 case ACCESS_CAN_PAUSE:
477 case ACCESS_CAN_CONTROL_PACE:
478 *va_arg( args, bool* ) = true;
482 case ACCESS_GET_PTS_DELAY:
483 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
487 case ACCESS_SET_PAUSE_STATE:
488 case ACCESS_GET_TITLE_INFO:
489 case ACCESS_SET_TITLE:
490 case ACCESS_SET_SEEKPOINT:
491 case ACCESS_SET_PRIVATE_ID_STATE:
492 case ACCESS_GET_CONTENT_TYPE:
493 case ACCESS_GET_META:
497 msg_Warn( p_access, "unimplemented query in control" );