Esempio n. 1
0
void Writer::set_format_helper(struct archive *ar, int format)
{
    std::string error_msg;

    switch(format) {
        case Archive::FORMAT_AR_BSD:
            archive_write_set_format_ar_bsd(ar);
            break;
        case Archive::FORMAT_AR_SVR4:
            archive_write_set_format_ar_svr4(ar);
            break;
        case Archive::FORMAT_CPIO:
            archive_write_set_format_cpio(ar);
            break;
        case Archive::FORMAT_CPIO_NEWC:
            archive_write_set_format_cpio_newc(ar);
            break;
        case Archive::FORMAT_MTREE:
            archive_write_set_format_mtree(ar);
            break;
        case Archive::FORMAT_TAR:
        case Archive::FORMAT_TAR_PAX_RESTRICTED:
            archive_write_set_format_pax_restricted(ar);
            break;
        case Archive::FORMAT_TAR_PAX_INTERCHANGE:
            archive_write_set_format_pax(ar);
            break;
        case Archive::FORMAT_TAR_USTAR:
            archive_write_set_format_ustar(ar);
            break;
        default:
            error_msg = "unknown or unsupported archive format";
            throw Error(error_msg);
    }
}
/* ArchiveWriter::__construct {{{
 *
*/
ZEND_METHOD(ArchiveWriter, __construct)
{
	archive_file_t *arch = NULL;
	int resource_id;
	zval *this = getThis();
	const char *error_string = NULL;
	char *filename;
	long error_num, filename_len, result, format=0, compression=0;
	zend_error_handling error_handling;

	zend_replace_error_handling(EH_THROW, ce_ArchiveException, &error_handling TSRMLS_CC);

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &filename, &filename_len, &format, &compression) == FAILURE) {
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}

#if PHP_API_VERSION < 20100412
	if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}
#endif

	if (php_check_open_basedir(filename TSRMLS_CC)) {
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}

	arch = (archive_file_t *) emalloc(sizeof(archive_file_t));

	arch->stream = NULL;

	ALLOC_HASHTABLE(arch->entries);
	zend_hash_init(arch->entries, 10, NULL, _archive_entries_hash_dtor, 0);

	arch->mode = PHP_ARCHIVE_WRITE_MODE;
	arch->buf = emalloc(PHP_ARCHIVE_BUF_LEN + 1);
	arch->filename = estrndup(filename, filename_len);
	arch->arch = archive_write_new();

	switch (compression) {
		case PHP_ARCHIVE_COMPRESSION_GZIP:
			if (archive_write_add_filter_gzip(arch->arch) != ARCHIVE_OK) {
				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Gzip compression is not supported in this build");
				zend_restore_error_handling(&error_handling TSRMLS_CC);
				return;
			}
			break;

		case PHP_ARCHIVE_COMPRESSION_BZIP2:
			if (archive_write_add_filter_bzip2(arch->arch) != ARCHIVE_OK) {
				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bzip2 compression is not supported in this build");
				zend_restore_error_handling(&error_handling TSRMLS_CC);
				return;
			}
			break;
		case 0: /* default value */
		case PHP_ARCHIVE_COMPRESSION_NONE:
			/* always supported */
			break;
		default:
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported compression type %ld", compression);
			zend_restore_error_handling(&error_handling TSRMLS_CC);
			return;
			break;
	}

	switch (format) {
		case 0: /* default value */
		case PHP_ARCHIVE_FORMAT_TAR:
		case PHP_ARCHIVE_FORMAT_PAX_RESTRICTED:
			archive_write_set_format_pax_restricted(arch->arch);
			break;
		case PHP_ARCHIVE_FORMAT_PAX:
			archive_write_set_format_pax(arch->arch);
			break;
		case PHP_ARCHIVE_FORMAT_CPIO:
			archive_write_set_format_cpio(arch->arch);
			break;
		case PHP_ARCHIVE_FORMAT_SHAR:
			archive_write_set_format_shar(arch->arch);
			break;
		case PHP_ARCHIVE_FORMAT_USTAR:
			archive_write_set_format_ustar(arch->arch);
			break;
		default:
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported archive format: %ld", format);
			zend_restore_error_handling(&error_handling TSRMLS_CC);
			return;
			break;
	}

	archive_write_set_bytes_per_block(arch->arch, DEFAULT_BYTES_PER_BLOCK);
	result = archive_write_open(arch->arch, arch, _archive_open_clbk, _archive_write_clbk, _archive_close_clbk);
	/* do not pad the last block */
	archive_write_set_bytes_in_last_block(arch->arch, 1);

	if (result) {
		error_num = archive_errno(arch->arch);
		error_string = archive_error_string(arch->arch);
		efree(arch->filename);
		efree(arch->buf);
		efree(arch);
		if (error_num && error_string) {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: error #%ld, %s", filename, error_num, error_string);
		}
		else {
			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to open file %s for writing: unknown error %ld", filename, result);
		}
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}

	resource_id = zend_list_insert(arch,le_archive);
	add_property_resource(this, "fd", resource_id);

	zend_restore_error_handling(&error_handling TSRMLS_CC);
	return;
}
Esempio n. 3
0
int archive_create2( char *arch_file, int compress, int format, char **files )
{
    int                     fd, len, ret;
    struct archive          *arch_w = NULL, *arch_r;
    struct archive_entry    *arch_entry = NULL;
    char                    buf[8192];

    if( !arch_file || !files )
        return -1;

    arch_w = archive_write_new();

    switch( compress )
    {
        case 'j': //bz2
            //archive_write_add_filter_bzip2( arch_w );
            archive_write_set_compression_bzip2( arch_w );
            break;

        case 'J': //xz
            //archive_write_add_filter_xz( arch_w );
            archive_write_set_compression_xz( arch_w );
            break;

        case 'z': //gzip
            //archive_write_add_filter_gzip( arch_w );
            archive_write_set_compression_gzip( arch_w );
            break;
    }

    switch( format )
    {
        case 'c': //cpio
            archive_write_set_format_cpio( arch_w );
            break;

        case 't': //tar
            archive_write_set_format_ustar( arch_w );
            break;

        default: //tar
            archive_write_set_format_ustar( arch_w );
            break;
    }

    ret = archive_write_open_filename( arch_w, arch_file );
    if( ret != ARCHIVE_OK )
    {
        archive_write_finish( arch_w );
        return -1;
    }

    while( *files )
    {
        arch_r = archive_read_disk_new();
        ret = archive_read_disk_open( arch_r, *files );
        if( ret != ARCHIVE_OK )
        {
#ifdef DEBUG
            printf( "%s\n", archive_error_string( arch_r ) );
#endif
            archive_write_finish( arch_w );
            return -1;
        }

        for( ; ; )
        {
            arch_entry = archive_entry_new();
            ret = archive_read_next_header2( arch_r, arch_entry );
            if( ret == ARCHIVE_EOF )
                break;
            
            if( ret != ARCHIVE_OK )
            {
#ifdef DEBUG
                printf( "%s\n", archive_error_string( arch_r ) );
#endif
                archive_entry_free( arch_entry );
                archive_write_finish( arch_r );
                archive_write_finish( arch_w );
                return -1;
            }

            archive_read_disk_descend( arch_r );

            ret = archive_write_header( arch_w, arch_entry );
            if( ret < ARCHIVE_OK )
            {
#ifdef DEBUG
                printf( "%s\n", archive_error_string( arch_w ) );
#endif
                archive_entry_free( arch_entry );
                archive_write_finish( arch_r );
                archive_write_finish( arch_w );
                return -1;
            }

            if( ( fd = open( archive_entry_sourcepath( arch_entry ), O_RDONLY ) ) != -1 )
            {
                len = read( fd, buf, 8192 );
                while( len > 0 )
                {
                    archive_write_data( arch_w, buf, len );
                    len = read( fd, buf, 8192 );
                }
                close( fd );
            }
            archive_entry_free( arch_entry );
        }
        /* libarchive 3
        archive_read_close( arch_r );
        archive_read_free( arch_r );
        */
        archive_read_finish( arch_r );
    }

    if( arch_w )
        archive_write_finish( arch_w );

    return 0;
}
Esempio n. 4
0
int archive_create( char *arch_file, int compress, int format, char *src_dir, char **exclude )
{
    int                     fd, len, ret, skip;
    char                    *pwd, **files;
    DIR                     *dir;
    struct dirent           *dir_entry;
    struct archive          *arch_w = NULL, *arch_r = NULL;
    struct archive_entry    *arch_entry = NULL;
    char                    buf[8192];

    if( !arch_file || !src_dir )
        return -1;


    dir = opendir( src_dir );
    if( !dir )
        return -1;


    arch_w = archive_write_new();

    switch( compress )
    {
        case 'j': //bz2
            //archive_write_add_filter_bzip2( arch_w ); //libarchive 3
            archive_write_set_compression_bzip2( arch_w );
            break;

        case 'J': //xz
            //archive_write_add_filter_xz( arch_w );
            archive_write_set_compression_xz( arch_w );
            break;

        case 'z': //gzip
            //archive_write_add_filter_gzip( arch_w );
            archive_write_set_compression_gzip( arch_w );
            break;
    }

    switch( format )
    {
        case 'c': //cpio
            archive_write_set_format_cpio( arch_w );
            break;

        case 't': //tar
            archive_write_set_format_ustar( arch_w );
            break;

        default: //tar
            archive_write_set_format_ustar( arch_w );
            break;
    }



    ret = archive_write_open_filename( arch_w, arch_file );
    if( ret != ARCHIVE_OK )
    {
        archive_write_finish( arch_w );
        return -1;
    }

    pwd = getcwd( NULL, 0 );
    chdir( src_dir );

    while( (dir_entry = readdir( dir )) )
    {
        if( !strcmp( dir_entry->d_name, "." ) || !strcmp( dir_entry->d_name, ".." ) )
        {
            continue;
        }

        if( exclude )
        {
            files = exclude;
            skip = 0;
            while( *files )
            {
                if( !strcmp( *files, dir_entry->d_name ) )
                {
                    skip = 1;
                    break;
                }

                files++;
            }

            if( skip )
                continue;
        }

        arch_r = archive_read_disk_new();
        ret = archive_read_disk_open( arch_r, dir_entry->d_name );
        if( ret != ARCHIVE_OK )
        {
#ifdef DEBUG
            printf( "%s\n", archive_error_string( arch_r ) );
#endif
            archive_write_finish( arch_w );
            return -1;
        }

        for( ; ; )
        {
            arch_entry = archive_entry_new();
            ret = archive_read_next_header2( arch_r, arch_entry );
            if( ret == ARCHIVE_EOF )
                break;
            
            if( ret != ARCHIVE_OK )
            {
#ifdef DEBUG
                printf( "%s\n", archive_error_string( arch_r ) );
#endif
                archive_entry_free( arch_entry );
                archive_write_finish( arch_r );
                archive_write_finish( arch_w );
                return -1;
            }

#ifdef DEBUG
            printf( "%s\n", archive_entry_pathname( arch_entry ) );
#endif

            archive_read_disk_descend( arch_r );

            ret = archive_write_header( arch_w, arch_entry );
            if( ret < ARCHIVE_OK )
            {
#ifdef DEBUG
                printf( "%s\n", archive_error_string( arch_w ) );
#endif
                archive_entry_free( arch_entry );
                archive_write_finish( arch_r );
                archive_write_finish( arch_w );
                return -1;
            }

            if( ( fd = open( archive_entry_sourcepath( arch_entry ), O_RDONLY ) ) != -1 )
            {
                len = read( fd, buf, 8192 );
                while( len > 0 )
                {
                    archive_write_data( arch_w, buf, len );
                    len = read( fd, buf, 8192 );
                }
                close( fd );
            }
            archive_entry_free( arch_entry );
        }
        /* libarchive 3
        archive_read_close( arch_r );
        archive_read_free( arch_r );
        */
        archive_read_finish( arch_r );

    }



    if( arch_w )
    {
        /* libarchive 3
        archive_write_close( arch_r );
        archive_write_free( arch_r );
        */
        archive_write_finish( arch_w );
    }

    if( pwd )
    {
        chdir( pwd );
        free( pwd );
    }
    return 0;
}
Esempio n. 5
0
const gchar* archive_create(const char* archive_name, GSList* files,
			COMPRESS_METHOD method, ARCHIVE_FORMAT format) {
	struct archive* arch;
	struct archive_entry* entry;
	char* buf = NULL;
	ssize_t len;
	int fd;
	struct stat st;
	struct file_info* file;
	gchar* filename = NULL;
	gchar* msg = NULL;

#ifndef _TEST
	gint num = 0;
	gint total = g_slist_length (files);
#endif

	g_return_val_if_fail(files != NULL, "No files for archiving");

	debug_print("File: %s\n", archive_name);
	arch = archive_write_new();
	switch (method) {
		case ZIP:
			if (archive_write_set_compression_gzip(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
		case BZIP2:
			if (archive_write_set_compression_bzip2(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
#if NEW_ARCHIVE_API
		case COMPRESS:
			if (archive_write_set_compression_compress(arch) != ARCHIVE_OK)
    			        return archive_error_string(arch);
			break;
#endif
		case NO_COMPRESS:
			if (archive_write_set_compression_none(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
	}
	switch (format) {
		case TAR:
			if (archive_write_set_format_ustar(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
		case SHAR:
			if (archive_write_set_format_shar(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
		case PAX:
			if (archive_write_set_format_pax(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
		case CPIO:
			if (archive_write_set_format_cpio(arch) != ARCHIVE_OK)
				return archive_error_string(arch);
			break;
		case NO_FORMAT:
			return "Missing archive format";
	}
	if (archive_write_open_file(arch, archive_name) != ARCHIVE_OK)
		return archive_error_string(arch);

	while (files && ! stop_action) {
#ifndef _TEST
		set_progress_print_all(num++, total, 30);
#endif
		file = (struct file_info *) files->data;
		if (!file)
			continue;
		filename = get_full_path(file);
		/* libarchive will crash if instructed to add archive to it self */
		if (g_utf8_collate(archive_name, filename) == 0) {
			buf = NULL;
			buf = g_strdup_printf(
						"%s: Not dumping to %s", archive_name, filename);
			g_warning("%s\n", buf);
#ifndef _TEST
			debug_print("%s\n", buf);
#endif
			g_free(buf);
		}
		else {
#ifndef _TEST
			debug_print("Adding: %s\n", filename);
			msg = g_strdup_printf("%s", filename);
			set_progress_file_label(msg);
			g_free(msg);
#endif
			entry = archive_entry_new();
			lstat(filename, &st);
			if ((fd = open(filename, O_RDONLY)) == -1) {
				perror("open file");
			}
			else {
				archive_entry_copy_stat(entry, &st);
				archive_entry_set_pathname(entry, filename);
				if (S_ISLNK(st.st_mode)) {
					buf = NULL;
					buf = malloc(PATH_MAX + 1);
					if ((len = readlink(filename, buf, PATH_MAX)) < 0)
						perror("error in readlink");
					else
						buf[len] = '\0';
					archive_entry_set_symlink(entry, buf);
					g_free(buf);
					archive_entry_set_size(entry, 0);
					archive_write_header(arch, entry);
				}
				else {
					if (archive_write_header(arch, entry) != ARCHIVE_OK)
						g_warning("%s", archive_error_string(arch));
					buf = NULL;
					buf = malloc(READ_BLOCK_SIZE);
					len = read(fd, buf, READ_BLOCK_SIZE);
					while (len > 0) {
						if (archive_write_data(arch, buf, len) == -1)
							g_warning("%s", archive_error_string(arch));
						memset(buf, 0, READ_BLOCK_SIZE);
						len = read(fd, buf, READ_BLOCK_SIZE);
					}
					g_free(buf);
				}
				close(fd);
				archive_entry_free(entry);
			}
		}
		g_free(filename);
		files = g_slist_next(files);
	}
#ifndef _TEST
	if (stop_action)
		unlink(archive_name);
	stop_action = FALSE;
#endif
	archive_write_close(arch);
	archive_write_finish(arch);
	return NULL;
}