Replace underscores with dashes in fsck command line options.
[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 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 ret = write_all(fd, buf, &size);
114 if (ret < 0)
115 goto out;
116 ret = 1;
117 out:
118 close(fd);
119 return ret;
120 }
121
122 /**
123 * Open a file and map it into memory.
124 *
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.
130 *
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.
134 *
135 * \return Standard.
136 *
137 * \sa osl_open(), mmap(2).
138 */
139 int mmap_full_file(const char *path, int open_mode, void **map,
140 size_t *size, int *fd_ptr)
141 {
142 int fd, ret, mmap_prot, mmap_flags;
143 struct stat file_status;
144
145 if (open_mode == O_RDONLY) {
146 mmap_prot = PROT_READ;
147 mmap_flags = MAP_PRIVATE;
148 } else {
149 mmap_prot = PROT_READ | PROT_WRITE;
150 mmap_flags = MAP_SHARED;
151 }
152 ret = osl_open(path, open_mode, 0);
153 if (ret < 0)
154 return ret;
155 fd = ret;
156 if (fstat(fd, &file_status) < 0) {
157 ret = errno == ENOENT? -E_OSL_NOENT : -E_OSL_STAT;
158 goto out;
159 }
160 *size = file_status.st_size;
161 ret = -E_OSL_EMPTY;
162 DEBUG_LOG("%s: size %zu\n", path, *size);
163 if (!*size)
164 goto out;
165 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
166 if (*map == MAP_FAILED) {
167 *map = NULL;
168 ret = -E_OSL_MMAP;
169 goto out;
170 }
171 ret = 1;
172 out:
173 if (ret < 0 || !fd_ptr)
174 close(fd);
175 else
176 *fd_ptr = fd;
177 return ret;
178 }
179
180 /**
181 * A wrapper for munmap(2).
182 *
183 * \param start The start address of the memory mapping.
184 * \param length The size of the mapping.
185 *
186 * \return Positive on success, \p -E_MUNMAP on errors.
187 *
188 * \sa munmap(2), mmap_full_file().
189 */
190 int osl_munmap(void *start, size_t length)
191 {
192 int err;
193 if (munmap(start, length) >= 0)
194 return 1;
195 err = errno;
196 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
197 strerror(err));
198 return -E_OSL_MUNMAP;
199 }
200
201 /**
202 * Allocate a sufficiently large string and print into it.
203 *
204 * \param fmt A usual format string.
205 *
206 * Produce output according to \p fmt. No artificial bound on the length of the
207 * resulting string is imposed.
208 *
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.
211 *
212 * \sa printf(3).
213 */
214 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
215 {
216 int n;
217 size_t size = 100;
218 char *p = malloc(size);
219
220 if (!p)
221 return NULL;
222 while (1) {
223 char *q;
224 va_list ap;
225 /* Try to print in the allocated space. */
226 va_start(ap, fmt);
227 n = vsnprintf(p, size, fmt, ap);
228 va_end(ap);
229 /* If that worked, return the string. */
230 if (n > -1 && n < size)
231 break;
232 /* Else try again with more space. */
233 if (n > -1) /* glibc 2.1 */
234 size = n + 1; /* precisely what is needed */
235 else /* glibc 2.0 */
236 size *= 2; /* twice the old size */
237 q = realloc(p, size);
238 if (!q) {
239 free(p);
240 return NULL;
241 }
242 p = q;
243 }
244 return p;
245 }
246
247 /**
248 * A wrapper for truncate(2)
249 *
250 * \param path Name of the regular file to truncate
251 * \param size Number of bytes to \b shave \b off
252 *
253 * Truncate the regular file named by \a path by \a size bytes.
254 *
255 * \return Standard.
256 *
257 * \sa truncate(2)
258 */
259 int truncate_file(const char *path, off_t size)
260 {
261 int ret;
262 struct stat statbuf;
263
264 ret = osl_stat(path, &statbuf);
265 if (ret < 0)
266 return ret;
267 ret = -E_OSL_BAD_SIZE;
268 if (statbuf.st_size < size)
269 return ret;
270 if (truncate(path, statbuf.st_size - size) < 0)
271 return -E_OSL_TRUNCATE;
272 return 1;
273 }