X-Git-Url: http://git.tuebingen.mpg.de/?p=adu.git;a=blobdiff_plain;f=fd.c;h=39e98374b3acb9ccc727426bf0b6b06d047b57e7;hp=e661e71fa32d2c20d92f558f9f40f62f576b9ba1;hb=2e24b91148dda8ea52b308a4b5e0ef08c0f11267;hpb=d0bef44ffe5b6f985f4ba6a718e08afb50f096c6 diff --git a/fd.c b/fd.c index e661e71..39e9837 100644 --- a/fd.c +++ b/fd.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2008 Andre Noll + * Copyright (C) 2006-2008 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -14,6 +14,7 @@ #include "adu.h" #include "error.h" #include "string.h" +#include "fd.h" /** * Wrapper for the write system call. @@ -23,19 +24,20 @@ * \param size The length of \a buf in bytes. * * This function writes out the given buffer and retries if an interrupt - * occurred during the write. + * occurred during the write. The file descriptor is assumed to be in blocking + * mode (i.e., EAGAIN is treated as an error). * * \return Standard. * * \sa write(2). */ -ssize_t __write(int fd, const void *buf, size_t size) +static ssize_t __write(int fd, const void *buf, size_t size) { ssize_t ret; for (;;) { ret = write(fd, buf, size); - if ((ret < 0) && (errno == EAGAIN || errno == EINTR)) + if (ret < 0 && errno == EINTR) continue; return ret >= 0? ret : -ERRNO_TO_ERROR(errno); } @@ -124,7 +126,7 @@ out: * * \return Standard. */ -int __chdir(const char *path) +static int __chdir(const char *path) { int ret = chdir(path); @@ -174,9 +176,10 @@ int adu_opendir(const char *dirname, DIR **dir, int *cwd) if (*dir) return 1; ret = -ERRNO_TO_ERROR(errno); -/* Ignore return value of fchdir() and close(). We're busted anyway. */ - if (cwd) - fchdir(*cwd); + /* Ignore return value of fchdir() and close(). We're busted anyway. */ + if (cwd) { + int __a_unused ret2 = fchdir(*cwd); /* STFU, gcc */ + } close_cwd: if (cwd) close(*cwd); @@ -198,36 +201,25 @@ int adu_fchdir(int fd) } /** - * Open a file and map it into memory. + * Open a file read-only and map it into memory. * * \param path Name of the regular file to map. - * \param open_mode Either \p O_RDONLY or \p O_RDWR. * \param map On success, the mapping is returned here. - * \param size size of the mapping. - * \param fd_ptr The file descriptor of the mapping. + * \param size Result parameter: size of the mapping in bytes. * - * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying - * open call is closed after mmap(). Otherwise the file is kept open and the - * file descriptor is returned in \a fd_ptr. + * The file will be mapped privately with memory protection PROT_READ. The file + * descriptor resulting from the underlying open call is closed after mmap(). * * \return Standard. * * \sa mmap(2). */ -int mmap_full_file(const char *path, int open_mode, void **map, - size_t *size, int *fd_ptr) +int mmap_file_ro(const char *path, void **map, size_t *size) { - int fd, ret, mmap_prot, mmap_flags; + int fd, ret; struct stat file_status; - if (open_mode == O_RDONLY) { - mmap_prot = PROT_READ; - mmap_flags = MAP_PRIVATE; - } else { - mmap_prot = PROT_READ | PROT_WRITE; - mmap_flags = MAP_SHARED; - } - ret = __open(path, open_mode, 0); + ret = __open(path, O_RDONLY, 0); if (ret < 0) return ret; fd = ret; @@ -240,7 +232,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, DEBUG_LOG("%s: size %zu\n", path, *size); if (!*size) goto out; - *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0); + *map = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); if (*map == MAP_FAILED) { *map = NULL; ret = -E_MMAP; @@ -248,10 +240,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, } ret = 1; out: - if (ret < 0 || !fd_ptr) - close(fd); - else - *fd_ptr = fd; + close(fd); return ret; }