directory.c 13.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * directory.c: expands a directory (directory: access plug-in)
 *****************************************************************************
4
 * Copyright (C) 2002-2008 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Derk-Jan Hartman <hartman at videolan dot org>
8
 *          Rémi Denis-Courmont
9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * 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 39 40 41 42 43
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
44
#   include <fcntl.h>
45 46 47 48
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
#endif

49
#ifdef HAVE_DIRENT_H
50 51
#   include <dirent.h>
#endif
52 53 54 55 56 57
#ifdef __sun__
static inline int dirfd (DIR *dir)
{
    return dir->dd_fd;
}
#endif
58

59
#include <vlc_fs.h>
60
#include <vlc_url.h>
61
#include <vlc_strings.h>
62

63 64
enum
{
65
    MODE_NONE,
66
    MODE_COLLAPSE,
67
    MODE_EXPAND,
68 69
};

70 71 72 73 74 75
typedef struct directory_t directory_t;
struct directory_t
{
    directory_t *parent;
    DIR         *handle;
    char        *uri;
76
#ifndef WIN32
77
    struct stat  st;
78
#endif
79
#ifndef HAVE_OPENAT
80 81
    char         *path;
#endif
82
};
83

84 85 86 87
struct access_sys_t
{
    directory_t *current;
    DIR *handle;
88
    char *uri;
89 90
    char *ignored_exts;
    int mode;
91 92
    int i_item_count;
    char *psz_xspf_extension;
93
};
94

95 96 97
/*****************************************************************************
 * Open: open the directory
 *****************************************************************************/
98
int DirOpen( vlc_object_t *p_this )
99
{
100
    access_t *p_access = (access_t*)p_this;
101

102
    if( !p_access->psz_filepath )
103 104
        return VLC_EGENERIC;

105
    DIR *handle = vlc_opendir (p_access->psz_filepath);
106
    if (handle == NULL)
107
        return VLC_EGENERIC;
108

109 110 111 112 113 114
    return DirInit (p_access, handle);
}

int DirInit (access_t *p_access, DIR *handle)
{
    access_sys_t *p_sys = malloc (sizeof (*p_sys));
115 116 117 118 119
    if (unlikely(p_sys == NULL))
        goto error;

    char *uri;
    if (!strcmp (p_access->psz_access, "fd"))
120
    {
121
        if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
122
            uri = NULL;
123
    }
124
    else
125
        uri = make_URI (p_access->psz_filepath);
126 127
    if (unlikely(uri == NULL))
        goto error;
128 129 130 131

    p_access->p_sys = p_sys;
    p_sys->current = NULL;
    p_sys->handle = handle;
132 133
    p_sys->uri = uri;
    p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
134 135
    p_sys->i_item_count = 0;
    p_sys->psz_xspf_extension = strdup( "" );
136

137
    /* Handle mode */
138 139
    char *psz = var_InheritString (p_access, "recursive");
    if (psz == NULL || !strcasecmp (psz, "none"))
140 141 142 143 144 145 146
        p_sys->mode = MODE_NONE;
    else if( !strcasecmp( psz, "collapse" )  )
        p_sys->mode = MODE_COLLAPSE;
    else
        p_sys->mode = MODE_EXPAND;
    free( psz );

147
    access_InitFields(p_access);
148
    p_access->pf_read  = NULL;
149
    p_access->pf_block = DirBlock;
150
    p_access->pf_seek  = NULL;
151
    p_access->pf_control= DirControl;
152 153
    free (p_access->psz_demux);
    p_access->psz_demux = strdup ("xspf-open");
154

155
    return VLC_SUCCESS;
156 157 158 159 160

error:
    closedir (handle);
    free (p_sys);
    return VLC_EGENERIC;
161 162 163 164 165
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
166
void DirClose( vlc_object_t * p_this )
167
{
168
    access_t *p_access = (access_t*)p_this;
169 170 171 172 173 174 175 176 177
    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);
178
#ifndef HAVE_OPENAT
179 180
        free (current->path);
#endif
181 182
        free (current);
    }
183 184

    /* corner case: Block() not called ever */
185
    if (p_sys->handle != NULL)
186 187 188
        closedir (p_sys->handle);
    free (p_sys->uri);

189
    free (p_sys->psz_xspf_extension);
190 191
    free (p_sys->ignored_exts);
    free (p_sys);
192 193
}

194 195
/* Detect directories that recurse into themselves. */
static bool has_inode_loop (const directory_t *dir)
196
{
197
#ifndef WIN32
198 199
    dev_t dev = dir->st.st_dev;
    ino_t inode = dir->st.st_ino;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
200

201 202 203
    while ((dir = dir->parent) != NULL)
        if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
            return true;
204
#else
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
205
# undef fstat
206
# define fstat( fd, st ) (0)
207
    VLC_UNUSED( dir );
208
#endif
209 210
    return false;
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
211

212
block_t *DirBlock (access_t *p_access)
213 214 215
{
    access_sys_t *p_sys = p_access->p_sys;
    directory_t *current = p_sys->current;
216

217 218
    if (p_access->info.b_eof)
        return NULL;
219

220 221 222 223
    if (current == NULL)
    {   /* Startup: send the XSPF header */
        static const char header[] =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
224
            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
225 226 227 228 229 230 231
            " <trackList>\n";
        block_t *block = block_Alloc (sizeof (header) - 1);
        if (!block)
            goto fatal;
        memcpy (block->p_buffer, header, sizeof (header) - 1);

        /* "Open" the base directory */
232
        current = malloc (sizeof (*current));
233 234 235 236 237 238 239
        if (current == NULL)
        {
            block_Release (block);
            goto fatal;
        }
        current->parent = NULL;
        current->handle = p_sys->handle;
240
#ifndef HAVE_OPENAT
241
        current->path = strdup (p_access->psz_filepath);
242
#endif
243 244
        current->uri = p_sys->uri;
        if (fstat (dirfd (current->handle), &current->st))
245 246 247 248 249
        {
            free (current);
            block_Release (block);
            goto fatal;
        }
250

251
        p_sys->handle = NULL;
252
        p_sys->uri = NULL;
253 254 255
        p_sys->current = current;
        return block;
    }
256

257
    char *entry = vlc_readdir (current->handle);
258 259 260 261
    if (entry == NULL)
    {   /* End of directory, go back to parent */
        closedir (current->handle);
        p_sys->current = current->parent;
262
        free (current->uri);
263
#ifndef HAVE_OPENAT
264 265
        free (current->path);
#endif
266 267 268 269
        free (current);

        if (p_sys->current == NULL)
        {   /* End of XSPF playlist */
270 271 272 273 274 275
            char *footer;
            int len = asprintf( &footer, " </trackList>\n" \
                " <extension application=\"http://www.videolan.org/vlc/playlist/0\">\n" \
                "%s" \
                " </extension>\n" \
                "</playlist>\n", p_sys->psz_xspf_extension );
276
            if (unlikely(len == -1))
277
                goto fatal;
278

279 280 281
            block_t *block = block_heap_Alloc (footer, footer, len);
            if (unlikely(block == NULL))
                free (footer);
282 283 284
            p_access->info.b_eof = true;
            return block;
        }
285 286 287 288 289 290 291 292 293 294 295 296 297
        else
        {
            /* This was the end of a "subnode" */
            /* Write the ID to the extension */
            char *old_xspf_extension = p_sys->psz_xspf_extension;
            if (old_xspf_extension == NULL)
                goto fatal;

            int len2 = asprintf( &p_sys->psz_xspf_extension, "%s  </vlc:node>\n", old_xspf_extension );
            if (len2 == -1)
                goto fatal;
            free( old_xspf_extension );
        }
298
        return NULL;
299 300
    }

301 302
    /* Skip current, parent and hidden directories */
    if (entry[0] == '.')
303 304
    {
        free (entry);
305
        return NULL;
306
    }
307 308
    /* Handle recursion */
    if (p_sys->mode != MODE_COLLAPSE)
309
    {
310
        directory_t *sub = malloc (sizeof (*sub));
311
        if (sub == NULL)
312 313
        {
            free (entry);
314
            return NULL;
315
        }
316

317
        DIR *handle;
318
#ifdef HAVE_OPENAT
319
        int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
320 321 322 323 324 325 326 327 328
        if (fd != -1)
        {
            handle = fdopendir (fd);
            if (handle == NULL)
                close (fd);
        }
        else
            handle = NULL;
#else
329 330 331 332
        if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1)
            handle = vlc_opendir (sub->path);
        else
            handle = NULL;
333
#endif
334
        if (handle != NULL)
335
        {
336 337
            sub->parent = current;
            sub->handle = handle;
338 339 340 341 342 343

            char *encoded = encode_URI_component (entry);
            if ((encoded == NULL)
             || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
                 sub->uri = NULL;
            free (encoded);
344 345 346 347 348 349

            if ((p_sys->mode == MODE_NONE)
             || fstat (dirfd (handle), &sub->st)
             || has_inode_loop (sub)
             || (sub->uri == NULL))
            {
350
                free (entry);
351
                closedir (handle);
352
                free (sub->uri);
353 354 355 356
                free (sub);
                return NULL;
            }
            p_sys->current = sub;
357 358 359 360

            /* Add node to xspf extension */
            char *old_xspf_extension = p_sys->psz_xspf_extension;
            if (old_xspf_extension == NULL)
361 362
            {
                free (entry);
363
                goto fatal;
364
            }
365

366
            char *title = convert_xml_special_chars (entry);
367
            free (entry);
368 369 370 371 372 373
            if (title == NULL
             || asprintf (&p_sys->psz_xspf_extension, "%s"
                          "  <vlc:node title=\"%s\">\n", old_xspf_extension,
                          title) == -1)
            {
                free (title);
374
                goto fatal;
375 376 377
            }
            free (title);
            free (old_xspf_extension);
378
            return NULL;
379
        }
380 381
        else
            free (sub);
382
    }
383

384 385 386 387 388 389 390 391 392 393 394 395 396
    /* 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);
397

398 399
                if (type + extlen == end
                 && !strncasecmp (ext, type, extlen))
400 401
                {
                    free (entry);
402
                    return NULL;
403
                }
404 405 406

                if (*end == '\0')
                    break;
407 408 409
            }
        }
    }
410

411 412 413 414 415
    char *encoded = encode_URI_component (entry);
    free (entry);
    if (encoded == NULL)
        goto fatal;
    int len = asprintf (&entry,
416
                        "  <track><location>%s/%s</location>\n" \
417 418 419 420 421
                        "   <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++);
422 423 424 425
    free (encoded);
    if (len == -1)
        goto fatal;

426 427 428 429 430 431 432 433 434 435 436
    /* Write the ID to the extension */
    char *old_xspf_extension = p_sys->psz_xspf_extension;
    if (old_xspf_extension == NULL)
        goto fatal;

    int len2 = asprintf( &p_sys->psz_xspf_extension, "%s   <vlc:item tid=\"%i\" />\n",
                            old_xspf_extension, p_sys->i_item_count-1 );
    if (len2 == -1)
        goto fatal;
    free( old_xspf_extension );

437 438
    block_t *block = block_heap_Alloc (entry, entry, len);
    if (unlikely(block == NULL))
439 440 441 442 443
    {
        free (entry);
        goto fatal;
    }
    return block;
444

445 446 447
fatal:
    p_access->info.b_eof = true;
    return NULL;
448 449 450
}

/*****************************************************************************
451
 * Control:
452
 *****************************************************************************/
453
int DirControl( access_t *p_access, int i_query, va_list args )
454 455 456 457 458 459
{
    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
460
            *va_arg( args, bool* ) = false;
461 462
            break;

463 464
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
465
            *va_arg( args, bool* ) = true;
466 467 468 469
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
470
            *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
471 472 473 474 475 476 477
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
478
        case ACCESS_SET_PRIVATE_ID_STATE:
479
        case ACCESS_GET_CONTENT_TYPE:
480
        case ACCESS_GET_META:
481 482 483
            return VLC_EGENERIC;

        default:
484
            msg_Warn( p_access, "unimplemented query in control" );
485 486 487 488
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}