diff options
-rw-r--r-- | libstore/copy.c | 237 | ||||
-rw-r--r-- | libstore/gunzip.c | 277 | ||||
-rw-r--r-- | libstore/typed.c | 62 |
3 files changed, 576 insertions, 0 deletions
diff --git a/libstore/copy.c b/libstore/copy.c new file mode 100644 index 00000000..e5e83121 --- /dev/null +++ b/libstore/copy.c @@ -0,0 +1,237 @@ +/* Copy store backend + + Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + + Written by Miles Bader <miles@gnu.ai.mit.edu> + + This task is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ + +#include <stdio.h> +#include <string.h> +#include <malloc.h> + +#include "store.h" + +static error_t +copy_read (struct store *store, + off_t addr, size_t index, size_t amount, void **buf, size_t *len) +{ + if (*len < amount) + /* Have to allocate memory for the return value. */ + { + error_t err = + vm_allocate (mach_task_self (), (vm_address_t *)buf, amount, 1); + if (err) + return err; + } + + bcopy (store->hook + (addr * store->block_size), *buf, amount); + *len = amount; + return 0; +} + +static error_t +copy_write (struct store *store, + off_t addr, size_t index, void *buf, size_t len, size_t *amount) +{ + bcopy (buf, store->hook + (addr * store->block_size), len); + *amount = len; + return 0; +} + +error_t +copy_allocate_encoding (const struct store *store, struct store_enc *enc) +{ + /* ... */ + return EOPNOTSUPP; +} + +error_t +copy_encode (const struct store *store, struct store_enc *enc) +{ + /* ... */ + return EOPNOTSUPP; +} + +static error_t +copy_decode (struct store_enc *enc, const struct store_class *const *classes, + struct store **store) +{ + /* ... */ + return EOPNOTSUPP; +} + +static error_t +copy_open (const char *name, int flags, + const struct store_class *const *classes, + struct store **store) +{ + return store_copy_open (name, flags, classes, store); +} + +static error_t +copy_set_flags (struct store *store, int flags) +{ + if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0) + /* Trying to set flags we don't support. */ + return EINVAL; + + /* ... */ + + store->flags |= flags; /* When inactive, anything goes. */ + + return 0; +} + +static error_t +copy_clear_flags (struct store *store, int flags) +{ + error_t err = 0; + if ((flags & ~(STORE_INACTIVE | STORE_ENFORCED)) != 0) + err = EINVAL; + /* ... */ + if (! err) + store->flags &= ~flags; + return err; +} + +/* Called just before deallocating STORE. */ +void +copy_cleanup (struct store *store) +{ + if (store->size > 0) + vm_deallocate (mach_task_self (), (vm_address_t)store->hook, store->size); +} + +/* Copy any format-dependent fields in FROM to TO; if there's some reason + why the copy can't be made, an error should be returned. This call is + made after all format-indendependent fields have been cloned. */ +error_t +copy_clone (const struct store *from, struct store *to) +{ + error_t err = + vm_allocate (mach_task_self (), (vm_address_t *)&to->hook, to->size, 1); + if (! err) + bcopy (from->hook, to->hook, from->size); + return err; +} + +struct store_class +store_copy_class = +{ + STORAGE_COPY, "copy", copy_read, copy_write, + copy_allocate_encoding, copy_encode, copy_decode, + copy_set_flags, copy_clear_flags, copy_cleanup, copy_clone, 0, copy_open +}; + +/* Return a new store in STORE which contains a snapshot of the contents of + the store FROM; FROM is consumed. */ +error_t +store_copy_create (struct store *from, int flags, struct store **store) +{ + error_t err; + struct store_run run; + + run.start = 0; + run.length = from->size; + + flags |= STORE_ENFORCED; /* Only uses local resources. */ + + err = + _store_create (&store_copy_class, + MACH_PORT_NULL, flags, from->block_size, &run, 1, 0, + store); + if (! err) + { + size_t buf_len = 0; + + /* Copy the input store. */ + err = store_read (from, 0, from->size, &(*store)->hook, &buf_len); + + if (! err) + /* Set the store name. */ + { + if (from->name) + { + size_t len = + strlen (from->class->name) + 1 + strlen (from->name) + 1; + (*store)->name = malloc (len); + if ((*store)->name) + snprintf ((*store)->name, len, + "%s:%s", from->class->name, from->name); + } + else + (*store)->name = strdup (from->class->name); + + if (! (*store)->name) + err = ENOMEM; + } + + if (err) + store_free (*store); + } + + return err; +} + +/* Return a new store in STORE which contains the memory buffer BUF, of + length BUF_LEN, and uses the block size BLOCK_SIZE. BUF must be + vm_allocated, and will be consumed, and BUF_LEN must be a multiple of + BLOCK_SIZE. */ +error_t +store_buffer_create (void *buf, size_t buf_len, int flags, + struct store **store) +{ + error_t err; + struct store_run run; + + run.start = 0; + run.length = buf_len; + + flags |= STORE_ENFORCED; /* Only uses local resources. */ + + err = + _store_create (&store_copy_class, + MACH_PORT_NULL, flags, 1, &run, 1, 0, store); + if (! err) + (*store)->hook = buf; + + return err; +} + +/* Open the copy store NAME -- which consists of another store-class name, a + ':', and a name for that store class to open -- and return the + corresponding store in STORE. CLASSES is used to select classes specified + by the type name; if it is 0, STORE_STD_CLASSES is used. */ +error_t +store_copy_open (const char *name, int flags, + const struct store_class *const *classes, + struct store **store) +{ + struct store *from; + error_t err = + store_typed_open (name, flags | STORE_HARD_READONLY, classes, &from); + + if (! err) + { + err = store_copy_create (from, flags, store); + if (err) + store_free (from); + } + + return err; +} diff --git a/libstore/gunzip.c b/libstore/gunzip.c new file mode 100644 index 00000000..230ce1b0 --- /dev/null +++ b/libstore/gunzip.c @@ -0,0 +1,277 @@ +/* Decompressing store backend + + Copyright (C) 1997 Free Software Foundation, Inc. + Written by Miles Bader <miles@gnu.ai.mit.edu> + This file is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ + +#include <stdio.h> +#include <string.h> +#include <setjmp.h> +#include <cthreads.h> + +#include "store.h" + +/* gzip.h makes several annoying defines & decls, which we have to work + around. */ +#define file_t gzip_file_t +#include "gzip.h" +#undef file_t +#undef head + +#define IN_BUFFERING (256*1024) +#define OUT_BUFFERING (512*1024) + +static struct mutex unzip_lock = MUTEX_INITIALIZER; + +/* Uncompress the contents of FROM, which should contain a valid gzip file, + into memory, returning the result buffer in BUF & BUF_LEN. */ +static error_t +gunzip (struct store *from, void **buf, size_t *buf_len) +{ + /* Entry points to unzip engine. */ + int get_method (int); + extern long int bytes_out; + /* Callbacks from unzip for I/O and error interface. */ + extern int (*unzip_read) (char *buf, size_t maxread); + extern void (*unzip_write) (const char *buf, size_t nwrite); + extern void (*unzip_read_error) (void); + extern void (*unzip_error) (const char *msg); + + /* How we return errors from our hook functions. */ + jmp_buf zerr_jmp_buf; + error_t zerr; + + /* vm_alloced buffer for the input store. */ + void *in_buf = 0; + size_t in_buf_len = 0; + size_t in_buf_offs = 0; /* Offset of read point in IN_BUF. */ + off_t in_buf_addr = 0; /* Address in FROM of *next* IN_BUF. */ + + /* Buffer input in units that are least IN_BUFFERING bytes, but are also a + multiple of FROM's block size. */ + size_t in_addr_mask = ((1 << from->log2_block_size) - 1); + size_t in_buffering = ((IN_BUFFERING + in_addr_mask) & ~in_addr_mask); + + /* Read at most MAXREAD (or 0 if eof) bytes into BUF from our current + position in FROM. */ + int zread (char *buf, size_t maxread) + { + size_t did_read = 0; + + while (maxread > 0) + { + size_t left = in_buf_len - in_buf_offs; + + if (left > 0) + /* Fill BUF with what we can from IN_BUF. */ + { + if (left > maxread) + left = maxread; + bcopy (in_buf + in_buf_offs, buf, left); + in_buf_offs += left; + buf += left; + maxread -= left; + did_read += left; + } + + /* Limit MAXREAD to the number of bytes left in the input. */ + if (maxread > (from->size - (in_buf_addr << from->log2_block_size))) + maxread = from->size - (in_buf_addr << from->log2_block_size); + + if (maxread > 0) + /* Have to fill IN_BUF again. */ + { + void *new_in_buf = in_buf; + size_t new_in_buf_len = in_buf_len; + + zerr = store_read (from, in_buf_addr, in_buffering, + &new_in_buf, &new_in_buf_len); + if (zerr) + longjmp (zerr_jmp_buf, 1); + + in_buf_addr += (new_in_buf_len >> from->log2_block_size); + + if (new_in_buf != in_buf) + { + if (in_buf_len > 0) + vm_deallocate (mach_task_self (), + (vm_address_t)in_buf, in_buf_len); + in_buf = new_in_buf; + in_buf_len = new_in_buf_len; + } + + in_buf_offs = 0; + } + } + return did_read; + } + + size_t out_buf_offs = 0; /* Position in the output buffer. */ + + /* Write compress data to our output buffer. */ + void zwrite (const char *wbuf, size_t nwrite) + { + size_t old_buf_len = *buf_len; + + if (out_buf_offs + nwrite > old_buf_len) + /* Have to grow the output buffer. */ + { + void *old_buf = *buf; + void *new_buf = old_buf + old_buf_len; /* First try. */ + size_t new_buf_len = round_page (old_buf_len + old_buf_len + nwrite); + + /* Try to grow the buffer. */ + zerr = + vm_allocate (mach_task_self (), + (vm_address_t *)&new_buf, new_buf_len - old_buf_len, + 0); + if (zerr) + /* Can't do that, try to make a bigger buffer elsewhere. */ + { + new_buf = old_buf; + zerr = + vm_allocate (mach_task_self (), + (vm_address_t *)&new_buf, new_buf_len, 1); + if (zerr) + longjmp (zerr_jmp_buf, 1); + + if (out_buf_offs > 0) + /* Copy the old buffer into the start of the new & free it. */ + bcopy (old_buf, new_buf, out_buf_offs); + + vm_deallocate (mach_task_self (), + (vm_address_t)old_buf, old_buf_len); + + *buf = new_buf; + } + + *buf_len = new_buf_len; + } + + bcopy (wbuf, *buf + out_buf_offs, nwrite); + out_buf_offs += nwrite; + } + + void zreaderr (void) + { + zerr = EIO; + longjmp (zerr_jmp_buf, 1); + } + void zerror (const char *msg) + { + zerr = EINVAL; + longjmp (zerr_jmp_buf, 2); + } + + /* Try to guess a reasonable output buffer size. */ + *buf_len = round_page (from->size * 2); + zerr = vm_allocate (mach_task_self (), (vm_address_t *)buf, *buf_len, 1); + if (zerr) + return zerr; + + mutex_lock (&unzip_lock); + + unzip_read = zread; + unzip_write = zwrite; + unzip_read_error = zreaderr; + unzip_error = zerror; + + if (! setjmp (zerr_jmp_buf)) + { + if (get_method (0) != 0) + /* Not a happy gzip file. */ + zerr = EINVAL; + else + /* Matched gzip magic number. Ready to unzip. + Set up the output stream and let 'er rip. */ + { + /* Call the gunzip engine. */ + bytes_out = 0; + unzip (17, 23); /* Arguments ignored. */ + zerr = 0; + } + } + + mutex_unlock (&unzip_lock); + + if (in_buf_len > 0) + vm_deallocate (mach_task_self (), (vm_address_t)in_buf, in_buf_len); + + if (zerr) + { + if (*buf_len > 0) + vm_deallocate (mach_task_self (), (vm_address_t)*buf, *buf_len); + } + else if (out_buf_offs < *buf_len) + /* Trim the output buffer to be the right length. */ + { + size_t end = round_page (out_buf_offs); + if (end < *buf_len) + vm_deallocate (mach_task_self (), + (vm_address_t)(*buf + end), *buf_len - end); + *buf_len = out_buf_offs; + } + + return zerr; +} + +/* Return a new store in STORE which contains a snapshot of the uncompressed + contents of the store FROM; FROM is consumed. */ +error_t +store_gunzip_create (struct store *from, int flags, struct store **store) +{ + void *buf; + size_t buf_len; + error_t err = gunzip (from, &buf, &buf_len); + + if (! err) + { + err = store_buffer_create (buf, buf_len, flags, store); + if (err) + vm_deallocate (mach_task_self (), (vm_address_t)buf, buf_len); + else + store_free (from); + } + + return err; +} + +/* Open the gunzip NAME -- which consists of another store-class name, a ':', + and a name for that store class to open -- and return the corresponding + store in STORE. CLASSES is used to select classes specified by the type + name; if it is 0, STORE_STD_CLASSES is used. */ +error_t +store_gunzip_open (const char *name, int flags, + const struct store_class *const *classes, + struct store **store) +{ + struct store *from; + error_t err = + store_typed_open (name, flags | STORE_HARD_READONLY, classes, &from); + + if (! err) + { + err = store_gunzip_create (from, flags, store); + if (err) + store_free (from); + } + + return err; +} + +struct store_class +store_gunzip_class = { -1, "gunzip", open: store_gunzip_open }; diff --git a/libstore/typed.c b/libstore/typed.c new file mode 100644 index 00000000..09f939d1 --- /dev/null +++ b/libstore/typed.c @@ -0,0 +1,62 @@ +/* Support for opening `typed' stores + + Copyright (C) 1997 Free Software Foundation, Inc. + + Written by Miles Bader <miles@gnu.ai.mit.edu> + + This task is part of the GNU Hurd. + + The GNU Hurd is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or (at + your option) any later version. + + The GNU Hurd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */ + +#include <string.h> + +#include "store.h" + +/* Open the store indicated by NAME, which should consist of a store type + name followed by a ':' and any type-specific name, returning the new store + in STORE. CLASSES is used to select classes specified by the type name; + if it is 0, STORE_STD_CLASSES is used. */ +error_t +store_typed_open (const char *name, int flags, + const struct store_class *const *classes, + struct store **store) +{ + const struct store_class *const *cl; + const char *clname_end = strchr (name, ':'); + + if (! clname_end) + clname_end = name + strlen (name); + + if (! classes) + classes = store_std_classes; + for (cl = classes; *cl; cl++) + if (strlen ((*cl)->name) == (clname_end - name) + && strncmp (name, (*cl)->name, (clname_end - name)) == 0) + break; + + if (! *cl) + return EINVAL; + + if (! (*cl)->open) + return EOPNOTSUPP; + + if (*clname_end) + clname_end++; /* Skip the ':' */ + + return (*(*cl)->open) (clname_end, flags, classes, store); +} + +struct store_class +store_typed_open_class = { -1, "typed", open: store_typed_open }; |