directory.c 11.6 KB
Newer Older
1
/*****************************************************************************
2
 * directory.c: expands a directory (directory: access_browser plug-in)
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002-2008 VLC authors and VideoLAN
5
 * $Id$
6
 *
7
 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8
 *          Rémi Denis-Courmont
9
 *          Julien 'Lta' BALLET <contact # lta.io>
10
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
11 12 13
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
14
 * (at your option) any later version.
15
 *
16 17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
20
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
21 22 23
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35
#include "fs.h"
Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_access.h>
37
#include <vlc_input_item.h>
38

39
#include <sys/types.h>
40
#include <sys/stat.h>
41
#include <errno.h>
42 43
#include <unistd.h>
#include <fcntl.h>
44

45
#include <vlc_fs.h>
46
#include <vlc_url.h>
47
#include <vlc_strings.h>
48
#include <vlc_charset.h>
49

50 51 52 53 54 55 56
enum
{
    ENTRY_DIR       = 0,
    ENTRY_ENOTDIR   = -1,
    ENTRY_EACCESS   = -2,
};

57 58
enum
{
59
    MODE_NONE,
60
    MODE_COLLAPSE,
61
    MODE_EXPAND,
62 63
};

64 65 66 67 68 69
typedef struct directory_t directory_t;
struct directory_t
{
    directory_t *parent;
    DIR         *handle;
    char        *uri;
70 71
    char       **filev;
    int          filec, i;
72 73 74 75
#ifdef HAVE_OPENAT
    dev_t        device;
    ino_t        inode;
#else
76 77
    char         *path;
#endif
78
};
79

80 81 82 83
struct access_sys_t
{
    directory_t *current;
    char *ignored_exts;
84 85
    char mode;
    bool header;
86
    int i_item_count;
87
    char *xspf_ext;
88
    int (*compar)(const char **a, const char **b);
89
};
90

91 92 93 94 95 96
/* Select non-hidden files only */
static int visible (const char *name)
{
    return name[0] != '.';
}

97 98 99 100 101 102 103 104 105
static int collate (const char **a, const char **b)
{
#ifdef HAVE_STRCOLL
    return strcoll (*a, *b);
#else
    return strcmp  (*a, *b);
#endif
}

106 107 108 109 110
static int version (const char **a, const char **b)
{
    return strverscmp (*a, *b);
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
 /**
+ * Does the provided URI/path/stuff has one of the extension provided ?
+ *
+ * \param psz_exts A comma separated list of extension without dot, or only
+ * one ext (ex: "avi,mkv,webm")
+ * \param psz_uri The uri/path to check (ex: "file:///home/foo/bar.avi")
+ *
+ * \return true if the uri/path has one of the provided extension
+ * false otherwise.
+ */
static bool has_ext (const char *psz_exts, const char *psz_uri)
{
    if (psz_exts == NULL)
        return false;

    const char *ext = strrchr (psz_uri, '.');
    if (ext == NULL)
        return false;

    size_t extlen = strlen (++ext);

    for (const char *type = psz_exts, *end; type[0]; type = end + 1)
    {
        end = strchr (type, ',');
        if (end == NULL)
            end = type + strlen (type);

        if (type + extlen == end && !strncasecmp (ext, type, extlen))
            return true;

        if (*end == '\0')
            break;
    }

    return false;
}


#ifdef HAVE_OPENAT
/* Detect directories that recurse into themselves. */
static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
{
    while (dir != NULL)
    {
        if ((dir->device == dev) && (dir->inode == inode))
            return true;
        dir = dir->parent;
    }
    return false;
}
#endif

/* success -> returns ENTRY_DIR and the handle parameter is set to the handle,
 * error -> return ENTRY_ENOTDIR or ENTRY_EACCESS */
static int directory_open (directory_t *p_dir, char *psz_entry, DIR **handle)
{
    *handle = NULL;

#ifdef HAVE_OPENAT
    int fd = vlc_openat (dirfd (p_dir->handle), psz_entry,
                         O_RDONLY | O_DIRECTORY);

    if (fd == -1)
    {
        if (errno == ENOTDIR)
            return ENTRY_ENOTDIR;
        else
            return ENTRY_EACCESS;
    }

    struct stat st;
    if (fstat (fd, &st)
        || has_inode_loop (p_dir, st.st_dev, st.st_ino)
        || (*handle = fdopendir (fd)) == NULL)
    {
        close (fd);
        return ENTRY_EACCESS;
    }
#else
    char *path;
    if (asprintf (&path, "%s/%s", current->path, entry) == -1)
        goto ENTRY_EACCESS;
    if ((*handle = vlc_opendir (path)) == NULL)
        goto ENTRY_ENOTDIR;
#endif

    return ENTRY_DIR;
}

static bool directory_push (access_sys_t *p_sys, DIR *handle, char *psz_uri)
{
    directory_t *p_dir;

    p_dir = malloc (sizeof (*p_dir));
    if (unlikely (p_dir == NULL))
        return NULL;

    psz_uri = strdup (psz_uri);
    if (unlikely (psz_uri == NULL))
        goto error;

    p_dir->parent = p_sys->current;
    p_dir->handle = handle;
    p_dir->uri = psz_uri;
    p_dir->filec = vlc_loaddir (handle, &p_dir->filev, visible, p_sys->compar);
    if (p_dir->filec < 0)
        p_dir->filev = NULL;
    p_dir->i = 0;

#ifdef HAVE_OPENAT
    struct stat st;
    if (fstat (dirfd (handle), &st))
        goto error;
    p_dir->device = st.st_dev;
    p_dir->inode = st.st_ino;
#else
    p_dir->path = make_path (psz_uri);
    if (p_dir->path == NULL)
        goto error;
#endif

    p_sys->current = p_dir;
    return true;

    error:
    closedir (handle);
    free (p_dir);
    free (psz_uri);

    return false;
}

static bool directory_pop (access_sys_t *p_sys)
{
    directory_t *p_old = p_sys->current;

    if (p_old == NULL)
        return false;

    p_sys->current = p_old->parent;
    closedir (p_old->handle);
    free (p_old->uri);
    free (p_old->filev);
#ifndef HAVE_OPENAT
    free (p_old->path);
#endif
    free (p_old);

    return p_sys->current != NULL;
}


263 264 265
/*****************************************************************************
 * Open: open the directory
 *****************************************************************************/
266
int DirOpen( vlc_object_t *p_this )
267
{
268
    access_t *p_access = (access_t*)p_this;
269

270
    if( !p_access->psz_filepath )
271 272
        return VLC_EGENERIC;

273
    DIR *handle = vlc_opendir (p_access->psz_filepath);
274
    if (handle == NULL)
275
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
276

277 278 279 280 281 282
    return DirInit (p_access, handle);
}

int DirInit (access_t *p_access, DIR *handle)
{
    access_sys_t *p_sys = malloc (sizeof (*p_sys));
283 284 285
    if (unlikely(p_sys == NULL))
        goto error;

286 287 288 289 290 291 292 293 294 295 296
    char *psz_sort = var_InheritString (p_access, "directory-sort");
    if (!psz_sort)
        p_sys->compar = collate;
    else if (!strcasecmp ( psz_sort, "version"))
        p_sys->compar = version;
    else if (!strcasecmp (psz_sort, "none"))
        p_sys->compar = NULL;
    else
        p_sys->compar = collate;
    free(psz_sort);

297 298
    char *uri;
    if (!strcmp (p_access->psz_access, "fd"))
299
    {
300
        if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
301
            uri = NULL;
302
    }
303
    else
304
        uri = vlc_path2uri (p_access->psz_filepath, "file");
305 306
    if (unlikely(uri == NULL))
        goto error;
307

308
    /* "Open" the base directory */
309 310
    p_sys->current = NULL;
    if( !directory_push (p_sys, handle, uri))
311 312 313 314
    {
        free (uri);
        goto error;
    }
315
    free (uri);
316

317
    p_access->p_sys = p_sys;
318
    p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
319

320

321
    /* Handle mode */
322 323
    char *psz_rec = var_InheritString (p_access, "recursive");
    if (psz_rec == NULL || !strcasecmp (psz_rec, "none"))
324 325 326 327 328 329 330
        p_sys->mode = MODE_NONE;
    else if( !strcasecmp( psz, "collapse" )  )
        p_sys->mode = MODE_COLLAPSE;
    else
        p_sys->mode = MODE_EXPAND;
    free( psz );

331
    p_access->pf_readdir = DirRead;
332
    p_access->pf_control = DirControl;
333
    return VLC_SUCCESS;
334 335 336 337 338

error:
    closedir (handle);
    free (p_sys);
    return VLC_EGENERIC;
339 340 341 342 343
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
344
void DirClose( vlc_object_t * p_this )
345
{
346
    access_t *p_access = (access_t*)p_this;
347 348
    access_sys_t *p_sys = p_access->p_sys;

349 350
    while (directory_pop (p_sys))
        ;
351

352 353
    free (p_sys->ignored_exts);
    free (p_sys);
354 355
}

356 357 358 359
/* This function is a little bit too complex for what it seems to do, but the
 * point is to de-recursify directory recusion to avoid overruning the stack
 * in case there's a high directory depth */
int DirRead (access_t *p_access, input_item_node_t *p_current_node)
360 361
{
    access_sys_t *p_sys = p_access->p_sys;
362

363 364 365 366
    while (p_sys->current != NULL
           && p_sys->current->i <= p_sys->current->filec)
    {
        directory_t *p_current = p_sys->current;
367

368 369
        /* End of the current folder, let's pop directory and node */
        if (p_current->i == p_current->filec)
370
        {
371 372 373
            directory_pop (p_sys);
            p_current_node = p_current_node->p_parent;
            continue;
374
        }
375

376 377
        char *psz_entry = p_current->filev[p_current->i++];
        char *psz_full_uri, *psz_uri;
378
        DIR *handle;
379 380
        input_item_t *p_new = NULL;
        int i_res;
381

382 383 384 385 386 387 388 389 390 391 392 393 394
        /* Check if it is a directory or even readable */
        i_res = directory_open (p_current, psz_entry, &handle);

        if (i_res == ENTRY_EACCESS
            || (i_res == ENTRY_DIR && p_sys->mode == MODE_NONE)
            || (i_res == ENTRY_ENOTDIR && has_ext (p_sys->ignored_exts, psz_entry)))
            continue;


        /* Create an input item for the current entry */
        psz_uri = encode_URI_component (psz_entry);
        if (psz_uri == NULL
            || asprintf (&psz_full_uri, "%s/%s", p_current->uri, psz_uri) == -1)
395 396
        {
            closedir (handle);
397
            continue;
398
        }
399 400 401 402 403 404
        free (psz_uri);

        int i_type = i_res == ENTRY_DIR ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
        p_new = input_item_NewWithType (psz_full_uri, psz_entry,
                                        0, NULL, 0, 0, i_type);
        if (p_new == NULL)
405
        {
406 407 408
            free (psz_full_uri);
            closedir (handle);
            continue;
409
        }
410

411 412
        input_item_CopyOptions (p_current_node->p_item, p_new);
        input_item_node_t *p_new_node = input_item_node_AppendItem (p_current_node, p_new);
413

414 415
        /* Handle directory flags and recursion if in EXPAND mode  */
        if (i_res == ENTRY_DIR)
416
        {
417 418
            if( p_sys->mode == MODE_EXPAND
                && directory_push (p_sys, handle, psz_full_uri))
419
            {
420
                p_current_node = p_new_node;
421 422
            }
        }
423 424 425

        free (psz_full_uri);
        input_item_Release (p_new);
426
    }
427

428
    return VLC_SUCCESS;
429 430 431
}

/*****************************************************************************
432
 * Control:
433
 *****************************************************************************/
434
int DirControl( access_t *p_access, int i_query, va_list args )
435
{
436 437 438
    VLC_UNUSED (p_access);

    switch (i_query)
439 440 441
    {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
442
            *va_arg( args, bool* ) = false;
443 444
            break;

445 446
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
447
            *va_arg( args, bool* ) = true;
448 449 450
            break;

        case ACCESS_GET_PTS_DELAY:
451
            *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
452 453 454 455
            break;

        default:
            return VLC_EGENERIC;
456 457 458
     }
     return VLC_SUCCESS;
 }