VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_fs.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_fs.h: File system helpers
3 *****************************************************************************
4 * Copyright © 2006-2010 Rémi Denis-Courmont
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20
21#ifndef VLC_FS_H
22#define VLC_FS_H 1
23
24#include <sys/types.h>
25
26struct stat;
27struct iovec;
28
29#ifdef _WIN32
30# include <io.h>
31# include <sys/stat.h>
32# ifndef stat
33# define stat _stati64
34# endif
35# ifndef fstat
36# define fstat _fstati64
37# endif
38# undef lseek
39# define lseek _lseeki64
40#else // !_WIN32
41#include <dirent.h>
42#endif
43
44#ifdef __ANDROID__
45# define lseek lseek64
46#endif
47
48#ifdef __OS2__
49# include <io.h>
50#endif
51
52/**
53 * \defgroup os Operating system
54 * \ingroup vlc
55 * \defgroup file File system
56 * \ingroup os
57 * @{
58 *
59 * \file
60 * The functions in this file help with using low-level Unix-style file
61 * descriptors, BSD sockets and directories. In general, they retain the
62 * prototype and most semantics from their respective standard equivalents.
63 * However, there are a few differences:
64 * - On Windows, file path arguments are expected in UTF-8 format.
65 * They are converted to UTF-16 internally, thus enabling access to paths
66 * outside of the local Windows ANSI code page.
67 * - On POSIX systems, file descriptors are created with the close-on-exec
68 * flag set (atomically where possible), so that they do not leak to
69 * child process after fork-and-exec.
70 * - vlc_scandir(), inspired by GNU scandir(), passes file names rather than
71 * dirent structure pointers to its callbacks.
72 * - vlc_accept() takes an extra boolean for nonblocking mode (compare with
73 * the flags parameter in POSIX.next accept4()).
74 * - Writing functions do not emit a SIGPIPE signal in case of broken pipe.
75 *
76 * \defgroup fd File descriptors
77 * @{
78 */
79
80/**
81 * Opens a system file handle.
82 *
83 * @param filename file path to open (with UTF-8 encoding)
84 * @param flags open() flags, see the C library open() documentation
85 * @return a file handle on success, -1 on error (see errno).
86 * @note Contrary to standard open(), this function returns a file handle
87 * with the close-on-exec flag preset.
88 */
89VLC_API int vlc_open(const char *filename, int flags, ...) VLC_USED;
90
91/**
92 * Opens a system file handle relative to an existing directory handle.
93 *
94 * @param dir directory file descriptor
95 * @param filename file path to open (with UTF-8 encoding)
96 * @param flags open() flags, see the C library open() documentation
97 * @return a file handle on success, -1 on error (see errno).
98 * @note Contrary to standard open(), this function returns a file handle
99 * with the close-on-exec flag preset.
100 */
101VLC_API int vlc_openat(int dir, const char *filename, int flags, ...) VLC_USED;
102
103VLC_API int vlc_mkstemp( char * );
104
105/**
106 * Duplicates a file descriptor.
107 *
108 * @param oldfd file descriptor to duplicate
109 *
110 * @note Contrary to standard dup(), the new file descriptor has the
111 * close-on-exec descriptor flag preset.
112 * @return a new file descriptor, -1 (see @c errno)
113 */
114VLC_API int vlc_dup(int oldfd) VLC_USED;
115
116/**
117 * Replaces a file descriptor.
118 *
119 * This function duplicates a file descriptor to a specified file descriptor.
120 * This is primarily used to atomically replace a described file.
121 *
122 * @param oldfd source file descriptor to copy
123 * @param newfd destination file descriptor to replace
124 *
125 * @note Contrary to standard dup2(), the new file descriptor has the
126 * close-on-exec descriptor flag preset.
127 *
128 * @retval newfd success
129 * @retval -1 failure (see @c errno)
130 */
131VLC_API int vlc_dup2(int oldfd, int newfd);
132
133/**
134 * Creates a pipe (see "man pipe" for further reference). The new file
135 * descriptors have the close-on-exec flag preset.
136 * @return 0 on success, -1 on error (see errno)
137 */
138VLC_API int vlc_pipe(int [2]) VLC_USED;
139
140/**
141 * Creates an anonymous regular file descriptor, i.e. a descriptor for a
142 * temporary file.
143 *
144 * The file is initially empty. The storage space is automatically reclaimed
145 * when all file descriptors referencing it are closed.
146 *
147 * The new file descriptor has the close-on-exec flag preset.
148 *
149 * @return a file descriptor on success, -1 on error (see errno)
150 */
151VLC_API int vlc_memfd(void) VLC_USED;
152
153/**
154 * Writes data to a file descriptor. Unlike write(), if EPIPE error occurs,
155 * this function does not generate a SIGPIPE signal.
156 * @note If the file descriptor is known to be neither a pipe/FIFO nor a
157 * connection-oriented socket, the normal write() should be used.
158 */
159VLC_API ssize_t vlc_write(int, const void *, size_t);
160
161/**
162 * Writes data from an iovec structure to a file descriptor. Unlike writev(),
163 * if EPIPE error occurs, this function does not generate a SIGPIPE signal.
164 */
165VLC_API ssize_t vlc_writev(int, const struct iovec *, int);
166
167/**
168 * Closes a file descriptor.
169 *
170 * This closes a file descriptor. If this is a last file descriptor for the
171 * underlying open file, the file is closed too; the exact semantics depend
172 * on the type of file.
173 *
174 * @note The file descriptor is always closed when the function returns, even
175 * if the function returns an error. The sole exception is if the file
176 * descriptor was not currently valid, and thus cannot be closed (errno will
177 * then be set to EBADF).
178 *
179 * @param fd file descriptor
180 * @return Normally, zero is returned.
181 * If an I/O error is detected before or while closing, the function may return
182 * -1. Such an error is unrecoverable since the descriptor is closed.
183 *
184 * A nul return value does not necessarily imply that all pending I/O
185 * succeeded, since I/O might still occur asynchronously afterwards.
186 */
187VLC_API int vlc_close(int fd);
188
189/**
190 * @}
191 */
192
193/**
194 * Finds file/inode information - like stat().
195 * @note As far as possible, fstat() should be used instead.
196 *
197 * @param filename UTF-8 file path
198 * @param st the POSIX stat structure to fill
199 */
200VLC_API int vlc_stat(const char *filename, struct stat *st) VLC_USED;
201
202/**
203 * Finds file/inode information, as lstat().
204 * Consider using fstat() instead, if possible.
205 *
206 * @param filename UTF-8 file path
207 * @param st the POSIX stat structure to fill
208 */
209VLC_API int vlc_lstat(const char *filename, struct stat *st) VLC_USED;
210
211/**
212 * Removes a file.
213 *
214 * @param filename a UTF-8 string with the name of the file you want to delete.
215 * @return A 0 return value indicates success. A -1 return value indicates an
216 * error, and an error code is stored in errno
217 */
218VLC_API int vlc_unlink(const char *filename);
219
220/**
221 * Moves a file atomically. This only works within a single file system.
222 *
223 * @param oldpath path to the file before the move
224 * @param newpath intended path to the file after the move
225 * @return A 0 return value indicates success. A -1 return value indicates an
226 * error, and an error code is stored in errno
227 */
228VLC_API int vlc_rename(const char *oldpath, const char *newpath);
229
230VLC_API FILE * vlc_fopen( const char *filename, const char *mode ) VLC_USED;
231
232/**
233 * \defgroup dir Directories
234 * @{
235 */
236
237#if defined( _WIN32 )
238typedef struct vlc_DIR vlc_DIR;
239#else // !_WIN32
240typedef DIR vlc_DIR;
241#endif
242
243/**
244 * Opens a DIR pointer.
245 *
246 * @param dirname UTF-8 representation of the directory name
247 * @return a pointer to the DIR struct, or NULL in case of error.
248 * Release with vlc_closedir().
249 */
250VLC_API vlc_DIR *vlc_opendir(const char *dirname) VLC_USED;
251
252/**
253 * Reads the next file name from an open directory.
254 *
255 * @param dir directory handle as returned by vlc_opendir()
256 * (must not be used by another thread concurrently)
257 *
258 * @return a UTF-8 string of the directory entry. The string is valid until
259 * the next call to vlc_readdir() or vlc_closedir() on the handle.
260 * If there are no more entries in the directory, NULL is returned.
261 * If an error occurs, errno is set and NULL is returned.
262 */
263VLC_API const char *vlc_readdir(vlc_DIR *dir) VLC_USED;
264
265VLC_API int vlc_loaddir( vlc_DIR *dir, char ***namelist, int (*select)( const char * ), int (*compar)( const char **, const char ** ) );
266VLC_API int vlc_scandir( const char *dirname, char ***namelist, int (*select)( const char * ), int (*compar)( const char **, const char ** ) );
267
268VLC_API void vlc_closedir( vlc_DIR *dir );
269VLC_API void vlc_rewinddir( vlc_DIR *dir );
270
271/**
272 * Creates a directory.
273 *
274 * @param dirname a UTF-8 string with the name of the directory that you
275 * want to create.
276 * @param mode directory permissions
277 * @return 0 on success, -1 on error (see errno).
278 */
279VLC_API int vlc_mkdir(const char *dirname, mode_t mode);
280
281/**
282 * Creates a directory and parent directories as needed.
283 *
284 * @param dirname a UTF-8 string containing the name of the directory to
285 * be created.
286 * @param mode directory permissions
287 * @return 0 on success, -1 on error (see errno).
288 */
289VLC_API int vlc_mkdir_parent(const char *dirname, mode_t mode);
290
291/**
292 * Determines the current working directory.
293 *
294 * @return the current working directory (must be free()'d)
295 * or NULL on error
296 */
297VLC_API char *vlc_getcwd(void) VLC_USED;
298
299/** @} */
300
301#ifdef __ANDROID__
302# define lseek lseek64
303#endif
304
305/** @} */
306#endif
#define VLC_USED
Definition fourcc_gen.c:32
#define VLC_API
Definition fourcc_gen.c:31
int vlc_mkdir_parent(const char *dirname, mode_t mode)
Creates a directory and parent directories as needed.
Definition filesystem.c:20
vlc_DIR * vlc_opendir(const char *dirname)
Opens a DIR pointer.
Definition filesystem.c:110
char * vlc_getcwd(void)
Determines the current working directory.
Definition filesystem.c:228
void vlc_rewinddir(vlc_DIR *dir)
Definition filesystem.c:367
DIR vlc_DIR
Definition vlc_fs.h:241
int vlc_mkdir(const char *dirname, mode_t mode)
Creates a directory.
Definition filesystem.c:96
const char * vlc_readdir(vlc_DIR *dir)
Reads the next file name from an open directory.
Definition filesystem.c:327
int vlc_loaddir(vlc_DIR *dir, char ***namelist, int(*select)(const char *), int(*compar)(const char **, const char **))
Does the same as vlc_scandir(), but takes an open directory pointer instead of a directory path.
Definition filesystem.c:121
void vlc_closedir(vlc_DIR *dir)
Definition filesystem.c:317
int vlc_scandir(const char *dirname, char ***namelist, int(*select)(const char *), int(*compar)(const char **, const char **))
Selects file entries from a directory, as GNU C scandir().
Definition filesystem.c:189
int vlc_mkstemp(char *)
Definition filesystem.c:206
int vlc_open(const char *filename, int flags,...)
Opens a system file handle.
Definition filesystem.c:52
int vlc_pipe(int[2])
Creates a pipe (see "man pipe" for further reference).
Definition filesystem.c:285
int vlc_openat(int dir, const char *filename, int flags,...)
Opens a system file handle relative to an existing directory handle.
Definition filesystem.c:78
int vlc_memfd(void)
Creates an anonymous regular file descriptor, i.e.
Definition filesystem.c:38
int vlc_dup2(int oldfd, int newfd)
Replaces a file descriptor.
Definition filesystem.c:276
ssize_t vlc_writev(int, const struct iovec *, int)
Writes data from an iovec structure to a file descriptor.
Definition filesystem.c:306
int vlc_dup(int oldfd)
Duplicates a file descriptor.
Definition filesystem.c:265
int vlc_close(int fd)
Closes a file descriptor.
Definition filesystem.c:91
ssize_t vlc_write(int, const void *, size_t)
Writes data to a file descriptor.
Definition filesystem.c:299
int vlc_stat(const char *filename, struct stat *st)
Finds file/inode information - like stat().
Definition filesystem.c:183
int vlc_lstat(const char *filename, struct stat *st)
Finds file/inode information, as lstat().
Definition filesystem.c:188
int vlc_rename(const char *oldpath, const char *newpath)
Moves a file atomically.
Definition filesystem.c:207
int vlc_unlink(const char *filename)
Removes a file.
Definition filesystem.c:193
FILE * vlc_fopen(const char *filename, const char *mode)
Opens a FILE pointer.
Definition filesystem.c:49
int(* compar)(const void *, const void *, void *)
Definition sort.c:31
Definition filesystem.c:201
This file is a collection of common definitions and types.