X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=nss%2Ffile.c;h=ba6c481a85daabae43873dd0e03a9fd95be51925;hb=526f6f1db39bde8ca1f7684225a3983634bddafe;hp=30ea4f9b5f96910f0cd0ba8d353cfa7dbccea076;hpb=92afde4e875a96e1ab865e29b9f0d11b08d7db1c;p=nsscash%2Fnsscash.git diff --git a/nss/file.c b/nss/file.c index 30ea4f9..ba6c481 100644 --- a/nss/file.c +++ b/nss/file.c @@ -31,10 +31,7 @@ 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) { @@ -44,8 +41,9 @@ bool map_file(const char *path, struct file *f) { 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; @@ -81,7 +79,7 @@ void unmap_file(struct file *f) { munmap((void *)f->header, f->size); f->header = NULL; } - if (f->fd != -1) { + if (f->fd >= 0) { close(f->fd); f->fd = -1; }