1 /*****************************************************************************
2 * filesystem.c: Windows file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VLC authors and VideoLAN
5 * Copyright © 2005-2008 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <sys/types.h>
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
45 #include "libvlc.h" /* vlc_mkdir */
49 # include <io.h> /* _pipe */
52 static wchar_t *widen_path (const char *path)
57 wpath = ToWide (path);
67 #define CONVERT_PATH(path, wpath, err) \
68 wchar_t *wpath = wide_path(path); \
69 if (wpath == NULL) return (err)
72 int vlc_open (const char *filename, int flags, ...)
77 flags |= O_NOINHERIT; /* O_CLOEXEC */
78 /* Defaults to binary mode */
79 if ((flags & O_TEXT) == 0)
84 mode = va_arg (ap, int);
88 * open() cannot open files with non-“ANSI” characters on Windows.
89 * We use _wopen() instead. Same thing for mkdir() and stat().
91 wchar_t *wpath = widen_path (filename);
95 int fd = _wopen (wpath, flags, mode);
100 int vlc_openat (int dir, const char *filename, int flags, ...)
102 (void) dir; (void) filename; (void) flags;
107 int vlc_mkdir( const char *dirname, mode_t mode )
109 wchar_t *wpath = widen_path (dirname);
113 int ret = _wmkdir (wpath);
119 char *vlc_getcwd (void)
121 wchar_t *wdir = _wgetcwd (NULL, 0);
125 char *dir = FromWide (wdir);
130 /* Under Windows, these wrappers return the list of drive letters
131 * when called with an empty argument or just '\'. */
132 DIR *vlc_opendir (const char *dirname)
134 wchar_t *wpath = widen_path (dirname);
138 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
139 if (unlikely(p_dir == NULL))
145 #if !VLC_WINSTORE_APP
146 /* Special mode to list drive letters */
147 if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
151 p_dir->u.drives = GetLogicalDrives ();
153 return (void *)p_dir;
157 assert (wpath[0]); // wpath[1] is defined
158 p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
160 _WDIR *wdir = _wopendir (wpath);
169 return (void *)p_dir;
172 char *vlc_readdir (DIR *dir)
174 vlc_DIR *p_dir = (vlc_DIR *)dir;
178 #if !VLC_WINSTORE_APP
179 /* Drive letters mode */
180 if (p_dir->wdir == NULL)
182 DWORD drives = p_dir->u.drives;
186 return NULL; /* end */
190 for (i = 0; !(drives & 1); i++)
192 p_dir->u.drives &= ~(1UL << i);
195 if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
200 if (p_dir->u.insert_dot_dot)
202 /* Adds "..", gruik! */
203 p_dir->u.insert_dot_dot = false;
204 p_dir->entry = strdup ("..");
208 struct _wdirent *ent = _wreaddir (p_dir->wdir);
209 p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
214 int vlc_stat (const char *filename, struct stat *buf)
216 wchar_t *wpath = widen_path (filename);
220 static_assert (sizeof (*buf) == sizeof (struct _stati64),
221 "Mismatched struct stat definition.");
223 int ret = _wstati64 (wpath, buf);
228 int vlc_lstat (const char *filename, struct stat *buf)
230 return vlc_stat (filename, buf);
233 int vlc_unlink (const char *filename)
235 wchar_t *wpath = widen_path (filename);
239 int ret = _wunlink (wpath);
244 int vlc_rename (const char *oldpath, const char *newpath)
248 wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
249 if (wold == NULL || wnew == NULL)
252 if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
253 { /* Windows does not allow atomic file replacement */
256 errno = EACCES; /* restore errno */
259 if (_wrename (wold, wnew))
269 int vlc_dup (int oldfd)
271 int fd = dup (oldfd);
273 setmode (fd, O_BINARY);
277 int vlc_pipe (int fds[2])
283 return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
287 #include <vlc_network.h>
289 int vlc_socket (int pf, int type, int proto, bool nonblock)
291 int fd = socket (pf, type, proto);
296 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
300 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
302 int fd = accept (lfd, addr, alen);
303 if (fd != -1 && nonblock)
304 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
308 #if !VLC_WINSTORE_APP
309 FILE *vlc_win32_tmpfile(void)
311 TCHAR tmp_path[MAX_PATH-14];
312 int i_ret = GetTempPath (MAX_PATH-14, tmp_path);
316 TCHAR tmp_name[MAX_PATH];
317 i_ret = GetTempFileName(tmp_path, TEXT("VLC"), 0, tmp_name);
321 HANDLE hFile = CreateFile(tmp_name,
322 GENERIC_READ | GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS,
323 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
324 if (hFile == INVALID_HANDLE_VALUE)
327 int fd = _open_osfhandle((intptr_t)hFile, 0);
333 FILE *stream = _fdopen(fd, "w+b");
334 if (stream == NULL) {
341 FILE *vlc_win32_tmpfile(void)