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