2 * Copyright (C) 2006-2008 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fd.c \brief Helper functions for file descriptor handling. */
20 * Wrapper for the write system call.
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.
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).
34 static ssize_t __write(int fd, const void *buf, size_t size)
39 ret = write(fd, buf, size);
40 if (ret < 0 && errno == EINTR)
42 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
47 * Write the whole buffer to a file descriptor.
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.
53 * This function writes the given buffer and continues on short writes and
54 * when interrupted by a signal.
58 static ssize_t write_all(int fd, const void *buf, size_t size)
60 // DEBUG_LOG("writing %zu bytes\n", size);
63 ssize_t ret = __write(fd, b, size);
64 // DEBUG_LOG("ret: %zd\n", ret);
74 * Wrapper for the open(2) system call.
76 * \param path The filename.
77 * \param flags The usual open(2) flags.
78 * \param mode Specifies the permissions to use.
80 * The mode parameter must be specified when O_CREAT is in the flags, and is
83 * \return The file descriptor on success, negative on errors.
87 static int __open(const char *path, int flags, mode_t mode)
89 int ret = open(path, flags, mode);
93 return -ERRNO_TO_ERROR(errno);
97 * Open a file, write the given buffer and close the file.
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.
105 int adu_write_file(const char *filename, const void *buf, size_t size)
109 ret = __open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
113 ret = write_all(fd, buf, size);
123 * Wrapper for chdir(2).
125 * \param path The specified directory.
129 static int __chdir(const char *path)
131 int ret = chdir(path);
135 return -ERRNO_TO_ERROR(errno);
139 * Save the cwd and open a given directory.
141 * \param dirname Path to the directory to open.
142 * \param dir Result pointer.
143 * \param cwd File descriptor of the current working directory.
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.
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
156 * On errors, the function undos everything, so the caller needs neither close
157 * any files, nor change back to the original working directory.
162 int adu_opendir(const char *dirname, DIR **dir, int *cwd)
167 ret = __open(".", O_RDONLY, 0);
172 ret = __chdir(dirname);
178 ret = -ERRNO_TO_ERROR(errno);
179 /* Ignore return value of fchdir() and close(). We're busted anyway. */
181 int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */
190 * A wrapper for fchdir().
192 * \param fd An open file descriptor.
196 int adu_fchdir(int fd)
199 return -ERRNO_TO_ERROR(errno);
204 * Open a file read-only and map it into memory.
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.
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().
217 int mmap_file_ro(const char *path, void **map, size_t *size)
220 struct stat file_status;
222 ret = __open(path, O_RDONLY, 0);
226 if (fstat(fd, &file_status) < 0) {
227 ret = -ERRNO_TO_ERROR(errno);
230 *size = file_status.st_size;
232 DEBUG_LOG("%s: size %zu\n", path, *size);
235 *map = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
236 if (*map == MAP_FAILED) {
248 * A wrapper for munmap(2).
250 * \param start The start address of the memory mapping.
251 * \param length The size of the mapping.
253 * \return Positive on success, \p -E_MUNMAP on errors.
255 * \sa munmap(2), mmap_full_file().
257 int adu_munmap(void *start, size_t length)
260 if (munmap(start, length) >= 0)
263 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
265 return -ERRNO_TO_ERROR(err);
268 __must_check __malloc static char *adu_dirname(const char *name)
274 ret = adu_strdup(name);
275 p = strrchr(ret, '/');
286 * \param p Full path that should be created.
288 * \param mode Use this mode when creating directories.
290 * \return 0 if successful, -E_MKDIR on errors.
292 int mkpath(const char *p, mode_t mode)
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");
302 path = adu_strdup(p);
303 parent = adu_dirname(p);
306 ret = mkpath(parent, mode);
309 INFO_LOG("making dir %s\n", path);
311 if ((mkdir(path, mode) == -1) && (errno != EEXIST))