meta_engine: support id3v2.4 tag for id3 chapter markers
[vlc.git] / src / win32 / filesystem.c
blob09b8ec70d39a6cbf8ac48cbf3c921ed973e03184
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
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 int vlc_open (const char *filename, int flags, ...)
69 int mode = 0;
70 va_list ap;
72 flags |= O_NOINHERIT; /* O_CLOEXEC */
73 /* Defaults to binary mode */
74 if ((flags & O_TEXT) == 0)
75 flags |= O_BINARY;
77 va_start (ap, flags);
78 if (flags & O_CREAT)
80 int unixmode = va_arg(ap, int);
81 if (unixmode & 0444)
82 mode |= _S_IREAD;
83 if (unixmode & 0222)
84 mode |= _S_IWRITE;
86 va_end (ap);
89 * open() cannot open files with non-“ANSI” characters on Windows.
90 * We use _wopen() instead. Same thing for mkdir() and stat().
92 wchar_t *wpath = widen_path (filename);
93 if (wpath == NULL)
94 return -1;
96 int fd = _wopen (wpath, flags, mode);
97 free (wpath);
98 return fd;
101 int vlc_openat (int dir, const char *filename, int flags, ...)
103 (void) dir; (void) filename; (void) flags;
104 errno = ENOSYS;
105 return -1;
108 int vlc_memfd (void)
110 #if 0
111 int fd, err;
113 FILE *stream = tmpfile();
114 if (stream == NULL)
115 return -1;
117 fd = vlc_dup(fileno(stream));
118 err = errno;
119 fclose(stream);
120 errno = err;
121 return fd;
122 #else /* Not currently used */
123 errno = ENOSYS;
124 return -1;
125 #endif
128 int vlc_close (int fd)
130 return close (fd);
133 int vlc_mkdir( const char *dirname, mode_t mode )
135 wchar_t *wpath = widen_path (dirname);
136 if (wpath == NULL)
137 return -1;
139 int ret = _wmkdir (wpath);
140 free (wpath);
141 (void) mode;
142 return ret;
145 char *vlc_getcwd (void)
147 #if VLC_WINSTORE_APP
148 return NULL;
149 #else
150 wchar_t *wdir = _wgetcwd (NULL, 0);
151 if (wdir == NULL)
152 return NULL;
154 char *dir = FromWide (wdir);
155 free (wdir);
156 return dir;
157 #endif
160 /* Under Windows, these wrappers return the list of drive letters
161 * when called with an empty argument or just '\'. */
162 DIR *vlc_opendir (const char *dirname)
164 wchar_t *wpath = widen_path (dirname);
165 if (wpath == NULL)
166 return NULL;
168 vlc_DIR *p_dir = malloc (sizeof (*p_dir));
169 if (unlikely(p_dir == NULL))
171 free(wpath);
172 return NULL;
175 #if !VLC_WINSTORE_APP
176 /* Special mode to list drive letters */
177 if (wpath[0] == L'\0' || (wcscmp (wpath, L"\\") == 0))
179 free (wpath);
180 p_dir->wdir = NULL;
181 p_dir->u.drives = GetLogicalDrives ();
182 p_dir->entry = NULL;
183 return (void *)p_dir;
185 #endif
187 assert (wpath[0]); // wpath[1] is defined
188 p_dir->u.insert_dot_dot = !wcscmp (wpath + 1, L":\\");
190 _WDIR *wdir = _wopendir (wpath);
191 free (wpath);
192 if (wdir == NULL)
194 free (p_dir);
195 return NULL;
197 p_dir->wdir = wdir;
198 p_dir->entry = NULL;
199 return (void *)p_dir;
202 const char *vlc_readdir (DIR *dir)
204 vlc_DIR *p_dir = (vlc_DIR *)dir;
206 free(p_dir->entry);
208 #if !VLC_WINSTORE_APP
209 /* Drive letters mode */
210 if (p_dir->wdir == NULL)
212 DWORD drives = p_dir->u.drives;
213 if (drives == 0)
215 p_dir->entry = NULL;
216 return NULL; /* end */
219 unsigned int i;
220 for (i = 0; !(drives & 1); i++)
221 drives >>= 1;
222 p_dir->u.drives &= ~(1UL << i);
223 assert (i < 26);
225 if (asprintf (&p_dir->entry, "%c:\\", 'A' + i) == -1)
226 p_dir->entry = NULL;
228 else
229 #endif
230 if (p_dir->u.insert_dot_dot)
232 /* Adds "..", gruik! */
233 p_dir->u.insert_dot_dot = false;
234 p_dir->entry = strdup ("..");
236 else
238 struct _wdirent *ent = _wreaddir (p_dir->wdir);
239 p_dir->entry = (ent != NULL) ? FromWide (ent->d_name) : NULL;
241 return p_dir->entry;
244 int vlc_stat (const char *filename, struct stat *buf)
246 wchar_t *wpath = widen_path (filename);
247 if (wpath == NULL)
248 return -1;
250 static_assert (sizeof (*buf) == sizeof (struct _stati64),
251 "Mismatched struct stat definition.");
253 int ret = _wstati64 (wpath, buf);
254 free (wpath);
255 return ret;
258 int vlc_lstat (const char *filename, struct stat *buf)
260 return vlc_stat (filename, buf);
263 int vlc_unlink (const char *filename)
265 wchar_t *wpath = widen_path (filename);
266 if (wpath == NULL)
267 return -1;
269 int ret = _wunlink (wpath);
270 free (wpath);
271 return ret;
274 int vlc_rename (const char *oldpath, const char *newpath)
276 int ret = -1;
278 wchar_t *wold = widen_path (oldpath), *wnew = widen_path (newpath);
279 if (wold == NULL || wnew == NULL)
280 goto out;
282 if (_wrename (wold, wnew) && (errno == EACCES || errno == EEXIST))
283 { /* Windows does not allow atomic file replacement */
284 if (_wremove (wnew))
286 errno = EACCES; /* restore errno */
287 goto out;
289 if (_wrename (wold, wnew))
290 goto out;
292 ret = 0;
293 out:
294 free (wnew);
295 free (wold);
296 return ret;
299 int vlc_dup (int oldfd)
301 int fd = dup (oldfd);
302 if (fd != -1)
303 setmode (fd, O_BINARY);
304 return fd;
307 int vlc_dup2(int oldfd, int newfd)
309 int fd = dup2(oldfd, newfd);
310 if (fd != -1)
311 setmode(fd, O_BINARY);
312 return fd;
315 int vlc_pipe (int fds[2])
317 #if VLC_WINSTORE_APP
318 _set_errno(EPERM);
319 return -1;
320 #else
321 return _pipe (fds, 32768, O_NOINHERIT | O_BINARY);
322 #endif
325 ssize_t vlc_write(int fd, const void *buf, size_t len)
327 return write(fd, buf, len);
330 ssize_t vlc_writev(int fd, const struct iovec *iov, int count)
332 (void) fd; (void) iov; (void) count;
333 vlc_assert_unreachable();
336 #include <vlc_network.h>
338 int vlc_socket (int pf, int type, int proto, bool nonblock)
340 int fd = socket (pf, type, proto);
341 if (fd == -1)
342 return -1;
344 if (nonblock)
345 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
346 return fd;
349 int vlc_socketpair(int pf, int type, int proto, int fds[2], bool nonblock)
351 (void) pf; (void) type; (void) proto; (void) fds; (void) nonblock;
352 errno = ENOSYS;
353 return -1;
356 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
358 int fd = accept (lfd, addr, alen);
359 if (fd != -1 && nonblock)
360 ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
361 return fd;
364 ssize_t vlc_send(int fd, const void *buf, size_t len, int flags)
366 WSABUF wsabuf = { .buf = (char *)buf, .len = len };
367 DWORD sent;
369 return WSASend(fd, &wsabuf, 1, &sent, flags,
370 NULL, NULL) ? -1 : (ssize_t)sent;
373 ssize_t vlc_sendto(int fd, const void *buf, size_t len, int flags,
374 const struct sockaddr *dst, socklen_t dstlen)
376 WSABUF wsabuf = { .buf = (char *)buf, .len = len };
377 DWORD sent;
379 return WSASendTo(fd, &wsabuf, 1, &sent, flags, dst, dstlen,
380 NULL, NULL) ? -1 : (ssize_t)sent;
383 ssize_t vlc_sendmsg(int fd, const struct msghdr *msg, int flags)
385 return sendmsg(fd, msg, flags);