/*
* Load and unload nsscash files
*
- * Copyright (C) 2019 Simon Ruderich
+ * Copyright (C) 2019-2021 Simon Ruderich
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
bool map_file(const char *path, struct file *f) {
// Fully initialize the struct for unmap_file() and other users
- f->fd = -1;
- f->size = 0;
- f->next_index = 0;
- f->header = NULL;
+ memset(f, 0, sizeof(*f));
f->fd = open(path, O_RDONLY | O_CLOEXEC);
if (f->fd < 0) {
if (fstat(f->fd, &s)) {
goto fail;
}
- f->size = (size_t)s.st_size;
+ f->size = (size_t)s.st_size; // for munmap()
+ // mmap is used for speed and simple random access
void *x = mmap(NULL, f->size, PROT_READ, MAP_PRIVATE, f->fd, 0);
if (x == MAP_FAILED) {
goto fail;
munmap((void *)f->header, f->size);
f->header = NULL;
}
- if (f->fd != -1) {
+ if (f->fd >= 0) {
close(f->fd);
f->fd = -1;
}