filesystem.c 5.81 KB
Newer Older
1
/*****************************************************************************
2
 * filesystem.c: Common file system helpers
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *****************************************************************************
 * Copyright (C) 2005-2006 the VideoLAN team
 * Copyright © 2005-2008 Rémi Denis-Courmont
 *
 * Authors: Rémi Denis-Courmont <rem # videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
32
#include <vlc_fs.h>
33
#include <vlc_rand.h>
34
35
36
37
38
39

#include <assert.h>

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
40
#include <fcntl.h>
41
42
43
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
44
45
46
47
48
49
50

/**
 * Opens a FILE pointer.
 * @param filename file path, using UTF-8 encoding
 * @param mode fopen file open mode
 * @return NULL on error, an open FILE pointer on success.
 */
51
FILE *vlc_fopen (const char *filename, const char *mode)
52
53
54
55
56
57
58
59
60
61
62
63
64
{
    int rwflags = 0, oflags = 0;

    for (const char *ptr = mode; *ptr; ptr++)
    {
        switch (*ptr)
        {
            case 'r':
                rwflags = O_RDONLY;
                break;

            case 'a':
                rwflags = O_WRONLY;
65
                oflags |= O_CREAT | O_APPEND;
66
67
68
69
70
71
72
73
74
75
76
77
                break;

            case 'w':
                rwflags = O_WRONLY;
                oflags |= O_CREAT | O_TRUNC;
                break;

            case '+':
                rwflags = O_RDWR;
                break;

#ifdef O_TEXT
78
79
80
81
            case 'b':
                oflags = (oflags & ~O_TEXT) | O_BINARY;
                break;

82
            case 't':
83
                oflags = (oflags & ~O_BINARY) | O_TEXT;
84
85
86
87
88
                break;
#endif
        }
    }

89
    int fd = vlc_open (filename, rwflags | oflags, 0666);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
    if (fd == -1)
        return NULL;

    FILE *stream = fdopen (fd, mode);
    if (stream == NULL)
        close (fd);

    return stream;
}


static int dummy_select( const char *str )
{
    (void)str;
    return 1;
}

/**
108
 * Does the same as vlc_scandir(), but takes an open directory pointer
109
110
 * instead of a directory path.
 */
111
int vlc_loaddir( DIR *dir, char ***namelist,
112
113
114
                  int (*select)( const char * ),
                  int (*compar)( const char **, const char ** ) )
{
115
116
117
    assert (dir);

    if (select == NULL)
118
119
        select = dummy_select;

120
121
    char **tab = NULL;
    unsigned num = 0;
122

123
    rewinddir (dir);
124

125
126
127
128
129
    for (unsigned size = 0;;)
    {
        errno = 0;
        char *entry = vlc_readdir (dir);
        if (entry == NULL)
130
        {
131
132
133
134
            if (errno)
                goto error;
            break;
        }
135

136
137
138
139
140
141
142
143
144
145
        if (!select (entry))
        {
            free (entry);
            continue;
        }

        if (num >= size)
        {
            size = size ? (2 * size) : 16;
            char **newtab = realloc (tab, sizeof (*tab) * (size));
146

147
            if (unlikely(newtab == NULL))
148
            {
149
                free (entry);
150
151
152
153
154
                goto error;
            }
            tab = newtab;
        }

155
156
        tab[num++] = entry;
    }
157

158
159
160
161
162
    if (compar != NULL)
        qsort (tab, num, sizeof (*tab),
               (int (*)( const void *, const void *))compar);
    *namelist = tab;
    return num;
163

164
165
166
167
error:
    for (unsigned i = 0; i < num; i++)
        free (tab[i]);
    free (tab);
168
169
170
171
172
173
174
    return -1;
}

/**
 * Selects file entries from a directory, as GNU C scandir().
 *
 * @param dirname UTF-8 diretory path
Rémi Denis-Courmont's avatar
Typos    
Rémi Denis-Courmont committed
175
 * @param pointer [OUT] pointer set, on successful completion, to the address
176
177
178
179
180
181
 * of a table of UTF-8 filenames. All filenames must be freed with free().
 * The table itself must be freed with free() as well.
 *
 * @return How many file names were selected (possibly 0),
 * or -1 in case of error.
 */
182
int vlc_scandir( const char *dirname, char ***namelist,
183
184
185
                  int (*select)( const char * ),
                  int (*compar)( const char **, const char ** ) )
{
186
    DIR *dir = vlc_opendir (dirname);
187
188
189
190
    int val = -1;

    if (dir != NULL)
    {
191
        val = vlc_loaddir (dir, namelist, select, compar);
192
193
194
195
196
        closedir (dir);
    }
    return val;
}

197
int vlc_mkstemp( char *template )
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
    static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;

    /* */
    assert( template );

    /* Check template validity */
    const size_t i_length = strlen( template );
    char *psz_rand = &template[i_length-6];

    if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
    {
        errno = EINVAL;
        return -1;
    }

    /* */
    for( int i = 0; i < 256; i++ )
    {
        /* Create a pseudo random file name */
219
220
221
        uint8_t pi_rand[6];

        vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
222
        for( int j = 0; j < 6; j++ )
223
            psz_rand[j] = digits[pi_rand[j] % i_digits];
224
225

        /* */
226
        int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
227
228
229
230
231
232
233
234
235
        if( fd >= 0 )
            return fd;
        if( errno != EEXIST )
            return -1;
    }

    errno = EEXIST;
    return -1;
}