directory.c 5.82 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 <sys/types.h>
40
#include <sys/stat.h>
41
#include <errno.h>
42 43
#include <unistd.h>
#include <fcntl.h>
44

45
#include <vlc_fs.h>
46
#include <vlc_url.h>
47
#include <vlc_strings.h>
48
#include <vlc_charset.h>
49

50 51
struct access_sys_t
{
52 53
    char *psz_base_uri;
    DIR *p_dir;
54
};
55

56
/*****************************************************************************
57
 * DirInit: Init the directory access with a directory stream
58
 *****************************************************************************/
59
int DirInit (access_t *p_access, DIR *p_dir)
60
{
61
    char *psz_base_uri;
62

63
    if (!p_access->psz_filepath)
64 65
        return VLC_EGENERIC;

66 67
    if (!p_dir)
        p_dir = vlc_opendir (p_access->psz_filepath);
68
    if (p_dir == NULL)
69
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
70

71
    if (!strcmp (p_access->psz_access, "fd"))
72
    {
73 74
        if (asprintf (&psz_base_uri, "fd://%s", p_access->psz_location) == -1)
            psz_base_uri = NULL;
75
    }
76
    else
77 78
        psz_base_uri = vlc_path2uri (p_access->psz_filepath, "file");
    if (unlikely (psz_base_uri == NULL))
79
    {
80 81
        closedir (p_dir);
        return VLC_ENOMEM;
82
    }
83

84 85 86

    p_access->p_sys = calloc (1, sizeof(access_sys_t));
    if (!p_access->p_sys)
87
    {
88 89 90
        closedir(p_dir);
        free( psz_base_uri );
        return VLC_ENOMEM;
91
    }
92 93
    p_access->p_sys->p_dir = p_dir;
    p_access->p_sys->psz_base_uri = psz_base_uri;
94
    p_access->pf_readdir = DirRead;
95
    p_access->pf_control = access_vaDirectoryControlHelper;
96

97 98 99
    return VLC_SUCCESS;
}

100 101 102 103 104 105 106 107
/*****************************************************************************
 * DirOpen: Open the directory access
 *****************************************************************************/
int DirOpen (vlc_object_t *p_this)
{
    return DirInit ((access_t*)p_this, NULL);
}

108 109 110
/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
111
void DirClose( vlc_object_t * p_this )
112
{
113
    access_t *p_access = (access_t*)p_this;
114 115
    access_sys_t *p_sys = p_access->p_sys;

116 117
    free (p_sys->psz_base_uri);
    closedir (p_sys->p_dir);
118

119
    free (p_sys);
120 121
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static bool is_looping(access_t *p_access, const char *psz_uri)
{
#ifdef S_ISLNK
    struct stat st;
    bool b_looping = false;

    if (vlc_lstat (psz_uri, &st) != 0)
        return false;
    if (S_ISLNK (st.st_mode))
    {
        char *psz_link = malloc(st.st_size + 1);
        ssize_t i_ret;

        if (psz_link)
        {
            i_ret = readlink(psz_uri, psz_link, st.st_size + 1);
            if (i_ret > 0 && i_ret <= st.st_size)
            {
                psz_link[i_ret] = '\0';
                if (strstr(p_access->psz_filepath, psz_link))
                    b_looping = true;
            }
            free (psz_link);
        }
    }
    return b_looping;
#else
149 150
    VLC_UNUSED(p_access);
    VLC_UNUSED(psz_uri);
151 152 153 154
    return false;
#endif
}

Thomas Guillem's avatar
Thomas Guillem committed
155
input_item_t* DirRead (access_t *p_access)
156 157
{
    access_sys_t *p_sys = p_access->p_sys;
158
    DIR *p_dir = p_sys->p_dir;
Thomas Guillem's avatar
Thomas Guillem committed
159
    input_item_t *p_item = NULL;
160
    const char *psz_entry;
161

162
    while (!p_item && (psz_entry = vlc_readdir (p_dir)))
163
    {
164 165 166
        char *psz_uri, *psz_encoded_entry;
        struct stat st;
        int i_type;
167

168
        /* Check if it is a directory or even readable */
169 170 171 172
        if (asprintf (&psz_uri, "%s/%s",
                      p_access->psz_filepath, psz_entry) == -1)
            return NULL;
        if (vlc_stat (psz_uri, &st) != 0)
173
        {
174
            free (psz_uri);
175
            continue;
176
        }
177 178
        i_type = S_ISDIR (st.st_mode) ? ITEM_TYPE_DIRECTORY : ITEM_TYPE_FILE;
        if (i_type == ITEM_TYPE_DIRECTORY && is_looping(p_access, psz_uri))
179
        {
180
            free (psz_uri);
181
            continue;
182
        }
183 184 185 186 187 188 189 190 191 192
        free (psz_uri);

        /* Create an input item for the current entry */
        psz_encoded_entry = encode_URI_component (psz_entry);
        if (psz_encoded_entry == NULL)
            continue;
        if (asprintf (&psz_uri, "%s/%s",
                      p_sys->psz_base_uri, psz_encoded_entry) == -1)
            return NULL;
        free (psz_encoded_entry);
193

194 195 196 197 198
        p_item = input_item_NewWithType (psz_uri, psz_entry,
                                         0, NULL, 0, 0, i_type);
        free (psz_uri);
        if (!p_item)
            return NULL;
199
    }
Thomas Guillem's avatar
Thomas Guillem committed
200
    return p_item;
201
}