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>
21 * Wrapper for the write system call.
23 * \param fd The file descriptor to write to.
24 * \param buf The buffer to write.
25 * \param size The length of \a buf in bytes.
27 * This function writes out the given buffer and retries if an interrupt
28 * occurred during the write.
30 * \return On success, the number of bytes written is returned, otherwise, a
31 * negative error code is returned.
35 static ssize_t __write(int fd, const void *buf, size_t size)
40 ret = write(fd, buf, size);
41 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
43 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
48 * Write a buffer to a file descriptor, re-write on short writes.
50 * \param fd The file descriptor.
51 * \param buf The buffer to be sent.
52 * \param len The length of \a buf.
54 * \return Standard. In any case, the number of bytes that have been written is
57 int write_all(int fd, const char *buf, size_t *len)
63 while (*len < total) {
64 int ret = __write(fd, buf + *len, total - *len);
73 * Wrapper for the open(2) system call.
75 * \param path The filename.
76 * \param flags The usual open(2) flags.
77 * \param mode Specifies the permissions to use.
79 * The mode parameter must be specified when O_CREAT is in the flags, and is
82 * \return The file descriptor on success, negative on errors.
86 int osl_open(const char *path, int flags, mode_t mode)
88 int ret = open(path, flags, mode);
92 return -ERRNO_TO_ERROR(errno);
96 * Open a file, write the given buffer and close the file.
98 * \param filename Full path to the file to open.
99 * \param buf The buffer to write to the file.
100 * \param size The size of \a buf.
104 int write_file(const char *filename, const void *buf, size_t size)
108 ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
112 ret = write_all(fd, buf, &size);
122 * Open a file and map it into memory.
124 * \param path Name of the regular file to map.
125 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
126 * \param map On success, the mapping is returned here.
127 * \param size size of the mapping.
128 * \param fd_ptr The file descriptor of the mapping.
130 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
131 * open call is closed after mmap(). Otherwise the file is kept open and the
132 * file descriptor is returned in \a fd_ptr.
136 * \sa osl_open(), mmap(2).
138 int mmap_full_file(const char *path, int open_mode, void **map,
139 size_t *size, int *fd_ptr)
141 int fd, ret, mmap_prot, mmap_flags;
142 struct stat file_status;
144 if (open_mode == O_RDONLY) {
145 mmap_prot = PROT_READ;
146 mmap_flags = MAP_PRIVATE;
148 mmap_prot = PROT_READ | PROT_WRITE;
149 mmap_flags = MAP_SHARED;
151 ret = osl_open(path, open_mode, 0);
155 if (fstat(fd, &file_status) < 0) {
156 ret = -ERRNO_TO_ERROR(errno);
159 *size = file_status.st_size;
161 DEBUG_LOG("%s: size %zu\n", path, *size);
164 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
165 if (*map == MAP_FAILED) {
172 if (ret < 0 || !fd_ptr)
180 * A wrapper for munmap(2).
182 * \param start The start address of the memory mapping.
183 * \param length The size of the mapping.
185 * \return Positive on success, \p -E_MUNMAP on errors.
187 * \sa munmap(2), mmap_full_file().
189 int osl_munmap(void *start, size_t length)
192 if (munmap(start, length) >= 0)
195 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
197 return -ERRNO_TO_ERROR(err);
201 * Allocate a sufficiently large string and print into it.
203 * \param fmt A usual format string.
205 * Produce output according to \p fmt. No artificial bound on the length of the
206 * resulting string is imposed.
208 * \return This function either returns a pointer to a string that must be
209 * freed by the caller or \p NULL if memory allocation failed.
213 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
217 char *p = malloc(size);
224 /* Try to print in the allocated space. */
226 n = vsnprintf(p, size, fmt, ap);
228 /* If that worked, return the string. */
229 if (n > -1 && n < size)
231 /* Else try again with more space. */
232 if (n > -1) /* glibc 2.1 */
233 size = n + 1; /* precisely what is needed */
235 size *= 2; /* twice the old size */
236 q = realloc(p, size);
247 * A wrapper for truncate(2)
249 * \param path Name of the regular file to truncate
250 * \param size Number of bytes to \b shave \b off
252 * Truncate the regular file named by \a path by \a size bytes.
258 int truncate_file(const char *path, off_t size)
263 ret = osl_stat(path, &statbuf);
266 ret = -E_OSL_BAD_SIZE;
267 if (statbuf.st_size < size)
269 if (truncate(path, statbuf.st_size - size) < 0)
270 return -ERRNO_TO_ERROR(errno);