mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-06 12:33:23 +01:00
gpgscm: Mmap script files.
* tests/gpgscm/main.c (load): Try to mmap the script. * tests/gpgscm/scheme.c (scheme_load_memory): New function, a generalization of 'scheme_load_string'. * tests/gpgscm/scheme.h (scheme_load_memory): New prototype. Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
parent
f3d1f68677
commit
c7f0d90592
@ -23,13 +23,20 @@
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <gcrypt.h>
|
||||
#include <gpg-error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if HAVE_MMAP
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
#include "private.h"
|
||||
#include "scheme.h"
|
||||
#include "scheme-private.h"
|
||||
@ -177,7 +184,39 @@ load (scheme *sc, char *file_name,
|
||||
}
|
||||
if (verbose > 1)
|
||||
fprintf (stderr, "Loading %s...\n", qualified_name);
|
||||
scheme_load_named_file (sc, h, qualified_name);
|
||||
|
||||
#if HAVE_MMAP
|
||||
/* Always try to mmap the file. This allows the pages to be shared
|
||||
* between processes. If anything fails, we fall back to using
|
||||
* buffered streams. */
|
||||
if (1)
|
||||
{
|
||||
struct stat st;
|
||||
void *map;
|
||||
size_t len;
|
||||
int fd = fileno (h);
|
||||
|
||||
if (fd < 0)
|
||||
goto fallback;
|
||||
|
||||
if (fstat (fd, &st))
|
||||
goto fallback;
|
||||
|
||||
len = (size_t) st.st_size;
|
||||
if ((off_t) len != st.st_size)
|
||||
goto fallback; /* Truncated. */
|
||||
|
||||
map = mmap (NULL, len, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (map == MAP_FAILED)
|
||||
goto fallback;
|
||||
|
||||
scheme_load_memory (sc, map, len, qualified_name);
|
||||
munmap (map, len);
|
||||
}
|
||||
else
|
||||
fallback:
|
||||
#endif
|
||||
scheme_load_named_file (sc, h, qualified_name);
|
||||
fclose (h);
|
||||
|
||||
if (sc->retcode && sc->nesting)
|
||||
|
@ -5693,14 +5693,18 @@ void scheme_load_named_file(scheme *sc, FILE *fin, const char *filename) {
|
||||
}
|
||||
|
||||
void scheme_load_string(scheme *sc, const char *cmd) {
|
||||
scheme_load_memory(sc, cmd, strlen(cmd), NULL);
|
||||
}
|
||||
|
||||
void scheme_load_memory(scheme *sc, const char *buf, size_t len, const char *filename) {
|
||||
dump_stack_reset(sc);
|
||||
sc->envir = sc->global_env;
|
||||
sc->file_i=0;
|
||||
sc->load_stack[0].kind=port_input|port_string;
|
||||
sc->load_stack[0].rep.string.start=(char*)cmd; /* This func respects const */
|
||||
sc->load_stack[0].rep.string.past_the_end=(char*)cmd+strlen(cmd);
|
||||
sc->load_stack[0].rep.string.curr=(char*)cmd;
|
||||
port_init_location(sc, &sc->load_stack[0], NULL);
|
||||
sc->load_stack[0].rep.string.start = (char *) buf; /* This func respects const */
|
||||
sc->load_stack[0].rep.string.past_the_end = (char *) buf + len;
|
||||
sc->load_stack[0].rep.string.curr = (char *) buf;
|
||||
port_init_location(sc, &sc->load_stack[0], filename ? mk_string(sc, filename) : NULL);
|
||||
sc->loadport=mk_port(sc,sc->load_stack);
|
||||
sc->retcode=0;
|
||||
sc->interactive_repl=0;
|
||||
|
@ -167,6 +167,8 @@ void scheme_set_output_port_string(scheme *sc, char *start, char *past_the_end);
|
||||
SCHEME_EXPORT void scheme_load_file(scheme *sc, FILE *fin);
|
||||
SCHEME_EXPORT void scheme_load_named_file(scheme *sc, FILE *fin, const char *filename);
|
||||
SCHEME_EXPORT void scheme_load_string(scheme *sc, const char *cmd);
|
||||
SCHEME_EXPORT void scheme_load_memory(scheme *sc, const char *buf, size_t len,
|
||||
const char *filename);
|
||||
SCHEME_EXPORT pointer scheme_apply0(scheme *sc, const char *procname);
|
||||
SCHEME_EXPORT pointer scheme_call(scheme *sc, pointer func, pointer args);
|
||||
SCHEME_EXPORT pointer scheme_eval(scheme *sc, pointer obj);
|
||||
|
Loading…
x
Reference in New Issue
Block a user