filesystem.c 8.86 KB
Newer Older
1 2 3
/*****************************************************************************
 * filesystem.c: Windows file system helpers
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2005-2006 VLC authors and VideoLAN
5 6 7 8
 * Copyright © 2005-2008 Rémi Denis-Courmont
 *
 * Authors: Rémi Denis-Courmont <rem # videolan.org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * 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
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * 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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *****************************************************************************/

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

#include <assert.h>

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <winsock2.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
40
#include <direct.h>
41

42 43 44 45 46
#include <vlc_common.h>
#include <vlc_charset.h>
#include <vlc_fs.h>
#include "libvlc.h" /* vlc_mkdir */

47 48 49 50 51
#ifdef _MSC_VER
# define __STDC__ 1
# include <io.h> /* _pipe */
#endif

52
static wchar_t *widen_path (const char *path)
53
{
54 55 56 57 58
    wchar_t *wpath;

    errno = 0;
    wpath = ToWide (path);
    if (wpath == NULL)
59
    {
60 61 62
        if (errno == 0)
            errno = ENOENT;
        return NULL;
63
    }
64
    return wpath;
65
}
66

67
#define CONVERT_PATH(path, wpath, err) \
68 69 70
    wchar_t *wpath = wide_path(path); \
    if (wpath == NULL) return (err)

71 72 73

int vlc_open (const char *filename, int flags, ...)
{
74
    int mode = 0;
75 76
    va_list ap;

77 78 79 80 81
    flags |= O_NOINHERIT; /* O_CLOEXEC */
    /* Defaults to binary mode */
    if ((flags & O_TEXT) == 0)
        flags |= O_BINARY;

82 83
    va_start (ap, flags);
    if (flags & O_CREAT)
84 85 86 87 88 89 90
    {
        int unixmode = va_arg(ap, int);
        if (unixmode & 0444)
            mode |= _S_IREAD;
        if (unixmode & 0222)
            mode |= _S_IWRITE;
    }
91 92 93 94 95 96
    va_end (ap);

    /*
     * open() cannot open files with non-“ANSI” characters on Windows.
     * We use _wopen() instead. Same thing for mkdir() and stat().
     */
97 98 99
    wchar_t *wpath = widen_path (filename);
    if (wpath == NULL)
        return -1;
100

101 102 103
    int fd = _wopen (wpath, flags, mode);
    free (wpath);
    return fd;
104 105 106 107 108 109 110 111 112
}

int vlc_openat (int dir, const char *filename, int flags, ...)
{
    (void) dir; (void) filename; (void) flags;
    errno = ENOSYS;
    return -1;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
int vlc_memfd (void)
{
#if 0
    int fd, err;

    FILE *stream = tmpfile();
    if (stream == NULL)
        return -1;

    fd = vlc_dup(fileno(stream));
    err = errno;
    fclose(stream);
    errno = err;
    return fd;
#else /* Not currently used */
    errno = ENOSYS;
    return -1;
#endif
}

133 134 135 136 137
int vlc_close (int fd)
{
    return close (fd);
}

138 139
int vlc_mkdir( const char *dirname, mode_t mode )
{
140 141 142
    wchar_t *wpath = widen_path (dirname);
    if (wpath == NULL)
        return -1;
143

144 145 146 147
    int ret = _wmkdir (wpath);
    free (wpath);
    (void) mode;
    return ret;
148 149
}

150 151
char *vlc_getcwd (void)
{
152 153 154
#if VLC_WINSTORE_APP
    return NULL;
#else
155 156 157 158 159 160 161
    wchar_t *wdir = _wgetcwd (NULL, 0);
    if (wdir == NULL)
        return NULL;

    char *dir = FromWide (wdir);
    free (wdir);
    return dir;
162
#endif
163 164
}

165 166 167
/* Under Windows, these wrappers return the list of drive letters
 * when called with an empty argument or just '\'. */
DIR *vlc_opendir (const char *dirname)
168
{
169 170 171
    wchar_t *wpath = widen_path (dirname);
    if (wpath == NULL)
        return NULL;
172 173 174

    vlc_DIR *p_dir = malloc (sizeof (*p_dir));
    if (unlikely(p_dir == NULL))
175 176
    {
        free(wpath);
177
        return NULL;
178
    }
179

180
#if !VLC_WINSTORE_APP
181
    /* Special mode to list drive letters */
182 183 184 185 186
    if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
    {
        free (wpath);
        p_dir->wdir = NULL;
        p_dir->u.drives = GetLogicalDrives ();
187
        p_dir->entry = NULL;
188 189
        return (void *)p_dir;
    }
190
#endif
191

192
    assert (wpath[0]); // wpath[1] is defined
193
    p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
194

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195
    _WDIR *wdir = _wopendir (wpath);
196
    free (wpath);
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
197
    if (wdir == NULL)
198 199 200 201
    {
        free (p_dir);
        return NULL;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
202
    p_dir->wdir = wdir;
203
    p_dir->entry = NULL;
204
    return (void *)p_dir;
205 206
}

207
const char *vlc_readdir (DIR *dir)
208
{
209 210
    vlc_DIR *p_dir = (vlc_DIR *)dir;

211 212
    free(p_dir->entry);

213
#if !VLC_WINSTORE_APP
214
    /* Drive letters mode */
215 216 217 218
    if (p_dir->wdir == NULL)
    {
        DWORD drives = p_dir->u.drives;
        if (drives == 0)
219 220
        {
            p_dir->entry = NULL;
221
            return NULL; /* end */
222
        }
223 224 225 226 227 228 229

        unsigned int i;
        for (i = 0; !(drives & 1); i++)
            drives >>= 1;
        p_dir->u.drives &= ~(1UL << i);
        assert (i < 26);

230 231
        if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
            p_dir->entry = NULL;
232
    }
233
    else
234
#endif
235
    if (p_dir->u.insert_dot_dot)
236 237
    {
        /* Adds "..", gruik! */
238
        p_dir->u.insert_dot_dot = false;
239
        p_dir->entry = strdup ("..");
240
    }
241 242 243 244 245 246
    else
    {
        struct _wdirent *ent = _wreaddir (p_dir->wdir);
        p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
    }
    return p_dir->entry;
247 248 249 250
}

int vlc_stat (const char *filename, struct stat *buf)
{
251 252 253 254
    wchar_t *wpath = widen_path (filename);
    if (wpath == NULL)
        return -1;

255 256 257
    static_assert (sizeof (*buf) == sizeof (struct _stati64),
                   "Mismatched struct stat definition.");

258 259 260
    int ret = _wstati64 (wpath, buf);
    free (wpath);
    return ret;
261 262 263 264 265 266 267 268 269
}

int vlc_lstat (const char *filename, struct stat *buf)
{
    return vlc_stat (filename, buf);
}

int vlc_unlink (const char *filename)
{
270 271 272 273 274 275 276
    wchar_t *wpath = widen_path (filename);
    if (wpath == NULL)
        return -1;

    int ret = _wunlink (wpath);
    free (wpath);
    return ret;
277 278 279 280
}

int vlc_rename (const char *oldpath, const char *newpath)
{
281 282 283 284 285 286
    int ret = -1;

    wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
    if (wold == NULL || wnew == NULL)
        goto out;

287
    if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
288 289 290 291
    {   /* Windows does not allow atomic file replacement */
        if (_wremove (wnew))
        {
            errno = EACCES; /* restore errno */
292
            goto out;
293 294
        }
        if (_wrename (wold, wnew))
295
            goto out;
296
    }
297 298 299 300 301
    ret = 0;
out:
    free (wnew);
    free (wold);
    return ret;
302 303 304 305
}

int vlc_dup (int oldfd)
{
306 307 308 309
    int fd = dup (oldfd);
    if (fd != -1)
        setmode (fd, O_BINARY);
    return fd;
310 311
}

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
312 313
int vlc_pipe (int fds[2])
{
314
#if VLC_WINSTORE_APP
315 316 317
    _set_errno(EPERM);
    return -1;
#else
318
    return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
319
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
320 321
}

322 323 324 325 326 327 328 329 330 331
ssize_t vlc_write(int fd, const void *buf, size_t len)
{
    return write(fd, buf, len);
}

ssize_t vlc_writev(int fd, const struct iovec *iov, int count)
{
    vlc_assert_unreachable();
}

332 333 334 335 336 337 338 339 340 341 342 343 344
#include <vlc_network.h>

int vlc_socket (int pf, int type, int proto, bool nonblock)
{
    int fd = socket (pf, type, proto);
    if (fd == -1)
        return -1;

    if (nonblock)
        ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
    return fd;
}

345 346 347 348 349 350 351
int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
{
    (void) pf; (void) type; (void) proto; (void) fds; (void) nonblock;
    errno = ENOSYS;
    return -1;
}

352 353 354 355 356 357 358
int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
{
    int fd = accept (lfd, addr, alen);
    if (fd != -1 && nonblock)
        ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
    return fd;
}
359

360
#if !VLC_WINSTORE_APP
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
FILE *vlc_win32_tmpfile(void)
{
    TCHAR tmp_path[MAX_PATH-14];
    int i_ret = GetTempPath (MAX_PATH-14, tmp_path);
    if (i_ret == 0)
        return NULL;

    TCHAR tmp_name[MAX_PATH];
    i_ret = GetTempFileName(tmp_path, TEXT("VLC"), 0, tmp_name);
    if (i_ret == 0)
        return NULL;

    HANDLE hFile = CreateFile(tmp_name,
            GENERIC_READ | GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS,
            FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
        return NULL;

    int fd = _open_osfhandle((intptr_t)hFile, 0);
    if (fd == -1) {
        CloseHandle(hFile);
        return NULL;
    }

    FILE *stream = _fdopen(fd, "w+b");
    if (stream == NULL) {
        _close(fd);
        return NULL;
    }
    return stream;
}
392 393 394 395 396 397
#else
FILE *vlc_win32_tmpfile(void)
{
    return NULL;
}
#endif
398