/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <wait.h>
struct thr_args {
/* output pipe (compressor reads the other end of pipe) */
int (*readf)(int, char *);
};
static struct worker {
} *workers;
/*
* Given a file descriptor that sources a possibly compressed file, returns
* one that will source the uncompressed one. Supported compression types
* are bzip2, gzip and compress.
*
* if the input file is seekable:
* read the magic number from the file, rewind it.
*
* if compressed:
* fork and exec approp. decompressor as needed w/ stdin as
* the input. The stdout would be a pipe, the read end
* fd of which is returned to the caller.
*
* else: # not compressed
* return input fd
*
* else: # not seekable
* read the magic number from the input file.
*
* if compressed:
* fork and exec approp. decompressor as needed w/ stdin as
* the input. The stdout would be a pipe, the read end
* fd of which will be returned to the caller. Write header
* that was read from input file to stdin of decompressor.
* Spawn worker thread that copies remainder of input file to
* stdin of decompressor. Return stdout of decompressor to caller.
*
* else: # not compressed
* create pipe
* write header that was read from input file to pipe.
* spawn worker thread that copies remainder of input file to pipe.
* return other end of pipe to caller.
*
* Limitations:
*
* 1/ The routine provides no method of waiting for its children.
* This means zombie processes may be left if the caller doesn't exit
* shortly after finishing the reading and that callers that wait for
* children may find unexpected children exiting.
*
* 2/ There is no specific error handling. If it fails for any reason,
* it just returns -1.
*/
/*
* Check if we can seek fd w/ affecting it.
*/
static int
{
}
static int
{
}
/*
* Read potential header into magic from input fd;
* return decompressor needed, NULL if none.
*/
static const char *
{
return (NULL);
return (GZCAT);
return (BZCAT);
return (ZCAT);
} else {
return (NULL);
}
}
/*
* Pass contents of inputfd through command;
* return fd from which output may be read.
*/
static int
{
return (-1);
perror("fork");
return (-1);
}
if (pid == 0) {
closefrom(3);
return (-1);
}
perror("malloc failed");
return (-1);
}
return (fd[1]);
}
/*
* Worker thread for splice routine.
*/
static void *
{
int nread;
/*
* write the first buffer contents which was used to check
* the header magic.
*/
perror("splice: write");
return (NULL);
}
/*
* The other end of pipe may be closed in the middle of input
* stream. We should quit properly when it happened.
*/
(void) sigemptyset(&set);
if (nread < 0) {
perror("splice: read");
break;
}
perror("splice: write");
break;
}
}
/* Avoid closing source fd to avoid it's reuse. */
return (NULL);
}
/*
* Copy from input fd to output fd in separate thread.
*/
static int
{
perror("malloc failed");
return (-1);
}
perror("pthread_create");
return (-1);
}
perror("malloc failed");
return (-1);
}
return (0);
}
/*
* Returns fd from which contents of fd may be read,
* decompressing if needed. Works on non-seekable file
* descriptors. Returns -1 upon error.
*/
int
{
char *buffer;
const char *decompressor;
int rsz;
return (-1);
/* check if we need to decompress */
/* rewind to start of file */
return (-1);
if (decompressor != NULL) {
/*
* file is compressed; filter it through
* appropriate decompressor.
*/
}
return (fd);
} else { /* not seekable */
return (-1);
}
return (-1);
}
if (decompressor != NULL) {
} else {
return (fds[1]);
}
} else {
return (-1);
}
}
/*NOTREACHED*/
}
void
{
}
/*
* Other end of pipe may have been closed. We
* shouldn't write anything which induces SIGPIPE
*/
}
}
}