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

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

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

44 45
struct access_sys_t
{
46 47
    char *base_uri;
    DIR *dir;
48
    bool special_files;
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
    sys->special_files = var_InheritBool(access, "list-special-files");
73

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

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

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

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

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

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

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

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

115
input_item_t *DirRead(access_t *access)
116
{
117
    access_sys_t *sys = access->p_sys;
118
    const char *entry;
119

120
    while ((entry = vlc_readdir(sys->dir)) != NULL)
121
    {
122
        struct stat st;
123
        int type;
124

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

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

164
        /* Create an input item for the current entry */
165
        char *encoded= encode_URI_component(entry);
166
        if (unlikely(entry == NULL))
167
            continue;
168

169
        char *uri;
170
        if (unlikely(asprintf(&uri, "%s/%s", sys->base_uri, encoded) == -1))
171
            uri = NULL;
172
        free(encoded);
173
        if (unlikely(uri == NULL))
174
            continue;
175

176 177 178
        input_item_t *item = input_item_NewWithType(uri, entry, 0, NULL, 0, 0,
                                                    type);
        free(uri);
179 180
        if (likely(item != NULL))
            return item;
181
    }
182
    return NULL;
183
}