2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fd.c Helper functions for file descriptor handling. */
17 * Wrapper for the write system call.
19 * \param fd The file descriptor to write to.
20 * \param buf The buffer to write.
21 * \param size The length of \a buf in bytes.
23 * This function writes out the given buffer and retries if an interrupt
24 * occurred during the write.
30 ssize_t __write(int fd, const void *buf, size_t size)
35 ret = write(fd, buf, size);
36 if ((ret < 0) && (errno == EAGAIN || errno == EINTR))
38 return ret >= 0? ret : -ERRNO_TO_ERROR(errno);
43 * Write the whole buffer to a file descriptor.
45 * \param fd The file descriptor to write to.
46 * \param buf The buffer to write.
47 * \param size The length of \a buf in bytes.
49 * This function writes the given buffer and continues on short writes and
50 * when interrupted by a signal.
54 static ssize_t write_all(int fd, const void *buf, size_t size)
56 // DEBUG_LOG("writing %zu bytes\n", size);
59 ssize_t ret = __write(fd, b, size);
60 // DEBUG_LOG("ret: %zd\n", ret);
70 * Wrapper for the open(2) system call.
72 * \param path The filename.
73 * \param flags The usual open(2) flags.
74 * \param mode Specifies the permissions to use.
76 * The mode parameter must be specified when O_CREAT is in the flags, and is
79 * \return The file descriptor on success, negative on errors.
83 static int __open(const char *path, int flags, mode_t mode)
85 int ret = open(path, flags, mode);
89 return -ERRNO_TO_ERROR(errno);
93 * Open a file, write the given buffer and close the file.
95 * \param filename Full path to the file to open.
96 * \param buf The buffer to write to the file.
97 * \param size The size of \a buf.
101 int adu_write_file(const char *filename, const void *buf, size_t size)
105 ret = __open(filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
109 ret = write_all(fd, buf, size);
119 * Wrapper for chdir(2).
121 * \param path The specified directory.
125 int __chdir(const char *path)
127 int ret = chdir(path);
131 return -ERRNO_TO_ERROR(errno);
135 * Save the cwd and open a given directory.
137 * \param dirname Path to the directory to open.
138 * \param dir Result pointer.
139 * \param cwd File descriptor of the current working directory.
143 * Opening the current directory (".") and calling fchdir() to return is
144 * usually faster and more reliable than saving cwd in some buffer and calling
145 * chdir() afterwards.
147 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
148 * stored in \a cwd. If the function returns success, and \a cwd is not \p
149 * NULL, the caller must close this file descriptor (probably after calling
152 * On errors, the function undos everything, so the caller needs neither close
153 * any files, nor change back to the original working directory.
158 int adu_opendir(const char *dirname, DIR **dir, int *cwd)
163 ret = __open(".", O_RDONLY, 0);
168 ret = __chdir(dirname);
174 ret = -ERRNO_TO_ERROR(errno);
175 /* Ignore return value of fchdir() and close(). We're busted anyway. */
185 * A wrapper for fchdir().
187 * \param fd An open file descriptor.
191 int adu_fchdir(int fd)
194 return -ERRNO_TO_ERROR(errno);
199 * Open a file and map it into memory.
201 * \param path Name of the regular file to map.
202 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
203 * \param map On success, the mapping is returned here.
204 * \param size size of the mapping.
205 * \param fd_ptr The file descriptor of the mapping.
207 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
208 * open call is closed after mmap(). Otherwise the file is kept open and the
209 * file descriptor is returned in \a fd_ptr.
215 int mmap_full_file(const char *path, int open_mode, void **map,
216 size_t *size, int *fd_ptr)
218 int fd, ret, mmap_prot, mmap_flags;
219 struct stat file_status;
221 if (open_mode == O_RDONLY) {
222 mmap_prot = PROT_READ;
223 mmap_flags = MAP_PRIVATE;
225 mmap_prot = PROT_READ | PROT_WRITE;
226 mmap_flags = MAP_SHARED;
228 ret = __open(path, open_mode, 0);
232 if (fstat(fd, &file_status) < 0) {
233 ret = -ERRNO_TO_ERROR(errno);
236 *size = file_status.st_size;
238 DEBUG_LOG("%s: size %zu\n", path, *size);
241 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
242 if (*map == MAP_FAILED) {
249 if (ret < 0 || !fd_ptr)
257 * A wrapper for munmap(2).
259 * \param start The start address of the memory mapping.
260 * \param length The size of the mapping.
262 * \return Positive on success, \p -E_MUNMAP on errors.
264 * \sa munmap(2), mmap_full_file().
266 int adu_munmap(void *start, size_t length)
269 if (munmap(start, length) >= 0)
272 ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
274 return -ERRNO_TO_ERROR(err);