directory.c 12.9 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 50 51 52 53 54
#ifdef __sun__
static inline int dirfd (DIR *dir)
{
    return dir->dd_fd;
}
#endif
55

56
#include <vlc_fs.h>
57
#include <vlc_url.h>
58
#include <vlc_strings.h>
59

60 61
enum
{
62
    MODE_NONE,
63
    MODE_COLLAPSE,
64
    MODE_EXPAND,
65 66
};

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

81 82 83 84
struct access_sys_t
{
    directory_t *current;
    char *ignored_exts;
85 86
    char mode;
    bool header;
87 88
    int i_item_count;
    char *psz_xspf_extension;
89
};
90

91 92 93
/*****************************************************************************
 * Open: open the directory
 *****************************************************************************/
94
int DirOpen( vlc_object_t *p_this )
95
{
96
    access_t *p_access = (access_t*)p_this;
97

98
    if( !p_access->psz_filepath )
99 100
        return VLC_EGENERIC;

101
    DIR *handle = vlc_opendir (p_access->psz_filepath);
102
    if (handle == NULL)
103
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
104

105 106 107 108 109 110
    return DirInit (p_access, handle);
}

int DirInit (access_t *p_access, DIR *handle)
{
    access_sys_t *p_sys = malloc (sizeof (*p_sys));
111 112 113 114 115
    if (unlikely(p_sys == NULL))
        goto error;

    char *uri;
    if (!strcmp (p_access->psz_access, "fd"))
116
    {
117
        if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
118
            uri = NULL;
119
    }
120
    else
121
        uri = make_URI (p_access->psz_filepath, "file");
122 123
    if (unlikely(uri == NULL))
        goto error;
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    /* "Open" the base directory */
    directory_t *root = malloc (sizeof (*root));
    if (unlikely(root == NULL))
    {
        free (uri);
        goto error;
    }
    root->parent = NULL;
    root->handle = handle;
    root->uri = uri;
#ifndef HAVE_OPENAT
    root->path = strdup (p_access->psz_filepath);
#endif
    if (fstat (dirfd (handle), &root->st))
    {
        free (root);
        free (uri);
        goto error;
    }

145
    p_access->p_sys = p_sys;
146
    p_sys->current = root;
147
    p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
148
    p_sys->header = true;
149 150
    p_sys->i_item_count = 0;
    p_sys->psz_xspf_extension = strdup( "" );
151

152
    /* Handle mode */
153 154
    char *psz = var_InheritString (p_access, "recursive");
    if (psz == NULL || !strcasecmp (psz, "none"))
155 156 157 158 159 160 161
        p_sys->mode = MODE_NONE;
    else if( !strcasecmp( psz, "collapse" )  )
        p_sys->mode = MODE_COLLAPSE;
    else
        p_sys->mode = MODE_EXPAND;
    free( psz );

162
    access_InitFields(p_access);
163
    p_access->pf_read  = NULL;
164
    p_access->pf_block = DirBlock;
165
    p_access->pf_seek  = NULL;
166
    p_access->pf_control= DirControl;
167 168
    free (p_access->psz_demux);
    p_access->psz_demux = strdup ("xspf-open");
Gildas Bazin's avatar
 
Gildas Bazin committed
169

170
    return VLC_SUCCESS;
171 172 173 174 175

error:
    closedir (handle);
    free (p_sys);
    return VLC_EGENERIC;
176 177 178 179 180
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
181
void DirClose( vlc_object_t * p_this )
182
{
183
    access_t *p_access = (access_t*)p_this;
184 185 186 187 188 189 190 191 192
    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);
193
#ifndef HAVE_OPENAT
194 195
        free (current->path);
#endif
196 197
        free (current);
    }
198

199
    free (p_sys->psz_xspf_extension);
200 201
    free (p_sys->ignored_exts);
    free (p_sys);
202 203
}

204 205
/* Detect directories that recurse into themselves. */
static bool has_inode_loop (const directory_t *dir)
206
{
207
#ifndef WIN32
208 209
    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
210

211 212 213
    while ((dir = dir->parent) != NULL)
        if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
            return true;
214
#else
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
215
# undef fstat
216
# define fstat( fd, st ) (0)
217
    VLC_UNUSED( dir );
218
#endif
219 220
    return false;
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221

222
block_t *DirBlock (access_t *p_access)
223 224 225
{
    access_sys_t *p_sys = p_access->p_sys;
    directory_t *current = p_sys->current;
226

227 228
    if (p_access->info.b_eof)
        return NULL;
229

230
    if (p_sys->header)
231 232 233
    {   /* Startup: send the XSPF header */
        static const char header[] =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
234
            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
235 236 237 238 239
            " <trackList>\n";
        block_t *block = block_Alloc (sizeof (header) - 1);
        if (!block)
            goto fatal;
        memcpy (block->p_buffer, header, sizeof (header) - 1);
240
        p_sys->header = false;
241 242
        return block;
    }
243

244
    char *entry = vlc_readdir (current->handle);
245 246 247 248
    if (entry == NULL)
    {   /* End of directory, go back to parent */
        closedir (current->handle);
        p_sys->current = current->parent;
249
        free (current->uri);
250
#ifndef HAVE_OPENAT
251 252
        free (current->path);
#endif
253 254 255 256
        free (current);

        if (p_sys->current == NULL)
        {   /* End of XSPF playlist */
257 258 259 260 261 262
            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 );
263
            if (unlikely(len == -1))
264
                goto fatal;
265

266 267 268
            block_t *block = block_heap_Alloc (footer, footer, len);
            if (unlikely(block == NULL))
                free (footer);
269 270 271
            p_access->info.b_eof = true;
            return block;
        }
272 273 274 275 276 277 278 279 280 281 282 283 284
        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 );
        }
285
        return NULL;
286 287
    }

288 289
    /* Skip current, parent and hidden directories */
    if (entry[0] == '.')
290 291
    {
        free (entry);
292
        return NULL;
293
    }
294 295
    /* Handle recursion */
    if (p_sys->mode != MODE_COLLAPSE)
296
    {
297
        directory_t *sub = malloc (sizeof (*sub));
298
        if (sub == NULL)
299 300
        {
            free (entry);
301
            return NULL;
302
        }
303

304
        DIR *handle;
305
#ifdef HAVE_OPENAT
306
        int fd = vlc_openat (dirfd (current->handle), entry, O_RDONLY);
307 308 309 310 311 312 313 314 315
        if (fd != -1)
        {
            handle = fdopendir (fd);
            if (handle == NULL)
                close (fd);
        }
        else
            handle = NULL;
#else
316 317 318 319
        if (asprintf (&sub->path, "%s/%s", current->path, entry) != -1)
            handle = vlc_opendir (sub->path);
        else
            handle = NULL;
320
#endif
321
        if (handle != NULL)
322
        {
323 324
            sub->parent = current;
            sub->handle = handle;
325 326 327 328 329 330

            char *encoded = encode_URI_component (entry);
            if ((encoded == NULL)
             || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
                 sub->uri = NULL;
            free (encoded);
331 332 333 334 335 336

            if ((p_sys->mode == MODE_NONE)
             || fstat (dirfd (handle), &sub->st)
             || has_inode_loop (sub)
             || (sub->uri == NULL))
            {
337
                free (entry);
338
                closedir (handle);
339
                free (sub->uri);
340 341 342 343
                free (sub);
                return NULL;
            }
            p_sys->current = sub;
344 345 346 347

            /* Add node to xspf extension */
            char *old_xspf_extension = p_sys->psz_xspf_extension;
            if (old_xspf_extension == NULL)
348 349
            {
                free (entry);
350
                goto fatal;
351
            }
352

353
            char *title = convert_xml_special_chars (entry);
354
            free (entry);
355 356 357 358 359 360
            if (title == NULL
             || asprintf (&p_sys->psz_xspf_extension, "%s"
                          "  <vlc:node title=\"%s\">\n", old_xspf_extension,
                          title) == -1)
            {
                free (title);
361
                goto fatal;
362 363 364
            }
            free (title);
            free (old_xspf_extension);
365
            return NULL;
366
        }
367 368
        else
            free (sub);
369
    }
370

371 372 373 374 375 376 377 378 379 380 381 382 383
    /* 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);
384

385 386
                if (type + extlen == end
                 && !strncasecmp (ext, type, extlen))
387 388
                {
                    free (entry);
389
                    return NULL;
390
                }
391 392 393

                if (*end == '\0')
                    break;
394 395 396
            }
        }
    }
397

398 399 400 401 402
    char *encoded = encode_URI_component (entry);
    free (entry);
    if (encoded == NULL)
        goto fatal;
    int len = asprintf (&entry,
403
                        "  <track><location>%s/%s</location>\n" \
404 405 406 407 408
                        "   <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++);
409 410 411 412
    free (encoded);
    if (len == -1)
        goto fatal;

413 414 415 416 417 418 419 420 421 422 423
    /* 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 );

424 425
    block_t *block = block_heap_Alloc (entry, entry, len);
    if (unlikely(block == NULL))
426 427 428 429 430
    {
        free (entry);
        goto fatal;
    }
    return block;
431

432 433 434
fatal:
    p_access->info.b_eof = true;
    return NULL;
435 436 437
}

/*****************************************************************************
438
 * Control:
439
 *****************************************************************************/
440
int DirControl( access_t *p_access, int i_query, va_list args )
441 442 443 444 445 446
{
    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
447
            *va_arg( args, bool* ) = false;
448 449
            break;

450 451
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
452
            *va_arg( args, bool* ) = true;
453 454 455 456
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
457
            *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
458 459 460 461 462 463 464
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
465
        case ACCESS_SET_PRIVATE_ID_STATE:
466
        case ACCESS_GET_CONTENT_TYPE:
467
        case ACCESS_GET_META:
468 469 470
            return VLC_EGENERIC;

        default:
471
            msg_Warn( p_access, "unimplemented query in control" );
472 473 474 475
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}