archive_write_add_filter_grzip.c revision 256281
1169691Skan/*- 2169691Skan * Copyright (c) 2003-2007 Tim Kientzle 3169691Skan * All rights reserved. 4169691Skan * 5169691Skan * Redistribution and use in source and binary forms, with or without 6169691Skan * modification, are permitted provided that the following conditions 7169691Skan * are met: 8169691Skan * 1. Redistributions of source code must retain the above copyright 9169691Skan * notice, this list of conditions and the following disclaimer. 10169691Skan * 2. Redistributions in binary form must reproduce the above copyright 11169691Skan * notice, this list of conditions and the following disclaimer in the 12169691Skan * documentation and/or other materials provided with the distribution. 13169691Skan * 14169691Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15169691Skan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16169691Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17169691Skan * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18169691Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19169691Skan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20169691Skan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21169691Skan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22169691Skan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23169691Skan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24169691Skan */ 25169691Skan 26169691Skan#include "archive_platform.h" 27169691Skan 28169691Skan__FBSDID("$FreeBSD$"); 29169691Skan 30169691Skan#ifdef HAVE_ERRNO_H 31169691Skan#include <errno.h> 32169691Skan#endif 33169691Skan#ifdef HAVE_STDLIB_H 34169691Skan#include <stdlib.h> 35169691Skan#endif 36169691Skan 37169691Skan#include "archive.h" 38169691Skan#include "archive_write_private.h" 39169691Skan 40169691Skanstruct write_grzip { 41169691Skan struct archive_write_program_data *pdata; 42169691Skan}; 43169691Skan 44169691Skanstatic int archive_write_grzip_open(struct archive_write_filter *); 45169691Skanstatic int archive_write_grzip_options(struct archive_write_filter *, 46169691Skan const char *, const char *); 47169691Skanstatic int archive_write_grzip_write(struct archive_write_filter *, 48169691Skan const void *, size_t); 49169691Skanstatic int archive_write_grzip_close(struct archive_write_filter *); 50169691Skanstatic int archive_write_grzip_free(struct archive_write_filter *); 51169691Skan 52169691Skanint 53169691Skanarchive_write_add_filter_grzip(struct archive *_a) 54169691Skan{ 55169691Skan struct archive_write_filter *f = __archive_write_allocate_filter(_a); 56169691Skan struct write_grzip *data; 57169691Skan 58169691Skan archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, 59169691Skan ARCHIVE_STATE_NEW, "archive_write_add_filter_grzip"); 60169691Skan 61169691Skan data = calloc(1, sizeof(*data)); 62169691Skan if (data == NULL) { 63169691Skan archive_set_error(_a, ENOMEM, "Can't allocate memory"); 64169691Skan return (ARCHIVE_FATAL); 65169691Skan } 66169691Skan data->pdata = __archive_write_program_allocate(); 67169691Skan if (data->pdata == NULL) { 68169691Skan free(data); 69169691Skan archive_set_error(_a, ENOMEM, "Can't allocate memory"); 70169691Skan return (ARCHIVE_FATAL); 71169691Skan } 72169691Skan 73169691Skan f->name = "grzip"; 74169691Skan f->code = ARCHIVE_FILTER_GRZIP; 75169691Skan f->data = data; 76 f->open = archive_write_grzip_open; 77 f->options = archive_write_grzip_options; 78 f->write = archive_write_grzip_write; 79 f->close = archive_write_grzip_close; 80 f->free = archive_write_grzip_free; 81 82 /* Note: This filter always uses an external program, so we 83 * return "warn" to inform of the fact. */ 84 archive_set_error(_a, ARCHIVE_ERRNO_MISC, 85 "Using external grzip program for grzip compression"); 86 return (ARCHIVE_WARN); 87} 88 89static int 90archive_write_grzip_options(struct archive_write_filter *f, const char *key, 91 const char *value) 92{ 93 (void)f; /* UNUSED */ 94 (void)key; /* UNUSED */ 95 (void)value; /* UNUSED */ 96 /* Note: The "warn" return is just to inform the options 97 * supervisor that we didn't handle it. It will generate 98 * a suitable error if no one used this option. */ 99 return (ARCHIVE_WARN); 100} 101 102static int 103archive_write_grzip_open(struct archive_write_filter *f) 104{ 105 struct write_grzip *data = (struct write_grzip *)f->data; 106 107 return __archive_write_program_open(f, data->pdata, "grzip"); 108} 109 110static int 111archive_write_grzip_write(struct archive_write_filter *f, 112 const void *buff, size_t length) 113{ 114 struct write_grzip *data = (struct write_grzip *)f->data; 115 116 return __archive_write_program_write(f, data->pdata, buff, length); 117} 118 119static int 120archive_write_grzip_close(struct archive_write_filter *f) 121{ 122 struct write_grzip *data = (struct write_grzip *)f->data; 123 124 return __archive_write_program_close(f, data->pdata); 125} 126 127static int 128archive_write_grzip_free(struct archive_write_filter *f) 129{ 130 struct write_grzip *data = (struct write_grzip *)f->data; 131 132 __archive_write_program_free(data->pdata); 133 free(data); 134 return (ARCHIVE_OK); 135} 136