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