1 /*****************************************************************************
2 * directory.c: expands a directory (directory: access_browser plug-in)
3 *****************************************************************************
4 * Copyright (C) 2002-2008 VLC authors and VideoLAN
7 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
9 * Julien 'Lta' BALLET <contact # lta.io>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_access.h>
37 #include <vlc_input_item.h>
39 #include <sys/types.h>
47 #include <vlc_strings.h>
48 #include <vlc_charset.h>
64 typedef struct directory_t directory_t;
85 int (*compar) (const char **a, const char **b);
88 /* Select non-hidden files only */
89 static int visible (const char *name)
91 return name[0] != '.';
94 static int collate (const char **a, const char **b)
97 return strcoll (*a, *b);
99 return strcmp (*a, *b);
103 static int version (const char **a, const char **b)
105 return strverscmp (*a, *b);
109 * Does the provided URI/path/stuff has one of the extension provided ?
111 * \param psz_exts A comma separated list of extension without dot, or only
112 * one ext (ex: "avi,mkv,webm")
113 * \param psz_uri The uri/path to check (ex: "file:///home/foo/bar.avi"). If
114 * providing an URI, it must not contain a query string.
116 * \return true if the uri/path has one of the provided extension
119 static bool has_ext (const char *psz_exts, const char *psz_uri)
121 if (psz_exts == NULL)
124 const char *ext = strrchr (psz_uri, '.');
128 size_t extlen = strlen (++ext);
130 for (const char *type = psz_exts, *end; type[0]; type = end + 1)
132 end = strchr (type, ',');
134 end = type + strlen (type);
136 if (type + extlen == end && !strncasecmp (ext, type, extlen))
148 /* Detect directories that recurse into themselves. */
149 static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
153 if ((dir->device == dev) && (dir->inode == inode))
161 /* success -> returns ENTRY_DIR and the handle parameter is set to the handle,
162 * error -> return ENTRY_ENOTDIR or ENTRY_EACCESS */
163 static int directory_open (directory_t *p_dir, char *psz_entry, DIR **handle)
168 int fd = vlc_openat (dirfd (p_dir->handle), psz_entry,
169 O_RDONLY | O_DIRECTORY);
173 if (errno == ENOTDIR)
174 return ENTRY_ENOTDIR;
176 return ENTRY_EACCESS;
181 || has_inode_loop (p_dir, st.st_dev, st.st_ino)
182 || (*handle = fdopendir (fd)) == NULL)
185 return ENTRY_EACCESS;
189 if (asprintf (&path, "%s/%s", current->path, entry) == -1)
191 if ((*handle = vlc_opendir (path)) == NULL)
198 static bool directory_push (access_sys_t *p_sys, DIR *handle, char *psz_uri)
202 p_dir = malloc (sizeof (*p_dir));
203 if (unlikely (p_dir == NULL))
206 psz_uri = strdup (psz_uri);
207 if (unlikely (psz_uri == NULL))
210 p_dir->parent = p_sys->current;
211 p_dir->handle = handle;
212 p_dir->uri = psz_uri;
213 p_dir->filec = vlc_loaddir (handle, &p_dir->filev, visible, p_sys->compar);
214 if (p_dir->filec < 0)
220 if (fstat (dirfd (handle), &st))
222 p_dir->device = st.st_dev;
223 p_dir->inode = st.st_ino;
225 p_dir->path = make_path (psz_uri);
226 if (p_dir->path == NULL)
230 p_sys->current = p_dir;
241 static bool directory_pop (access_sys_t *p_sys)
243 directory_t *p_old = p_sys->current;
248 p_sys->current = p_old->parent;
249 closedir (p_old->handle);
257 return p_sys->current != NULL;
261 /*****************************************************************************
262 * Open: open the directory
263 *****************************************************************************/
264 int DirOpen (vlc_object_t *p_this)
266 access_t *p_access = (access_t*)p_this;
268 if (!p_access->psz_filepath)
271 DIR *handle = vlc_opendir (p_access->psz_filepath);
275 return DirInit (p_access, handle);
278 int DirInit (access_t *p_access, DIR *handle)
280 access_sys_t *p_sys = malloc (sizeof (*p_sys));
281 if (unlikely (p_sys == NULL))
284 char *psz_sort = var_InheritString (p_access, "directory-sort");
286 p_sys->compar = collate;
287 else if (!strcasecmp ( psz_sort, "version"))
288 p_sys->compar = version;
289 else if (!strcasecmp (psz_sort, "none"))
290 p_sys->compar = NULL;
292 p_sys->compar = collate;
296 if (!strcmp (p_access->psz_access, "fd"))
298 if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
302 uri = vlc_path2uri (p_access->psz_filepath, "file");
303 if (unlikely (uri == NULL))
306 /* "Open" the base directory */
307 p_sys->current = NULL;
308 if (!directory_push (p_sys, handle, uri))
315 p_access->p_sys = p_sys;
316 p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
320 char *psz_rec = var_InheritString (p_access, "recursive");
321 if (psz_rec == NULL || !strcasecmp (psz_rec, "none"))
322 p_sys->mode = MODE_NONE;
323 else if (!strcasecmp (psz_rec, "collapse"))
324 p_sys->mode = MODE_COLLAPSE;
326 p_sys->mode = MODE_EXPAND;
329 p_access->pf_readdir = DirRead;
330 p_access->pf_control = DirControl;
340 /*****************************************************************************
341 * Close: close the target
342 *****************************************************************************/
343 void DirClose( vlc_object_t * p_this )
345 access_t *p_access = (access_t*)p_this;
346 access_sys_t *p_sys = p_access->p_sys;
348 while (directory_pop (p_sys))
351 free (p_sys->ignored_exts);
355 /* This function is a little bit too complex for what it seems to do, but the
356 * point is to de-recursify directory recusion to avoid overruning the stack
357 * in case there's a high directory depth */
358 int DirRead (access_t *p_access, input_item_node_t *p_current_node)
360 access_sys_t *p_sys = p_access->p_sys;
362 while (p_sys->current != NULL
363 && p_sys->current->i <= p_sys->current->filec)
365 directory_t *p_current = p_sys->current;
367 /* End of the current folder, let's pop directory and node */
368 if (p_current->i == p_current->filec)
370 directory_pop (p_sys);
371 p_current_node = p_current_node->p_parent;
375 char *psz_entry = p_current->filev[p_current->i++];
376 char *psz_full_uri, *psz_uri;
378 input_item_t *p_new = NULL;
381 /* Check if it is a directory or even readable */
382 i_res = directory_open (p_current, psz_entry, &handle);
384 if (i_res == ENTRY_EACCESS
385 || (i_res == ENTRY_DIR && p_sys->mode == MODE_NONE)
386 || (i_res == ENTRY_ENOTDIR && has_ext (p_sys->ignored_exts, psz_entry)))
390 /* Create an input item for the current entry */
391 psz_uri = encode_URI_component (psz_entry);
393 || asprintf (&psz_full_uri, "%s/%s", p_current->uri, psz_uri) == -1)
400 int i_type = i_res == ENTRY_DIR ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
401 p_new = input_item_NewWithType (psz_full_uri, psz_entry,
402 0, NULL, 0, 0, i_type);
410 input_item_CopyOptions (p_current_node->p_item, p_new);
411 input_item_node_t *p_new_node = input_item_node_AppendItem (p_current_node, p_new);
413 /* Handle directory flags and recursion if in EXPAND mode */
414 if (i_res == ENTRY_DIR)
416 if (p_sys->mode == MODE_EXPAND
417 && directory_push (p_sys, handle, psz_full_uri))
419 p_current_node = p_new_node;
424 input_item_Release (p_new);
430 /*****************************************************************************
432 *****************************************************************************/
433 int DirControl (access_t *p_access, int i_query, va_list args)
435 VLC_UNUSED (p_access);
439 case ACCESS_CAN_SEEK:
440 case ACCESS_CAN_FASTSEEK:
441 *va_arg (args, bool*) = false;
444 case ACCESS_CAN_PAUSE:
445 case ACCESS_CAN_CONTROL_PACE:
446 *va_arg (args, bool*) = true;
449 case ACCESS_GET_PTS_DELAY:
450 *va_arg (args, int64_t *) = DEFAULT_PTS_DELAY * 1000;