skins2: add support for the new key accelerator feature.
[vlc.git] / src / win32 / filesystem.c
blobf938147021ce83e479ccda46c50c2c43ea7a1fe9
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <assert.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <winsock2.h>
40 #include <direct.h>
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
44 #include <vlc_fs.h>
45 #include "libvlc.h" /* vlc_mkdir */
47 #ifdef _MSC_VER
48 # define __STDC__ 1
49 # include <io.h> /* _pipe */
50 #endif
52 static wchar_t *widen_path (const char *path)
54 wchar_t *wpath;
56 errno = 0;
57 wpath = ToWide (path);
58 if (wpath == NULL)
60 if (errno == 0)
61 errno = ENOENT;
62 return NULL;
64 return wpath;
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, ...)
74 int mode = 0;
75 va_list ap;
77 flags |= O_NOINHERIT; /* O_CLOEXEC */
78 /* Defaults to binary mode */
79 if ((flags & O_TEXT) == 0)
80 flags |= O_BINARY;
82 va_start (ap, flags);
83 if (flags & O_CREAT)
84 mode = va_arg (ap, int);
85 va_end (ap);
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);
92 if (wpath == NULL)
93 return -1;
95 int fd = _wopen (wpath, flags, mode);
96 free (wpath);
97 return fd;
100 int vlc_openat (int dir, const char *filename, int flags, ...)
102 (void) dir; (void) filename; (void) flags;
103 errno = ENOSYS;
104 return -1;
107 int vlc_mkdir( const char *dirname, mode_t mode )
109 wchar_t *wpath = widen_path (dirname);
110 if (wpath == NULL)
111 return -1;
113 int ret = _wmkdir (wpath);
114 free (wpath);
115 (void) mode;
116 return ret;
119 char *vlc_getcwd (void)
121 wchar_t *wdir = _wgetcwd (NULL, 0);
122 if (wdir == NULL)
123 return NULL;
125 char *dir = FromWide (wdir);
126 free (wdir);
127 return dir;
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);
135 if (wpath == NULL)
136 return NULL;
138 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
139 if (unlikely(p_dir == NULL))
141 free(wpath);
142 return NULL;
145 #if !VLC_WINSTORE_APP
146 /* Special mode to list drive letters */
147 if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
149 free (wpath);
150 p_dir->wdir = NULL;
151 p_dir->u.drives = GetLogicalDrives ();
152 return (void *)p_dir;
154 #endif
156 assert (wpath[0]); // wpath[1] is defined
157 p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
159 _WDIR *wdir = _wopendir (wpath);
160 free (wpath);
161 if (wdir == NULL)
163 free (p_dir);
164 return NULL;
166 p_dir->wdir = wdir;
167 p_dir->entry = NULL;
168 return (void *)p_dir;
171 char *vlc_readdir (DIR *dir)
173 vlc_DIR *p_dir = (vlc_DIR *)dir;
175 free(p_dir->entry);
177 #if !VLC_WINSTORE_APP
178 /* Drive letters mode */
179 if (p_dir->wdir == NULL)
181 DWORD drives = p_dir->u.drives;
182 if (drives == 0)
183 return NULL; /* end */
185 unsigned int i;
186 for (i = 0; !(drives & 1); i++)
187 drives >>= 1;
188 p_dir->u.drives &= ~(1UL << i);
189 assert (i < 26);
191 if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
192 p_dir->entry = NULL;
194 else
195 #endif
196 if (p_dir->u.insert_dot_dot)
198 /* Adds "..", gruik! */
199 p_dir->u.insert_dot_dot = false;
200 p_dir->entry = strdup ("..");
202 else
204 struct _wdirent *ent = _wreaddir (p_dir->wdir);
205 p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
207 return p_dir->entry;
210 int vlc_stat (const char *filename, struct stat *buf)
212 wchar_t *wpath = widen_path (filename);
213 if (wpath == NULL)
214 return -1;
216 static_assert (sizeof (*buf) == sizeof (struct _stati64),
217 "Mismatched struct stat definition.");
219 int ret = _wstati64 (wpath, buf);
220 free (wpath);
221 return ret;
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);
232 if (wpath == NULL)
233 return -1;
235 int ret = _wunlink (wpath);
236 free (wpath);
237 return ret;
240 int vlc_rename (const char *oldpath, const char *newpath)
242 int ret = -1;
244 wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
245 if (wold == NULL || wnew == NULL)
246 goto out;
248 if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
249 { /* Windows does not allow atomic file replacement */
250 if (_wremove (wnew))
252 errno = EACCES; /* restore errno */
253 goto out;
255 if (_wrename (wold, wnew))
256 goto out;
258 ret = 0;
259 out:
260 free (wnew);
261 free (wold);
262 return ret;
265 int vlc_dup (int oldfd)
267 int fd = dup (oldfd);
268 if (fd != -1)
269 setmode (fd, O_BINARY);
270 return fd;
273 int vlc_pipe (int fds[2])
275 #if VLC_WINSTORE_APP
276 _set_errno(EPERM);
277 return -1;
278 #else
279 return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
280 #endif
283 #include <vlc_network.h>
285 int vlc_socket (int pf, int type, int proto, bool nonblock)
287 int fd = socket (pf, type, proto);
288 if (fd == -1)
289 return -1;
291 if (nonblock)
292 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
293 return fd;
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 });
301 return fd;