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.
33 ssize_t __write(int fd, const void *buf, size_t size)
38 ret = write(fd, buf, size);
39 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
41 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
46 * Write the whole buffer to a file descriptor.
48 * \param fd The file descriptor to write to.
49 * \param buf The buffer to write.
50 * \param size The length of \a buf in bytes.
52 * This function writes the given buffer and continues on short writes and
53 * when interrupted by a signal.
57 static ssize_t write_all(int fd, const void *buf, size_t size)
59 // DEBUG_LOG("writing %zu bytes\n", size);
62 ssize_t ret = __write(fd, b, size);
63 // DEBUG_LOG("ret: %zd\n", ret);
73 * Wrapper for the open(2) system call.
75 * \param path The filename.
76 * \param flags The usual open(2) flags.
77 * \param mode Specifies the permissions to use.
79 * The mode parameter must be specified when O_CREAT is in the flags, and is
82 * \return The file descriptor on success, negative on errors.
86 static int __open(const char *path, int flags, mode_t mode)
88 int ret = open(path, flags, mode);
92 return -ERRNO_TO_ERROR(errno);
96 * Open a file, write the given buffer and close the file.
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.
104 int adu_write_file(const char *filename, const void *buf, size_t size)
108 ret = __open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
112 ret = write_all(fd, buf, size);
122 * Wrapper for chdir(2).
124 * \param path The specified directory.
128 static int __chdir(const char *path)
130 int ret = chdir(path);
134 return -ERRNO_TO_ERROR(errno);
138 * Save the cwd and open a given directory.
140 * \param dirname Path to the directory to open.
141 * \param dir Result pointer.
142 * \param cwd File descriptor of the current working directory.
146 * Opening the current directory (".") and calling fchdir() to return is
147 * usually faster and more reliable than saving cwd in some buffer and calling
148 * chdir() afterwards.
150 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
151 * stored in \a cwd. If the function returns success, and \a cwd is not \p
152 * NULL, the caller must close this file descriptor (probably after calling
155 * On errors, the function undos everything, so the caller needs neither close
156 * any files, nor change back to the original working directory.
161 int adu_opendir(const char *dirname, DIR **dir, int *cwd)
166 ret = __open(".", O_RDONLY, 0);
171 ret = __chdir(dirname);
177 ret = -ERRNO_TO_ERROR(errno);
178 /* Ignore return value of fchdir() and close(). We're busted anyway. */
188 * A wrapper for fchdir().
190 * \param fd An open file descriptor.
194 int adu_fchdir(int fd)
197 return -ERRNO_TO_ERROR(errno);
202 * Open a file and map it into memory.
204 * \param path Name of the regular file to map.
205 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
206 * \param map On success, the mapping is returned here.
207 * \param size size of the mapping.
208 * \param fd_ptr The file descriptor of the mapping.
210 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
211 * open call is closed after mmap(). Otherwise the file is kept open and the
212 * file descriptor is returned in \a fd_ptr.
218 int mmap_full_file(const char *path, int open_mode, void **map,
219 size_t *size, int *fd_ptr)
221 int fd, ret, mmap_prot, mmap_flags;
222 struct stat file_status;
224 if (open_mode == O_RDONLY) {
225 mmap_prot = PROT_READ;
226 mmap_flags = MAP_PRIVATE;
228 mmap_prot = PROT_READ | PROT_WRITE;
229 mmap_flags = MAP_SHARED;
231 ret = __open(path, open_mode, 0);
235 if (fstat(fd, &file_status) < 0) {
236 ret = -ERRNO_TO_ERROR(errno);
239 *size = file_status.st_size;
241 DEBUG_LOG("%s: size %zu\n", path, *size);
244 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
245 if (*map == MAP_FAILED) {
252 if (ret < 0 || !fd_ptr)
260 * A wrapper for munmap(2).
262 * \param start The start address of the memory mapping.
263 * \param length The size of the mapping.
265 * \return Positive on success, \p -E_MUNMAP on errors.
267 * \sa munmap(2), mmap_full_file().
269 int adu_munmap(void *start, size_t length)
272 if (munmap(start, length) >= 0)
275 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
277 return -ERRNO_TO_ERROR(err);
280 __must_check __malloc static char *adu_dirname(const char *name)
286 ret = adu_strdup(name);
287 p = strrchr(ret, '/');
298 * \param p Full path that should be created.
300 * \param mode Use this mode when creating directories.
302 * \return 0 if successful, -E_MKDIR on errors.
304 int mkpath(const char *p, mode_t mode)
309 DEBUG_LOG("%s\n", p);
310 if (strcmp(p, ".") == 0 || strcmp(p, "/") == 0 || strcmp(p, "") == 0) {
311 DEBUG_LOG("reached beginning of path\n");
314 path = adu_strdup(p);
315 parent = adu_dirname(p);
318 ret = mkpath(parent, mode);
321 INFO_LOG("making dir %s\n", path);
323 if ((mkdir(path, mode) == -1) && (errno != EEXIST))