directory.c 5.54 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 58
/*****************************************************************************
 * Open: open the directory
 *****************************************************************************/
59
int DirOpen (vlc_object_t *p_this)
60
{
61
    access_t *p_access = (access_t*)p_this;
62 63
    DIR *p_dir;
    char *psz_base_uri;
64

65
    if (!p_access->psz_filepath)
66 67
        return VLC_EGENERIC;

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

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

85 86 87

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

98 99 100 101 102 103
    return VLC_SUCCESS;
}

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

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

112
    free (p_sys);
113 114
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
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
142 143
    VLC_UNUSED(p_access);
    VLC_UNUSED(psz_uri);
144 145 146 147
    return false;
#endif
}

Thomas Guillem's avatar
Thomas Guillem committed
148
input_item_t* DirRead (access_t *p_access)
149 150
{
    access_sys_t *p_sys = p_access->p_sys;
151
    DIR *p_dir = p_sys->p_dir;
Thomas Guillem's avatar
Thomas Guillem committed
152
    input_item_t *p_item = NULL;
153
    const char *psz_entry;
154

155
    while (!p_item && (psz_entry = vlc_readdir (p_dir)))
156
    {
157 158 159
        char *psz_uri, *psz_encoded_entry;
        struct stat st;
        int i_type;
160

161
        /* Check if it is a directory or even readable */
162 163 164 165
        if (asprintf (&psz_uri, "%s/%s",
                      p_access->psz_filepath, psz_entry) == -1)
            return NULL;
        if (vlc_stat (psz_uri, &st) != 0)
166
        {
167
            free (psz_uri);
168
            continue;
169
        }
170 171
        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))
172
        {
173
            free (psz_uri);
174
            continue;
175
        }
176 177 178 179 180 181 182 183 184 185
        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);
186

187 188 189 190 191
        p_item = input_item_NewWithType (psz_uri, psz_entry,
                                         0, NULL, 0, 0, i_type);
        free (psz_uri);
        if (!p_item)
            return NULL;
192
    }
Thomas Guillem's avatar
Thomas Guillem committed
193
    return p_item;
194
}