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>
19 * Write a buffer to a file descriptor, re-write on short writes.
21 * \param fd The file descriptor.
22 * \param buf The buffer to be sent.
23 * \param len The length of \a buf.
25 * \return Standard. In any case, the number of bytes that have been written is
28 int write_all(int fd, const char *buf, size_t *len)
34 while (*len < total) {
35 int ret = write(fd, buf + *len, total - *len);
37 return -ERRNO_TO_ERROR(errno);
44 * Wrapper for the open(2) system call.
46 * \param path The filename.
47 * \param flags The usual open(2) flags.
48 * \param mode Specifies the permissions to use.
50 * The mode parameter must be specified when O_CREAT is in the flags, and is
53 * \return The file descriptor on success, negative on errors.
57 int para_open(const char *path, int flags, mode_t mode)
59 int ret = open(path, flags, mode);
63 return -ERRNO_TO_ERROR(errno);
67 * Wrapper for chdir(2).
69 * \param path The specified directory.
73 static int para_chdir(const char *path)
75 int ret = chdir(path);
79 return -ERRNO_TO_ERROR(errno);
83 * Save the cwd and open a given directory.
85 * \param dirname Path to the directory to open.
86 * \param dir Result pointer.
87 * \param cwd File descriptor of the current working directory.
91 * Opening the current directory (".") and calling fchdir() to return is
92 * usually faster and more reliable than saving cwd in some buffer and calling
95 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
96 * stored in \a cwd. If the function returns success, and \a cwd is not \p
97 * NULL, the caller must close this file descriptor (probably after calling
100 * On errors, the function undos everything, so the caller needs neither close
101 * any files, nor change back to the original working directory.
106 int para_opendir(const char *dirname, DIR **dir, int *cwd)
111 ret = para_open(".", O_RDONLY, 0);
116 ret = para_chdir(dirname);
122 ret = -ERRNO_TO_ERROR(errno);
123 /* Ignore return value of fchdir() and close(). We're busted anyway. */
133 * A wrapper for fchdir().
135 * \param fd An open file descriptor.
139 int para_fchdir(int fd)
142 return -ERRNO_TO_ERROR(errno);
147 * A wrapper for mkdir(2).
149 * \param path Name of the directory to create.
150 * \param mode The permissions to use.
154 int para_mkdir(const char *path, mode_t mode)
156 if (!mkdir(path, mode))
158 return -ERRNO_TO_ERROR(errno);
162 * Open a file and map it into memory.
164 * \param path Name of the regular file to map.
165 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
166 * \param map On success, the mapping is returned here.
167 * \param size size of the mapping.
168 * \param fd_ptr The file descriptor of the mapping.
170 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
171 * open call is closed after mmap(). Otherwise the file is kept open and the
172 * file descriptor is returned in \a fd_ptr.
176 * \sa para_open(), mmap(2).
178 int mmap_full_file(const char *path, int open_mode, void **map,
179 size_t *size, int *fd_ptr)
181 int fd, ret, mmap_prot, mmap_flags;
182 struct stat file_status;
184 if (open_mode == O_RDONLY) {
185 mmap_prot = PROT_READ;
186 mmap_flags = MAP_PRIVATE;
188 mmap_prot = PROT_READ | PROT_WRITE;
189 mmap_flags = MAP_SHARED;
191 ret = para_open(path, open_mode, 0);
195 if (fstat(fd, &file_status) < 0) {
196 ret = -ERRNO_TO_ERROR(errno);
199 *size = file_status.st_size;
201 DEBUG_LOG("%s: size %zu\n", path, *size);
204 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
205 if (*map == MAP_FAILED) {
212 if (ret < 0 || !fd_ptr)
220 * A wrapper for munmap(2).
222 * \param start The start address of the memory mapping.
223 * \param length The size of the mapping.
225 * \return Positive on success, \p -E_MUNMAP on errors.
227 * \sa munmap(2), mmap_full_file().
229 int para_munmap(void *start, size_t length)
232 if (munmap(start, length) >= 0)
235 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
237 return -ERRNO_TO_ERROR(err);