directory.c 12.5 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 44 45 46 47
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
#endif

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

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

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

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

81 82 83 84 85 86
struct access_sys_t
{
    directory_t *current;
    DIR *handle;
    char *ignored_exts;
    int mode;
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 99 100
    if( !p_access->psz_path )
        return VLC_EGENERIC;

101
    DIR *handle = vlc_opendir (p_access->psz_path);
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
    if (!p_sys)
112 113
    {
        closedir( handle );
114
        return VLC_ENOMEM;
115
    }
116 117 118 119 120

    p_access->p_sys = p_sys;
    p_sys->current = NULL;
    p_sys->handle = handle;
    p_sys->ignored_exts = var_CreateGetString (p_access, "ignore-filetypes");
121 122
    p_sys->i_item_count = 0;
    p_sys->psz_xspf_extension = strdup( "" );
123

124 125 126 127 128 129 130 131 132 133
    /* Handle mode */
    char *psz = var_CreateGetString( p_access, "recursive" );
    if( *psz == '\0' || !strcasecmp( psz, "none" )  )
        p_sys->mode = MODE_NONE;
    else if( !strcasecmp( psz, "collapse" )  )
        p_sys->mode = MODE_COLLAPSE;
    else
        p_sys->mode = MODE_EXPAND;
    free( psz );

134
    access_InitFields(p_access);
135
    p_access->pf_read  = NULL;
136
    p_access->pf_block = DirBlock;
137
    p_access->pf_seek  = NULL;
138
    p_access->pf_control= DirControl;
139 140
    free (p_access->psz_demux);
    p_access->psz_demux = strdup ("xspf-open");
Gildas Bazin's avatar
 
Gildas Bazin committed
141

142 143 144 145 146 147
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
148
void DirClose( vlc_object_t * p_this )
149
{
150
    access_t *p_access = (access_t*)p_this;
151 152 153 154 155 156 157 158 159 160 161 162 163
    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);
        free (current);
    }
    if (p_sys->handle != NULL)
        closedir (p_sys->handle); /* corner case,:Block() not called ever */
164
    free (p_sys->psz_xspf_extension);
165 166
    free (p_sys->ignored_exts);
    free (p_sys);
167 168
}

169 170
/* Detect directories that recurse into themselves. */
static bool has_inode_loop (const directory_t *dir)
171
{
172
#ifndef WIN32
173 174
    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
175

176 177 178
    while ((dir = dir->parent) != NULL)
        if ((dir->st.st_dev == dev) && (dir->st.st_ino == inode))
            return true;
179
#else
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
180
# undef fstat
181
# define fstat( fd, st ) (0)
182
    VLC_UNUSED( dir );
183
#endif
184 185
    return false;
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
186

187
block_t *DirBlock (access_t *p_access)
188 189 190
{
    access_sys_t *p_sys = p_access->p_sys;
    directory_t *current = p_sys->current;
191

192 193
    if (p_access->info.b_eof)
        return NULL;
194

195 196 197 198
    if (current == NULL)
    {   /* Startup: send the XSPF header */
        static const char header[] =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
199
            "<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\" xmlns:vlc=\"http://www.videolan.org/vlc/playlist/ns/0/\">\n"
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
            " <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 */
        current = malloc (sizeof (*current) + strlen (p_access->psz_path));
        if (current == NULL)
        {
            block_Release (block);
            goto fatal;
        }
        current->parent = NULL;
        current->handle = p_sys->handle;
        strcpy (current->path, p_access->psz_path);
216
        current->uri = make_URI (current->path);
217 218 219 220 221 222 223 224
        if ((current->uri == NULL)
         || fstat (dirfd (current->handle), &current->st))
        {
            free (current->uri);
            free (current);
            block_Release (block);
            goto fatal;
        }
225

226 227 228 229
        p_sys->handle = NULL;
        p_sys->current = current;
        return block;
    }
230

231
    char *entry = vlc_readdir (current->handle);
232 233 234 235
    if (entry == NULL)
    {   /* End of directory, go back to parent */
        closedir (current->handle);
        p_sys->current = current->parent;
236
        free (current->uri);
237 238 239 240
        free (current);

        if (p_sys->current == NULL)
        {   /* End of XSPF playlist */
241 242 243 244 245 246
            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 );
247
            if (unlikely(len == -1))
248
                goto fatal;
249

250 251 252
            block_t *block = block_heap_Alloc (footer, footer, len);
            if (unlikely(block == NULL))
                free (footer);
253 254 255
            p_access->info.b_eof = true;
            return block;
        }
256 257 258 259 260 261 262 263 264 265 266 267 268
        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 );
        }
269
        return NULL;
270 271
    }

272 273
    /* Skip current, parent and hidden directories */
    if (entry[0] == '.')
274 275
    {
        free (entry);
276
        return NULL;
277
    }
278 279
    /* Handle recursion */
    if (p_sys->mode != MODE_COLLAPSE)
280
    {
281 282 283
        directory_t *sub = malloc (sizeof (*sub) + strlen (current->path) + 1
                                                 + strlen (entry));
        if (sub == NULL)
284 285
        {
            free (entry);
286
            return NULL;
287
        }
288 289
        sprintf (sub->path, "%s/%s", current->path, entry);

290
        DIR *handle = vlc_opendir (sub->path);
291
        if (handle != NULL)
292
        {
293 294
            sub->parent = current;
            sub->handle = handle;
295 296 297 298 299 300

            char *encoded = encode_URI_component (entry);
            if ((encoded == NULL)
             || (asprintf (&sub->uri, "%s/%s", current->uri, encoded) == -1))
                 sub->uri = NULL;
            free (encoded);
301 302 303 304 305 306

            if ((p_sys->mode == MODE_NONE)
             || fstat (dirfd (handle), &sub->st)
             || has_inode_loop (sub)
             || (sub->uri == NULL))
            {
307
                free (entry);
308
                closedir (handle);
309
                free (sub->uri);
310 311 312 313
                free (sub);
                return NULL;
            }
            p_sys->current = sub;
314 315 316 317

            /* Add node to xspf extension */
            char *old_xspf_extension = p_sys->psz_xspf_extension;
            if (old_xspf_extension == NULL)
318 319
            {
                free (entry);
320
                goto fatal;
321
            }
322

323
            char *title = convert_xml_special_chars (entry);
324
            free (entry);
325 326 327 328 329 330
            if (title == NULL
             || asprintf (&p_sys->psz_xspf_extension, "%s"
                          "  <vlc:node title=\"%s\">\n", old_xspf_extension,
                          title) == -1)
            {
                free (title);
331
                goto fatal;
332 333 334
            }
            free (title);
            free (old_xspf_extension);
335
            return NULL;
336
        }
337 338
        else
            free (sub);
339
    }
340

341 342 343 344 345 346 347 348 349 350 351 352 353
    /* 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);
354

355 356
                if (type + extlen == end
                 && !strncasecmp (ext, type, extlen))
357 358
                {
                    free (entry);
359
                    return NULL;
360
                }
361 362 363

                if (*end == '\0')
                    break;
364 365 366
            }
        }
    }
367

368 369 370 371 372
    char *encoded = encode_URI_component (entry);
    free (entry);
    if (encoded == NULL)
        goto fatal;
    int len = asprintf (&entry,
373
                        "  <track><location>%s/%s</location>\n" \
374 375 376 377 378
                        "   <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++);
379 380 381 382
    free (encoded);
    if (len == -1)
        goto fatal;

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

394 395
    block_t *block = block_heap_Alloc (entry, entry, len);
    if (unlikely(block == NULL))
396 397 398 399 400
    {
        free (entry);
        goto fatal;
    }
    return block;
401

402 403 404
fatal:
    p_access->info.b_eof = true;
    return NULL;
405 406 407
}

/*****************************************************************************
408
 * Control:
409
 *****************************************************************************/
410
int DirControl( access_t *p_access, int i_query, va_list args )
411 412 413 414 415 416
{
    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
417
            *va_arg( args, bool* ) = false;
418 419
            break;

420 421
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
422
            *va_arg( args, bool* ) = true;
423 424 425 426
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
427
            *va_arg( args, int64_t * ) = DEFAULT_PTS_DELAY * 1000;
428 429 430 431 432 433 434
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
435
        case ACCESS_SET_PRIVATE_ID_STATE:
436
        case ACCESS_GET_CONTENT_TYPE:
437
        case ACCESS_GET_META:
438 439 440
            return VLC_EGENERIC;

        default:
441
            msg_Warn( p_access, "unimplemented query in control" );
442 443 444 445
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}