2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file util.c Helper functions needed by both libosl and oslfsck. */
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 : -E_OSL_WRITE;
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);
65 ERROR_LOG("write error: %s\n", strerror(errno));
74 * Wrapper for the open(2) system call.
76 * \param path The filename.
77 * \param flags The usual open(2) flags.
78 * \param mode Specifies the permissions to use.
80 * The mode parameter must be specified when O_CREAT is in the flags, and is
83 * \return The file descriptor on success, negative on errors.
87 int osl_open(const char *path, int flags, mode_t mode)
89 int ret = open(path, flags, mode);
93 return errno == ENOENT? -E_OSL_NOENT : -E_OSL_OPEN;
97 * Open a file, write the given buffer and close the file.
99 * \param filename Full path to the file to open.
100 * \param buf The buffer to write to the file.
101 * \param size The size of \a buf.
105 int write_file(const char *filename, const void *buf, size_t size)
109 ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
113 ret = write_all(fd, buf, &size);
123 * Open a file and map it into memory.
125 * \param path Name of the regular file to map.
126 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
127 * \param map On success, the mapping is returned here.
128 * \param size size of the mapping.
129 * \param fd_ptr The file descriptor of the mapping.
131 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
132 * open call is closed after mmap(). Otherwise the file is kept open and the
133 * file descriptor is returned in \a fd_ptr.
137 * \sa osl_open(), mmap(2).
139 int mmap_full_file(const char *path, int open_mode, void **map,
140 size_t *size, int *fd_ptr)
142 int fd, ret, mmap_prot, mmap_flags;
143 struct stat file_status;
145 if (open_mode == O_RDONLY) {
146 mmap_prot = PROT_READ;
147 mmap_flags = MAP_PRIVATE;
149 mmap_prot = PROT_READ | PROT_WRITE;
150 mmap_flags = MAP_SHARED;
152 ret = osl_open(path, open_mode, 0);
156 if (fstat(fd, &file_status) < 0) {
157 ret = errno == ENOENT? -E_OSL_NOENT : -E_OSL_STAT;
160 *size = file_status.st_size;
162 DEBUG_LOG("%s: size %zu\n", path, *size);
165 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
166 if (*map == MAP_FAILED) {
173 if (ret < 0 || !fd_ptr)
181 * A wrapper for munmap(2).
183 * \param start The start address of the memory mapping.
184 * \param length The size of the mapping.
186 * \return Positive on success, \p -E_MUNMAP on errors.
188 * \sa munmap(2), mmap_full_file().
190 int osl_munmap(void *start, size_t length)
193 if (munmap(start, length) >= 0)
196 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
198 return -E_OSL_MUNMAP;
202 * Allocate a sufficiently large string and print into it.
204 * \param fmt A usual format string.
206 * Produce output according to \p fmt. No artificial bound on the length of the
207 * resulting string is imposed.
209 * \return This function either returns a pointer to a string that must be
210 * freed by the caller or \p NULL if memory allocation failed.
214 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
218 char *p = malloc(size);
225 /* Try to print in the allocated space. */
227 n = vsnprintf(p, size, fmt, ap);
229 /* If that worked, return the string. */
230 if (n > -1 && n < size)
232 /* Else try again with more space. */
233 if (n > -1) /* glibc 2.1 */
234 size = n + 1; /* precisely what is needed */
236 size *= 2; /* twice the old size */
237 q = realloc(p, size);
248 * A wrapper for truncate(2)
250 * \param path Name of the regular file to truncate
251 * \param size Number of bytes to \b shave \b off
253 * Truncate the regular file named by \a path by \a size bytes.
259 int truncate_file(const char *path, off_t size)
264 ret = osl_stat(path, &statbuf);
267 ret = -E_OSL_BAD_SIZE;
268 if (statbuf.st_size < size)
270 if (truncate(path, statbuf.st_size - size) < 0)
271 return -E_OSL_TRUNCATE;