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 * Write a buffer to a file descriptor, re-write on short writes.
22 * \param fd The file descriptor.
23 * \param buf The buffer to be sent.
24 * \param len The length of \a buf.
26 * \return Standard. In any case, the number of bytes that have been written is
29 int write_all(int fd, const char *buf, size_t *len)
35 while (*len < total) {
36 int ret = write(fd, buf + *len, total - *len);
38 return -ERRNO_TO_ERROR(errno);
45 * Wrapper for the open(2) system call.
47 * \param path The filename.
48 * \param flags The usual open(2) flags.
49 * \param mode Specifies the permissions to use.
51 * The mode parameter must be specified when O_CREAT is in the flags, and is
54 * \return The file descriptor on success, negative on errors.
58 int osl_open(const char *path, int flags, mode_t mode)
60 int ret = open(path, flags, mode);
64 return -ERRNO_TO_ERROR(errno);
68 * Wrapper for chdir(2).
70 * \param path The specified directory.
74 static int para_chdir(const char *path)
76 int ret = chdir(path);
80 return -ERRNO_TO_ERROR(errno);
84 * Save the cwd and open a given directory.
86 * \param dirname Path to the directory to open.
87 * \param dir Result pointer.
88 * \param cwd File descriptor of the current working directory.
92 * Opening the current directory (".") and calling fchdir() to return is
93 * usually faster and more reliable than saving cwd in some buffer and calling
96 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
97 * stored in \a cwd. If the function returns success, and \a cwd is not \p
98 * NULL, the caller must close this file descriptor (probably after calling
101 * On errors, the function undos everything, so the caller needs neither close
102 * any files, nor change back to the original working directory.
107 int para_opendir(const char *dirname, DIR **dir, int *cwd)
112 ret = osl_open(".", O_RDONLY, 0);
117 ret = para_chdir(dirname);
123 ret = -ERRNO_TO_ERROR(errno);
124 /* Ignore return value of fchdir() and close(). We're busted anyway. */
134 * A wrapper for fchdir().
136 * \param fd An open file descriptor.
140 int para_fchdir(int fd)
143 return -ERRNO_TO_ERROR(errno);
148 * Open a file and map it into memory.
150 * \param path Name of the regular file to map.
151 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
152 * \param map On success, the mapping is returned here.
153 * \param size size of the mapping.
154 * \param fd_ptr The file descriptor of the mapping.
156 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
157 * open call is closed after mmap(). Otherwise the file is kept open and the
158 * file descriptor is returned in \a fd_ptr.
162 * \sa osl_open(), mmap(2).
164 int mmap_full_file(const char *path, int open_mode, void **map,
165 size_t *size, int *fd_ptr)
167 int fd, ret, mmap_prot, mmap_flags;
168 struct stat file_status;
170 if (open_mode == O_RDONLY) {
171 mmap_prot = PROT_READ;
172 mmap_flags = MAP_PRIVATE;
174 mmap_prot = PROT_READ | PROT_WRITE;
175 mmap_flags = MAP_SHARED;
177 ret = osl_open(path, open_mode, 0);
181 if (fstat(fd, &file_status) < 0) {
182 ret = -ERRNO_TO_ERROR(errno);
185 *size = file_status.st_size;
187 DEBUG_LOG("%s: size %zu\n", path, *size);
190 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
191 if (*map == MAP_FAILED) {
198 if (ret < 0 || !fd_ptr)
206 * A wrapper for munmap(2).
208 * \param start The start address of the memory mapping.
209 * \param length The size of the mapping.
211 * \return Positive on success, \p -E_MUNMAP on errors.
213 * \sa munmap(2), mmap_full_file().
215 int osl_munmap(void *start, size_t length)
218 if (munmap(start, length) >= 0)
221 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
223 return -ERRNO_TO_ERROR(err);