]> git.sesse.net Git - vlc/blob - src/win32/filesystem.c
modules: warn when the return value is unused. It helps tracking common memory leaks.
[vlc] / src / win32 / filesystem.c
1 /*****************************************************************************
2  * filesystem.c: Windows file system helpers
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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 <vlc_common.h>
32 #include <vlc_charset.h>
33 #include <vlc_fs.h>
34 #include "libvlc.h" /* vlc_mkdir */
35
36 #include <assert.h>
37
38 #include <stdio.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <dirent.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <winsock2.h>
45 #ifndef UNDER_CE
46 # include <direct.h>
47 #else
48 # include <tchar.h>
49 #endif
50
51 static int convert_path (const char *restrict path, wchar_t *restrict wpath)
52 {
53     if (!MultiByteToWideChar (CP_UTF8, 0, path, -1, wpath, MAX_PATH))
54     {
55         errno = ENOENT;
56         return -1;
57     }
58     wpath[MAX_PATH] = L'\0';
59     return 0;
60 }
61 #define CONVERT_PATH(path, wpath, err) \
62   wchar_t wpath[MAX_PATH+1]; \
63   if (convert_path (path, wpath)) \
64       return (err)
65
66 int vlc_open (const char *filename, int flags, ...)
67 {
68     unsigned int mode = 0;
69     va_list ap;
70
71     va_start (ap, flags);
72     if (flags & O_CREAT)
73         mode = va_arg (ap, unsigned int);
74     va_end (ap);
75
76 #ifdef UNDER_CE
77     /*_open translates to wchar internally on WinCE*/
78     return _open (filename, flags, mode);
79 #else
80     /*
81      * open() cannot open files with non-“ANSI” characters on Windows.
82      * We use _wopen() instead. Same thing for mkdir() and stat().
83      */
84     CONVERT_PATH(filename, wpath, -1);
85     return _wopen (wpath, flags, mode);
86
87 #endif
88 }
89
90 int vlc_openat (int dir, const char *filename, int flags, ...)
91 {
92     (void) dir; (void) filename; (void) flags;
93     errno = ENOSYS;
94     return -1;
95 }
96
97 int vlc_mkdir( const char *dirname, mode_t mode )
98 {
99 #if defined (UNDER_CE)
100     (void) mode;
101     /* mkdir converts internally to wchar */
102     return _mkdir(dirname);
103 #else
104     (void) mode;
105     CONVERT_PATH (dirname, wpath, -1);
106     return _wmkdir (wpath);
107
108 #endif
109 }
110
111 /* Under Windows, these wrappers return the list of drive letters
112  * when called with an empty argument or just '\'. */
113 typedef struct vlc_DIR
114 {
115     _WDIR *wdir; /* MUST be first, see <vlc_fs.h> */
116     union
117     {
118         DWORD drives;
119         bool insert_dot_dot;
120     } u;
121 } vlc_DIR;
122
123
124 DIR *vlc_opendir (const char *dirname)
125 {
126     CONVERT_PATH (dirname, wpath, NULL);
127
128     vlc_DIR *p_dir = malloc (sizeof (*p_dir));
129     if (unlikely(p_dir == NULL))
130         return NULL;
131
132     if (wpath == NULL || wpath[0] == '\0'
133      || (wcscmp (wpath, L"\\") == 0))
134     {
135         /* Special mode to list drive letters */
136         p_dir->wdir = NULL;
137 #ifdef UNDER_CE
138         p_dir->u.drives = 1;
139 #else
140         p_dir->u.drives = GetLogicalDrives ();
141 #endif
142         return (void *)p_dir;
143     }
144
145     _WDIR *wdir = _wopendir (wpath);
146     if (wdir == NULL)
147     {
148         free (p_dir);
149         return NULL;
150     }
151
152     p_dir->wdir = wdir;
153     assert (wpath[0]); // wpath[1] is defined
154     p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
155     return (void *)p_dir;
156 }
157
158 char *vlc_readdir (DIR *dir)
159 {
160     vlc_DIR *p_dir = (vlc_DIR *)dir;
161
162     if (p_dir->wdir == NULL)
163     {
164         /* Drive letters mode */
165         DWORD drives = p_dir->u.drives;
166         if (drives == 0)
167             return NULL; /* end */
168 #ifdef UNDER_CE
169         p_dir->u.drives = 0;
170         return strdup ("\\");
171 #else
172         unsigned int i;
173         for (i = 0; !(drives & 1); i++)
174             drives >>= 1;
175         p_dir->u.drives &= ~(1UL << i);
176         assert (i < 26);
177
178         char *ret;
179         if (asprintf (&ret, "%c:\\", 'A' + i) == -1)
180             return NULL;
181         return ret;
182 #endif
183     }
184
185     if (p_dir->u.insert_dot_dot)
186     {
187         /* Adds "..", gruik! */
188         p_dir->u.insert_dot_dot = false;
189         return strdup ("..");
190     }
191
192     struct _wdirent *ent = _wreaddir (p_dir->wdir);
193     if (ent == NULL)
194         return NULL;
195     return FromWide (ent->d_name);
196 }
197
198 int vlc_stat (const char *filename, struct stat *buf)
199 {
200 #ifdef UNDER_CE
201     /* _stat translates to wchar internally on WinCE */
202     return _stat (filename, buf);
203 #else
204     CONVERT_PATH (filename, wpath, -1);
205     return _wstati64 (wpath, buf);
206 #endif
207 }
208
209 int vlc_lstat (const char *filename, struct stat *buf)
210 {
211     return vlc_stat (filename, buf);
212 }
213
214 int vlc_unlink (const char *filename)
215 {
216 #ifdef UNDER_CE
217     /*_open translates to wchar internally on WinCE*/
218     return _unlink( filename );
219 #else
220     CONVERT_PATH (filename, wpath, -1);
221     return _wunlink (wpath);
222 #endif
223 }
224
225 int vlc_rename (const char *oldpath, const char *newpath)
226 {
227     CONVERT_PATH (oldpath, wold, -1);
228     CONVERT_PATH (newpath, wnew, -1);
229 # ifdef UNDER_CE
230     /* FIXME: errno support */
231     return MoveFileW (wold, wnew) ? 0 : -1;
232 #else
233     if (_wrename (wold, wnew) && errno == EACCES)
234     {   /* Windows does not allow atomic file replacement */
235         if (_wremove (wnew))
236         {
237             errno = EACCES; /* restore errno */
238             return -1;
239         }
240         if (_wrename (wold, wnew))
241             return -1;
242     }
243     return 0;
244 #endif
245 }
246
247 int vlc_dup (int oldfd)
248 {
249 #ifdef UNDER_CE
250     (void) oldfd;
251     errno = ENOSYS;
252     return -1;
253 #else
254     return dup (oldfd);
255 #endif
256 }
257
258 int vlc_pipe (int fds[2])
259 {
260 #ifdef UNDER_CE
261     (void) fds;
262     errno = ENOSYS;
263     return -1;
264 #else
265     return _pipe (fds, 32768, O_BINARY);
266 #endif
267 }
268
269 #include <vlc_network.h>
270
271 int vlc_socket (int pf, int type, int proto, bool nonblock)
272 {
273     int fd = socket (pf, type, proto);
274     if (fd == -1)
275         return -1;
276
277     if (nonblock)
278         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
279     return fd;
280 }
281
282 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
283 {
284     int fd = accept (lfd, addr, alen);
285     if (fd != -1 && nonblock)
286         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
287     return fd;
288 }