]> git.sesse.net Git - vlc/blob - src/win32/filesystem.c
Revert "win32 opendir: remove broken and obsolete special mode"
[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 (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
152     {
153         free (wpath);
154         /* Special mode to list drive letters */
155         p_dir->wdir = NULL;
156         p_dir->u.drives = GetLogicalDrives ();
157         return (void *)p_dir;
158     }
159
160     assert (wpath[0]); // wpath[1] is defined
161     p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
162
163     _WDIR *wdir = _wopendir (wpath);
164     free (wpath);
165     if (wdir == NULL)
166     {
167         free (p_dir);
168         return NULL;
169     }
170     p_dir->wdir = wdir;
171     return (void *)p_dir;
172 }
173
174 char *vlc_readdir (DIR *dir)
175 {
176     vlc_DIR *p_dir = (vlc_DIR *)dir;
177
178     if (p_dir->wdir == NULL)
179     {
180         /* Drive letters mode */
181         DWORD drives = p_dir->u.drives;
182         if (drives == 0)
183             return NULL; /* end */
184
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);
190
191         char *ret;
192         if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
193             return NULL;
194         return ret;
195     }
196
197     if (p_dir->u.insert_dot_dot)
198     {
199         /* Adds "..", gruik! */
200         p_dir->u.insert_dot_dot = false;
201         return strdup ("..");
202     }
203
204     struct _wdirent *ent = _wreaddir (p_dir->wdir);
205     if (ent == NULL)
206         return NULL;
207     return FromWide (ent->d_name);
208 }
209
210 int vlc_stat (const char *filename, struct stat *buf)
211 {
212     wchar_t *wpath = widen_path (filename);
213     if (wpath == NULL)
214         return -1;
215
216     static_assert (sizeof (*buf) == sizeof (struct _stati64),
217                    "Mismatched struct stat definition.");
218
219     int ret = _wstati64 (wpath, buf);
220     free (wpath);
221     return ret;
222 }
223
224 int vlc_lstat (const char *filename, struct stat *buf)
225 {
226     return vlc_stat (filename, buf);
227 }
228
229 int vlc_unlink (const char *filename)
230 {
231     wchar_t *wpath = widen_path (filename);
232     if (wpath == NULL)
233         return -1;
234
235     int ret = _wunlink (wpath);
236     free (wpath);
237     return ret;
238 }
239
240 int vlc_rename (const char *oldpath, const char *newpath)
241 {
242     int ret = -1;
243
244     wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
245     if (wold == NULL || wnew == NULL)
246         goto out;
247
248     if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
249     {   /* Windows does not allow atomic file replacement */
250         if (_wremove (wnew))
251         {
252             errno = EACCES; /* restore errno */
253             goto out;
254         }
255         if (_wrename (wold, wnew))
256             goto out;
257     }
258     ret = 0;
259 out:
260     free (wnew);
261     free (wold);
262     return ret;
263 }
264
265 int vlc_dup (int oldfd)
266 {
267     int fd = dup (oldfd);
268     if (fd != -1)
269         setmode (fd, O_BINARY);
270     return fd;
271 }
272
273 int vlc_pipe (int fds[2])
274 {
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
281 }
282
283 #include <vlc_network.h>
284
285 int vlc_socket (int pf, int type, int proto, bool nonblock)
286 {
287     int fd = socket (pf, type, proto);
288     if (fd == -1)
289         return -1;
290
291     if (nonblock)
292         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
293     return fd;
294 }
295
296 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
297 {
298     int fd = accept (lfd, addr, alen);
299     if (fd != -1 && nonblock)
300         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
301     return fd;
302 }