directory.c 5.09 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 *psz_base_uri;
    DIR *p_dir;
48
};
49

50
/*****************************************************************************
51
 * DirInit: Init the directory access with a directory stream
52
 *****************************************************************************/
53
int DirInit (access_t *p_access, DIR *p_dir)
54
{
55
    char *psz_base_uri;
56

57
    if (!strcmp (p_access->psz_access, "fd"))
58
    {
59 60
        if (asprintf (&psz_base_uri, "fd://%s", p_access->psz_location) == -1)
            psz_base_uri = NULL;
61
    }
62
    else
63 64
        psz_base_uri = vlc_path2uri (p_access->psz_filepath, "file");
    if (unlikely (psz_base_uri == NULL))
65
    {
66 67
        closedir (p_dir);
        return VLC_ENOMEM;
68
    }
69

70 71
    p_access->p_sys = calloc (1, sizeof(access_sys_t));
    if (!p_access->p_sys)
72
    {
73 74 75
        closedir(p_dir);
        free( psz_base_uri );
        return VLC_ENOMEM;
76
    }
77 78
    p_access->p_sys->p_dir = p_dir;
    p_access->p_sys->psz_base_uri = psz_base_uri;
79
    p_access->pf_readdir = DirRead;
80
    p_access->pf_control = access_vaDirectoryControlHelper;
81

82 83 84
    return VLC_SUCCESS;
}

85 86 87 88 89
/*****************************************************************************
 * DirOpen: Open the directory access
 *****************************************************************************/
int DirOpen (vlc_object_t *p_this)
{
90 91 92 93 94 95 96 97 98 99
    access_t *access = (access_t *)p_this;

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

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

    return DirInit (access, dir);
100 101
}

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

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

113
    free (p_sys);
114 115
}

Thomas Guillem's avatar
Thomas Guillem committed
116
input_item_t* DirRead (access_t *p_access)
117 118
{
    access_sys_t *p_sys = p_access->p_sys;
119
    const char *entry;
120

121
    while ((entry = vlc_readdir (p_sys->p_dir)) != NULL)
122
    {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        int type;
#ifdef HAVE_OPENAT
        struct stat st;

        if (fstatat (dirfd (p_sys->p_dir), entry, &st, 0))
            continue;

        switch (st.st_mode & S_IFMT)
        {
            case S_IFBLK: type = ITEM_TYPE_DISC;      break;
            case S_IFCHR: type = ITEM_TYPE_CARD;      break;
            case S_IFIFO: type = ITEM_TYPE_STREAM;    break;
            case S_IFREG: type = ITEM_TYPE_FILE;      break;
            case S_IFDIR: type = ITEM_TYPE_DIRECTORY; break;
            /* S_IFLNK cannot occur while following symbolic links */
            /* S_IFSOCK cannot be opened with open()/openat() */
            default:      continue; /* ignore */
        }
#else
        type = ITEM_TYPE_FILE;
#endif

145
        /* Create an input item for the current entry */
146 147
        char *encoded_entry = encode_URI_component (entry);
        if (unlikely(entry == NULL))
148
            return NULL;
149

150 151 152 153 154 155
        char *uri;
        if (unlikely(asprintf (&uri, "%s/%s", p_sys->psz_base_uri,
                               encoded_entry) == -1))
            uri = NULL;
        free (encoded_entry);
        if (unlikely(uri == NULL))
156
            return NULL;
157 158

        input_item_t *item = input_item_NewWithType (uri, entry, 0, NULL, 0, 0,
159
                                                     type);
160 161 162
        free (uri);
        if (likely(item != NULL))
            return item;
163
    }
164
    return NULL;
165
}