]> git.sesse.net Git - vlc/blob - src/win32/filesystem.c
aout: remove unused "equalizer" object variable
[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 #ifdef _MSC_VER
48 # define __STDC__ 1
49 # include <io.h> /* _pipe */
50 #endif
51
52 static wchar_t *widen_path (const char *path)
53 {
54     wchar_t *wpath;
55
56     errno = 0;
57     wpath = ToWide (path);
58     if (wpath == NULL)
59     {
60         if (errno == 0)
61             errno = ENOENT;
62         return NULL;
63     }
64     return wpath;
65 }
66
67 #define CONVERT_PATH(path, wpath, err) \
68     wchar_t *wpath = wide_path(path); \
69     if (wpath == NULL) return (err)
70
71
72 int vlc_open (const char *filename, int flags, ...)
73 {
74     int mode = 0;
75     va_list ap;
76
77     flags |= O_NOINHERIT; /* O_CLOEXEC */
78     /* Defaults to binary mode */
79     if ((flags & O_TEXT) == 0)
80         flags |= O_BINARY;
81
82     va_start (ap, flags);
83     if (flags & O_CREAT)
84         mode = va_arg (ap, int);
85     va_end (ap);
86
87     /*
88      * open() cannot open files with non-“ANSI” characters on Windows.
89      * We use _wopen() instead. Same thing for mkdir() and stat().
90      */
91     wchar_t *wpath = widen_path (filename);
92     if (wpath == NULL)
93         return -1;
94
95     int fd = _wopen (wpath, flags, mode);
96     free (wpath);
97     return fd;
98 }
99
100 int vlc_openat (int dir, const char *filename, int flags, ...)
101 {
102     (void) dir; (void) filename; (void) flags;
103     errno = ENOSYS;
104     return -1;
105 }
106
107 int vlc_mkdir( const char *dirname, mode_t mode )
108 {
109     wchar_t *wpath = widen_path (dirname);
110     if (wpath == NULL)
111         return -1;
112
113     int ret = _wmkdir (wpath);
114     free (wpath);
115     (void) mode;
116     return ret;
117 }
118
119 char *vlc_getcwd (void)
120 {
121     wchar_t *wdir = _wgetcwd (NULL, 0);
122     if (wdir == NULL)
123         return NULL;
124
125     char *dir = FromWide (wdir);
126     free (wdir);
127     return dir;
128 }
129
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)
133 {
134     wchar_t *wpath = widen_path (dirname);
135     if (wpath == NULL)
136         return NULL;
137
138     vlc_DIR *p_dir = malloc (sizeof (*p_dir));
139     if (unlikely(p_dir == NULL))
140     {
141         free(wpath);
142         return NULL;
143     }
144
145 #if !VLC_WINSTORE_APP
146     /* Special mode to list drive letters */
147     if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
148     {
149         free (wpath);
150         p_dir->wdir = NULL;
151         p_dir->u.drives = GetLogicalDrives ();
152         return (void *)p_dir;
153     }
154 #endif
155
156     assert (wpath[0]); // wpath[1] is defined
157     p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
158
159     _WDIR *wdir = _wopendir (wpath);
160     free (wpath);
161     if (wdir == NULL)
162     {
163         free (p_dir);
164         return NULL;
165     }
166     p_dir->wdir = wdir;
167     p_dir->entry = NULL;
168     return (void *)p_dir;
169 }
170
171 char *vlc_readdir (DIR *dir)
172 {
173     vlc_DIR *p_dir = (vlc_DIR *)dir;
174
175     free(p_dir->entry);
176
177 #if !VLC_WINSTORE_APP
178     /* Drive letters mode */
179     if (p_dir->wdir == NULL)
180     {
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         if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
192             p_dir->entry = NULL;
193     }
194     else
195 #endif
196     if (p_dir->u.insert_dot_dot)
197     {
198         /* Adds "..", gruik! */
199         p_dir->u.insert_dot_dot = false;
200         p_dir->entry = strdup ("..");
201     }
202     else
203     {
204         struct _wdirent *ent = _wreaddir (p_dir->wdir);
205         p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
206     }
207     return p_dir->entry;
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 }