Пример #1
0
LIB_EXPORT rc_t CC WriteNamelistToFileByName( const VNamelist * namelist, const char * filename,
        const char * delim )
{
    rc_t rc;
    if ( namelist == NULL || filename == NULL || delim == NULL )
        rc = RC( rcFS, rcFile, rcValidating, rcParam, rcNull );
    else
    {
        KDirectory * dir;
        rc = KDirectoryNativeDir ( &dir );
        if ( rc == 0 )
        {
            KFile * f;
            rc = KDirectoryCreateFile ( dir, &f, true, 0664, kcmCreate, filename );
            if ( rc == 0 )
            {
                if ( rc == 0 )
                    rc = SaveToFile( f, namelist, delim );
                KFileRelease ( f );
            }
            KDirectoryRelease ( dir );
        }
    }
    return rc;
}
Пример #2
0
struct rna_splice_log * make_rna_splice_log( const char * filename, const char * toolname )
{
    struct rna_splice_log * res = NULL;
    KDirectory * dir;
    rc_t rc = KDirectoryNativeDir ( &dir );
    if ( rc == 0 )
    {
        KFile * f;
        rc = KDirectoryCreateFile ( dir, &f, false, 0664, kcmInit, "%s", filename );
        if ( rc == 0 )
        {
            res = calloc( 1, sizeof * res );
            if ( res != NULL )
            {
                res->log_file = f;
                if ( toolname != NULL )
                    res->tool_name = string_dup_measure ( toolname, NULL );
            }
            else
                KFileRelease ( f );
        }
        KDirectoryRelease ( dir );
    }
    return res;
}
Пример #3
0
static rc_t prepare_decompress( const compress_context * ctx,
                                KDirectory *dir, const KFile **src, KFile **dst )
{
    const KFile *temp = NULL;

    rc_t rc = KDirectoryCreateFile ( dir, dst, false, 0664, kcmInit, ctx->dst_file );
    DISP_RC( rc, "KDirectoryCreateFile() failed" );
    if ( rc != 0 ) return rc;

    rc = KDirectoryOpenFileRead ( dir, &temp, ctx->src_file );
    DISP_RC( rc, "KDirectoryOpenFileRead() failed" );
    if ( rc == 0 )
        switch( ctx->method )
        {
        case KC_GZIP : rc = KFileMakeGzipForRead ( src, temp );
                       DISP_RC( rc, "KFileMakeGzipForRead() failed" );
                       break;
        case KC_BZIP : rc = KFileMakeBzip2ForRead ( src, temp );
                       DISP_RC( rc, "KFileMakeBzip2ForRead() failed" );
                       break;
        case KC_SZIP : /* rc = KFileMakeSzipForRead ( src, temp ); */
                       rc = RC( rcExe, rcFile, rcPacking, rcFormat, rcUnknown );
                       DISP_RC( rc, "KFileMakeSzip2ForRead() failed" );
                       break;
        }
    KFileRelease ( temp );
    return rc;
}
Пример #4
0
/* CreateLockFile
 *  attempts to create a KLockFile
 *
 *  "lock" [ OUT ] - return parameter for newly created lock file
 *
 *  "path" [ IN ] - NUL terminated string in directory-native
 *  character set denoting lock file
 */
LIB_EXPORT rc_t CC KDirectoryVCreateLockFile ( KDirectory *self,
    KLockFile **lock, const char *path, va_list args )
{
    rc_t rc;

    if ( lock == NULL )
        rc = RC ( rcFS, rcFile, rcLocking, rcParam, rcNull );
    else
    {
        if ( self == NULL )
            rc = RC ( rcFS, rcFile, rcLocking, rcSelf, rcNull );
        else if ( path == NULL )
            rc = RC ( rcFS, rcFile, rcLocking, rcPath, rcNull );
        else if ( path [ 0 ] == 0 )
            rc = RC ( rcFS, rcFile, rcLocking, rcPath, rcEmpty );
        else
        {
            char full [ 4096 ];
            rc = KDirectoryVResolvePath ( self, false, full, sizeof full, path, args );
            if ( rc == 0 )
            {
                KFile *lock_file;
                rc = KDirectoryCreateFile ( self, & lock_file, false, 0600, kcmCreate | kcmParents, "%s", full );
                if ( rc == 0 )
                {
                    rc_t rc2;

                    /* no longer need the file - not going to write to it anyway */
                    KFileRelease ( lock_file );

                    /* we have the lock */
                    rc = KLockFileMake ( lock, self, full );
                    if ( rc == 0 )
                        return 0;

                    /* must unlink lockfile */
                    rc2 = KDirectoryRemove ( self, true, "%s", full );
                    if ( rc2 != 0 )
                        /* issue a report */;
                }
                else if ( GetRCState ( rc ) == rcExists )
                {
                    /* map the rc to kproc type values */
                    rc = RC ( rcFS, rcFile, rcLocking, rcLocking, rcBusy );
                }
                else
                {
                    rc = ResetRCContext ( rc, rcFS, rcFile, rcLocking );
                }
            }
        }

        * lock = NULL;
    }

    return rc;
}
Пример #5
0
static
rc_t CopierMakeF (Copier * cp, KDirectory * dir, const char * path)
{
    rc_t rc, orc;
    KFile * fm;

    rc = KDirectoryCreateFile (dir, &fm, true, 0666, kcmCreate|kcmParents,
				"%s.md5", path);
    if (rc == 0)
    {
	KMD5SumFmt * md5f;
	rc = KMD5SumFmtMakeUpdate (&md5f, fm);
	if (rc == 0)
	{
	    KFile * f;
	    rc = KDirectoryCreateFile (dir, &f, false, 0666, kcmCreate, 
                                    "%s", path);
	    if (rc == 0)
	    {
		KMD5File * fmd5;
		rc = KMD5FileMakeWrite (&fmd5, f, md5f, path);
		if (rc == 0)
		{
		    cp->f = KMD5FileToKFile (fmd5);
		    orc = KMD5SumFmtRelease (md5f);
                    if (orc)
                        LOGERR (klogInt, orc, "Failure releasing MD5 format");
		    cp->o = 0; /* start of file */
		    return rc;
		}
		orc = KFileRelease (f);
                if (orc)
                    LOGERR (klogInt, orc, "Failure releasing Copier file");
	    }
	    orc = KMD5SumFmtRelease (md5f);
            if (orc)
                LOGERR (klogInt, orc, "Failure releasing MD5 format");
	}
	orc = KFileRelease (fm);
        if (orc)
            LOGERR (klogInt, orc, "Failure releasing MD5SUM file");
    }
    return rc;
}
Пример #6
0
rc_t make_stdout_redir ( stdout_redir ** writer, const char * filename, size_t bufsize )
{
    KDirectory *dir;
    rc_t rc = 0;
    stdout_redir * wr = NULL;

    if ( writer == NULL )
    {
        *writer = NULL;
        rc = RC ( rcApp, rcNoTarg, rcAllocating, rcSelf, rcNull );
    }
    else
    {
        wr = calloc( 1, sizeof *wr );
        if ( wr == NULL )
            rc = RC ( rcApp, rcNoTarg, rcAllocating, rcMemory, rcExhausted );
    }

    if ( rc == 0 )
    {
        rc = KDirectoryNativeDir( &dir );
        if ( rc == 0 )
        {
            KFile *of;
            rc = KDirectoryCreateFile ( dir, &of, false, 0664, kcmInit | kcmCreate, "%s", filename );
            if ( rc == 0 )
            {
                KFile* buf;
                rc = KBufFileMakeWrite( &buf, of, false, bufsize );
                if ( rc == 0 )
                {
                    wr->kfile = buf;
                    wr->org_writer = KOutWriterGet();
                    wr->org_data = KOutDataGet();
                    rc = KOutHandlerSet( stdout_redir_callback, wr );
                    if ( rc != 0 )
                        LOGERR( klogInt, rc, "KOutHandlerSet() failed" );
                }
                KFileRelease( of );
            }
            KDirectoryRelease( dir );
        }
    }

    if ( rc == 0 )
        *writer = wr;
    else
    {
        if ( wr != NULL )
            free( wr );
    }

    return rc;
}
Пример #7
0
rc_t CC ReportRedirect
( KWrtHandler* handler, const char* filename, bool* to_file, bool finalize )
{
    rc_t rc = 0;
    if (!finalize) {
        if (handler) {
            handler->writer = KOutWriterGet();
            handler->data = KOutDataGet();
        }
        if (filename) {
            KDirectory* dir = NULL;
            SFile* data = calloc(1, sizeof *data);
            if (data == NULL) {
                return RC(rcFS, rcMemory, rcAllocating, rcMemory, rcExhausted);
            }
            data->magic = MAGIC;
            rc = KDirectoryNativeDir(&dir);
            if (rc == 0) {
                rc = KDirectoryCreateFile
                    (dir, &data->f, false, 0664, kcmInit, "%s", filename);
            }
            if (rc == 0) {
                rc = KOutHandlerSet(fileWriter, data);
            }
            RELEASE(KDirectory, dir);
        }
        if (rc != 0 || filename == NULL) {
            KOutHandlerSetStdErr();
            if (to_file)
            {   *to_file = false; }
        }
        else if (to_file)
        {   *to_file = true; }

    }
    else {
        void* data = KOutDataGet();
        SFile* self = (SFile*)data;
        if (self) {
            if (self->magic == MAGIC) {
                RELEASE(KFile, self->f);;
            }
            memset(self, 0, sizeof *self);
            free(self);
        }
        if (handler) {
            KOutHandlerSet(handler->writer, handler->data);
        }
    }
    return rc;
}
Пример #8
0
rc_t write_output_file( KDirectory *dir, statistic * data,
                        const char * path, uint64_t * written )
{
    write_ctx wctx;
    rc_t rc;

    if ( written != NULL )
    {
        *written = 0;
    }
    wctx.out = NULL;
    wctx.pos = 0;
    wctx.lines = 0;
    rc = KDirectoryCreateFile ( dir, &wctx.out, false, 0664, kcmInit, path );
    if ( rc != 0 )
        LogErr( klogInt, rc, "KDirectoryCreateFile() failed\n" );
    else
    {
        char buffer[ 256 ];
        size_t num_writ;
        rc = string_printf ( buffer, sizeof buffer, &num_writ,
          "SPOTGROUP\tCYCLE\tNRead\tDIMER\tGC_CONTENT\tHP_RUN\tMaxQ\tQuality\tTOTAL\tMISMATCH\n" );
        if ( rc == 0 )
        {
            size_t f_writ;
            rc = KFileWrite ( wctx.out, wctx.pos, buffer, num_writ, &f_writ );
            if ( rc == 0 )
            {
                if ( written != NULL ) *written = f_writ;
                wctx.pos += f_writ;

                make_progressbar( &wctx.progress );
                wctx.entries = data->entries;
                wctx.fract_digits = progressbar_calc_fract_digits( wctx.entries );

                foreach_statistic( data, write_to_file_cb, &wctx );

                destroy_progressbar( wctx.progress );
                OUTMSG(( "\n" ));

                KFileRelease ( wctx.out );
                if ( written != NULL )
                {
                    *written = wctx.lines;
                }
            }
        }
    }
    return rc;
}
Пример #9
0
rc_t ArchiveAndEncrypt(KDirectory* wd, const char* inpath, const char* outpath, const char* passwd)
{
    const KDirectory* d;
    rc_t rc = KDirectoryOpenDirRead (wd, &d, false, "%s", inpath);
    if (rc == 0)
    {
        const KFile* infile;
        rc_t rc2;
    
        rc = KDirectoryOpenTocFileRead (d, &infile, 4, NULL, NULL, NULL);
        if (rc == 0)
        {
            KFile* outfile;
                
            /* if the file exists, add write access */
            KDirectorySetAccess( wd, false, 0600, 0777, "%s", outpath );
            rc = KDirectoryCreateFile(wd, &outfile, false, 0600, kcmCreate|kcmInit, "%s", outpath);
            if ( rc == 0 )
            {
                KFile* enc_outfile;
                KKey key;
                rc = KKeyInitRead(&key, kkeyAES256, passwd, string_measure(passwd, NULL));
                if ( rc == 0 )
                    rc = KEncFileMakeWrite(&enc_outfile, outfile, &key);
                    
                if (rc == 0)
                    rc = copy_file(infile, enc_outfile);
        
                rc2 = KFileRelease(outfile);
                if (rc == 0)
                    rc = rc2;
                /* remove write access */
                rc2 = KDirectorySetAccess( wd, false, 0400, 0777, "%s", outpath );
                if (rc == 0)
                    rc = rc2;
                rc2 = KFileRelease(enc_outfile);
                if (rc == 0)
                    rc = rc2;
            }
            rc2 = KFileRelease(infile);
            if (rc == 0)
                rc = rc2;
        }
        rc2 = KDirectoryRelease(d);
        if (rc == 0)
            rc = rc2;
    }
    return rc;
}
Пример #10
0
rc_t CC
TCreatePtFile ( struct KDirectory * current_dir, const char * path, TFileOpenMode mode, struct KFile ** out_file )
{
    rc_t rc;
    struct KFile * file;
    
    rc = 0;
    file = NULL;
    
    assert ( path );
    assert ( out_file );
    
    * out_file = NULL;
    
    switch (mode)
    {
        case TFileOpenMode_Write:
            rc = KDirectoryCreateFile ( current_dir, & file, false, 0644, kcmParents|kcmOpen, path );
            break;
                
        case TFileOpenMode_ReadWrite:
            rc = KDirectoryCreateFile ( current_dir, & file, true, 0644, kcmParents|kcmOpen, path );
            break;
                
        case TFileOpenMode_Read:
        default:
            assert(false);
    }
        
    if (rc == 0)
    {
        * out_file = file;
    }
    
    return rc;
}   /* TCreatePtFile () */
Пример #11
0
/*  ----------------------------------------------------------------------
 */
static
bool CC extract_one (BSTNode * n, void * data_)
{
    extnode * node;
    rc_data * data = data_;
    rc_t rc;
    size_t z;
    char buff [8193];

    assert (n);
    assert (data);

    node = (extnode*)n;

    rc = VPathReadPath (node->path, buff, sizeof (buff) - 1, &z);
    if (rc)
        LOGERR (klogErr, rc, "error pulling path for an extraction");
    else
    {
        const KFile * sfile;

        buff[z] = '\0';

/*
 * use base unless we have to revert to root.
 * base allows more control over options like password where the outside
 * archive might have a different password than an inner file
 */
#if 1 
        rc = VFSManagerOpenFileReadDirectoryRelative (options.vfsmgr, options.base,
                                                          &sfile, node->path);
#else
        rc = VFSManagerOpenFileReadDirectoryRelative (options.vfsmgr, options.root,
                                                      &sfile, node->path);
#endif
        if (rc)
            LOGERR (klogErr, rc, "error opening file within the archive");
        else
        {
            KFile * dfile;

/*             KOutMsg ("%s: %s %x\n", __func__, node->path, options.cm); */
            rc = KDirectoryCreateFile (options.dir, &dfile, false, 0640, options.cm, "%s", buff);
            if (rc)
                PLOGERR (klogErr, (klogErr, rc, "failed to create file '$(P)'", "P=%s", buff));
            else
            {
                const KFile * teefile;

                rc = KFileMakeTeeRead (&teefile, sfile, dfile);
                if (rc)
                    PLOGERR (klogErr, (klogErr, rc, "failed pipefitting file '$(P)'", "P=%s", buff));
                else
                {
                    KFileAddRef (sfile);
                    KFileAddRef (dfile);
                    rc = KFileRelease (teefile);
                    if (rc)
                    PLOGERR (klogErr, (klogErr, rc, "failed copying file '$(P)'", "P=%s", buff));
                }
            }
            KFileRelease (sfile);
        }
        KFileRelease (sfile);
    }
    data->rc = rc;
    return (rc != 0);
}
Пример #12
0
rc_t KU64IndexPersist_v3(KU64Index_v3* self, bool proj, KDirectory *dir, const char *path, bool use_md5)
{
    KU64Index_PersistData pd;
    char tmpname[256];
    char tmpmd5name[256];
    char md5path[256];

    self->rc = 0;
    memset(&pd, 0, sizeof(KU64Index_PersistData));

    self->rc = KDirectoryResolvePath(dir, false, tmpname, sizeof(tmpname), "%s.tmp", path);

    if( self->rc == 0 ) {
        self->rc = KDirectoryCreateFile(dir, &pd.file, true, 0664, kcmInit, "%s", tmpname);

        if(use_md5 && self->rc == 0 ) {

            KMD5SumFmt *km = NULL;
            size_t tmplen = snprintf(tmpmd5name, sizeof(tmpmd5name), "%s.md5", tmpname);
            KFile* kf = NULL;

            if( tmplen >= sizeof(tmpmd5name) ) {
                self->rc = RC(rcDB, rcIndex, rcPersisting, rcName, rcExcessive);
            } else {

                tmplen = snprintf(md5path, sizeof(md5path), "%s.md5", path);
                if( tmplen >= sizeof(md5path) ) {
                    self->rc = RC(rcDB, rcIndex, rcPersisting, rcName, rcExcessive);
                } else {

                    self->rc = KDirectoryCreateFile(dir, &kf, true, 0664, kcmInit, "%s", tmpmd5name);
                    if( self->rc == 0 ) {
                        self->rc = KMD5SumFmtMakeUpdate(&km, kf);
                        if( self->rc == 0 ) {
                            KMD5File * k5f;
                            kf = NULL;
                            self->rc = KMD5FileMakeWrite(&k5f, pd.file, km, path);
                            if( self->rc == 0 ) {
                                pd.file_md5 = k5f;
                                pd.file = KMD5FileToKFile(k5f);
                            }
                            /* release pass or fail */
                            KMD5SumFmtRelease(km);
                        } else {
                            KFileRelease ( kf );
                        }
                    } else {
                        KFileRelease ( kf );
                    }
                }
            }
            if( self->rc != 0 ) {
                KFileRelease(pd.file);
            }
        }

        if( self->rc == 0 ) {
            struct KIndexFileHeader_v3 head;
            size_t writ = 0;

            KDBHdrInit(&head.h, 3);
            head.index_type = kitU64;
            self->rc = KFileWrite(pd.file, pd.pos, &head, sizeof(struct KIndexFileHeader_v3), &writ);
            if( self->rc == 0 ) {
                pd.pos += writ;
                if( use_md5 ) {
                    KMD5FileBeginTransaction(pd.file_md5);
                }
                self->rc = BSTreePersist(&self->tree, NULL, KU64Index_WriteFunc, &pd, KU64Index_AuxFunc, &pd);
            }
            KFileRelease(pd.file);
            pd.file = NULL;
        }
    }

    if( self->rc == 0 ) {
        self->rc = KDirectoryRename(dir, false, tmpname, path);
        if( self->rc == 0 ) {
            if ( use_md5 ) {
                self->rc = KDirectoryRename(dir, false, tmpmd5name, md5path);
            }
            if( self->rc == 0 ) {
                /* done */
                return 0;
            }
        }
    }

    /* whack temporary file */
    KDirectoryRemove(dir, false, "%s", tmpname);
    if( use_md5 ) {
        KDirectoryRemove(dir, false, "%s", tmpmd5name);
    }
    return self->rc;
}
Пример #13
0
/* CreateDB
 * VCreateDB
 *  create a new or open an existing database
 *
 *  "db" [ OUT ] - return parameter for newly opened database
 *
 *  "cmode" [ IN ] - creation mode
 *
 *  "path" [ IN ] - NUL terminated string in
 *  wd-native character set giving path to database
 */
static
rc_t KDBManagerVCreateDBInt ( KDBManager *self,
    KDatabase **db, KDirectory *wd, KCreateMode cmode,
    const char *path, va_list args )
{
    char dbpath [ 4096 ];
    rc_t rc = KDirectoryVResolvePath ( wd, true,
        dbpath, sizeof dbpath, path, args );
    if ( rc == 0 )
    {
        /* we won't try accession resolution here */
        int type = KDBPathType ( /*NULL,*/ wd, NULL, dbpath );
        switch ( type )
        {
        case kptNotFound:
            /* first good path */
            break;

        case kptBadPath:
            return RC ( rcDB, rcMgr, rcCreating, rcPath, rcInvalid );

        case kptDatabase:
        case kptDatabase | kptAlias:
            /* found so is not good if we want to create new and not
             * clear/init or open old
             */
            if ((cmode & kcmValueMask) == kcmCreate)
                return RC ( rcDB, rcMgr, rcCreating, rcDatabase, rcExists );
            if (KDBManagerOpenObjectBusy (self, dbpath))
                return RC ( rcDB, rcMgr, rcCreating, rcDatabase, rcBusy );
            /* test now for locked directory */
            rc = KDBWritable ( wd, dbpath );
            if (rc)
            {
                switch (GetRCState(rc))
                {
                default:
                    return rc;
                case rcLocked:
                    return RC ( rcDB, rcMgr, rcCreating, rcDatabase, rcLocked );
                case rcReadonly:
                    return RC ( rcDB, rcMgr, rcCreating, rcDatabase, rcReadonly );
                case rcNotFound:
                    /* not found is good but probably unreachable */
                    break;
                case 0:
                    rc = 0;
                    break;
                }
            }
            /* second good path */
            break;

        case kptTable:
        case kptTable | kptAlias:
            return RC (rcDB, rcMgr, rcCreating, rcTable, rcExists);

        case kptColumn:
        case kptColumn | kptAlias:
            return RC (rcDB, rcMgr, rcCreating, rcColumn, rcExists);

        case kptIndex:
        case kptIndex | kptAlias:
            return RC (rcDB, rcMgr, rcCreating, rcIndex, rcExists);

        case kptMetadata:
        case kptMetadata | kptAlias:
            return RC (rcDB, rcMgr, rcCreating, rcMetadata, rcExists);

	case kptFile:
	case kptFile | kptAlias:
	    /* if we find a file, vary the failure if it is an archive that is a database
	     * or a non related file */
	    if ( KDBOpenPathTypeRead ( self, wd, dbpath, NULL, kptDatabase, NULL, false ) == 0 )
		return RC ( rcDB, rcMgr, rcCreating, rcDirectory, rcUnauthorized );
	    /* fall through */
        default:
            return RC ( rcDB, rcMgr, rcCreating, rcPath, rcIncorrect );
        }

        /* [re]create directory */
        rc = KDirectoryCreateDir ( wd, 0775, cmode, "%s", dbpath );
        if (rc == 0)
            /* create tbl subdirectory as required for db */
            rc = KDirectoryCreateDir ( wd, 0775, kcmOpen, "%s/tbl", dbpath );

        if ( rc == 0 )
        {
            KMD5SumFmt *md5 = NULL;

            if ( ( cmode & kcmMD5 ) != 0 )
            {
                KFile * f;

                /* if needed create the md5 digest file */
                rc = KDirectoryCreateFile ( wd, &f, true, 0664, kcmOpen, "%s/md5", dbpath );
                if ( rc == 0 )
                {
                    /* create a formatter around file
                       formatter will own "f" afterward */
                    rc = KMD5SumFmtMakeUpdate ( & md5, f );

                    /* if failed to create formatter, release "f" */
                    if ( rc != 0 )
                        KFileRelease ( f );
                }
            }

            if ( rc == 0 )
                rc = KDBManagerMakeDBUpdate ( self, db, wd, dbpath, md5 );

            KMD5SumFmtRelease ( md5 );
        }
    }
    return rc;
}
Пример #14
0
static rc_t main_1(int argc, char *argv[], bool const continuing, unsigned const load)
{
    Args *args;
    rc_t rc;
    unsigned n_aligned = 0;
    unsigned n_unalgnd = 0;
    char *aligned[256];
    char *unalgnd[256];
    char *name_buffer = NULL;
    unsigned next_name = 0;
    unsigned nbsz = 0;
    char const *value;
    char *dummy;

    rc = ArgsMakeAndHandle (&args, argc, argv, 1, Options, sizeof(Options)/sizeof(Options[0]));
    while (rc == 0) {
        uint32_t pcount;

        rc = ArgsOptionCount(args, option_only_verify, &pcount);
        if (rc)
            break;
        G.onlyVerifyReferences |= (pcount > 0);
        
        rc = ArgsOptionCount(args, option_no_verify, &pcount);
        if (rc)
            break;
        G.noVerifyReferences |= (pcount > 0);
        
        rc = ArgsOptionCount(args, option_use_qual, &pcount);
        if (rc)
            break;
        G.useQUAL |= (pcount > 0);
        
        rc = ArgsOptionCount(args, option_ref_config, &pcount);
        if (rc)
            break;
        G.limit2config |= (pcount > 0);
        
        rc = ArgsOptionCount(args, OPTION_REF_FILE, &pcount);
        if (rc)
            break;
        if (pcount && G.refFiles) {
            int i;

            for (i = 0; G.refFiles[i]; ++i)
                free((void *)G.refFiles[i]);
            free((void *)G.refFiles);
        }
        G.refFiles = calloc(pcount + 1, sizeof(*(G.refFiles)));
        if (!G.refFiles) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
            break;
        }
        while(pcount-- > 0) {
            rc = getArgValue(args, OPTION_REF_FILE, pcount, &G.refFiles[pcount]);
            if (rc)
                break;
        }

        rc = ArgsOptionCount (args, OPTION_TMPFS, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, OPTION_TMPFS, 0, &G.tmpfs);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_INPUT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, OPTION_INPUT, 0, &G.inpath);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single input parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, option_ref_filter, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, option_ref_filter, 0, &G.refFilter);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_CONFIG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, OPTION_CONFIG, 0, &G.refXRefPath);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single input parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_OUTPUT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, OPTION_OUTPUT, 0, &G.outpath);
            if (rc)
                break;
            if (load == 0) {
                G.firstOut = strdup(G.outpath);
            }
            value = strrchr(G.outpath, '/');
            G.outname = value ? (value + 1) : G.outpath;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single output parameter required\n"));
            MiniUsage (args);
            break;
        }
        else if (!G.onlyVerifyReferences) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
            OUTMSG (("Output parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_MINMAPQ, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue(args, OPTION_MINMAPQ, 0, (const void **)&value);
            if (rc)
                break;
            G.minMapQual = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_QCOMP, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = getArgValue(args, OPTION_QCOMP, 0, &G.QualQuantizer);
            if (rc)
                break;
        }

        rc = ArgsOptionCount (args, option_edit_aligned_qual, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, option_edit_aligned_qual, 0, (const void **)&value);
            if (rc)
                break;
            G.alignedQualValue = strtoul(value, &dummy, 0);
            if (G.alignedQualValue == 0) {
                rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
                OUTMSG (("edit-aligned-qual: bad value\n"));
                MiniUsage (args);
                break;
            }
            G.editAlignedQual = true;
        }
        
        rc = ArgsOptionCount (args, OPTION_CACHE_SIZE, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_CACHE_SIZE, 0, (const void **)&value);
            if (rc)
                break;
            G.cache_size = strtoul(value, &dummy, 0) * 1024UL * 1024UL;
            if (G.cache_size == 0) {
                rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
                OUTMSG (("cache-size: bad value\n"));
                MiniUsage (args);
                break;
            }
        }
        
        rc = ArgsOptionCount (args, OPTION_MAX_WARN_DUP_FLAG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_WARN_DUP_FLAG, 0, (const void **)&value);
            if (rc)
                break;
            G.maxWarnCount_DupConflict = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, option_unsorted, &pcount);
        if (rc)
            break;
        G.expectUnsorted |= (pcount > 0);
        
        rc = ArgsOptionCount (args, option_sorted, &pcount);
        if (rc)
            break;
        G.requireSorted |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_MAX_REC_COUNT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_REC_COUNT, 0, (const void **)&value);
            if (rc)
                break;
            G.maxAlignCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_MAX_ERR_COUNT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_ERR_COUNT, 0, (const void **)&value);
            if (rc)
                break;
            G.maxErrCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_MIN_MATCH, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MIN_MATCH, 0, (const void **)&value);
            if (rc)
                break;
            G.minMatchCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_DUP, &pcount);
        if (rc)
            break;
        G.acceptBadDups |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_NOMATCH, &pcount);
        if (rc)
            break;
        G.acceptNoMatch |= (pcount > 0);
        
        rc = ArgsOptionCount (args, option_keep_mismatch_qual, &pcount);
        if (rc)
            break;
        G.keepMismatchQual |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_NO_CS, &pcount);
        if (rc)
            break;
        G.noColorSpace |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_NO_SECONDARY, &pcount);
        if (rc)
            break;
        G.noSecondary |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_TI, &pcount);
        if (rc)
            break;
        G.hasTI |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_HARD_CLIP, &pcount);
        if (rc)
            break;
        G.acceptHardClip |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_ALLOW_MULTI_MAP, &pcount);
        if (rc)
            break;
        G.allowMultiMapping |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_ALLOW_SECONDARY, &pcount);
        if (rc)
            break;
        G.assembleWithSecondary |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_DEFER_SECONDARY, &pcount);
        if (rc)
            break;
        G.deferSecondary |= (pcount > 0);
        
        rc = ArgsOptionCount (args, OPTION_NOMATCH_LOG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            KDirectory *dir;
            
            rc = ArgsOptionValue (args, OPTION_NOMATCH_LOG, 0, (const void **)&value);
            if (rc) break;
            rc = KDirectoryNativeDir(&dir);
            if (rc) break;
            rc = KDirectoryCreateFile(dir, &G.noMatchLog, 0, 0664, kcmInit, "%s", value);
            KDirectoryRelease(dir);
            if (rc) break;
        }
        
        rc = ArgsOptionCount (args, OPTION_HEADER, &pcount);
        if (rc)
            break;
        if (pcount == 1) {
            rc = ArgsOptionValue (args, OPTION_HEADER, 0, (const void **)&value);
            if (rc) break;
            free((void *)G.headerText);
            rc = LoadHeader(&G.headerText, value, G.inpath);
            if (rc) break;
        }
        
        rc = ArgsParamCount (args, &pcount);
        if (rc) break;
        if (pcount == 0)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
            MiniUsage (args);
            break;
        }
        else if (pcount > sizeof(aligned)/sizeof(aligned[0])) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            (void)PLOGERR(klogErr, (klogErr, rc, "$(count) input files is too many, $(max) is the limit",
                        "count=%u,max=%u", (unsigned)pcount, (unsigned)(sizeof(aligned)/sizeof(aligned[0]))));
            break;
        }
        else {
            unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsParamValue(args, i, (const void **)&value);
                if (rc) break;
                need += strlen(value) + 1;
            }
            nbsz = need;
        }
        
        rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
        if (rc)
            break;
        if (pcount > 0)
        {
            unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, (const void **)&value);
                if (rc) break;
                need += strlen(value) + 1;
            }
            if (rc) break;
            nbsz += need;
        }
        
        name_buffer = malloc(nbsz);
        if (name_buffer == NULL) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
        if (rc == 0) {
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, (const void **)&value);
                if (rc) break;
                
                unalgnd[n_unalgnd++] = name_buffer + next_name;
                rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
                if (rc) break;
                next_name += strlen(name_buffer + next_name) + 1;
            }
            if (rc) break;
        }
        else
            break;
        
        rc = ArgsParamCount (args, &pcount);
        if (rc == 0) {
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsParamValue(args, i, (const void **)&value);
                if (rc) break;
                
                aligned[n_aligned++] = name_buffer + next_name;
                rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
                if (rc) break;
                next_name += strlen(name_buffer + next_name) + 1;
            }
        }
        else
            break;

        rc = run(argv[0], n_aligned, (char const **)aligned, n_unalgnd, (char const **)unalgnd, continuing);
        break;
    }
    free(name_buffer);

    if (rc) {
        (void)PLOGERR(klogErr, (klogErr, rc, "load failed",
                "severity=total,status=failure,accession=%s,errors=%u", G.outname, G.errCount));
    } else {
        (void)PLOGMSG(klogInfo, (klogInfo, "loaded",
                "severity=total,status=success,accession=%s,errors=%u", G.outname, G.errCount));
    }
    ArgsWhack(args);
    return rc;
}
Пример #15
0
static
rc_t FileToFile (const KDirectory * sd, const char * source,
                 KDirectory *dd, const char * dest_, bool try_rename,
                 char * base)
{
    const KFile * infile;
    rc_t rc;
    uint32_t access;
    KTime_t date;
    bool is_tmp;
    char dest [MY_MAX_PATH + sizeof EncExt];

    strcpy (dest, dest_);
    if (try_rename)
        NameFixUp (dest);

    if ((sd == dd) && (strcmp (source, dest) == 0))
        return FileInPlace (dd, dest, try_rename);

    if (base == NULL)
        STSMSG (1, ("%scrypting file %s to %s", De, source, dest));
    else
        STSMSG (1, ("%scrypting file %s to %s/%s", De, source, base, dest));

    /*
     * A Hack to make stdin/stout work within KFS
     */
    if (UseStdin)
    {
        const KFile * iinfile;
        rc = KFileMakeStdIn (&iinfile);
        if (rc == 0)
        {
            rc = KBufReadFileMakeRead (&infile, iinfile, 64 * 1024);
            KFileRelease (iinfile);
            if (rc == 0)
            {
                access = 0640;
                date = 0;
                goto stdin_shortcut;
            }
            LOGERR (klogErr, rc, "error wrapping stdin");
            return rc;
        }
    }
    rc = 0;
    is_tmp = IsTmpFile (source);

    if (is_tmp)
    {
        TmpFoundFlag = true;
        if (ForceFlag)
            ; /* LOG OVERWRITE */
        else
            ; /* LOG TMP */
    }
    if (!is_tmp || ForceFlag)
    {
        rc = KDirectoryAccess (sd, &access, "%s", source);
        if (rc)
            LOGERR (klogErr, rc, "Error check permission of source");

        else
        {
            rc = KDirectoryDate (sd, &date, "%s", source);
            if (rc)
                LOGERR (klogErr, rc, "Error check date of source");

            else
            {
                rc = KDirectoryOpenFileRead (sd, &infile, "%s", source);
                if (rc)
                    PLOGERR (klogErr, (klogErr, rc,
                                       "Error opening source file '$(S)'",
                                       "S=%s", source));
                else
                {
                    EncScheme scheme;

stdin_shortcut:
                    rc = EncryptionTypeCheck (infile, source, &scheme);
                    if (rc == 0)
                    {
                        KFile * outfile;
                        uint32_t kcm;

                        /*
                         * Hack to support stdout before VFS is complete enough to use here
                         */
                        if (UseStdout)
                        {
                            rc = KFileMakeStdOut (&outfile);
                            if (rc)
                                LOGERR (klogErr, rc, "error wrapping stdout");
                        }
                        else
                        {
                            kcm = ForceFlag ? kcmInit|kcmParents : kcmCreate|kcmParents;

                            rc = KDirectoryCreateFile (dd, &outfile, false, 0600, kcm, "%s", dest);
                            if (rc)
                                PLOGERR (klogErr,(klogErr, rc, "error opening output '$(O)'",
                                                  "O=%s", dest));
                        }
                        if (rc == 0)
                        {
                            const KFile * Infile;
                            KFile * Outfile;

                            rc = CryptFile (infile, &Infile, outfile, &Outfile, scheme);
                            if (rc == 0)
                            {
                                rc = CopyFile (Infile, Outfile, source, dest);
                                if (rc == 0)
                                {
                                    if (UseStdin || UseStdout)
                                        ;
                                    else
                                    {
                                        rc = KDirectorySetAccess (dd, false, access, 0777,
                                                                  "%s", dest);

                                        if (rc == 0 && date != 0)
                                            rc = KDirectorySetDate (dd, false, date, "%s", dest);
                                    }
                                }
                                KFileRelease (Infile);
                                KFileRelease (Outfile);
                            }
                            KFileRelease (outfile);
                        }
                    }
                    KFileRelease (infile);
                }
            }
        }
    }
    return rc;
}
Пример #16
0
rc_t CopyFileToFile( const KDirectory *top, const char *inname, KDirectory *targettop, const char *outname )
{
  const KFile *in = NULL;
  KFile *out = NULL;
  KFile *md5file = NULL;
  KMD5File *md5out = NULL;
  KMD5SumFmt *md5sumfmt = NULL;
  char md5filename[1024];
  rc_t rc = 0;
  uint32_t mode = 0;
  uint32_t pathtype = 0;
  uint32_t failed = 0;

  if (PathIsMD5File(top, inname)) {
    /* Skip it */
    return 0;
  }
  
  rc = KDirectoryOpenFileRead( top, &in, "%s", inname );
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }
  mode = DEFAULTMODE;
  rc = KDirectoryAccess( top, &mode, inname);
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }

  /*
   * Not sure here -- does kcmInit re-initialize the file mode as we specify?
   * Or does it preserve the existing mode (and do we want it to)?
   */
  if (clobber_protections) {
      pathtype = KDirectoryPathType( targettop, "%s", outname );
    if ((pathtype & ~kptAlias) == kptFile) {
        rc = KDirectorySetAccess( targettop, false, mode, 0777, "%s", outname);
      if (rc != 0) {
	failed = rc;
	goto FAIL;
      }
    }
  }

  rc = KDirectoryCreateFile( targettop, &out, false, mode, (force? kcmInit: kcmCreate), "%s", outname );
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }
  sprintf(md5filename, "%s.md5", outname);
  rc = KDirectoryCreateFile( targettop, &md5file, false, DEFAULTMODE, (force? kcmInit: kcmCreate), "%s", md5filename);
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }

  rc = KMD5SumFmtMakeUpdate( &md5sumfmt, md5file);
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }

  rc = KMD5FileMakeWrite( &md5out, out, md5sumfmt, outname );
  if (rc != 0) {
    failed = rc;
    goto FAIL;
  }
    
  {  
    uint64_t rpos = 0;
    uint64_t wpos = 0;
      
    size_t numread;

    while (true) {
      rc = KFileRead( in, rpos, buffer, BUFSIZE, &numread );
      /* fprintf(stderr, "Read %d bytes.\n", numread); */
      if (rc == 0 && numread == 0) 
	break;
      if (rc != 0) {
	failed = rc;
	goto FAIL;
      }
      rpos += numread;

      {
	size_t numwritten = 0;
	int written = 0;
	while (written < numread) {
	  rc = KFileWrite( (KFile *)md5out, wpos, buffer+written, numread-written, &numwritten );
	  if (rc != 0) {
	    failed = rc;
	    break;
	  }
	  if (numwritten == 0) {
	    fprintf(stderr, "Didn't write anything.\n");
	    failed = -1;
	    goto FAIL;
	  }
	  wpos += numwritten;
	  written += numwritten;
	}
      }
    }
  }

  /* Success also, check the value of failed to see if failed */
 FAIL:

  if (NULL != md5out) {
    KFileRelease((KFile *)md5out);
    md5out = NULL;
  }

  /*KFileRelease(out); */
  if (NULL != md5sumfmt) {
    KMD5SumFmtRelease(md5sumfmt);
    md5sumfmt = NULL;
  }
  /*  KFileRelease(md5file); */
  if (NULL != in) {
    KFileRelease(in);
    in = NULL;
  }
  /* KDirectoryRelease(top); */

  if (failed) {
      KDirectoryRemove( targettop, false, "%s", md5filename );
      KDirectoryRemove( targettop, false, "%s", outname);
  }

  return failed;

}  
Пример #17
0
static
rc_t run ( srakar_parms *pb )
{
    KFile * outfile;
    rc_t rc;

    const SRAMgr *mgr;

    rc = SRAMgrMakeRead ( & mgr );
    if ( rc != 0 )
        LOGERR ( klogInt, rc, "failed to open SRAMgr" );
    else
    {
        const SRATable *tbl;
        rc = SRAMgrOpenTableRead ( mgr, & tbl, "%s", pb -> src_path );
        if ( rc != 0 )
            PLOGERR ( klogInt, (klogInt, rc,
                "failed to open SRATable '$(spec)'", "spec=%s",
                pb -> src_path ));
        else
        {
            rc = KDirectoryCreateFile (pb->dir, &outfile, false, 0446,
                                       kcmParents | ( pb->force ? kcmInit : kcmCreate) , "%s", pb->dst_path);
            if (rc == 0)
            {
                const KFile * archive;

                rc = SRATableMakeSingleFileArchive (tbl, &archive, pb->lite,
                    NULL);
                if (rc == 0)
                {
                    rc = copy_file (archive, outfile);
                    KFileRelease (archive);
                }
                KFileRelease (outfile);
            }
            SRATableRelease ( tbl );
        }
        SRAMgrRelease (mgr);
    }
/*
    rc = KDirectoryCreateFile (pb->dir, &outfile, false, 0446, kcmParents | ( pb->force ? kcmInit : kcmCreate) , "%s", pb->dst_path);

    if (rc == 0)
    {
        const SRAMgr *mgr;

        rc = SRAMgrMakeRead ( & mgr );
        if ( rc != 0 )
            LOGERR ( klogInt, rc, "failed to open SRAMgr" );
        else
        {
            const SRATable *tbl;
            rc = SRAMgrOpenTableRead ( mgr, & tbl, "%s", pb -> src_path );
            if ( rc != 0 )
                PLOGERR ( klogInt, (klogInt, rc, "failed to open SRATable '$(spec)'", "spec=%s", pb -> src_path ));
            else
            {
                const KFile * archive;

                rc = SRATableMakeSingleFileArchive (tbl, &archive, pb->lite, NULL);
                if (rc == 0)
                {
                    rc = copy_file (archive, outfile);
                    KFileRelease (archive);
                }
                SRATableRelease ( tbl );
            }
            SRAMgrRelease (mgr);
        }
        KFileRelease (outfile);
    }
*/
    return rc;
}
Пример #18
0
LIB_EXPORT rc_t CC XMLLogger_Make2(const XMLLogger** self, KDirectory* dir, const char* logpath, const int fd)
{
    rc_t rc = 0;
    XMLLogger* obj;
    KDirectory* my_dir = NULL;

    const uint32_t lopt = klogFmtTimestamp | klogFmtSeverity |
                          klogFmtMessage | klogFmtAppName | klogFmtAppVersion | klogFmtReasonShort;

    const uint32_t sopt = kstsFmtTimestamp | kstsFmtMessage | kstsFmtAppName | kstsFmtAppVersion;

    KLogFmtFlagsSet(lopt);
    KLogLibFmtFlagsSet(lopt);
    KStsFmtFlagsSet(sopt);
    KStsLibFmtFlagsSet(sopt);

    obj = calloc(1, sizeof(*obj));
    if( obj == NULL ) {
        rc = RC(rcApp, rcLog, rcAllocating, rcMemory, rcExhausted);
    } else if( fd < 0 && dir == NULL && (rc = KDirectoryNativeDir(&my_dir)) != 0 ) {
    } else if( fd >= 0 && (rc = KFileMakeFDFileWrite(&obj->file.file, false, fd)) != 0 ) {
    } else if( logpath != NULL && fd < 0 && (rc = KDirectoryCreateFile(dir ? dir : my_dir, &obj->file.file, false, 0644, kcmInit, "%s", logpath)) != 0 ) {
    } else {
    
        obj->file.pos = 0;
        obj->log.file = &obj->file;
        obj->log.fmt.formatter = KLogFmtWriterGet();
        obj->log.fmt.data = KLogFmtDataGet();
        obj->log.wrt.writer = KLogWriterGet();
        obj->log.wrt.data = KLogDataGet();

        obj->loglib.file = &obj->file;
        obj->loglib.fmt.formatter = KLogLibFmtWriterGet();
        obj->loglib.fmt.data = KLogLibFmtDataGet();
        obj->loglib.wrt.writer = KLogLibWriterGet();
        obj->loglib.wrt.data = KLogLibDataGet();

        obj->sts.file = &obj->file;
        obj->sts.fmt.formatter = KStsFmtWriterGet();
        obj->sts.fmt.data = KStsFmtDataGet();
        obj->sts.wrt.writer = KStsWriterGet();
        obj->sts.wrt.data = KStsDataGet();

        obj->stslib.file = &obj->file;
        obj->stslib.fmt.formatter = KStsLibFmtWriterGet();
        obj->stslib.fmt.data = KStsLibFmtDataGet();
        obj->stslib.wrt.writer = KStsLibWriterGet();
        obj->stslib.wrt.data = KStsLibDataGet();

        if( (rc = KLogFmtHandlerSet(LoaderXMLFormatter, lopt, &obj->log)) == 0 &&
            (rc = KLogLibFmtHandlerSet(LoaderXMLFormatter, lopt, &obj->loglib)) == 0 &&
            (rc = KStsFmtHandlerSet(LoaderXMLFormatter, sopt, &obj->sts)) == 0 &&
            (rc = KStsLibFmtHandlerSet(LoaderXMLFormatter, sopt, &obj->stslib)) == 0 ) {
            /* make log valid XML */
            if( obj->file.file != NULL ) {
                size_t num_writ = 0;
                rc = KFileWrite(obj->file.file, obj->file.pos, "<Log>\n", 6, &num_writ);
                obj->file.pos += num_writ;
            }
        }
    }
    KDirectoryRelease(my_dir);
    if( rc == 0 ) {
        *self = obj;
        if( fd >= 0 ) {
            DBG(("XML Log file set to handle %d\n", fd));
        } else if( logpath != NULL) {
            DBG(("XML Log file set to %s\n", logpath));
        } 
    } else {
        XMLLogger_Release(obj);
        *self = NULL;
    }
    return rc;
}
Пример #19
0
static
rc_t run()
{
    KDirectory * pwd;
    rc_t rc;

    STSMSG (1, ("Open file system"));
    rc = KDirectoryNativeDir (&pwd);
    if (rc)
        LOGERR (klogErr, rc, "Failed to open filesystem");
    else
    {
        /* encrypt the file */
        {
            const KFile * sourcefile;   /* open source first to make sure we are in the right place */

            rc = KDirectoryOpenFileRead (pwd, &sourcefile, UNENCRYPT);
            if (rc)
                LOGERR (klogErr, rc, "Failed to open source file");
            else
            {
                KFile * encfile;

                rc = KDirectoryCreateFile (pwd, &encfile, true, 0664, kcmInit, TEST);
                if (rc)
                    LOGERR (klogErr, rc, "Failed to open encrypted output");
                else
                {
                    KFile * writefile;

                    rc = KFileMakeWGAEncWrite (&writefile, encfile, WGA_KEY, 1, 4096, fer_encAES);
                    if (rc)
                        LOGERR (klogErr, rc, "Failed to open encrypter");
                    else
                    {
                        uint64_t pos = 0;
                        size_t num_read;
                        uint64_t buffer [64*1024];

                        do
                        {
                            rc = KFileRead (sourcefile, pos, buffer, sizeof buffer, &num_read);
                            if (rc)
                                LOGERR (klogErr, rc, "Failed to read");

                            else if (num_read)
                            {
                                size_t tot_writ;
                                size_t num_writ;

                                for (tot_writ = 0; tot_writ < num_read; tot_writ += num_writ)
                                {
                                    rc = KFileWrite (writefile, pos + tot_writ, buffer + tot_writ,
                                                     num_read - tot_writ, &num_writ);
                                    if (rc)
                                    {
                                        LOGERR (klogErr, rc, "Failed to write");
                                        break;
                                    }
                                    else if (num_writ == 0)
                                    {
                                        rc = -1;
                                        break;
                                    }
                                }
                                pos += num_read;
                            }
                        } while ((rc == 0) && (num_read > 0));

                        KFileRelease (writefile);
                    }
                    KFileRelease (encfile);
                }
                KFileRelease (sourcefile);
            }
        }
        if (rc == 0)
        {
            const KFile * unencrypt;

            STSMSG (1, ("Open unencryptd file %s\n", UNENCRYPT));
            rc = KDirectoryOpenFileRead (pwd, &unencrypt, UNENCRYPT);
            if (rc)
                LOGERR (klogErr, rc, "failed to open unencryptd file");
            else
            {
                const KFile * encrypt;

                STSMSG (1, ("Open encryptd file %s\n", ENCRYPT));
                rc = KDirectoryOpenFileRead (pwd, &encrypt, ENCRYPT);
                if (rc)
                    LOGERR (klogErr, rc, "Failed to open encryptd file");
                else
                {
                    const KFile * decrypt;

                    STSMSG (1, ("Open decrypt file\n"));
                    rc = KFileMakeWGAEncRead (&decrypt, encrypt, WGA_KEY, 1);
                    if (rc)
                        LOGERR (klogErr, rc, "Failed to open decrypter");

                    else
                    {
                        size_t tot_readu;
                        size_t tot_readd;
                        size_t num_read;
                        uint8_t decoded [64 * 1024 * 24];
                        uint8_t unencoded [64 * 1024 * 24];
                        int comp;

                        memset (decoded, 0, sizeof decoded);
                        memset (unencoded, 0, sizeof unencoded);

                        for (tot_readu = 0; rc == 0 && tot_readu < sizeof unencoded; tot_readu += num_read)
                        {
                            STSMSG (5, ("Read unencrypted '%u' @ %lu\n", sizeof unencoded - tot_readu, tot_readu));
                            rc = KFileRead (unencrypt, tot_readu, unencoded + tot_readu,
                                            sizeof unencoded - tot_readu, &num_read);
                            if (num_read == 0)
                                break;

                        }
                        if (rc == 0)
                        {
                            for (tot_readd = 0; rc == 0 && tot_readd < sizeof decoded; tot_readd += num_read)
                            {

                                STSMSG (5, ("Read decrypted '%u' @ %lu\n", sizeof decoded - tot_readd, tot_readd));
                                rc = KFileRead (decrypt, tot_readd, decoded + tot_readd,
                                                sizeof decoded - tot_readd, &num_read);
                                if (num_read == 0)
                                    break;
                            }

                            comp = memcmp(decoded,unencoded, sizeof decoded);

                            STSMSG (1, ("Read u '%zu' d '%zu' cmp '%d'", tot_readu, tot_readd, comp));

                        
                            if (comp != 0)
                            {
                                rc = RC (rcExe, rcNoTarg, rcValidating, rcFile, rcInconsistent);
                                LOGERR (klogErr, rc, "Unencryptfailed");


                                {
                                    size_t ix;
                                    size_t limit;
                                    size_t matched = 0;
                                    size_t mismatched = 0;

                                    limit = tot_readu;
                                    if (limit < tot_readd)
                                        limit = tot_readd;

                                    for (ix = 0; ix < limit; ++ix)
                                    {
                                        if (decoded[ix] != unencoded[ix])
                                        {
                                            ++mismatched;
                                            STSMSG (2, ("%5.5zu: D %c %2.2X U %c %2.2X\n", 
                                                        ix,
                                                        decoded[ix]?decoded[ix]:'?',decoded[ix],
                                                        unencoded[ix]?unencoded[ix]:'?',unencoded[ix]));
                                        }
                                        else
                                            ++matched;
                                    }
                                    STSMSG (2, ("matched %zu mismatched %zu",matched,mismatched));
                                }
                            }
                        
                        }
                    }
                    KFileRelease (decrypt);
                }
                KFileRelease (encrypt);
            }
            KFileRelease (unencrypt);
        }
        if (rc == 0)
        {
            const KFile * unencrypt;

            STSMSG (1, ("Open unencryptd file %s\n", UNENCRYPT));
            rc = KDirectoryOpenFileRead (pwd, &unencrypt, UNENCRYPT);
            if (rc)
                LOGERR (klogErr, rc, "failed to open unencryptd file");
            else
            {
                const KFile * encrypt;

                STSMSG (1, ("Open encryptd file %s\n", TEST));
                rc = KDirectoryOpenFileRead (pwd, &encrypt, TEST);
                if (rc)
                    LOGERR (klogErr, rc, "Failed to open encryptd file");
                else
                {
                    const KFile * decrypt;

                    STSMSG (1, ("Open decrypt file\n"));
                    rc = KFileMakeWGAEncRead (&decrypt, encrypt, WGA_KEY, 1);
                    if (rc)
                        LOGERR (klogErr, rc, "Failed to open decrypter");

                    else
                    {
                        size_t tot_readu;
                        size_t tot_readd;
                        size_t num_read;
                        uint8_t decoded [64 * 1024 * 24];
                        uint8_t unencoded [64 * 1024 * 24];
                        int comp;

                        memset (decoded, 0, sizeof decoded);
                        memset (unencoded, 0, sizeof unencoded);

                        for (tot_readu = 0; rc == 0 && tot_readu < sizeof unencoded; tot_readu += num_read)
                        {
                            STSMSG (5, ("Read unencrypted '%u' @ %lu\n", sizeof unencoded - tot_readu, tot_readu));
                            rc = KFileRead (unencrypt, tot_readu, unencoded + tot_readu,
                                            sizeof unencoded - tot_readu, &num_read);
                            if (num_read == 0)
                                break;

                        }
                        if (rc == 0)
                        {
                            for (tot_readd = 0; rc == 0 && tot_readd < sizeof decoded; tot_readd += num_read)
                            {

                                STSMSG (5, ("Read decrypted '%u' @ %lu\n", sizeof decoded - tot_readd, tot_readd));
                                rc = KFileRead (decrypt, tot_readd, decoded + tot_readd,
                                                sizeof decoded - tot_readd, &num_read);
                                if (num_read == 0)
                                    break;
                            }

                            comp = memcmp(decoded,unencoded, sizeof decoded);

                            STSMSG (1, ("Read u '%zu' d '%zu' cmp '%d'", tot_readu, tot_readd, comp));

                        
                            if (comp != 0)
                            {
                                rc = RC (rcExe, rcNoTarg, rcValidating, rcFile, rcInconsistent);
                                LOGERR (klogErr, rc, "Unencryptfailed");


                                {
                                    size_t ix;
                                    size_t limit;
                                    size_t matched = 0;
                                    size_t mismatched = 0;

                                    limit = tot_readu;
                                    if (limit < tot_readd)
                                        limit = tot_readd;

                                    for (ix = 0; ix < limit; ++ix)
                                    {
                                        if (decoded[ix] != unencoded[ix])
                                        {
                                            ++mismatched;
                                            STSMSG (2, ("%5.5zu: D %c %2.2X U %c %2.2X\n", 
                                                        ix,
                                                        decoded[ix]?decoded[ix]:'?',decoded[ix],
                                                        unencoded[ix]?unencoded[ix]:'?',unencoded[ix]));
                                        }
                                        else
                                            ++matched;
                                    }
                                    STSMSG (2, ("matched %zu mismatched %zu",matched,mismatched));
                                }
                            }
                        
                        }
                    }
                    KFileRelease (decrypt);
                }
                KFileRelease (encrypt);
            }
            KFileRelease (unencrypt);
        }
    }
    KDirectoryRelease (pwd);
    return rc;
}
Пример #20
0
rc_t CC KMain (int argc, char * argv[])
{
    Args * args;
    rc_t rc;
    unsigned n_aligned = 0;
    unsigned n_unalgnd = 0;
    char *aligned[256];
    char *unalgnd[256];
    char *name_buffer = NULL;
    unsigned next_name = 0;
    unsigned nbsz = 0;
    char const *value;
    char *dummy;
    const XMLLogger* xml_logger = NULL;
    
    memset(&G, 0, sizeof(G));
    
    G.mode = mode_Archive;
    G.maxSeqLen = TableWriterRefSeq_MAX_SEQ_LEN;
    G.schemaPath = SCHEMAFILE;
    G.omit_aligned_reads = true;
    G.omit_reference_reads = true;
    G.minMapQual = 0; /* accept all */
    G.tmpfs = "/tmp";
#if _ARCH_BITS == 32
    G.cache_size = ( size_t ) 1 << 30;
#else
    G.cache_size = ( size_t ) 10 << 30;
#endif
    G.maxErrCount = 1000;
    G.minMatchCount = 10;
    
    set_pid();

    rc = ArgsMakeAndHandle (&args, argc, argv, 2, Options,
                            sizeof Options / sizeof (OptDef), XMLLogger_Args, XMLLogger_ArgsQty);

    while (rc == 0) {
        uint32_t pcount;

        if( (rc = XMLLogger_Make(&xml_logger, NULL, args)) != 0 ) {
            break;
        }
        rc = ArgsOptionCount(args, option_only_verify, &pcount);
        if (rc)
            break;
        G.onlyVerifyReferences = (pcount > 0);
        
        rc = ArgsOptionCount(args, option_no_verify, &pcount);
        if (rc)
            break;
        G.noVerifyReferences = (pcount > 0);
        
        rc = ArgsOptionCount(args, option_use_qual, &pcount);
        if (rc)
            break;
        G.useQUAL = (pcount > 0);
        
        rc = ArgsOptionCount(args, option_ref_config, &pcount);
        if (rc)
            break;
        G.limit2config = (pcount > 0);
        
        rc = ArgsOptionCount(args, OPTION_REF_FILE, &pcount);
        if (rc)
            break;
        G.refFiles = calloc(pcount + 1, sizeof(*(G.refFiles)));
        if( !G.refFiles ) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
            break;
        }
        while(pcount-- > 0) {
            rc = ArgsOptionValue(args, OPTION_REF_FILE, pcount, &G.refFiles[pcount]);
            if (rc)
                break;
        }

        rc = ArgsOptionCount (args, OPTION_TMPFS, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_TMPFS, 0, &G.tmpfs);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_INPUT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_INPUT, 0, &G.inpath);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single input parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, option_ref_filter, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, option_ref_filter, 0, &G.refFilter);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_CONFIG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_CONFIG, 0, &G.refXRefPath);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single input parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_OUTPUT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_OUTPUT, 0, &G.outpath);
            if (rc)
                break;
        }
        else if (pcount > 1)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            OUTMSG (("Single output parameter required\n"));
            MiniUsage (args);
            break;
        }
        else if (!G.onlyVerifyReferences) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
            OUTMSG (("Output parameter required\n"));
            MiniUsage (args);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_MINMAPQ, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MINMAPQ, 0, &value);
            if (rc)
                break;
            G.minMapQual = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_QCOMP, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_QCOMP, 0, &G.QualQuantizer);
            if (rc)
                break;
        }
        
        rc = ArgsOptionCount (args, option_edit_aligned_qual, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, option_edit_aligned_qual, 0, &value);
            if (rc)
                break;
            G.alignedQualValue = strtoul(value, &dummy, 0);
            if (G.alignedQualValue == 0) {
                rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
                OUTMSG (("edit-aligned-qual: bad value\n"));
                MiniUsage (args);
                break;
            }
            G.editAlignedQual = true;
        }
        
        rc = ArgsOptionCount (args, OPTION_CACHE_SIZE, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_CACHE_SIZE, 0, &value);
            if (rc)
                break;
            G.cache_size = strtoul(value, &dummy, 0) * 1024UL * 1024UL;
            if (G.cache_size == 0) {
                rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcIncorrect);
                OUTMSG (("cache-size: bad value\n"));
                MiniUsage (args);
                break;
            }
        }
        
        rc = ArgsOptionCount (args, OPTION_MAX_WARN_DUP_FLAG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_WARN_DUP_FLAG, 0, &value);
            if (rc)
                break;
            G.maxWarnCount_DupConflict = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, option_unsorted, &pcount);
        if (rc)
            break;
        G.expectUnsorted = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_MAX_REC_COUNT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_REC_COUNT, 0, &value);
            if (rc)
                break;
            G.maxAlignCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_MAX_ERR_COUNT, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MAX_ERR_COUNT, 0, &value);
            if (rc)
                break;
            G.maxErrCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_MIN_MATCH, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            rc = ArgsOptionValue (args, OPTION_MIN_MATCH, 0, &value);
            if (rc)
                break;
            G.minMatchCount = strtoul(value, &dummy, 0);
        }
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_DUP, &pcount);
        if (rc)
            break;
        G.acceptBadDups = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_NOMATCH, &pcount);
        if (rc)
            break;
        G.acceptNoMatch = pcount > 0;
        
        rc = ArgsOptionCount (args, option_keep_mismatch_qual, &pcount);
        if (rc)
            break;
        G.keepMismatchQual = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_NO_CS, &pcount);
        if (rc)
            break;
        G.noColorSpace = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_NO_SECONDARY, &pcount);
        if (rc)
            break;
        G.noSecondary = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_TI, &pcount);
        if (rc)
            break;
        G.hasTI = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_ACCEPT_HARD_CLIP, &pcount);
        if (rc)
            break;
        G.acceptHardClip = pcount > 0;
        
        rc = ArgsOptionCount (args, OPTION_NOMATCH_LOG, &pcount);
        if (rc)
            break;
        if (pcount == 1)
        {
            KDirectory *dir;
            
            rc = ArgsOptionValue (args, OPTION_NOMATCH_LOG, 0, &value);
            if (rc) break;
            rc = KDirectoryNativeDir(&dir);
            if (rc) break;
            rc = KDirectoryCreateFile(dir, &G.noMatchLog, 0, 0664, kcmInit, value);
            KDirectoryRelease(dir);
            if (rc) break;
        }
        
        rc = ArgsOptionCount (args, OPTION_HEADER, &pcount);
        if (rc)
            break;
        if (pcount == 1) {
            rc = ArgsOptionValue (args, OPTION_HEADER, 0, &value);
            if (rc) break;
            rc = LoadHeader(&G.headerText, value, G.inpath);
            if (rc) break;
        }
        
        rc = ArgsParamCount (args, &pcount);
        if (rc) break;
        if (pcount == 0)
        {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcInsufficient);
            MiniUsage (args);
            break;
        }
        else if (pcount > sizeof(aligned)/sizeof(aligned[0])) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcParam, rcExcessive);
            (void)PLOGERR(klogErr, (klogErr, rc, "$(count) input files is too many, $(max) is the limit",
                        "count=%u,max=%u", (unsigned)pcount, (unsigned)(sizeof(aligned)/sizeof(aligned[0]))));
            break;
        }
        else {
            unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsParamValue(args, i, &value);
                if (rc) break;
                need += strlen(value) + 1;
            }
            nbsz = need;
        }
        
        rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
        if (rc)
            break;
        if (pcount > 0)
        {
            unsigned need = G.inpath ? (strlen(G.inpath) + 1) * pcount : 0;
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, &value);
                if (rc) break;
                need += strlen(value) + 1;
            }
            if (rc) break;
            nbsz += need;
        }
        
        name_buffer = malloc(nbsz);
        if (name_buffer == NULL) {
            rc = RC(rcApp, rcArgv, rcAccessing, rcMemory, rcExhausted);
            break;
        }
        
        rc = ArgsOptionCount (args, OPTION_UNALIGNED, &pcount);
        if (rc == 0) {
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsOptionValue(args, OPTION_UNALIGNED, i, &value);
                if (rc) break;
                
                unalgnd[n_unalgnd++] = name_buffer + next_name;
                rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
                if (rc) break;
                next_name += strlen(name_buffer + next_name) + 1;
            }
            if (rc) break;
        }
        else
            break;
        
        rc = ArgsParamCount (args, &pcount);
        if (rc == 0) {
            unsigned i;
            
            for (i = 0; i < pcount; ++i) {
                rc = ArgsParamValue(args, i, &value);
                if (rc) break;
                
                aligned[n_aligned++] = name_buffer + next_name;
                rc = PathWithBasePath(name_buffer + next_name, nbsz - next_name, value, G.inpath);
                if (rc) break;
                next_name += strlen(name_buffer + next_name) + 1;
            }
        }
        else
            break;
        
        rc = run(argv[0], n_aligned, (char const **)aligned, n_unalgnd, (char const **)unalgnd);
        break;
    }
    free(name_buffer);
    free((void *)G.headerText);
    free(G.refFiles);

    value = G.outpath ? strrchr(G.outpath, '/') : "/???";
    if( value == NULL ) {
        value = G.outpath;
    } else {
        value++;
    }
    if (rc) {
        (void)PLOGERR(klogErr, (klogErr, rc, "load failed",
                "severity=total,status=failure,accession=%s,errors=%u", value, G.errCount));
    } else {
        (void)PLOGMSG(klogInfo, (klogInfo, "loaded",
                "severity=total,status=success,accession=%s,errors=%u", value, G.errCount));
    }
    ArgsWhack(args);
    XMLLogger_Release(xml_logger);
    return rc;
}
Пример #21
0
static rc_t make_read_file( cg_dump_opts * opts, struct sg_lookup * lookup, KDirectory * dir, lane * l )
{
    rc_t rc;
    if ( opts->comp == oc_null )
    {
        l->reads = NULL;
        rc = 0;
    }
    else
    {
        const char * ext;
        switch( opts->comp )
        {
            case oc_none : ext = none_ext; break;
            case oc_gzip : ext = gzip_ext; break;
            case oc_bzip : ext = bzip_ext; break;
            case oc_null : ext = none_ext; break;
        }
        rc = KDirectoryCreateFile ( dir, &l->reads, true, 0664, kcmCreate, 
                                    "%s%.*s_%03d.tsv%s", opts->prefix, l->name->len, l->name->addr, l->chunk, ext );
        if ( rc != 0 )
        {
            (void)PLOGERR( klogErr, ( klogErr, rc, "cannot create reads-file for lane '$(lane)' / chunk '$(chunk)'",
                                      "lane=%S,chunk=%d", l->name, l->chunk ) );
        }
        else if ( opts->wbuff_size > 0 )
        {
            KFile * buffered;
            rc = KBufWriteFileMakeWrite ( &buffered, l->reads, opts->wbuff_size );
            if ( rc != 0 )
            {
                (void)PLOGERR( klogErr, ( klogErr, rc, "cannot create buffered reads-file for lane '$(lane)' / chunk '$(chunk)'",
                                          "lane=%S,chunk=%d", l->name, l->chunk ) );
            }
            else
            {
                KFileRelease( l->reads );
                l->reads = buffered;
            }
        }

        if ( rc == 0 && opts->comp != oc_none )
        {

            KFile * compressed = l->reads;
            if ( opts->comp == oc_bzip )
            {
                rc = KFileMakeBzip2ForWrite ( &compressed, l->reads );
            }
            else if ( opts->comp == oc_gzip )
            {
                rc = KFileMakeGzipForWrite ( &compressed, l->reads );
            }
            if ( rc != 0 )
            {
                (void)PLOGERR( klogErr, ( klogErr, rc, "cannot create bzip reads-file for lane '$(lane)' / chunk '$(chunk)'",
                                          "lane=%S,chunk=%d", l->name, l->chunk ) );
            }
            else
            {
                KFileRelease( l->reads );
                l->reads = compressed;
            }
        }

        if ( rc == 0 && opts->use_queue )
        {
            KFile * qf;
            rc  = KQueueFileMakeWrite ( &qf, l->reads, opts->qbytes, opts->qblock, opts->qtimeout );
            if ( rc != 0 )
            {
                (void)PLOGERR( klogErr, ( klogErr, rc, "cannot create background writer for lane '$(lane)' / chunk '$(chunk)'",
                                          "lane=%S,chunk=%d", l->name, l->chunk ) );
            }
            else
            {
                KFileRelease( l->reads );
                l->reads = qf;
            }
        }

    }

    if ( rc == 0 )
    {
        rc = write_header( opts, lookup, l );
    }

    return rc;
}
Пример #22
0
rc_t init_out_redir( out_redir * self, out_redir_mode_t mode, const char * filename, size_t bufsize )
{
    rc_t rc;
    KFile *output_file;

    if ( filename != NULL )
    {
        KDirectory *dir;
        rc = KDirectoryNativeDir( &dir );
        if ( rc != 0 )
            LOGERR( klogInt, rc, "KDirectoryNativeDir() failed" );
        else
        {
            rc = KDirectoryCreateFile ( dir, &output_file, false, 0664, kcmInit, "%s", filename );
            KDirectoryRelease( dir );
        }
    }
    else
        rc = KFileMakeStdOut ( &output_file );

    if ( rc == 0 )
    {
        KFile *temp_file;

        /* wrap the output-file in compression, if requested */
        switch ( mode )
        {
            case orm_gzip  : rc = KFileMakeGzipForWrite( &temp_file, output_file ); break;
            case orm_bzip2 : rc = KFileMakeBzip2ForWrite( &temp_file, output_file ); break;
            case orm_uncompressed : break;
        }
        if ( rc == 0 )
        {
            if ( mode != orm_uncompressed )
            {
                KFileRelease( output_file );
                output_file = temp_file;
            }

            /* wrap the output/compressed-file in buffering, if requested */
            if ( bufsize != 0 )
            {
                rc = KBufFileMakeWrite( &temp_file, output_file, false, bufsize );
                if ( rc == 0 )
                {
                    KFileRelease( output_file );
                    output_file = temp_file;
                }
            }

            if ( rc == 0 )
            {
                self->kfile = output_file;
                self->org_writer = KOutWriterGet();
                self->org_data = KOutDataGet();
                self->pos = 0;
                rc = KOutHandlerSet( out_redir_callback, self );
                if ( rc != 0 )
                    LOGERR( klogInt, rc, "KOutHandlerSet() failed" );
            }
        }
    }
    return rc;
}
Пример #23
0
static rc_t CreateConfig(char* argv0) {
    const KFile* std_in = NULL;
    KDirectory* native = NULL;
    KDirectory* dir = NULL;
    rc_t rc = 0;
    char* location = NULL;
    char* mod = NULL;
    char* wmod = NULL;
    char* refseq = NULL;
    if (rc == 0) {
        rc = KDirectoryNativeDir(&native);
    }
    if (rc == 0) {
        const char* def = NULL;
        char cwd[PATH_MAX + 1] = "";
        const char* home = getenv("HOME");
        if (home)
        {   def = home; }
        else {
            rc = VPathGetCWD(cwd, sizeof cwd);
            if (rc == 0 && cwd[0])
            {   def = cwd; }
            else
            {   def = "."; }
        }
        while (rc == 0) {
            char buffer[PATH_MAX + 1];
            rc = In("Specify configuration files directory", def, &location);
            if (rc == 0) {
                rc = KDirectoryOpenDirUpdate(native, &dir, false, location);
                if (rc == 0) {
                    rc = KDirectoryVVisit
                        (dir, false, scan_config_dir, buffer, ".", NULL);
                    if (rc != 0) {
                        if (rc ==
                             RC(rcExe, rcDirectory, rcListing, rcFile, rcExists)
                            && buffer[0])
                        {
                            PLOGERR(klogErr, (klogErr, rc,
                                "Configuration file found: $(dir)/$(name)",
                                "dir=%s,name=%s", location, buffer));
                            rc = 0;
                            buffer[0] = '\0';
                            continue;
                        }
                        else {
                            PLOGERR(klogErr, (klogErr, rc, "$(dir)/$(name)",
                                "dir=%s,name=%s", location, buffer));
                        }
                    }
                    break;
                }
                else if (GetRCObject(rc) == rcPath &&
                (GetRCState(rc) == rcIncorrect || GetRCState(rc) == rcNotFound))
                {
                    PLOGERR(klogErr,
                        (klogErr, rc, "$(path)", "path=%s", location));
                    rc = 0;
                }
                else { DISP_RC(rc, location); }
            }
        }
    }
    while (rc == 0) {
        const KDirectory* dir = NULL;
        rc = In("Specify refseq installation directory", NULL, &refseq);
        if (rc != 0)
        {   break; }
        rc = KDirectoryOpenDirRead(native, &dir, false, refseq);
        if (rc == 0) {
            RELEASE(KDirectory, dir);
            break;
        }
        else if (GetRCObject(rc) == rcPath
              && GetRCState(rc) == rcIncorrect)
        {
            PLOGERR(klogErr,
                (klogErr, rc, "$(path)", "path=%s", refseq));
            rc = 0;
        }
        DISP_RC(rc, refseq);
    }
    if (rc == 0) {
        char buffer[512];
        const char path[] = "vdb-config.kfg";
        uint64_t pos = 0;
        KFile* f = NULL;
        rc = KDirectoryCreateFile(dir, &f, false, 0664, kcmCreate, path);
        DISP_RC(rc, path);
        if (rc == 0) {
            int n = snprintf(buffer, sizeof buffer,
                "refseq/servers = \"%s\"\n", refseq);
            if (n >= sizeof buffer) {
                rc = RC(rcExe, rcFile, rcWriting, rcBuffer, rcInsufficient);
            }
            else {
                size_t num_writ = 0;
                rc = KFileWrite(f, pos, buffer, strlen(buffer), &num_writ);
                pos += num_writ;
            }
        }
        if (rc == 0) {
            const char buffer[] = "refseq/volumes = \".\"\n";
            size_t num_writ = 0;
            rc = KFileWrite(f, pos, buffer, strlen(buffer), &num_writ);
            pos += num_writ;
        }
        if (rc == 0 && mod && mod[0]) {
            int n = snprintf(buffer, sizeof buffer,
                "vdb/module/paths = \"%s\"\n", mod);
            if (n >= sizeof buffer) {
                rc = RC(rcExe, rcFile, rcWriting, rcBuffer, rcInsufficient);
            }
            else {
                size_t num_writ = 0;
                rc = KFileWrite(f, pos, buffer, strlen(buffer), &num_writ);
                pos += num_writ;
            }
        }
        if (rc == 0 && wmod && wmod[0]) {
            int n = snprintf(buffer, sizeof buffer,
                "vdb/wmodule/paths = \"%s\"\n", wmod);
            if (n >= sizeof buffer) {
                rc = RC(rcExe, rcFile, rcWriting, rcBuffer, rcInsufficient);
            }
            else {
                size_t num_writ = 0;
                rc = KFileWrite(f, pos, buffer, strlen(buffer), &num_writ);
                pos += num_writ;
            }
        }
        RELEASE(KFile, f);
    }
    free(mod);
    free(wmod);
    free(refseq);
    free(location);
    RELEASE(KDirectory, dir);
    RELEASE(KDirectory, native);
    RELEASE(KFile, std_in);
    DestroyStdin();
    return rc;
}
Пример #24
0
static
rc_t CCCopyDoFile (CCCopy * self)
{
    const KFile * original;
    rc_t rc = 0;
    enum KCreateMode mode;
    
    PLOGMSG (klogDebug9, "CCCopyDoFile $(f)", PLOG_S(f), self->path);

    if (! self->force)
    {
	/* if not forced replace mode we fail on existing file */
	mode = kcmCreate;
    }
    else
    {
	uint32_t tt;

	tt = KDirectoryPathType (self->out, self->path);
	switch (tt)
	{
	default:
	    PLOGMSG (klogWarn, "File exists and will be replaced in output directory $(f)",
		     PLOG_S(f), self->path);
	    break;
	    /* if the path to the file or the file do not exist no warning */
	case kptNotFound:
	case kptBadPath:
	    break;
	}
	tt = KDirectoryPathType (self->xml, self->path);
	switch (tt)
	{
	default:
	    PLOGMSG (klogWarn, "File exists and might be replaced in xml directory $(f)",
		     PLOG_S(f), self->path);
	    break;
	    /* if the path to the file or the file do not exist no warning */
	case kptNotFound:
	case kptBadPath:
	    break;
	}

	/* forced mode we create with init instead of create forcing a delete/create effect */
	mode = kcmInit;
    }

    /* open original source for read */
    rc = KDirectoryVOpenFileRead (self->in, &original, self->path, NULL);
    if (rc == 0)
    {
	KFile * copy;

	/* create copy output for write */
	rc = KDirectoryVCreateFile (self->out, &copy, false, 0644, mode|kcmParents,
				    self->path, NULL);
	if (rc == 0)
	{
	    KFile * fm;

	    /* create parallel <path>.md5 */
	    rc = KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5",
				       self->path);
	    if (rc == 0)
	    {
		KMD5SumFmt * md5f;

		/* make the .md5 an MD5 sum format file */
		rc = KMD5SumFmtMakeUpdate (&md5f, fm);
		if (rc == 0)
		{
		    union u
		    {
			KFile * kf;
			KMD5File * mf;
		    } outf;

		    /* combine the copy and MD5 file into our special KFile */
		    rc = KMD5FileMakeWrite (&outf.mf, copy, md5f, self->path);
		    if (rc == 0)
		    {
			const KFile * inf;

			/* release this outside reference to the MD5SumFMT leaving
			 * only the one internal to the KMD5File */
			KMD5SumFmtRelease (md5f);

			/* create the KTeeFile that copies reads from the
			 * original as writes to the copy.  Reads will be
			 * made by the cataloging process */
			rc = KFileMakeTeeRead (&inf, original, outf.kf);
			if (rc == 0)
			{
			    CCCat * po;
			    KTime_t mtime;

			    /* try to get a modification time for this pathname */
			    rc = KDirectoryVDate (self->in, &mtime, self->path, NULL);
			    if (rc != 0)
				mtime = 0;	/* default to ? 0? */

			    /* create the cataloger giving it the infile which
			     * is the KTeeFile, Indirectory, the XML directory,
			     * and the original path for the file */

			    rc = CCCatMake (&po, self->in, self->xml, inf, self->md5,
					    self->ff, mtime, self->tree, false, 0, self->path);
			    if (rc == 0)
			    {
				/* do the catalog (and thus copy) */
				rc = CCCatDo(po);
				/* release the cataloger object */
				CCCatRelease (po);
			    }
			    else
				pLOGERR (klogDebug6, rc, "failure in CCCatMake $(P)",
				     PLOG_S(P), self->path);
			    /* release the infile which will complete a  copy 
			     * regardless of the state of the cataloger */
			    KFileRelease (inf);
/* 			    return rc; */
			}
			else
			{
			    KFileRelease (outf.kf);
			    KFileRelease (original);
			    pLOGERR (klogDebug4, rc, "failure with kfilemaketeeread $(P)",
				     PLOG_S(P), self->path);
			} /* rc = KFileMakeTeeRead (&inf, original, outf.kf);*/
		    }
		    else
		    {
			KFileRelease (copy);
			KMD5SumFmtRelease (md5f);
			pLOGERR (klogDebug4, rc, "failure with KMD5FileMakeWrite $(P)",
				 PLOG_S(P), self->path);
		    } /* KMD5FileMakeWrite (&outf.mf, copy, md5f, self->path); */
		} /* KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5", */
		else
		    pLOGERR (klogDebug4, rc, "failure with KMD5SumFmtMakeUpdate $(P)",
			     PLOG_S(P), self->path);

		KFileRelease (fm);
	    } /* KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5", */
	    else
		pLOGERR (klogDebug4, rc, "failure with KDirectoryCreateFile $(P).md5",
			 PLOG_S(P), self->path);
	    KFileRelease (copy);
	} /* rc = KDirectoryVCreateFile (self->out, &copy, false, 0644, mode|kcmParents, */
	else
	    pLOGERR (klogDebug4, rc, "failure with KDirectoryVCreateFile $(P)",
		     PLOG_S(P), self->path);
	KFileRelease (original);
    } /* rc = KDirectoryVOpenFileRead (self->in, &original, self->path, NULL); */
    else
	pLOGERR (klogDebug4, rc, "failure with KDirectoryVOpenFileRead $(pP)",
		 PLOG_S(P), self->path);
    return rc;
}