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); }
FILE* gbMustOpenOutput(char* path) /* Open an output file for atomic file creation. Create the directory if it * doesn't exist. If the path has one of the compress extensions (.gz, .Z, * .bz2) the output is compressed. File is opened under a tmp name until * installed. */ { char tmpPath[PATH_LEN]; gbGetOutputTmp(path, tmpPath); gbMakeFileDirs(tmpPath); return gzMustOpen(tmpPath, "w"); }
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); } }
FILE *openSortOutput(char *fileName, char **sortSpec) /* Open a pipeline to sort and compress output. SortSpec is null-terminated * list of fields options to pass to sort. gSortTmpDir maybe null. * gbOutputRename is used to install and close. */ { struct gbPipeline *pipeline; char tmpPath[PATH_LEN]; char **compressor; char **sortCmd, **cmds[3]; int nSort, iSort = 0, i; /* sort cmd, nSort includes cmd and NULL */ for (nSort = 0; sortSpec[nSort] != NULL; nSort++) continue; if (gSortTmp != NULL) nSort += 2; nSort += 2; /* for "sort" and NULL */ sortCmd = alloca(nSort*sizeof(char*)); sortCmd[iSort++] = "sort"; if (gSortTmp != NULL) { sortCmd[iSort++] = "-T"; sortCmd[iSort++] = gSortTmp; } for (i = 0; sortSpec[i] != NULL; i++) sortCmd[iSort++] = sortSpec[i]; sortCmd[iSort] = NULL; assert(iSort < nSort); cmds[0] = sortCmd; cmds[1] = NULL; /* compress process, if needed */ compressor = gbGetCompressor(fileName, "w"); if (compressor != NULL) { cmds[1] = compressor; cmds[2] = NULL; } /* tmp name to output to */ gbGetOutputTmp(fileName, tmpPath); gbMakeFileDirs(tmpPath); pipeline = gbPipelineCreate(cmds, PIPELINE_WRITE|PIPELINE_FDOPEN, NULL, tmpPath); return gbPipelineFile(pipeline); }