]> git.sesse.net Git - vlc/blob - src/win32/filesystem.c
Win32 threads: remove functions forbidden on Windows Store
[vlc] / src / win32 / filesystem.c
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
6  *
7  * Authors: Rémi Denis-Courmont <rem # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <assert.h>
32
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>
41
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
44 #include <vlc_fs.h>
45 #include "libvlc.h" /* vlc_mkdir */
46
47 static wchar_t *widen_path (const char *path)
48 {
49     wchar_t *wpath;
50
51     errno = 0;
52     wpath = ToWide (path);
53     if (wpath == NULL)
54     {
55         if (errno == 0)
56             errno = ENOENT;
57         return NULL;
58     }
59     return wpath;
60 }
61
62 #define CONVERT_PATH(path, wpath, err) \
63     wchar_t *wpath = wide_path(path); \
64     if (wpath == NULL) return (err)
65
66
67 int vlc_open (const char *filename, int flags, ...)
68 {
69     int mode = 0;
70     va_list ap;
71
72     flags |= O_NOINHERIT; /* O_CLOEXEC */
73     /* Defaults to binary mode */
74     if ((flags & O_TEXT) == 0)
75         flags |= O_BINARY;
76
77     va_start (ap, flags);
78     if (flags & O_CREAT)
79         mode = va_arg (ap, int);
80     va_end (ap);
81
82     /*
83      * open() cannot open files with non-“ANSI” characters on Windows.
84      * We use _wopen() instead. Same thing for mkdir() and stat().
85      */
86     wchar_t *wpath = widen_path (filename);
87     if (wpath == NULL)
88         return -1;
89
90     int fd = _wopen (wpath, flags, mode);
91     free (wpath);
92     return fd;
93 }
94
95 int vlc_openat (int dir, const char *filename, int flags, ...)
96 {
97     (void) dir; (void) filename; (void) flags;
98     errno = ENOSYS;
99     return -1;
100 }
101
102 int vlc_mkdir( const char *dirname, mode_t mode )
103 {
104     wchar_t *wpath = widen_path (dirname);
105     if (wpath == NULL)
106         return -1;
107
108     int ret = _wmkdir (wpath);
109     free (wpath);
110     (void) mode;
111     return ret;
112 }
113
114 char *vlc_getcwd (void)
115 {
116     wchar_t *wdir = _wgetcwd (NULL, 0);
117     if (wdir == NULL)
118         return NULL;
119
120     char *dir = FromWide (wdir);
121     free (wdir);
122     return dir;
123 }
124
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
128 {
129     _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
130     union
131     {
132         DWORD drives;
133         bool insert_dot_dot;
134     } u;
135 } vlc_DIR;
136
137
138 DIR *vlc_opendir (const char *dirname)
139 {
140     wchar_t *wpath = widen_path (dirname);
141     if (wpath == NULL)
142         return NULL;
143
144     vlc_DIR *p_dir = malloc (sizeof (*p_dir));
145     if (unlikely(p_dir == NULL))
146     {
147         free(wpath);
148         return NULL;
149     }
150
151 #if !VLC_WINSTORE_APP
152     /* Special mode to list drive letters */
153     if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
154     {
155         free (wpath);
156         p_dir->wdir = NULL;
157         p_dir->u.drives = GetLogicalDrives ();
158         return (void *)p_dir;
159     }
160 #endif
161
162     assert (wpath[0]); // wpath[1] is defined
163     p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
164
165     _WDIR *wdir = _wopendir (wpath);
166     free (wpath);
167     if (wdir == NULL)
168     {
169         free (p_dir);
170         return NULL;
171     }
172     p_dir->wdir = wdir;
173     return (void *)p_dir;
174 }
175
176 char *vlc_readdir (DIR *dir)
177 {
178     vlc_DIR *p_dir = (vlc_DIR *)dir;
179
180 #if !VLC_WINSTORE_APP
181     /* Drive letters mode */
182     if (p_dir->wdir == NULL)
183     {
184         DWORD drives = p_dir->u.drives;
185         if (drives == 0)
186             return NULL; /* end */
187
188         unsigned int i;
189         for (i = 0; !(drives & 1); i++)
190             drives >>= 1;
191         p_dir->u.drives &= ~(1UL << i);
192         assert (i < 26);
193
194         char *ret;
195         if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
196             return NULL;
197         return ret;
198     }
199 #endif
200
201     if (p_dir->u.insert_dot_dot)
202     {
203         /* Adds "..", gruik! */
204         p_dir->u.insert_dot_dot = false;
205         return strdup ("..");
206     }
207
208     struct _wdirent *ent = _wreaddir (p_dir->wdir);
209     if (ent == NULL)
210         return NULL;
211     return FromWide (ent->d_name);
212 }
213
214 int vlc_stat (const char *filename, struct stat *buf)
215 {
216     wchar_t *wpath = widen_path (filename);
217     if (wpath == NULL)
218         return -1;
219
220     static_assert (sizeof (*buf) == sizeof (struct _stati64),
221                    "Mismatched struct stat definition.");
222
223     int ret = _wstati64 (wpath, buf);
224     free (wpath);
225     return ret;
226 }
227
228 int vlc_lstat (const char *filename, struct stat *buf)
229 {
230     return vlc_stat (filename, buf);
231 }
232
233 int vlc_unlink (const char *filename)
234 {
235     wchar_t *wpath = widen_path (filename);
236     if (wpath == NULL)
237         return -1;
238
239     int ret = _wunlink (wpath);
240     free (wpath);
241     return ret;
242 }
243
244 int vlc_rename (const char *oldpath, const char *newpath)
245 {
246     int ret = -1;
247
248     wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
249     if (wold == NULL || wnew == NULL)
250         goto out;
251
252     if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
253     {   /* Windows does not allow atomic file replacement */
254         if (_wremove (wnew))
255         {
256             errno = EACCES; /* restore errno */
257             goto out;
258         }
259         if (_wrename (wold, wnew))
260             goto out;
261     }
262     ret = 0;
263 out:
264     free (wnew);
265     free (wold);
266     return ret;
267 }
268
269 int vlc_dup (int oldfd)
270 {
271     int fd = dup (oldfd);
272     if (fd != -1)
273         setmode (fd, O_BINARY);
274     return fd;
275 }
276
277 int vlc_pipe (int fds[2])
278 {
279 #if VLC_WINSTORE_APP
280     _set_errno(EPERM);
281     return -1;
282 #else
283     return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
284 #endif
285 }
286
287 #include <vlc_network.h>
288
289 int vlc_socket (int pf, int type, int proto, bool nonblock)
290 {
291     int fd = socket (pf, type, proto);
292     if (fd == -1)
293         return -1;
294
295     if (nonblock)
296         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
297     return fd;
298 }
299
300 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
301 {
302     int fd = accept (lfd, addr, alen);
303     if (fd != -1 && nonblock)
304         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
305     return fd;
306 }