]> git.tuebingen.mpg.de Git - adu.git/commitdiff
Simplify and rename mmap_full_file().
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 15 Feb 2015 15:42:05 +0000 (16:42 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 15 Feb 2015 15:45:32 +0000 (16:45 +0100)
There is only one caller which maps the file privately into read-only
memory. Moreover, this caller does not care about the file descriptor
from which the mapping was created.

This commit removes the argument from mmap_full_file() which control
the type of the map and the result pointer for the file descriptor,
thereby simplifying the function slightly. Rename it to mmap_file_ro()
to indicate that read-only private mappings are implied.

fd.c
fd.h
user.c

diff --git a/fd.c b/fd.c
index 79843561a73e050bd6a362662b5aff70cd096a30..9fe88679d2d88855a27a3486d4646bd1632c965f 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -199,36 +199,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;
@@ -241,7 +230,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;
@@ -249,10 +238,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;
 }
 
diff --git a/fd.h b/fd.h
index 108993209ce19ddabdaf27f706dd5f4d7ef1d91e..a3d1d9d8f8ed2cb8be5e10fc95235ae4b3017b17 100644 (file)
--- a/fd.h
+++ b/fd.h
@@ -8,8 +8,7 @@
 
 int adu_opendir(const char *dirname, DIR **dir, int *cwd);
 int adu_fchdir(int fd);
-int mmap_full_file(const char *filename, int open_mode, void **map,
-       size_t *size, int *fd_ptr);
+int mmap_file_ro(const char *filename, void **map, size_t *size);
 int adu_munmap(void *start, size_t length);
 int adu_write_file(const char *filename, const void *buf, size_t size);
 int mkpath(const char *p, mode_t mode);
diff --git a/user.c b/user.c
index a1513a4cd6ffbc6f96ab7d1573c2cc210a7c7703..ed16463afed9372426c5c08284a65ec863a19e2e 100644 (file)
--- a/user.c
+++ b/user.c
@@ -511,7 +511,7 @@ int read_uid_file(void)
        size_t size;
        uint32_t n;
        char *filename = get_uid_list_name(), *map;
-       int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
+       int ret = mmap_file_ro(filename, (void **)&map, &size);
        unsigned bits;
 
        if (ret < 0) {