2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fd.c Helper functions for file descriptor handling. */
13 #include <sys/select.h>
20 * Wrapper for the write system call.
22 * \param fd The file descriptor to write to.
23 * \param buf The buffer to write.
24 * \param size The length of \a buf in bytes.
26 * This function writes out the given buffer and retries if an interrupt
27 * occurred during the write.
29 * \return On success, the number of bytes written is returned, otherwise, a
30 * negative error code is returned.
34 static ssize_t __write(int fd, const void *buf, size_t size)
39 ret = write(fd, buf, size);
40 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
42 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
47 * Write a buffer to a file descriptor, re-write on short writes.
49 * \param fd The file descriptor.
50 * \param buf The buffer to be sent.
51 * \param len The length of \a buf.
53 * \return Standard. In any case, the number of bytes that have been written is
56 int write_all(int fd, const char *buf, size_t *len)
62 while (*len < total) {
63 int ret = __write(fd, buf + *len, total - *len);
72 * Wrapper for the open(2) system call.
74 * \param path The filename.
75 * \param flags The usual open(2) flags.
76 * \param mode Specifies the permissions to use.
78 * The mode parameter must be specified when O_CREAT is in the flags, and is
81 * \return The file descriptor on success, negative on errors.
85 int osl_open(const char *path, int flags, mode_t mode)
87 int ret = open(path, flags, mode);
91 return -ERRNO_TO_ERROR(errno);
95 * Open a file, write the given buffer and close the file.
97 * \param filename Full path to the file to open.
98 * \param buf The buffer to write to the file.
99 * \param size The size of \a buf.
103 int write_file(const char *filename, const void *buf, size_t size)
107 ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
111 ret = write_all(fd, buf, &size);
121 * Wrapper for chdir(2).
123 * \param path The specified directory.
127 static int para_chdir(const char *path)
129 int ret = chdir(path);
133 return -ERRNO_TO_ERROR(errno);
137 * Save the cwd and open a given directory.
139 * \param dirname Path to the directory to open.
140 * \param dir Result pointer.
141 * \param cwd File descriptor of the current working directory.
145 * Opening the current directory (".") and calling fchdir() to return is
146 * usually faster and more reliable than saving cwd in some buffer and calling
147 * chdir() afterwards.
149 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
150 * stored in \a cwd. If the function returns success, and \a cwd is not \p
151 * NULL, the caller must close this file descriptor (probably after calling
154 * On errors, the function undos everything, so the caller needs neither close
155 * any files, nor change back to the original working directory.
160 int para_opendir(const char *dirname, DIR **dir, int *cwd)
165 ret = osl_open(".", O_RDONLY, 0);
170 ret = para_chdir(dirname);
176 ret = -ERRNO_TO_ERROR(errno);
177 /* Ignore return value of fchdir() and close(). We're busted anyway. */
187 * A wrapper for fchdir().
189 * \param fd An open file descriptor.
193 int para_fchdir(int fd)
196 return -ERRNO_TO_ERROR(errno);
201 * Open a file and map it into memory.
203 * \param path Name of the regular file to map.
204 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
205 * \param map On success, the mapping is returned here.
206 * \param size size of the mapping.
207 * \param fd_ptr The file descriptor of the mapping.
209 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
210 * open call is closed after mmap(). Otherwise the file is kept open and the
211 * file descriptor is returned in \a fd_ptr.
215 * \sa osl_open(), mmap(2).
217 int mmap_full_file(const char *path, int open_mode, void **map,
218 size_t *size, int *fd_ptr)
220 int fd, ret, mmap_prot, mmap_flags;
221 struct stat file_status;
223 if (open_mode == O_RDONLY) {
224 mmap_prot = PROT_READ;
225 mmap_flags = MAP_PRIVATE;
227 mmap_prot = PROT_READ | PROT_WRITE;
228 mmap_flags = MAP_SHARED;
230 ret = osl_open(path, open_mode, 0);
234 if (fstat(fd, &file_status) < 0) {
235 ret = -ERRNO_TO_ERROR(errno);
238 *size = file_status.st_size;
240 DEBUG_LOG("%s: size %zu\n", path, *size);
243 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
244 if (*map == MAP_FAILED) {
251 if (ret < 0 || !fd_ptr)
259 * A wrapper for munmap(2).
261 * \param start The start address of the memory mapping.
262 * \param length The size of the mapping.
264 * \return Positive on success, \p -E_MUNMAP on errors.
266 * \sa munmap(2), mmap_full_file().
268 int osl_munmap(void *start, size_t length)
271 if (munmap(start, length) >= 0)
274 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
276 return -ERRNO_TO_ERROR(err);
280 * Allocate a sufficiently large string and print into it.
282 * \param fmt A usual format string.
284 * Produce output according to \p fmt. No artificial bound on the length of the
285 * resulting string is imposed.
287 * \return This function either returns a pointer to a string that must be
288 * freed by the caller or \p NULL if memory allocation failed.
292 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
296 char *p = malloc(size);
303 /* Try to print in the allocated space. */
305 n = vsnprintf(p, size, fmt, ap);
307 /* If that worked, return the string. */
308 if (n > -1 && n < size)
310 /* Else try again with more space. */
311 if (n > -1) /* glibc 2.1 */
312 size = n + 1; /* precisely what is needed */
314 size *= 2; /* twice the old size */
315 q = realloc(p, size);
326 * A wrapper for truncate(2)
328 * \param path Name of the regular file to truncate
329 * \param size Number of bytes to \b shave \b off
331 * Truncate the regular file named by \a path by \a size bytes.
337 int para_truncate(const char *path, off_t size)
343 if (stat(path, &statbuf) < 0)
345 ret = -E_OSL_BAD_SIZE;
346 if (statbuf.st_size < size)
349 if (truncate(path, statbuf.st_size - size) < 0)