Beispiel #1
0
/**
 * Compress data from fd_source into fd_dest.
 *
 * @param fd_source	Source file descriptor
 * @param fd_dest	Destination file descriptor
 * @return 		Number of bytes readed from fd_source or -1 on error
 */
static off_t gzCompress(void *cancel_cookie, int fd_source, int fd_dest)
{
	char buf[BUF_SIZE];
	int wr;
	int rd;
	gzFile fd_gz = NULL;
	off_t size = 0;
	int dup_fd;

	dup_fd = dup(fd_dest);
	if (dup_fd == -1)
	{
		return (off_t) FAIL;
	}

	fd_gz = gzdopen(dup_fd, COMPRESSLEVEL_BACKGROUND);
	if (fd_gz == NULL)
	{
		file_close(&dup_fd);
		return (off_t) FAIL;
	}

	while ((rd = read(fd_source, buf, sizeof(buf))) > 0)
	{
		size += rd;

		wr = gzwrite(fd_gz, buf, rd);
		if(wr == -1)
		{
			gzClose(fd_gz);
			return (off_t) FAIL;
		}

		if (compress_testcancel(cancel_cookie))
		{
			break;
		}
	}

	if (rd < 0)
	{
		gzClose(fd_gz);
		return (off_t) FAIL;
	}
	
	gzClose(fd_gz);
	
	return size;
}
void loadMgcStatus(struct sqlConnection *conn, char *mgcStatusTab, char *statusTblName)
/* load the mgcStatus or mgcFullStatus tables, return name loaded */
{
struct lineFile* inLf;
FILE *outFh;
char tmpFile[PATH_LEN];
gbVerbEnter(2, "loading %s", statusTblName);

/* uncompress to tmp file */
safef(tmpFile, sizeof(tmpFile), "%s/mgcStatus.%s.%d.tmp", workDir,
      getHost(), getpid());
inLf = gzLineFileOpen(mgcStatusTab);
outFh = gzMustOpen(tmpFile, "w");

while (mgcStatusTblCopyRow(inLf, outFh))
    continue;

gzClose(&outFh);
gzLineFileClose(&inLf);

mgcStatusTblCreate(conn, statusTblName);

sqlLoadTabFile(conn, tmpFile, statusTblName, SQL_TAB_FILE_ON_SERVER);
unlink(tmpFile);

gbVerbLeave(2, "loading %s", statusTblName);
}
Beispiel #3
0
 void gzifstream::close()
 {
   if (f)
   {
     gzClose();
     fclose(f);
     f = NULL;
   }
 }
Beispiel #4
0
/**
 * Decompress data from fd_source into fd_dest.
 *
 * @param fd_source	Source file descriptor
 * @param fd_dest	Destination file descriptor
 * @return 		Number of bytes written to fd_dest or (off_t)-1 on error
 */
static off_t gzDecompress(int fd_source, int fd_dest)
{
	int wr;
	int rd;
	char buf[BUF_SIZE];
	off_t size = 0;
	gzFile fd_gz;
	int dup_fd;

	dup_fd = dup(fd_source);
	if (dup_fd == -1)
	{
		return (off_t) FAIL;
	}

	fd_gz = gzdopen(dup_fd, "rb");
	if (fd_gz == NULL)
	{
		file_close(&dup_fd);
		return (off_t) FAIL;
	}

	while ((rd = gzread(fd_gz, buf, BUF_SIZE)) > 0)
	{
		wr = write(fd_dest, buf, rd);
		if(wr == -1)
		{
			gzClose(fd_gz);
			return (off_t) FAIL;
		}
		size += wr;
	}

	if (rd == -1)
	{
		gzClose(fd_gz);
		return (off_t) FAIL;
	}
	
	gzClose(fd_gz);
	return size;
}
Beispiel #5
0
void gbOutputRemove(char* path, FILE** fhPtr)
/* Remove an output file instead of installing it. If fh is not NULL, the file
 * is also closed. */
{
char tmpPath[PATH_LEN];
gbGetOutputTmp(path, tmpPath);

if (fhPtr != NULL)
    gzClose(fhPtr);
unlink(tmpPath);    
}
Beispiel #6
0
  void gzofstream::close()
  {
    if (f)
    {
      bool write_error = !gzClose();

      fclose(f);
      f = NULL;

      if (write_error)
        throw FileWriteError(gz_filename);
    }
  }
Beispiel #7
0
void gbOutputRename(char* path, FILE** fhPtr)
/* Install an output file create by gbOutputOpen.  If fh is not NULL, the file
 * is also closed.  If closed separately, must use gzClose. */
{
char tmpPath[PATH_LEN];
gbGetOutputTmp(path, tmpPath);

if (fhPtr != NULL)
    gzClose(fhPtr);
if (!sameString(path, tmpPath))
    {
    if (rename(tmpPath, path) < 0)
        errnoAbort("renaming %s to %s", tmpPath, path);
    }
}
void gbFaClose(struct gbFa **faPtr)
/* close a fasta file, check for any undetected I/O errors. */
{
struct gbFa *fa = *faPtr;
if (fa != NULL)
    {
    if (ferror(fa->fh))
        errAbort("%s error on %s", ((fa->mode[0] != 'r') ? "write" : "read"),
                 fa->fileName);
    if (fa->mode[0] == 'w')
        gbOutputRename(fa->fileName, &fa->fh);
    else
        gzClose(&fa->fh);
    freez(&fa->headerBuf);
    freez(&fa->seqBuf);
    freez(&fa->fhBuf);
    freez(faPtr);  /* NULLs var */
    }
}
Beispiel #9
0
void oiDataClose()
/* close the output file */
{
gzClose(&gOutOi);
}
Beispiel #10
0
void extractAccFromGb(char *inName, char* outName, struct hash *accTbl)
/* Parse records of genBank file and print ones that match accession names.
 * (yanked from gbOneAcc, changed to use stdio so we can access compressed). */
{
enum {maxHeadLines=20, headLineSize=256 };
char *headLines[maxHeadLines];	/* Store stuff between locus and accession. */
char line[headLineSize];
FILE *inFh;
FILE *outFh = NULL;
int lineNum = 0;
int i;
char* acc;

verbose(1, "copying from %s\n", inName);

inFh = gzMustOpen(inName, "r");

for (i=0; i<maxHeadLines; ++i)
    headLines[i] = needMem(headLineSize);

while (TRUE)
    {
    boolean gotAcc = FALSE;
    boolean gotMyAcc = FALSE;
    int headLineCount = 0;
    /* Seek to LOCUS */
    for (;;)
	{
	if (!readData(inFh, inName, line, headLineSize, FALSE))
	    break;
        lineNum++;
	if (startsWith("LOCUS", line))
	    break;
	}
    if (feof(inFh))
        break;
    for (i=0; i<maxHeadLines; ++i)
	{
	++headLineCount;
	strcpy(headLines[i], line);
	readData(inFh, inName, line, headLineSize, TRUE);
        lineNum++;
	if (startsWith("ACCESSION", line))
	    {
	    gotAcc = TRUE;
	    break;
	    }
	}
    if (!gotAcc)
	errAbort("LOCUS without ACCESSION in %d lines at line %d of %s",
                 maxHeadLines, lineNum, inName);
    acc = lastWordInLine(line);
    gotMyAcc = (hashLookup(accTbl, acc) != NULL);
    if (gotMyAcc)
	{
        if (outFh == NULL)
            outFh = gbMustOpenOutput(outName);
	for (i=0; i<headLineCount; ++i)
	    {
	    fputs(headLines[i], outFh);
	    fputc('\n', outFh);
	    }
	fputs(line, outFh);
	fputc('\n', outFh);
	}
    for (;;)
	{
	readData(inFh, inName, line, headLineSize, TRUE);
        lineNum++;
	if (gotMyAcc)
	    {
	    fputs(line, outFh);
	    fputc('\n', outFh);
	    }
	if (startsWith("//", line))
	    break;
	}
    if ((outFh != NULL) && ferror(outFh))
        break;  /* write error */
    }
if (outFh != NULL)
    gbOutputRename(outName, &outFh);
gzClose(&inFh);
}