directory.c 4.36 KB
Newer Older
1
/*****************************************************************************
2
 * directory.c: expands a directory (directory: access_browser plug-in)
3
 *****************************************************************************
4
 * Copyright (C) 2002-2015 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 <vlc_fs.h>
40
#include <vlc_url.h>
41

42 43
struct access_sys_t
{
44 45
    char *psz_base_uri;
    DIR *p_dir;
46
};
47

48
/*****************************************************************************
49
 * DirInit: Init the directory access with a directory stream
50
 *****************************************************************************/
51
int DirInit (access_t *p_access, DIR *p_dir)
52
{
53
    char *psz_base_uri;
54

55
    if (!strcmp (p_access->psz_access, "fd"))
56
    {
57 58
        if (asprintf (&psz_base_uri, "fd://%s", p_access->psz_location) == -1)
            psz_base_uri = NULL;
59
    }
60
    else
61 62
        psz_base_uri = vlc_path2uri (p_access->psz_filepath, "file");
    if (unlikely (psz_base_uri == NULL))
63
    {
64 65
        closedir (p_dir);
        return VLC_ENOMEM;
66
    }
67

68 69
    p_access->p_sys = calloc (1, sizeof(access_sys_t));
    if (!p_access->p_sys)
70
    {
71 72 73
        closedir(p_dir);
        free( psz_base_uri );
        return VLC_ENOMEM;
74
    }
75 76
    p_access->p_sys->p_dir = p_dir;
    p_access->p_sys->psz_base_uri = psz_base_uri;
77
    p_access->pf_readdir = DirRead;
78
    p_access->pf_control = access_vaDirectoryControlHelper;
79

80 81 82
    return VLC_SUCCESS;
}

83 84 85 86 87
/*****************************************************************************
 * DirOpen: Open the directory access
 *****************************************************************************/
int DirOpen (vlc_object_t *p_this)
{
88 89 90 91 92 93 94 95 96 97
    access_t *access = (access_t *)p_this;

    if (access->psz_filepath == NULL)
        return VLC_EGENERIC;

    DIR *dir = vlc_opendir (access->psz_filepath);
    if (dir == NULL)
        return VLC_EGENERIC;

    return DirInit (access, dir);
98 99
}

100 101 102
/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
103
void DirClose( vlc_object_t * p_this )
104
{
105
    access_t *p_access = (access_t*)p_this;
106 107
    access_sys_t *p_sys = p_access->p_sys;

108 109
    free (p_sys->psz_base_uri);
    closedir (p_sys->p_dir);
110

111
    free (p_sys);
112 113
}

Thomas Guillem's avatar
Thomas Guillem committed
114
input_item_t* DirRead (access_t *p_access)
115 116
{
    access_sys_t *p_sys = p_access->p_sys;
117
    const char *entry;
118

119
    while ((entry = vlc_readdir (p_sys->p_dir)) != NULL)
120
    {
121
        /* Create an input item for the current entry */
122 123
        char *encoded_entry = encode_URI_component (entry);
        if (unlikely(entry == NULL))
124
            return NULL;
125

126 127 128 129 130 131
        char *uri;
        if (unlikely(asprintf (&uri, "%s/%s", p_sys->psz_base_uri,
                               encoded_entry) == -1))
            uri = NULL;
        free (encoded_entry);
        if (unlikely(uri == NULL))
132
            return NULL;
133 134 135 136 137 138

        input_item_t *item = input_item_NewWithType (uri, entry, 0, NULL, 0, 0,
                                                     ITEM_TYPE_FILE);
        free (uri);
        if (likely(item != NULL))
            return item;
139
    }
140
    return NULL;
141
}