]> ruderich.org/simon Gitweb - nsscash/nsscash.git/blob - nss/file.c
Update copyright years
[nsscash/nsscash.git] / nss / file.c
1 /*
2  * Load and unload nsscash files
3  *
4  * Copyright (C) 2019-2020  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     memset(f, 0, sizeof(*f));
35
36     f->fd = open(path, O_RDONLY | O_CLOEXEC);
37     if (f->fd < 0) {
38         goto fail;
39     }
40     struct stat s;
41     if (fstat(f->fd, &s)) {
42         goto fail;
43     }
44     f->size = (size_t)s.st_size; // for munmap()
45
46     // mmap is used for speed and simple random access
47     void *x = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
48     if (x == MAP_FAILED) {
49         goto fail;
50     }
51
52     const struct header *h = x;
53     f->header = h;
54
55     // Check MAGIC
56     if (memcmp(h->magic, MAGIC, sizeof(h->magic))) {
57         errno = EINVAL;
58         goto fail;
59     }
60     // Only version 1 is supported at the moment; this will also prevent
61     // running on big-endian systems which is currently not possible
62     if (h->version != 1) {
63         errno = EINVAL;
64         goto fail;
65     }
66
67     return true;
68
69 fail: {
70         int save_errno = errno;
71         unmap_file(f);
72         errno = save_errno;
73         return false;
74     }
75 }
76
77 void unmap_file(struct file *f) {
78     if (f->header != NULL) {
79         munmap((void *)f->header, f->size);
80         f->header = NULL;
81     }
82     if (f->fd >= 0) {
83         close(f->fd);
84         f->fd = -1;
85     }
86 }