directory.c 5.62 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 <limits.h>
35 36
#include <sys/stat.h>

37
#include <vlc_common.h>
38
#include "fs.h"
Clément Stenac's avatar
Clément Stenac committed
39
#include <vlc_access.h>
40
#include <vlc_input_item.h>
41

42
#include <vlc_fs.h>
43
#include <vlc_url.h>
44

45 46
struct access_sys_t
{
47 48
    char *base_uri;
    DIR *dir;
49
};
50

51
/*****************************************************************************
52
 * DirInit: Init the directory access with a directory stream
53
 *****************************************************************************/
54
int DirInit (access_t *access, DIR *dir)
55
{
56 57 58
    access_sys_t *sys = malloc(sizeof (*sys));
    if (unlikely(sys == NULL))
        goto error;
59

60
    if (!strcmp(access->psz_access, "fd"))
61
    {
62 63 64
        if (unlikely(asprintf(&sys->base_uri, "fd://%s",
                              access->psz_location) == -1))
            sys->base_uri = NULL;
65
    }
66
    else
67 68 69
        sys->base_uri = vlc_path2uri(access->psz_filepath, "file");
    if (unlikely(sys->base_uri == NULL))
        goto error;
70

71
    sys->dir = dir;
72

73 74 75
    access->p_sys = sys;
    access->pf_readdir = DirRead;
    access->pf_control = access_vaDirectoryControlHelper;
76
    return VLC_SUCCESS;
77 78 79 80 81

error:
    closedir(dir);
    free(sys);
    return VLC_ENOMEM;
82 83
}

84 85 86
/*****************************************************************************
 * DirOpen: Open the directory access
 *****************************************************************************/
87
int DirOpen (vlc_object_t *obj)
88
{
89
    access_t *access = (access_t *)obj;
90 91 92 93

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

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

98
    return DirInit(access, dir);
99 100
}

101 102 103
/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
104
void DirClose(vlc_object_t *obj)
105
{
106 107
    access_t *access = (access_t *)obj;
    access_sys_t *sys = access->p_sys;
108

109 110 111
    free(sys->base_uri);
    closedir(sys->dir);
    free(sys);
112 113
}

114
int DirRead (access_t *access, input_item_node_t *node)
115
{
116
    access_sys_t *sys = access->p_sys;
117
    const char *entry;
118
    int ret = VLC_SUCCESS;
119

120 121 122 123 124 125
    bool special_files = var_InheritBool(access, "list-special-files");

    struct access_fsdir fsdir;
    access_fsdir_init(&fsdir, access, node);

    while (ret == VLC_SUCCESS && (entry = vlc_readdir(sys->dir)) != NULL)
126
    {
127
        struct stat st;
128
        int type;
129

130
#ifdef HAVE_OPENAT
131
        if (fstatat(dirfd(sys->dir), entry, &st, 0))
132
            continue;
133 134
#else
        char path[PATH_MAX];
135

136 137 138 139
        if (snprintf(path, PATH_MAX, "%s"DIR_SEP"%s", access->psz_filepath,
                     entry) >= PATH_MAX || vlc_stat(path, &st))
            continue;
#endif
140 141
        switch (st.st_mode & S_IFMT)
        {
142
            case S_IFBLK:
143
                if (!special_files)
144 145 146 147
                    continue;
                type = ITEM_TYPE_DISC;
                break;
            case S_IFCHR:
148
                if (!special_files)
149 150 151 152
                    continue;
                type = ITEM_TYPE_CARD;
                break;
            case S_IFIFO:
153
                if (!special_files)
154 155 156 157 158 159 160 161 162
                    continue;
                type = ITEM_TYPE_STREAM;
                break;
            case S_IFREG:
                type = ITEM_TYPE_FILE;
                break;
            case S_IFDIR:
                type = ITEM_TYPE_DIRECTORY;
                break;
163 164
            /* S_IFLNK cannot occur while following symbolic links */
            /* S_IFSOCK cannot be opened with open()/openat() */
165 166
            default:
                continue; /* ignore */
167 168
        }

169
        /* Create an input item for the current entry */
170 171
        char *encoded = vlc_uri_encode(entry);
        if (unlikely(encoded == NULL))
172 173 174 175
        {
            ret = VLC_ENOMEM;
            break;
        }
176

177
        char *uri;
178
        if (unlikely(asprintf(&uri, "%s/%s", sys->base_uri, encoded) == -1))
179
            uri = NULL;
180
        free(encoded);
181
        if (unlikely(uri == NULL))
182 183 184 185 186
        {
            ret = VLC_ENOMEM;
            break;
        }
        ret = access_fsdir_additem(&fsdir, uri, entry, type, ITEM_NET_UNKNOWN);
187
        free(uri);
188
    }
189 190 191 192

    access_fsdir_finish(&fsdir, ret == VLC_SUCCESS);

    return ret;
193
}