Merge commit 'stark/master'
[osl.git] / util.c
1 /*
2  * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file util.c Helper functions needed by both libosl and oslfsck. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/mman.h>
12 #include <fcntl.h>
13 #include <sys/select.h>
14
15 #include "log.h"
16 #include "osl.h"
17 #include "error.h"
18 #include "util.h"
19
20 /**
21  * Wrapper for the write system call.
22  *
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.
26  *
27  * This function writes out the given buffer and retries if an interrupt
28  * occurred during the write.
29  *
30  * \return On success, the number of bytes written is returned, otherwise, a
31  * negative error code is returned.
32  *
33  * \sa write(2).
34  */
35 static ssize_t __write(int fd, const void *buf, size_t size)
36 {
37         ssize_t ret;
38
39         for (;;) {
40                 ret = write(fd, buf, size);
41                 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
42                         continue;
43                 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
44         }
45 }
46
47 /**
48  * Write a buffer to a file descriptor, re-write on short writes.
49  *
50  * \param fd The file descriptor.
51  * \param buf The buffer to be sent.
52  * \param len The length of \a buf.
53  *
54  * \return Standard. In any case, the number of bytes that have been written is
55  * stored in \a len.
56  */
57 int write_all(int fd, const char *buf, size_t *len)
58 {
59         size_t total = *len;
60
61         assert(total);
62         *len = 0;
63         while (*len < total) {
64                 int ret = __write(fd, buf + *len, total - *len);
65                 if (ret < 0)
66                         return ret;
67                 *len += ret;
68         }
69         return 1;
70 }
71
72 /**
73  * Wrapper for the open(2) system call.
74  *
75  * \param path The filename.
76  * \param flags The usual open(2) flags.
77  * \param mode Specifies the permissions to use.
78  *
79  * The mode parameter must be specified when O_CREAT is in the flags, and is
80  * ignored otherwise.
81  *
82  * \return The file descriptor on success, negative on errors.
83  *
84  * \sa open(2).
85  */
86 int osl_open(const char *path, int flags, mode_t mode)
87 {
88         int ret = open(path, flags, mode);
89
90         if (ret >= 0)
91                 return ret;
92         return -ERRNO_TO_ERROR(errno);
93 }
94
95 /**
96  * Open a file, write the given buffer and close the file.
97  *
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.
101  *
102  * \return Standard.
103  */
104 int write_file(const char *filename, const void *buf, size_t size)
105 {
106         int ret, fd;
107
108         ret = osl_open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
109         if (ret < 0)
110                 return ret;
111         fd = ret;
112         ret = write_all(fd, buf, &size);
113         if (ret < 0)
114                 goto out;
115         ret = 1;
116 out:
117         close(fd);
118         return ret;
119 }
120
121 /**
122  * Open a file and map it into memory.
123  *
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.
129  *
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.
133  *
134  * \return Standard.
135  *
136  * \sa osl_open(), mmap(2).
137  */
138 int mmap_full_file(const char *path, int open_mode, void **map,
139                 size_t *size, int *fd_ptr)
140 {
141         int fd, ret, mmap_prot, mmap_flags;
142         struct stat file_status;
143
144         if (open_mode == O_RDONLY) {
145                 mmap_prot = PROT_READ;
146                 mmap_flags = MAP_PRIVATE;
147         } else {
148                 mmap_prot = PROT_READ | PROT_WRITE;
149                 mmap_flags = MAP_SHARED;
150         }
151         ret = osl_open(path, open_mode, 0);
152         if (ret < 0)
153                 return ret;
154         fd = ret;
155         if (fstat(fd, &file_status) < 0) {
156                 ret = -ERRNO_TO_ERROR(errno);
157                 goto out;
158         }
159         *size = file_status.st_size;
160         ret = -E_OSL_EMPTY;
161         DEBUG_LOG("%s: size %zu\n", path, *size);
162         if (!*size)
163                 goto out;
164         *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
165         if (*map == MAP_FAILED) {
166                 *map = NULL;
167                 ret = -E_OSL_MMAP;
168                 goto out;
169         }
170         ret = 1;
171 out:
172         if (ret < 0 || !fd_ptr)
173                 close(fd);
174         else
175                 *fd_ptr = fd;
176         return ret;
177 }
178
179 /**
180  * A wrapper for munmap(2).
181  *
182  * \param start The start address of the memory mapping.
183  * \param length The size of the mapping.
184  *
185  * \return Positive on success, \p -E_MUNMAP on errors.
186  *
187  * \sa munmap(2), mmap_full_file().
188  */
189 int osl_munmap(void *start, size_t length)
190 {
191         int err;
192         if (munmap(start, length) >= 0)
193                 return 1;
194         err = errno;
195         ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
196                 strerror(err));
197         return -ERRNO_TO_ERROR(err);
198 }
199
200 /**
201  * Allocate a sufficiently large string and print into it.
202  *
203  * \param fmt A usual format string.
204  *
205  * Produce output according to \p fmt. No artificial bound on the length of the
206  * resulting string is imposed.
207  *
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.
210  *
211  * \sa printf(3).
212  */
213 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
214 {
215         int n;
216         size_t size = 100;
217         char *p = malloc(size);
218
219         if (!p)
220                 return NULL;
221         while (1) {
222                 char *q;
223                 va_list ap;
224                 /* Try to print in the allocated space. */
225                 va_start(ap, fmt);
226                 n = vsnprintf(p, size, fmt, ap);
227                 va_end(ap);
228                 /* If that worked, return the string. */
229                 if (n > -1 && n < size)
230                         break;
231                 /* Else try again with more space. */
232                 if (n > -1) /* glibc 2.1 */
233                         size = n + 1; /* precisely what is needed */
234                 else /* glibc 2.0 */
235                         size *= 2; /* twice the old size */
236                 q = realloc(p, size);
237                 if (!q) {
238                         free(p);
239                         return NULL;
240                 }
241                 p = q;
242         }
243         return p;
244 }
245
246 /**
247  * A wrapper for truncate(2)
248  *
249  * \param path Name of the regular file to truncate
250  * \param size Number of bytes to \b shave \b off
251  *
252  * Truncate the regular file named by \a path by \a size bytes.
253  *
254  * \return Standard.
255  *
256  * \sa truncate(2)
257  */
258 int truncate_file(const char *path, off_t size)
259 {
260         int ret;
261         struct stat statbuf;
262
263         ret = osl_stat(path, &statbuf);
264         if (ret < 0)
265                 return ret;
266         ret = -E_OSL_BAD_SIZE;
267         if (statbuf.st_size < size)
268                 return ret;
269         if (truncate(path, statbuf.st_size - size) < 0)
270                 return -ERRNO_TO_ERROR(errno);
271         return 1;
272 }