1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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>
43 #elif defined( _WIN32 )
49 #include <vlc_strings.h>
50 #include <vlc_charset.h>
59 typedef struct directory_t directory_t;
83 int (*compar)(const char **a, const char **b);
86 /* Select non-hidden files only */
87 static int visible (const char *name)
89 return name[0] != '.';
92 static int collate (const char **a, const char **b)
95 return strcoll (*a, *b);
97 return strcmp (*a, *b);
101 static int version (const char **a, const char **b)
103 return strverscmp (*a, *b);
106 /*****************************************************************************
107 * Open: open the directory
108 *****************************************************************************/
109 int DirOpen( vlc_object_t *p_this )
111 access_t *p_access = (access_t*)p_this;
113 if( !p_access->psz_filepath )
116 DIR *handle = vlc_opendir (p_access->psz_filepath);
120 return DirInit (p_access, handle);
123 int DirInit (access_t *p_access, DIR *handle)
125 access_sys_t *p_sys = malloc (sizeof (*p_sys));
126 if (unlikely(p_sys == NULL))
130 if (!strcmp (p_access->psz_access, "fd"))
132 if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
136 uri = vlc_path2uri (p_access->psz_filepath, "file");
137 if (unlikely(uri == NULL))
140 /* "Open" the base directory */
141 directory_t *root = malloc (sizeof (*root));
142 if (unlikely(root == NULL))
148 char *psz_sort = var_InheritString (p_access, "directory-sort");
150 p_sys->compar = collate;
151 else if (!strcasecmp (psz_sort, "version"))
152 p_sys->compar = version;
153 else if (!strcasecmp (psz_sort, "none"))
154 p_sys->compar = NULL;
156 p_sys->compar = collate;
160 root->handle = handle;
162 root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
168 if (fstat (dirfd (handle), &st))
174 root->device = st.st_dev;
175 root->inode = st.st_ino;
177 root->path = strdup (p_access->psz_filepath);
180 p_access->p_sys = p_sys;
181 p_sys->current = root;
182 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
183 p_sys->header = true;
184 p_sys->i_item_count = 0;
185 p_sys->xspf_ext = strdup ("");
188 char *psz = var_InheritString (p_access, "recursive");
189 if (psz == NULL || !strcasecmp (psz, "none"))
190 p_sys->mode = MODE_NONE;
191 else if( !strcasecmp( psz, "collapse" ) )
192 p_sys->mode = MODE_COLLAPSE;
194 p_sys->mode = MODE_EXPAND;
197 access_InitFields(p_access);
198 p_access->pf_read = NULL;
199 p_access->pf_block = DirBlock;
200 p_access->pf_seek = NULL;
201 p_access->pf_control= DirControl;
202 free (p_access->psz_demux);
203 p_access->psz_demux = strdup ("xspf-open");
213 /*****************************************************************************
214 * Close: close the target
215 *****************************************************************************/
216 void DirClose( vlc_object_t * p_this )
218 access_t *p_access = (access_t*)p_this;
219 access_sys_t *p_sys = p_access->p_sys;
221 while (p_sys->current)
223 directory_t *current = p_sys->current;
225 p_sys->current = current->parent;
226 closedir (current->handle);
228 while (current->i < current->filec)
229 free (current->filev[current->i++]);
230 free (current->filev);
232 free (current->path);
237 free (p_sys->xspf_ext);
238 free (p_sys->ignored_exts);
243 /* Detect directories that recurse into themselves. */
244 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
248 if ((dir->device == dev) && (dir->inode == inode))
256 block_t *DirBlock (access_t *p_access)
258 access_sys_t *p_sys = p_access->p_sys;
259 directory_t *current = p_sys->current;
261 if (p_access->info.b_eof)
265 { /* Startup: send the XSPF header */
266 static const char header[] =
267 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
268 "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
270 block_t *block = block_Alloc (sizeof (header) - 1);
273 memcpy (block->p_buffer, header, sizeof (header) - 1);
274 p_sys->header = false;
278 if (current->i >= current->filec)
279 { /* End of directory, go back to parent */
280 closedir (current->handle);
281 p_sys->current = current->parent;
283 free (current->filev);
285 free (current->path);
289 if (p_sys->current == NULL)
290 { /* End of XSPF playlist */
292 int len = asprintf (&footer, " </trackList>\n"
293 " <extension application=\"http://www.videolan.org/"
294 "vlc/playlist/0\">\n"
297 "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
298 if (unlikely(len == -1))
301 block_t *block = block_heap_Alloc (footer, len);
302 p_access->info.b_eof = true;
307 /* This was the end of a "subnode" */
308 /* Write the ID to the extension */
309 char *old_xspf_ext = p_sys->xspf_ext;
310 if (old_xspf_ext != NULL
311 && asprintf (&p_sys->xspf_ext, "%s </vlc:node>\n",
312 old_xspf_ext ? old_xspf_ext : "") == -1)
313 p_sys->xspf_ext = NULL;
319 char *entry = current->filev[current->i++];
321 /* Handle recursion */
322 if (p_sys->mode != MODE_COLLAPSE)
326 int fd = vlc_openat (dirfd (current->handle), entry,
327 O_RDONLY | O_DIRECTORY);
330 if (errno == ENOTDIR)
332 goto skip; /* File cannot be opened... forget it */
337 || p_sys->mode == MODE_NONE
338 || has_inode_loop (current, st.st_dev, st.st_ino)
339 || (handle = fdopendir (fd)) == NULL)
346 if (asprintf (&path, "%s/%s", current->path, entry) == -1)
348 if ((handle = vlc_opendir (path)) == NULL)
350 if (p_sys->mode == MODE_NONE)
353 directory_t *sub = malloc (sizeof (*sub));
354 if (unlikely(sub == NULL))
362 sub->parent = current;
363 sub->handle = handle;
364 sub->filec = vlc_loaddir (handle, &sub->filev, visible, p_sys->compar);
369 sub->device = st.st_dev;
370 sub->inode = st.st_ino;
374 p_sys->current = sub;
376 char *encoded = encode_URI_component (entry);
378 || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
381 if (unlikely(sub->uri == NULL))
387 /* Add node to XSPF extension */
388 char *old_xspf_ext = p_sys->xspf_ext;
390 char *title = convert_xml_special_chars (entry);
391 if (old_xspf_ext != NULL
392 && asprintf (&p_sys->xspf_ext, "%s <vlc:node title=\"%s\">\n",
393 old_xspf_ext, title ? title : "?") == -1)
394 p_sys->xspf_ext = NULL;
401 /* Skip files with ignored extensions */
402 if (p_sys->ignored_exts != NULL)
404 const char *ext = strrchr (entry, '.');
407 size_t extlen = strlen (++ext);
408 for (const char *type = p_sys->ignored_exts, *end;
409 type[0]; type = end + 1)
411 end = strchr (type, ',');
413 end = type + strlen (type);
415 if (type + extlen == end
416 && !strncasecmp (ext, type, extlen))
428 char *encoded = encode_URI_component (entry);
432 int len = asprintf (&entry,
433 " <track><location>%s/%s</location>\n" \
434 " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
435 " <vlc:id>%d</vlc:id>\n" \
438 current->uri, encoded, p_sys->i_item_count++);
443 /* Write the ID to the extension */
444 char *old_xspf_ext = p_sys->xspf_ext;
445 if (old_xspf_ext != NULL
446 && asprintf (&p_sys->xspf_ext, "%s <vlc:item tid=\"%i\" />\n",
447 old_xspf_ext, p_sys->i_item_count - 1) == -1)
448 p_sys->xspf_ext = NULL;
451 block_t *block = block_heap_Alloc (entry, len);
452 if (unlikely(block == NULL))
457 p_access->info.b_eof = true;
465 /*****************************************************************************
467 *****************************************************************************/
468 int DirControl( access_t *p_access, int i_query, va_list args )
473 case ACCESS_CAN_SEEK:
474 case ACCESS_CAN_FASTSEEK:
475 *va_arg( args, bool* ) = false;
478 case ACCESS_CAN_PAUSE:
479 case ACCESS_CAN_CONTROL_PACE:
480 *va_arg( args, bool* ) = true;
484 case ACCESS_GET_PTS_DELAY:
485 *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
489 case ACCESS_SET_PAUSE_STATE:
490 case ACCESS_GET_TITLE_INFO:
491 case ACCESS_SET_TITLE:
492 case ACCESS_SET_SEEKPOINT:
493 case ACCESS_SET_PRIVATE_ID_STATE:
494 case ACCESS_GET_CONTENT_TYPE:
495 case ACCESS_GET_META:
499 msg_Warn( p_access, "unimplemented query in control" );