directory.c 5.49 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
    bool special_files;
50
};
51

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

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

72
    sys->dir = dir;
73
    sys->special_files = var_InheritBool(access, "list-special-files");
74

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

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

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

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

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

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

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

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

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

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

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

132 133 134 135
        if (snprintf(path, PATH_MAX, "%s"DIR_SEP"%s", access->psz_filepath,
                     entry) >= PATH_MAX || vlc_stat(path, &st))
            continue;
#endif
136 137
        switch (st.st_mode & S_IFMT)
        {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            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;
159 160
            /* S_IFLNK cannot occur while following symbolic links */
            /* S_IFSOCK cannot be opened with open()/openat() */
161 162
            default:
                continue; /* ignore */
163 164
        }

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

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

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