directory.c 5.68 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
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
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
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"
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
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
int DirInit (stream_t *access, DIR *dir)
55
{
56
    access_sys_t *sys = vlc_obj_malloc(VLC_OBJECT(access), sizeof (*sys));
57 58
    if (unlikely(sys == NULL))
        goto error;
59

60
    if (!strcmp(access->psz_name, "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

error:
    closedir(dir);
    return VLC_ENOMEM;
81 82
}

83 84 85
/*****************************************************************************
 * DirOpen: Open the directory access
 *****************************************************************************/
86
int DirOpen (vlc_object_t *obj)
87
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
    stream_t *access = (stream_t *)obj;
89 90 91 92

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

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

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

100 101 102
/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
103
void DirClose(vlc_object_t *obj)
104
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
105
    stream_t *access = (stream_t *)obj;
106
    access_sys_t *sys = access->p_sys;
107

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
112
int DirRead (stream_t *access, input_item_node_t *node)
113
{
114
    access_sys_t *sys = access->p_sys;
115
    const char *entry;
116
    int ret = VLC_SUCCESS;
117

118 119
    bool special_files = var_InheritBool(access, "list-special-files");

120 121
    struct vlc_readdir_helper rdh;
    vlc_readdir_helper_init(&rdh, access, node);
122 123

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

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

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

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

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

189
    vlc_readdir_helper_finish(&rdh, ret == VLC_SUCCESS);
190 191

    return ret;
192
}