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 * A wrapper for mkdir(2).
150 * \param path Name of the directory to create.
151 * \param mode The permissions to use.
155 int para_mkdir(const char *path, mode_t mode)
157 if (!mkdir(path, mode))
159 return -ERRNO_TO_ERROR(errno);
163 * Open a file and map it into memory.
165 * \param path Name of the regular file to map.
166 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
167 * \param map On success, the mapping is returned here.
168 * \param size size of the mapping.
169 * \param fd_ptr The file descriptor of the mapping.
171 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
172 * open call is closed after mmap(). Otherwise the file is kept open and the
173 * file descriptor is returned in \a fd_ptr.
177 * \sa osl_open(), mmap(2).
179 int mmap_full_file(const char *path, int open_mode, void **map,
180 size_t *size, int *fd_ptr)
182 int fd, ret, mmap_prot, mmap_flags;
183 struct stat file_status;
185 if (open_mode == O_RDONLY) {
186 mmap_prot = PROT_READ;
187 mmap_flags = MAP_PRIVATE;
189 mmap_prot = PROT_READ | PROT_WRITE;
190 mmap_flags = MAP_SHARED;
192 ret = osl_open(path, open_mode, 0);
196 if (fstat(fd, &file_status) < 0) {
197 ret = -ERRNO_TO_ERROR(errno);
200 *size = file_status.st_size;
202 DEBUG_LOG("%s: size %zu\n", path, *size);
205 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
206 if (*map == MAP_FAILED) {
213 if (ret < 0 || !fd_ptr)
221 * A wrapper for munmap(2).
223 * \param start The start address of the memory mapping.
224 * \param length The size of the mapping.
226 * \return Positive on success, \p -E_MUNMAP on errors.
228 * \sa munmap(2), mmap_full_file().
230 int para_munmap(void *start, size_t length)
233 if (munmap(start, length) >= 0)
236 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
238 return -ERRNO_TO_ERROR(err);