]> git.tuebingen.mpg.de Git - osl.git/blob - fd.c
Inline para_fchdir(), move it to fsck.c and rename it.
[osl.git] / fd.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 fd.c Helper functions for file descriptor handling. */
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 "fd.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  * Wrapper for chdir(2).
123  *
124  * \param path The specified directory.
125  *
126  * \return Standard.
127  */
128 _static_inline_ int __chdir(const char *path)
129 {
130         if (chdir(path) >= 0)
131                 return 1;
132         return -ERRNO_TO_ERROR(errno);
133 }
134
135 /**
136  * Save the cwd and open a given directory.
137  *
138  * \param dirname Path to the directory to open.
139  * \param dir Result pointer.
140  * \param cwd File descriptor of the current working directory.
141  *
142  * \return Standard.
143  *
144  * Opening the current directory (".") and calling fchdir() to return is
145  * usually faster and more reliable than saving cwd in some buffer and calling
146  * chdir() afterwards.
147  *
148  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
149  * stored in \a cwd. If the function returns success, and \a cwd is not \p
150  * NULL, the caller must close this file descriptor (probably after calling
151  * fchdir(*cwd)).
152  *
153  * On errors, the function undos everything, so the caller needs neither close
154  * any files, nor change back to the original working directory.
155  *
156  * \sa getcwd(3).
157  *
158  */
159 int para_opendir(const char *dirname, DIR **dir, int *cwd)
160 {
161         int ret;
162
163         if (cwd) {
164                 ret = osl_open(".", O_RDONLY, 0);
165                 if (ret < 0)
166                         return ret;
167                 *cwd = ret;
168         }
169         ret = __chdir(dirname);
170         if (ret < 0)
171                 goto close_cwd;
172         *dir = opendir(".");
173         if (*dir)
174                 return 1;
175         ret = -ERRNO_TO_ERROR(errno);
176 /* Ignore return value of fchdir() and close(). We're busted anyway. */
177         if (cwd)
178                 fchdir(*cwd);
179 close_cwd:
180         if (cwd)
181                 close(*cwd);
182         return ret;
183 }
184
185 /**
186  * Open a file and map it into memory.
187  *
188  * \param path Name of the regular file to map.
189  * \param open_mode Either \p O_RDONLY or \p O_RDWR.
190  * \param map On success, the mapping is returned here.
191  * \param size size of the mapping.
192  * \param fd_ptr The file descriptor of the mapping.
193  *
194  * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
195  * open call is closed after mmap().  Otherwise the file is kept open and the
196  * file descriptor is returned in \a fd_ptr.
197  *
198  * \return Standard.
199  *
200  * \sa osl_open(), mmap(2).
201  */
202 int mmap_full_file(const char *path, int open_mode, void **map,
203                 size_t *size, int *fd_ptr)
204 {
205         int fd, ret, mmap_prot, mmap_flags;
206         struct stat file_status;
207
208         if (open_mode == O_RDONLY) {
209                 mmap_prot = PROT_READ;
210                 mmap_flags = MAP_PRIVATE;
211         } else {
212                 mmap_prot = PROT_READ | PROT_WRITE;
213                 mmap_flags = MAP_SHARED;
214         }
215         ret = osl_open(path, open_mode, 0);
216         if (ret < 0)
217                 return ret;
218         fd = ret;
219         if (fstat(fd, &file_status) < 0) {
220                 ret = -ERRNO_TO_ERROR(errno);
221                 goto out;
222         }
223         *size = file_status.st_size;
224         ret = -E_OSL_EMPTY;
225         DEBUG_LOG("%s: size %zu\n", path, *size);
226         if (!*size)
227                 goto out;
228         *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
229         if (*map == MAP_FAILED) {
230                 *map = NULL;
231                 ret = -E_OSL_MMAP;
232                 goto out;
233         }
234         ret = 1;
235 out:
236         if (ret < 0 || !fd_ptr)
237                 close(fd);
238         else
239                 *fd_ptr = fd;
240         return ret;
241 }
242
243 /**
244  * A wrapper for munmap(2).
245  *
246  * \param start The start address of the memory mapping.
247  * \param length The size of the mapping.
248  *
249  * \return Positive on success, \p -E_MUNMAP on errors.
250  *
251  * \sa munmap(2), mmap_full_file().
252  */
253 int osl_munmap(void *start, size_t length)
254 {
255         int err;
256         if (munmap(start, length) >= 0)
257                 return 1;
258         err = errno;
259         ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
260                 strerror(err));
261         return -ERRNO_TO_ERROR(err);
262 }
263
264 /**
265  * Allocate a sufficiently large string and print into it.
266  *
267  * \param fmt A usual format string.
268  *
269  * Produce output according to \p fmt. No artificial bound on the length of the
270  * resulting string is imposed.
271  *
272  * \return This function either returns a pointer to a string that must be
273  * freed by the caller or \p NULL if memory allocation failed.
274  *
275  * \sa printf(3).
276  */
277 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
278 {
279         int n;
280         size_t size = 100;
281         char *p = malloc(size);
282
283         if (!p)
284                 return NULL;
285         while (1) {
286                 char *q;
287                 va_list ap;
288                 /* Try to print in the allocated space. */
289                 va_start(ap, fmt);
290                 n = vsnprintf(p, size, fmt, ap);
291                 va_end(ap);
292                 /* If that worked, return the string. */
293                 if (n > -1 && n < size)
294                         break;
295                 /* Else try again with more space. */
296                 if (n > -1) /* glibc 2.1 */
297                         size = n + 1; /* precisely what is needed */
298                 else /* glibc 2.0 */
299                         size *= 2; /* twice the old size */
300                 q = realloc(p, size);
301                 if (!q) {
302                         free(p);
303                         return NULL;
304                 }
305                 p = q;
306         }
307         return p;
308 }
309
310 /**
311  * A wrapper for truncate(2)
312  *
313  * \param path Name of the regular file to truncate
314  * \param size Number of bytes to \b shave \b off
315  *
316  * Truncate the regular file named by \a path by \a size bytes.
317  *
318  * \return Standard.
319  *
320  * \sa truncate(2)
321  */
322 int truncate_file(const char *path, off_t size)
323 {
324         int ret;
325         struct stat statbuf;
326
327         ret = osl_stat(path, &statbuf);
328         if (ret < 0)
329                 return ret;
330         ret = -E_OSL_BAD_SIZE;
331         if (statbuf.st_size < size)
332                 return ret;
333         if (truncate(path, statbuf.st_size - size) < 0)
334                 return -ERRNO_TO_ERROR(errno);
335         return 1;
336 }