Add wrapper for isspace() which is needed for NetBSD.
[adu.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 \brief Helper functions for file descriptor handling. */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/mman.h>
12 #include <string.h>
13
14 #include "adu.h"
15 #include "error.h"
16 #include "string.h"
17
18 /**
19  * Wrapper for the write system call.
20  *
21  * \param fd The file descriptor to write to.
22  * \param buf The buffer to write.
23  * \param size The length of \a buf in bytes.
24  *
25  * This function writes out the given buffer and retries if an interrupt
26  * occurred during the write.
27  *
28  * \return Standard.
29  *
30  * \sa write(2).
31  */
32 ssize_t __write(int fd, const void *buf, size_t size)
33 {
34         ssize_t ret;
35
36         for (;;) {
37                 ret = write(fd, buf, size);
38                 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
39                         continue;
40                 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
41         }
42 }
43
44 /**
45  * Write the whole buffer to a file descriptor.
46  *
47  * \param fd The file descriptor to write to.
48  * \param buf The buffer to write.
49  * \param size The length of \a buf in bytes.
50  *
51  * This function writes the given buffer and continues on short writes and
52  * when interrupted by a signal.
53  *
54  * \return Standard.
55  */
56 static ssize_t write_all(int fd, const void *buf, size_t size)
57 {
58 //      DEBUG_LOG("writing %zu bytes\n", size);
59         const char *b = buf;
60         while (size) {
61                 ssize_t ret = __write(fd, b, size);
62 //              DEBUG_LOG("ret: %zd\n", ret);
63                 if (ret < 0)
64                         return ret;
65                 b += ret;
66                 size -= 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 static int __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_TO_ERROR(errno);
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 adu_write_file(const char *filename, const void *buf, size_t size)
104 {
105         int ret, fd;
106
107         ret = __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  * Wrapper for chdir(2).
122  *
123  * \param path The specified directory.
124  *
125  * \return Standard.
126  */
127 int __chdir(const char *path)
128 {
129         int ret = chdir(path);
130
131         if (ret >= 0)
132                 return 1;
133         return -ERRNO_TO_ERROR(errno);
134 }
135
136 /**
137  * Save the cwd and open a given directory.
138  *
139  * \param dirname Path to the directory to open.
140  * \param dir Result pointer.
141  * \param cwd File descriptor of the current working directory.
142  *
143  * \return Standard.
144  *
145  * Opening the current directory (".") and calling fchdir() to return is
146  * usually faster and more reliable than saving cwd in some buffer and calling
147  * chdir() afterwards.
148  *
149  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
150  * stored in \a cwd. If the function returns success, and \a cwd is not \p
151  * NULL, the caller must close this file descriptor (probably after calling
152  * fchdir(*cwd)).
153  *
154  * On errors, the function undos everything, so the caller needs neither close
155  * any files, nor change back to the original working directory.
156  *
157  * \sa getcwd(3).
158  *
159  */
160 int adu_opendir(const char *dirname, DIR **dir, int *cwd)
161 {
162         int ret;
163
164         if (cwd) {
165                 ret = __open(".", O_RDONLY, 0);
166                 if (ret < 0)
167                         return ret;
168                 *cwd = ret;
169         }
170         ret = __chdir(dirname);
171         if (ret < 0)
172                 goto close_cwd;
173         *dir = opendir(".");
174         if (*dir)
175                 return 1;
176         ret = -ERRNO_TO_ERROR(errno);
177 /* Ignore return value of fchdir() and close(). We're busted anyway. */
178         if (cwd)
179                 fchdir(*cwd);
180 close_cwd:
181         if (cwd)
182                 close(*cwd);
183         return ret;
184 }
185
186 /**
187  * A wrapper for fchdir().
188  *
189  * \param fd An open file descriptor.
190  *
191  * \return Standard.
192  */
193 int adu_fchdir(int fd)
194 {
195         if (fchdir(fd) < 0)
196                 return -ERRNO_TO_ERROR(errno);
197         return 1;
198 }
199
200 /**
201  * Open a file and map it into memory.
202  *
203  * \param path Name of the regular file to map.
204  * \param open_mode Either \p O_RDONLY or \p O_RDWR.
205  * \param map On success, the mapping is returned here.
206  * \param size size of the mapping.
207  * \param fd_ptr The file descriptor of the mapping.
208  *
209  * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
210  * open call is closed after mmap().  Otherwise the file is kept open and the
211  * file descriptor is returned in \a fd_ptr.
212  *
213  * \return Standard.
214  *
215  * \sa mmap(2).
216  */
217 int mmap_full_file(const char *path, int open_mode, void **map,
218                 size_t *size, int *fd_ptr)
219 {
220         int fd, ret, mmap_prot, mmap_flags;
221         struct stat file_status;
222
223         if (open_mode == O_RDONLY) {
224                 mmap_prot = PROT_READ;
225                 mmap_flags = MAP_PRIVATE;
226         } else {
227                 mmap_prot = PROT_READ | PROT_WRITE;
228                 mmap_flags = MAP_SHARED;
229         }
230         ret = __open(path, open_mode, 0);
231         if (ret < 0)
232                 return ret;
233         fd = ret;
234         if (fstat(fd, &file_status) < 0) {
235                 ret = -ERRNO_TO_ERROR(errno);
236                 goto out;
237         }
238         *size = file_status.st_size;
239         ret = -E_EMPTY;
240         DEBUG_LOG("%s: size %zu\n", path, *size);
241         if (!*size)
242                 goto out;
243         *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
244         if (*map == MAP_FAILED) {
245                 *map = NULL;
246                 ret = -E_MMAP;
247                 goto out;
248         }
249         ret = 1;
250 out:
251         if (ret < 0 || !fd_ptr)
252                 close(fd);
253         else
254                 *fd_ptr = fd;
255         return ret;
256 }
257
258 /**
259  * A wrapper for munmap(2).
260  *
261  * \param start The start address of the memory mapping.
262  * \param length The size of the mapping.
263  *
264  * \return Positive on success, \p -E_MUNMAP on errors.
265  *
266  * \sa munmap(2), mmap_full_file().
267  */
268 int adu_munmap(void *start, size_t length)
269 {
270         int err;
271         if (munmap(start, length) >= 0)
272                 return 1;
273         err = errno;
274         ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
275                 strerror(err));
276         return -ERRNO_TO_ERROR(err);
277 }
278
279 __must_check __malloc static char *adu_dirname(const char *name)
280 {
281         char *p, *ret;
282
283         if (!name || !*name)
284                 return NULL;
285         ret = adu_strdup(name);
286         p = strrchr(ret, '/');
287         if (!p)
288                 *ret = '\0';
289         else
290                 *p = '\0';
291         return ret;
292 }
293
294 /**
295  * Recursive mkdir
296  *
297  * \param p Full path that should be created.
298  *
299  * \param mode Use this mode when creating directories.
300  *
301  * \return 0 if successful, -E_MKDIR on errors.
302  */
303 int mkpath(const char *p, mode_t mode)
304 {
305         char *parent, *path;
306         int ret = -E_MKDIR;
307
308         DEBUG_LOG("%s\n", p);
309         if (strcmp(p, ".") == 0 || strcmp(p, "/") == 0 || strcmp(p, "") == 0) {
310                 DEBUG_LOG("reached beginning of path\n");
311                 return 0;
312         }
313         path = adu_strdup(p);
314         parent = adu_dirname(p);
315         if (!parent)
316                 goto out;
317         ret = mkpath(parent, mode);
318         if (ret < 0)
319                 goto out;
320         INFO_LOG("making dir %s\n", path);
321         ret = 0;
322         if ((mkdir(path, mode) == -1) && (errno != EEXIST))
323                 ret = -E_MKDIR;
324 out:
325         free(parent);
326         free(path);
327         return ret;
328 }