directory.c 13.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * directory.c: expands a directory (directory: access plug-in)
 *****************************************************************************
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
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
10 11 12
 * 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
13
 * (at your option) any later version.
14
 *
15 16
 * 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
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

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

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

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

43
#include <vlc_fs.h>
44
#include <vlc_url.h>
45
#include <vlc_strings.h>
46
#include <vlc_charset.h>
47

48 49
enum
{
50
    MODE_NONE,
51
    MODE_COLLAPSE,
52
    MODE_EXPAND,
53 54
};

55 56 57 58 59 60
typedef struct directory_t directory_t;
struct directory_t
{
    directory_t *parent;
    DIR         *handle;
    char        *uri;
61 62
    char       **filev;
    int          filec, i;
63 64 65 66
#ifdef HAVE_OPENAT
    dev_t        device;
    ino_t        inode;
#else
67 68
    char         *path;
#endif
69
};
70

71 72 73 74
struct access_sys_t
{
    directory_t *current;
    char *ignored_exts;
75 76
    char mode;
    bool header;
77
    int i_item_count;
78
    char *xspf_ext;
79
    int (*compar)(const char **a, const char **b);
80
};
81

82 83 84 85 86 87
/* Select non-hidden files only */
static int visible (const char *name)
{
    return name[0] != '.';
}

88 89 90 91 92 93 94 95 96
static int collate (const char **a, const char **b)
{
#ifdef HAVE_STRCOLL
    return strcoll (*a, *b);
#else
    return strcmp  (*a, *b);
#endif
}

97 98 99 100 101
static int version (const char **a, const char **b)
{
    return strverscmp (*a, *b);
}

102 103 104
/*****************************************************************************
 * Open: open the directory
 *****************************************************************************/
105
int DirOpen( vlc_object_t *p_this )
106
{
107
    access_t *p_access = (access_t*)p_this;
108

109
    if( !p_access->psz_filepath )
110 111
        return VLC_EGENERIC;

112
    DIR *handle = vlc_opendir (p_access->psz_filepath);
113
    if (handle == NULL)
114
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
115

116 117 118 119 120 121
    return DirInit (p_access, handle);
}

int DirInit (access_t *p_access, DIR *handle)
{
    access_sys_t *p_sys = malloc (sizeof (*p_sys));
122 123 124 125 126
    if (unlikely(p_sys == NULL))
        goto error;

    char *uri;
    if (!strcmp (p_access->psz_access, "fd"))
127
    {
128
        if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
129
            uri = NULL;
130
    }
131
    else
132
        uri = vlc_path2uri (p_access->psz_filepath, "file");
133 134
    if (unlikely(uri == NULL))
        goto error;
135

136 137 138 139 140 141 142
    /* "Open" the base directory */
    directory_t *root = malloc (sizeof (*root));
    if (unlikely(root == NULL))
    {
        free (uri);
        goto error;
    }
143

144 145 146 147
    char *psz_sort = var_InheritString (p_access, "directory-sort");
    if (!psz_sort)
        p_sys->compar = collate;
    else if (!strcasecmp (psz_sort, "version"))
148
        p_sys->compar = version;
149 150
    else if (!strcasecmp (psz_sort, "none"))
        p_sys->compar = NULL;
151 152
    else
        p_sys->compar = collate;
153
    free(psz_sort);
154

155 156 157
    root->parent = NULL;
    root->handle = handle;
    root->uri = uri;
158
    root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
159 160 161
    if (root->filec < 0)
        root->filev = NULL;
    root->i = 0;
162 163 164
#ifdef HAVE_OPENAT
    struct stat st;
    if (fstat (dirfd (handle), &st))
165 166 167 168 169
    {
        free (root);
        free (uri);
        goto error;
    }
170 171 172 173 174
    root->device = st.st_dev;
    root->inode = st.st_ino;
#else
    root->path = strdup (p_access->psz_filepath);
#endif
175

176
    p_access->p_sys = p_sys;
177
    p_sys->current = root;
178
    p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
179
    p_sys->header = true;
180
    p_sys->i_item_count = 0;
181
    p_sys->xspf_ext = strdup ("");
182

183
    /* Handle mode */
184 185
    char *psz = var_InheritString (p_access, "recursive");
    if (psz == NULL || !strcasecmp (psz, "none"))
186 187 188 189 190 191 192
        p_sys->mode = MODE_NONE;
    else if( !strcasecmp( psz, "collapse" )  )
        p_sys->mode = MODE_COLLAPSE;
    else
        p_sys->mode = MODE_EXPAND;
    free( psz );

193
    access_InitFields(p_access);
194
    p_access->pf_read  = NULL;
195
    p_access->pf_block = DirBlock;
196
    p_access->pf_seek  = NULL;
197
    p_access->pf_control = DirControl;
198
    return VLC_SUCCESS;
199 200 201 202 203

error:
    closedir (handle);
    free (p_sys);
    return VLC_EGENERIC;
204 205 206 207 208
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
209
void DirClose( vlc_object_t * p_this )
210
{
211
    access_t *p_access = (access_t*)p_this;
212 213 214 215 216 217 218 219 220
    access_sys_t *p_sys = p_access->p_sys;

    while (p_sys->current)
    {
        directory_t *current = p_sys->current;

        p_sys->current = current->parent;
        closedir (current->handle);
        free (current->uri);
221 222 223
        while (current->i < current->filec)
            free (current->filev[current->i++]);
        free (current->filev);
224
#ifndef HAVE_OPENAT
225 226
        free (current->path);
#endif
227 228
        free (current);
    }
229

230
    free (p_sys->xspf_ext);
231 232
    free (p_sys->ignored_exts);
    free (p_sys);
233 234
}

Pierre Ynard's avatar
Pierre Ynard committed
235
#ifdef HAVE_OPENAT
236
/* Detect directories that recurse into themselves. */
237
static bool has_inode_loop (const directory_t *dir, dev_t dev, ino_t inode)
238
{
239 240 241
    while (dir != NULL)
    {
        if ((dir->device == dev) && (dir->inode == inode))
242
            return true;
243 244
        dir = dir->parent;
    }
245 246
    return false;
}
Pierre Ynard's avatar
Pierre Ynard committed
247
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
248

249
block_t *DirBlock (access_t *p_access)
250 251 252
{
    access_sys_t *p_sys = p_access->p_sys;
    directory_t *current = p_sys->current;
253

254 255
    if (p_access->info.b_eof)
        return NULL;
256

257
    if (p_sys->header)
258 259 260
    {   /* Startup: send the XSPF header */
        static const char header[] =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
261
            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
262 263 264 265 266
            " <trackList>\n";
        block_t *block = block_Alloc (sizeof (header) - 1);
        if (!block)
            goto fatal;
        memcpy (block->p_buffer, header, sizeof (header) - 1);
267
        p_sys->header = false;
268 269
        return block;
    }
270

271
    if (current->i >= current->filec)
272 273 274
    {   /* End of directory, go back to parent */
        closedir (current->handle);
        p_sys->current = current->parent;
275
        free (current->uri);
276
        free (current->filev);
277
#ifndef HAVE_OPENAT
278 279
        free (current->path);
#endif
280 281 282 283
        free (current);

        if (p_sys->current == NULL)
        {   /* End of XSPF playlist */
284
            char *footer;
285 286 287 288 289 290
            int len = asprintf (&footer, " </trackList>\n"
                " <extension application=\"http://www.videolan.org/"
                                             "vlc/playlist/0\">\n"
                "%s"
                " </extension>\n"
                "</playlist>\n", p_sys->xspf_ext ? p_sys->xspf_ext : "");
291
            if (unlikely(len == -1))
292
                goto fatal;
293

294
            block_t *block = block_heap_Alloc (footer, len);
295 296 297
            p_access->info.b_eof = true;
            return block;
        }
298 299 300 301
        else
        {
            /* This was the end of a "subnode" */
            /* Write the ID to the extension */
302 303 304 305 306 307
            char *old_xspf_ext = p_sys->xspf_ext;
            if (old_xspf_ext != NULL
             && asprintf (&p_sys->xspf_ext, "%s  </vlc:node>\n",
                          old_xspf_ext ? old_xspf_ext : "") == -1)
                p_sys->xspf_ext = NULL;
            free (old_xspf_ext);
308
        }
309
        return NULL;
310 311
    }

312
    char *entry = current->filev[current->i++];
313

314 315
    /* Handle recursion */
    if (p_sys->mode != MODE_COLLAPSE)
316
    {
317
        DIR *handle;
318
#ifdef HAVE_OPENAT
319 320
        int fd = vlc_openat (dirfd (current->handle), entry,
                             O_RDONLY | O_DIRECTORY);
321
        if (fd == -1)
322 323 324
        {
            if (errno == ENOTDIR)
                goto notdir;
325
            goto skip; /* File cannot be opened... forget it */
326
        }
327 328

        struct stat st;
329 330
        if (fstat (fd, &st)
         || p_sys->mode == MODE_NONE
331 332 333 334 335
         || has_inode_loop (current, st.st_dev, st.st_ino)
         || (handle = fdopendir (fd)) == NULL)
        {
            close (fd);
            goto skip;
336 337
        }
#else
338 339 340 341 342 343 344
        char *path;
        if (asprintf (&path, "%s/%s", current->path, entry) == -1)
            goto skip;
        if ((handle = vlc_opendir (path)) == NULL)
            goto notdir;
        if (p_sys->mode == MODE_NONE)
            goto skip;
345
#endif
346 347 348 349 350 351 352 353 354 355 356
        directory_t *sub = malloc (sizeof (*sub));
        if (unlikely(sub == NULL))
        {
            closedir (handle);
#ifndef HAVE_OPENAT
            free (path);
#endif
            goto skip;
        }
        sub->parent = current;
        sub->handle = handle;
357
        sub->filec = vlc_loaddir (handle, &sub->filev, visible, p_sys->compar);
358 359 360
        if (sub->filec < 0)
            sub->filev = NULL;
        sub->i = 0;
361 362 363 364 365 366 367 368 369 370 371 372 373 374
#ifdef HAVE_OPENAT
        sub->device = st.st_dev;
        sub->inode = st.st_ino;
#else
        sub->path = path;
#endif
        p_sys->current = sub;

        char *encoded = encode_URI_component (entry);
        if (encoded == NULL
         || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
             sub->uri = NULL;
        free (encoded);
        if (unlikely(sub->uri == NULL))
375
        {
376
            free (entry);
377
            goto fatal;
378
        }
379 380 381

        /* Add node to XSPF extension */
        char *old_xspf_ext = p_sys->xspf_ext;
382
        EnsureUTF8 (entry);
383
        char *title = convert_xml_special_chars (entry);
384
        if (old_xspf_ext != NULL
385
         && asprintf (&p_sys->xspf_ext, "%s  <vlc:node title=\"%s\">\n",
386
                      old_xspf_ext, title ? title : "?") == -1)
387 388 389 390
            p_sys->xspf_ext = NULL;
        free (old_xspf_ext);
        free (title);
        goto skip;
391
    }
392

393
notdir:
394 395 396 397 398 399 400 401 402 403 404 405 406
    /* Skip files with ignored extensions */
    if (p_sys->ignored_exts != NULL)
    {
        const char *ext = strrchr (entry, '.');
        if (ext != NULL)
        {
            size_t extlen = strlen (++ext);
            for (const char *type = p_sys->ignored_exts, *end;
                 type[0]; type = end + 1)
            {
                end = strchr (type, ',');
                if (end == NULL)
                    end = type + strlen (type);
407

408 409
                if (type + extlen == end
                 && !strncasecmp (ext, type, extlen))
410 411
                {
                    free (entry);
412
                    return NULL;
413
                }
414 415 416

                if (*end == '\0')
                    break;
417 418 419
            }
        }
    }
420

421 422 423 424 425
    char *encoded = encode_URI_component (entry);
    free (entry);
    if (encoded == NULL)
        goto fatal;
    int len = asprintf (&entry,
426
                        "  <track><location>%s/%s</location>\n" \
427 428 429 430 431
                        "   <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
                        "    <vlc:id>%d</vlc:id>\n" \
                        "   </extension>\n" \
                        "  </track>\n",
                        current->uri, encoded, p_sys->i_item_count++);
432 433 434 435
    free (encoded);
    if (len == -1)
        goto fatal;

436
    /* Write the ID to the extension */
437 438 439 440 441 442
    char *old_xspf_ext = p_sys->xspf_ext;
    if (old_xspf_ext != NULL
     && asprintf (&p_sys->xspf_ext, "%s   <vlc:item tid=\"%i\" />\n",
                  old_xspf_ext, p_sys->i_item_count - 1) == -1)
        p_sys->xspf_ext = NULL;
    free (old_xspf_ext);
443

444
    block_t *block = block_heap_Alloc (entry, len);
445
    if (unlikely(block == NULL))
446 447
        goto fatal;
    return block;
448

449 450 451
fatal:
    p_access->info.b_eof = true;
    return NULL;
452 453 454 455

skip:
    free (entry);
    return NULL;
456 457 458
}

/*****************************************************************************
459
 * Control:
460
 *****************************************************************************/
461
int DirControl( access_t *p_access, int i_query, va_list args )
462 463 464 465 466
{
    switch( i_query )
    {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
467
            *va_arg( args, bool* ) = false;
468 469
            break;

470 471
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
472
            *va_arg( args, bool* ) = true;
473 474 475
            break;

        case ACCESS_GET_PTS_DELAY:
476
            *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
477 478
            break;

479 480 481 482
        case ACCESS_GET_CONTENT_TYPE:
            *va_arg( args, char** ) = strdup("application/xspf+xml");
            return VLC_SUCCESS;

483 484 485
        default:
            return VLC_EGENERIC;
    }
486
    (void) p_access;
487 488
    return VLC_SUCCESS;
}