demux: libmp4: add and parse 3DDS uuid
[vlc.git] / src / win32 / filesystem.c
blob29a7a3fc4db48414df1ae53ebedd89d9e350d411
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_memfd (void)
109 #if 0
110 int fd, err;
112 FILE *stream = tmpfile();
113 if (stream == NULL)
114 return -1;
116 fd = vlc_dup(fileno(stream));
117 err = errno;
118 fclose(stream);
119 errno = err;
120 return fd;
121 #else /* Not currently used */
122 errno = ENOSYS;
123 return -1;
124 #endif
127 int vlc_close (int fd)
129 return close (fd);
132 int vlc_mkdir( const char *dirname, mode_t mode )
134 wchar_t *wpath = widen_path (dirname);
135 if (wpath == NULL)
136 return -1;
138 int ret = _wmkdir (wpath);
139 free (wpath);
140 (void) mode;
141 return ret;
144 char *vlc_getcwd (void)
146 #if VLC_WINSTORE_APP
147 return NULL;
148 #else
149 wchar_t *wdir = _wgetcwd (NULL, 0);
150 if (wdir == NULL)
151 return NULL;
153 char *dir = FromWide (wdir);
154 free (wdir);
155 return dir;
156 #endif
159 /* Under Windows, these wrappers return the list of drive letters
160 * when called with an empty argument or just '\'. */
161 DIR *vlc_opendir (const char *dirname)
163 wchar_t *wpath = widen_path (dirname);
164 if (wpath == NULL)
165 return NULL;
167 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
168 if (unlikely(p_dir == NULL))
170 free(wpath);
171 return NULL;
174 #if !VLC_WINSTORE_APP
175 /* Special mode to list drive letters */
176 if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
178 free (wpath);
179 p_dir->wdir = NULL;
180 p_dir->u.drives = GetLogicalDrives ();
181 p_dir->entry = NULL;
182 return (void *)p_dir;
184 #endif
186 assert (wpath[0]); // wpath[1] is defined
187 p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
189 _WDIR *wdir = _wopendir (wpath);
190 free (wpath);
191 if (wdir == NULL)
193 free (p_dir);
194 return NULL;
196 p_dir->wdir = wdir;
197 p_dir->entry = NULL;
198 return (void *)p_dir;
201 const char *vlc_readdir (DIR *dir)
203 vlc_DIR *p_dir = (vlc_DIR *)dir;
205 free(p_dir->entry);
207 #if !VLC_WINSTORE_APP
208 /* Drive letters mode */
209 if (p_dir->wdir == NULL)
211 DWORD drives = p_dir->u.drives;
212 if (drives == 0)
214 p_dir->entry = NULL;
215 return NULL; /* end */
218 unsigned int i;
219 for (i = 0; !(drives & 1); i++)
220 drives >>= 1;
221 p_dir->u.drives &= ~(1UL << i);
222 assert (i < 26);
224 if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
225 p_dir->entry = NULL;
227 else
228 #endif
229 if (p_dir->u.insert_dot_dot)
231 /* Adds "..", gruik! */
232 p_dir->u.insert_dot_dot = false;
233 p_dir->entry = strdup ("..");
235 else
237 struct _wdirent *ent = _wreaddir (p_dir->wdir);
238 p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
240 return p_dir->entry;
243 int vlc_stat (const char *filename, struct stat *buf)
245 wchar_t *wpath = widen_path (filename);
246 if (wpath == NULL)
247 return -1;
249 static_assert (sizeof (*buf) == sizeof (struct _stati64),
250 "Mismatched struct stat definition.");
252 int ret = _wstati64 (wpath, buf);
253 free (wpath);
254 return ret;
257 int vlc_lstat (const char *filename, struct stat *buf)
259 return vlc_stat (filename, buf);
262 int vlc_unlink (const char *filename)
264 wchar_t *wpath = widen_path (filename);
265 if (wpath == NULL)
266 return -1;
268 int ret = _wunlink (wpath);
269 free (wpath);
270 return ret;
273 int vlc_rename (const char *oldpath, const char *newpath)
275 int ret = -1;
277 wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
278 if (wold == NULL || wnew == NULL)
279 goto out;
281 if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
282 { /* Windows does not allow atomic file replacement */
283 if (_wremove (wnew))
285 errno = EACCES; /* restore errno */
286 goto out;
288 if (_wrename (wold, wnew))
289 goto out;
291 ret = 0;
292 out:
293 free (wnew);
294 free (wold);
295 return ret;
298 int vlc_dup (int oldfd)
300 int fd = dup (oldfd);
301 if (fd != -1)
302 setmode (fd, O_BINARY);
303 return fd;
306 int vlc_pipe (int fds[2])
308 #if VLC_WINSTORE_APP
309 _set_errno(EPERM);
310 return -1;
311 #else
312 return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
313 #endif
316 ssize_t vlc_write(int fd, const void *buf, size_t len)
318 return write(fd, buf, len);
321 ssize_t vlc_writev(int fd, const struct iovec *iov, int count)
323 vlc_assert_unreachable();
326 #include <vlc_network.h>
328 int vlc_socket (int pf, int type, int proto, bool nonblock)
330 int fd = socket (pf, type, proto);
331 if (fd == -1)
332 return -1;
334 if (nonblock)
335 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
336 return fd;
339 int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
341 (void) pf; (void) type; (void) proto; (void) fds; (void) nonblock;
342 errno = ENOSYS;
343 return -1;
346 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
348 int fd = accept (lfd, addr, alen);
349 if (fd != -1 && nonblock)
350 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
351 return fd;
354 #if !VLC_WINSTORE_APP
355 FILE *vlc_win32_tmpfile(void)
357 TCHAR tmp_path[MAX_PATH-14];
358 int i_ret = GetTempPath (MAX_PATH-14, tmp_path);
359 if (i_ret == 0)
360 return NULL;
362 TCHAR tmp_name[MAX_PATH];
363 i_ret = GetTempFileName(tmp_path, TEXT("VLC"), 0, tmp_name);
364 if (i_ret == 0)
365 return NULL;
367 HANDLE hFile = CreateFile(tmp_name,
368 GENERIC_READ | GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS,
369 FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
370 if (hFile == INVALID_HANDLE_VALUE)
371 return NULL;
373 int fd = _open_osfhandle((intptr_t)hFile, 0);
374 if (fd == -1) {
375 CloseHandle(hFile);
376 return NULL;
379 FILE *stream = _fdopen(fd, "w+b");
380 if (stream == NULL) {
381 _close(fd);
382 return NULL;
384 return stream;
386 #else
387 FILE *vlc_win32_tmpfile(void)
389 return NULL;
391 #endif