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