]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/file.c
30ea4f9b5f96910f0cd0ba8d353cfa7dbccea076
[nsscash/nsscash.git] / nss / file.c
1 /*
2  * Load and unload nsscash files
3  *
4  * Copyright (C) 2019  Simon Ruderich
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include "file.h"
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31
32 bool map_file(const char *path, struct file *f) {
33     // Fully initialize the struct for unmap_file() and other users
34     f->fd = -1;
35     f->size = 0;
36     f->next_index = 0;
37     f->header = NULL;
38
39     f->fd = open(path, O_RDONLY | O_CLOEXEC);
40     if (f->fd < 0) {
41         goto fail;
42     }
43     struct stat s;
44     if (fstat(f->fd, &s)) {
45         goto fail;
46     }
47     f->size = (size_t)s.st_size;
48
49     void *x = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
50     if (x == MAP_FAILED) {
51         goto fail;
52     }
53
54     const struct header *h = x;
55     f->header = h;
56
57     // Check MAGIC
58     if (memcmp(h->magic, MAGIC, sizeof(h->magic))) {
59         errno = EINVAL;
60         goto fail;
61     }
62     // Only version 1 is supported at the moment; this will also prevent
63     // running on big-endian systems which is currently not possible
64     if (h->version != 1) {
65         errno = EINVAL;
66         goto fail;
67     }
68
69     return true;
70
71 fail: {
72         int save_errno = errno;
73         unmap_file(f);
74         errno = save_errno;
75         return false;
76     }
77 }
78
79 void unmap_file(struct file *f) {
80     if (f->header != NULL) {
81         munmap((void *)f->header, f->size);
82         f->header = NULL;
83     }
84     if (f->fd != -1) {
85         close(f->fd);
86         f->fd = -1;
87     }
88 }