1 /*****************************************************************************
2 * filesystem.c: OS/2 file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VLC authors and VideoLAN
5 * Copyright © 2005-2008 Rémi Denis-Courmont
6 * Copyright (C) 2012 KO Myung-Hun
8 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * KO Myung-Hun <komh@chollian.net>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
35 #include <sys/types.h>
40 #include <sys/socket.h>
42 #include <vlc_common.h>
43 #include <vlc_charset.h>
45 #include "libvlc.h" /* vlc_mkdir */
48 * Opens a system file handle.
50 * @param filename file path to open (with UTF-8 encoding)
51 * @param flags open() flags, see the C library open() documentation
52 * @return a file handle on success, -1 on error (see errno).
53 * @note Contrary to standard open(), this function returns file handles
54 * with the close-on-exec flag enabled.
56 int vlc_open (const char *filename, int flags, ...)
58 unsigned int mode = 0;
63 mode = va_arg (ap, unsigned int);
66 const char *local_name = ToLocale (filename);
68 if (local_name == NULL)
74 int fd = open (local_name, flags, mode);
76 fcntl (fd, F_SETFD, FD_CLOEXEC);
78 LocaleFree (local_name);
83 * Opens a system file handle relative to an existing directory handle.
85 * @param dir directory file descriptor
86 * @param filename file path to open (with UTF-8 encoding)
87 * @param flags open() flags, see the C library open() documentation
88 * @return a file handle on success, -1 on error (see errno).
89 * @note Contrary to standard open(), this function returns file handles
90 * with the close-on-exec flag enabled.
92 int vlc_openat (int dir, const char *filename, int flags, ...)
101 * Creates a directory using UTF-8 paths.
103 * @param dirname a UTF-8 string with the name of the directory that you
105 * @param mode directory permissions
106 * @return 0 on success, -1 on error (see errno).
108 int vlc_mkdir (const char *dirname, mode_t mode)
110 char *locname = ToLocale (dirname);
111 if (unlikely(locname == NULL))
117 int res = mkdir (locname, mode);
118 LocaleFree (locname);
123 * Opens a DIR pointer.
125 * @param dirname UTF-8 representation of the directory name
126 * @return a pointer to the DIR struct, or NULL in case of error.
127 * Release with standard closedir().
129 DIR *vlc_opendir (const char *dirname)
131 const char *locname = ToLocale (dirname);
132 if (unlikely(locname == NULL))
138 DIR *dir = opendir (locname);
140 LocaleFree (locname);
146 * Reads the next file name from an open directory.
148 * @param dir The directory that is being read
150 * @return a UTF-8 string of the directory entry. Use free() to release it.
151 * If there are no more entries in the directory, NULL is returned.
152 * If an error occurs, errno is set and NULL is returned.
154 char *vlc_readdir( DIR *dir )
156 /* Beware that readdir_r() assumes <buf> is large enough to hold the result
157 * dirent including the file name. A buffer overflow could occur otherwise.
158 * In particular, pathconf() and _POSIX_NAME_MAX cannot be used here. */
162 /* In the implementation of Innotek LIBC, aka kLIBC on OS/2,
163 * fpathconf (_PC_NAME_MAX) is broken, and errno is set to EBADF.
164 * Moreover, d_name is not the last member of struct dirent.
165 * So just allocate as many as the size of struct dirent. */
167 long len = sizeof (struct dirent);
169 long len = fpathconf (dirfd (dir), _PC_NAME_MAX);
170 len += offsetof (struct dirent, d_name) + 1;
173 struct dirent *buf = malloc (len);
174 if (unlikely(buf == NULL))
177 int val = readdir_r (dir, buf, &ent);
180 else if (ent != NULL)
181 path = FromLocaleDup (ent->d_name);
186 static int vlc_statEx (const char *filename, struct stat *buf, bool deref)
188 const char *local_name = ToLocale (filename);
189 if (unlikely(local_name == NULL))
195 int res = deref ? stat (local_name, buf)
196 : lstat (local_name, buf);
197 LocaleFree (local_name);
202 * Finds file/inode information, as stat().
203 * Consider using fstat() instead, if possible.
205 * @param filename UTF-8 file path
207 int vlc_stat (const char *filename, struct stat *buf)
209 return vlc_statEx (filename, buf, true);
213 * Finds file/inode information, as lstat().
214 * Consider using fstat() instead, if possible.
216 * @param filename UTF-8 file path
218 int vlc_lstat (const char *filename, struct stat *buf)
220 return vlc_statEx (filename, buf, false);
226 * @param filename a UTF-8 string with the name of the file you want to delete.
227 * @return A 0 return value indicates success. A -1 return value indicates an
228 * error, and an error code is stored in errno
230 int vlc_unlink (const char *filename)
232 const char *local_name = ToLocale (filename);
233 if (unlikely(local_name == NULL))
239 int ret = unlink (local_name);
240 LocaleFree (local_name);
245 * Moves a file atomically. This only works within a single file system.
247 * @param oldpath path to the file before the move
248 * @param newpath intended path to the file after the move
249 * @return A 0 return value indicates success. A -1 return value indicates an
250 * error, and an error code is stored in errno
252 int vlc_rename (const char *oldpath, const char *newpath)
254 const char *lo = ToLocale (oldpath);
258 const char *ln = ToLocale (newpath);
267 int ret = rename (lo, ln);
274 * Determines the current working directory.
276 * @return the current working directory (must be free()'d)
279 char *vlc_getcwd (void)
282 const char *pwd = getenv ("PWD");
286 /* Make sure $PWD is correct */
287 if (stat (pwd, &s1) == 0 && stat (".", &s2) == 0
288 && s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino)
289 return ToLocaleDup (pwd);
292 /* Otherwise iterate getcwd() until the buffer is big enough */
293 long path_max = pathconf (".", _PC_PATH_MAX);
294 size_t size = (path_max == -1 || path_max > 4096) ? 4096 : path_max;
298 char *buf = malloc (size);
299 if (unlikely(buf == NULL))
302 if (getcwd (buf, size) != NULL)
304 char *ret = ToLocaleDup (buf);
306 return ret; /* success */
317 * Duplicates a file descriptor. The new file descriptor has the close-on-exec
318 * descriptor flag set.
319 * @return a new file descriptor or -1
321 int vlc_dup (int oldfd)
326 if (likely(newfd != -1))
327 fcntl (newfd, F_SETFD, FD_CLOEXEC);
333 * Creates a pipe (see "man pipe" for further reference).
335 int vlc_pipe (int fds[2])
340 fcntl (fds[0], F_SETFD, FD_CLOEXEC);
341 fcntl (fds[1], F_SETFD, FD_CLOEXEC);
345 #include <vlc_network.h>
348 * Creates a socket file descriptor. The new file descriptor has the
349 * close-on-exec flag set.
350 * @param pf protocol family
351 * @param type socket type
352 * @param proto network protocol
353 * @param nonblock true to create a non-blocking socket
354 * @return a new file descriptor or -1
356 int vlc_socket (int pf, int type, int proto, bool nonblock)
360 fd = socket (pf, type, proto);
364 fcntl (fd, F_SETFD, FD_CLOEXEC);
366 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
371 * Accepts an inbound connection request on a listening socket.
372 * The new file descriptor has the close-on-exec flag set.
373 * @param lfd listening socket file descriptor
374 * @param addr pointer to the peer address or NULL [OUT]
375 * @param alen pointer to the length of the peer address or NULL [OUT]
376 * @param nonblock whether to put the new socket in non-blocking mode
377 * @return a new file descriptor, or -1 on error.
379 int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
383 int fd = accept (lfd, addr, alen);
386 fcntl (fd, F_SETFD, FD_CLOEXEC);
388 fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
392 while (errno == EINTR);