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 */
47 static wchar_t *widen_path (const char *path)
52 wpath = ToWide (path);
62 #define CONVERT_PATH(path, wpath, err) \
63 wchar_t *wpath = wide_path(path); \
64 if (wpath == NULL) return (err)
67 int vlc_open (const char *filename, int flags, ...)
72 flags |= O_NOINHERIT; /* O_CLOEXEC */
73 /* Defaults to binary mode */
74 if ((flags & O_TEXT) == 0)
79 mode = va_arg (ap, int);
83 * open() cannot open files with non-“ANSI” characters on Windows.
84 * We use _wopen() instead. Same thing for mkdir() and stat().
86 wchar_t *wpath = widen_path (filename);
90 int fd = _wopen (wpath, flags, mode);
95 int vlc_openat (int dir, const char *filename, int flags, ...)
97 (void) dir; (void) filename; (void) flags;
102 int vlc_mkdir( const char *dirname, mode_t mode )
104 wchar_t *wpath = widen_path (dirname);
108 int ret = _wmkdir (wpath);
114 char *vlc_getcwd (void)
116 wchar_t *wdir = _wgetcwd (NULL, 0);
120 char *dir = FromWide (wdir);
125 /* Under Windows, these wrappers return the list of drive letters
126 * when called with an empty argument or just '\'. */
127 typedef struct vlc_DIR
129 _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
138 DIR *vlc_opendir (const char *dirname)
140 wchar_t *wpath = widen_path (dirname);
144 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
145 if (unlikely(p_dir == NULL))
151 /* Special mode to list drive letters */
152 if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
156 p_dir->u.drives = GetLogicalDrives ();
157 return (void *)p_dir;
160 assert (wpath[0]); // wpath[1] is defined
161 p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
163 _WDIR *wdir = _wopendir (wpath);
171 return (void *)p_dir;
174 char *vlc_readdir (DIR *dir)
176 vlc_DIR *p_dir = (vlc_DIR *)dir;
178 /* Drive letters mode */
179 if (p_dir->wdir == NULL)
181 DWORD drives = p_dir->u.drives;
183 return NULL; /* end */
186 for (i = 0; !(drives & 1); i++)
188 p_dir->u.drives &= ~(1UL << i);
192 if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
197 if (p_dir->u.insert_dot_dot)
199 /* Adds "..", gruik! */
200 p_dir->u.insert_dot_dot = false;
201 return strdup ("..");
204 struct _wdirent *ent = _wreaddir (p_dir->wdir);
207 return FromWide (ent->d_name);
210 int vlc_stat (const char *filename, struct stat *buf)
212 wchar_t *wpath = widen_path (filename);
216 static_assert (sizeof (*buf) == sizeof (struct _stati64),
217 "Mismatched struct stat definition.");
219 int ret = _wstati64 (wpath, buf);
224 int vlc_lstat (const char *filename, struct stat *buf)
226 return vlc_stat (filename, buf);
229 int vlc_unlink (const char *filename)
231 wchar_t *wpath = widen_path (filename);
235 int ret = _wunlink (wpath);
240 int vlc_rename (const char *oldpath, const char *newpath)
244 wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
245 if (wold == NULL || wnew == NULL)
248 if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
249 { /* Windows does not allow atomic file replacement */
252 errno = EACCES; /* restore errno */
255 if (_wrename (wold, wnew))
265 int vlc_dup (int oldfd)
267 int fd = dup (oldfd);
269 setmode (fd, O_BINARY);
273 int vlc_pipe (int fds[2])
279 return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
283 #include <vlc_network.h>
285 int vlc_socket (int pf, int type, int proto, bool nonblock)
287 int fd = socket (pf, type, proto);
292 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
296 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
298 int fd = accept (lfd, addr, alen);
299 if (fd != -1 && nonblock)
300 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });