chunk: Provide a fallback chunk_map() if mmap is not available
[strongswan.git] / src / libstrongswan / utils / chunk.c
index 5c00c5b..5961407 100644 (file)
@@ -18,7 +18,9 @@
 #include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
+#ifdef HAVE_MMAP
+# include <sys/mman.h>
+#endif
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
@@ -247,33 +249,62 @@ bool chunk_write(chunk_t chunk, char *path, char *label, mode_t mask, bool force
 /**
  * Described in header.
  */
-chunk_t chunk_from_fd(int fd)
+bool chunk_from_fd(int fd, chunk_t *out)
 {
-       char buf[8096];
-       char *pos = buf;
-       ssize_t len, total = 0;
+       struct stat sb;
+       char *buf, *tmp;
+       ssize_t len, total = 0, bufsize;
+
+       if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode))
+       {
+               bufsize = sb.st_size;
+       }
+       else
+       {
+               bufsize = 256;
+       }
+       buf = malloc(bufsize);
+       if (!buf)
+       {       /* for huge files */
+               return FALSE;
+       }
 
        while (TRUE)
        {
-               len = read(fd, pos, buf + sizeof(buf) - pos);
+               len = read(fd, buf + total, bufsize - total);
                if (len < 0)
                {
-                       DBG1(DBG_LIB, "reading from file descriptor failed: %s",
-                                strerror(errno));
-                       return chunk_empty;
+                       free(buf);
+                       return FALSE;
                }
                if (len == 0)
                {
                        break;
                }
                total += len;
-               if (total == sizeof(buf))
+               if (total == bufsize)
                {
-                       DBG1(DBG_LIB, "buffer too small to read from file descriptor");
-                       return chunk_empty;
+                       bufsize *= 2;
+                       tmp = realloc(buf, bufsize);
+                       if (!tmp)
+                       {
+                               free(buf);
+                               return FALSE;
+                       }
+                       buf = tmp;
                }
        }
-       return chunk_clone(chunk_create(buf, total));
+       if (total == 0)
+       {
+               free(buf);
+               buf = NULL;
+       }
+       else if (total < bufsize)
+       {
+               buf = realloc(buf, total);
+       }
+       *out = chunk_create(buf, total);
+       return TRUE;
 }
 
 /**
@@ -288,6 +319,8 @@ typedef struct {
        void *map;
        /* size of map */
        size_t len;
+       /* do we write? */
+       bool wr;
 } mmaped_chunk_t;
 
 /**
@@ -301,6 +334,7 @@ chunk_t *chunk_map(char *path, bool wr)
 
        INIT(chunk,
                .fd = open(path, wr ? O_RDWR : O_RDONLY),
+               .wr = wr,
        );
 
        if (chunk->fd == -1)
@@ -315,6 +349,7 @@ chunk_t *chunk_map(char *path, bool wr)
                errno = tmp;
                return NULL;
        }
+#ifdef HAVE_MMAP
        chunk->len = sb.st_size;
        /* map non-empty files only, as mmap() complains otherwise */
        if (chunk->len)
@@ -331,6 +366,17 @@ chunk_t *chunk_map(char *path, bool wr)
                }
        }
        chunk->public = chunk_create(chunk->map, chunk->len);
+#else /* !HAVE_MMAP */
+       if (!chunk_from_fd(chunk->fd, &chunk->public))
+       {
+               tmp = errno;
+               chunk_unmap(&chunk->public);
+               errno = tmp;
+               return NULL;
+       }
+       chunk->map = chunk->public.ptr;
+       chunk->len = chunk->public.len;
+#endif /* !HAVE_MMAP */
        return &chunk->public;
 }
 
@@ -344,11 +390,39 @@ bool chunk_unmap(chunk_t *public)
        int tmp = 0;
 
        chunk = (mmaped_chunk_t*)public;
+#ifdef HAVE_MMAP
        if (chunk->map && chunk->map != MAP_FAILED)
        {
                ret = munmap(chunk->map, chunk->len) == 0;
                tmp = errno;
        }
+#else /* !HAVE_MMAP */
+       if (chunk->wr)
+       {
+               if (lseek(chunk->fd, 0, SEEK_SET) != -1)
+               {
+                       int len, total = 0;
+
+                       ret = TRUE;
+                       while (total < chunk->len)
+                       {
+                               len = write(chunk->fd, chunk->map + total, chunk->len - total);
+                               if (len <= 0)
+                               {
+                                       ret = FALSE;
+                                       break;
+                               }
+                               total += len;
+                       }
+               }
+               tmp = errno;
+       }
+       else
+       {
+               ret = TRUE;
+       }
+       free(chunk->map);
+#endif /* !HAVE_MMAP */
        close(chunk->fd);
        free(chunk);
        errno = tmp;