예제 #1
0
파일: sra-kar.c 프로젝트: ncbi/sra-tools
/* KMain - EXTERN
 *  executable entrypoint "main" is implemented by
 *  an OS-specific wrapper that takes care of establishing
 *  signal handlers, logging, etc.
 *
 *  in turn, OS-specific "main" will invoke "KMain" as
 *  platform independent main entrypoint.
 *
 *  "argc" [ IN ] - the number of textual parameters in "argv"
 *  should never be < 0, but has been left as a signed int
 *  for reasons of tradition.
 *
 *  "argv" [ IN ] - array of NUL terminated strings expected
 *  to be in the shell-native character set: ASCII or UTF-8
 *  element 0 is expected to be executable identity or path.
 */
rc_t CC KMain ( int argc, char *argv [] )
{
    Args * args;
    rc_t rc;

    default_log_level = KLogLevelGet();

    rc = ArgsMakeAndHandle (&args, argc, argv, 1, Options, sizeof Options / sizeof (OptDef));
    if (rc == 0)
    {
        do
        {
            srakar_parms pb;
            KDirectory * pwd;
            const char * name;
            uint32_t pcount;
            char archive_name [256];

            rc = KDirectoryNativeDir (&pwd);
            if (rc)
                break;

            pb.lite = false;
            pb.force = false;
            pb.dir = pwd;

            rc = ArgsOptionCount (args, OPTION_LITE, &pcount);
            if (rc)
                break;

            if (pcount == 1)
            {
                STSMSG (1, ("Using lite option"));
                pb.lite = true;
            }

#if USE_FORCE
            rc = ArgsOptionCount (args, OPTION_FORCE, &pcount);
            if (rc)
                break;

            if (pcount == 1)
            {
                STSMSG (1, ("Using force option"));
                pb.force = true;
            }
#endif
            rc = ArgsParamCount (args, &pcount);
            if (rc)
                break;

            if (pcount == 0)
            {
                KOutMsg ("missing source table\n");
                MiniUsage (args);
                rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcInsufficient);
                break;
            }
            else if (pcount > 2)
            {
                KOutMsg ("Too many parameters\n");
                MiniUsage (args);
                rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcExcessive);
                break;
            }

            rc = ArgsParamValue (args, 0, (const void **)&pb.src_path);
            if (rc)
            {
                KOutMsg ("failure to get source path/name\n");
                break;
            }

            pb.dst_path = archive_name;

            name = string_rchr (pb.src_path, string_size (pb.src_path), '/');
            if (name == NULL)
                name = pb.src_path;

            if (pcount == 1)
            {
                size_t size;

                rc = string_printf (archive_name, sizeof (archive_name), & size,
                                    "%s%s", name, pb.lite ? ".lite.sra" : ".sra");
                if ( rc != 0 )
                {
                    rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcInsufficient);
                    PLOGERR (klogFatal, (klogFatal, rc, "Failure building archive name $(P)", "P=%s", archive_name));
                    break;
                }
            }
            else
            {
                rc = ArgsParamValue (args, 1, (const void **)&pb.dst_path);
                if (rc)
                {
                    LOGERR (klogInt, rc, "failure to get destination path");
                    break;
                }
            }

            if (rc == 0)
            {
                KPathType kpt;

                kpt = KDirectoryPathType (pwd, "%s", pb.dst_path);

                switch (kpt & ~kptAlias)
                {
                case kptNotFound:
                    /* found nothing so assume its a valid new file name
                     * not gonna tweak extensions at this point but that can be upgraded */
                    break;
                case kptFile:
                    /* got a file name, use it  - would need force... */
                    if (pb.force == false)
                    {
                        rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcBusy);
                        PLOGERR (klogFatal, (klogFatal, rc,
                                             "Output file already exists $(P)",
                                             "P=%s", archive_name));
                    }
                    break;

                case kptDir:
                {
                    size_t size;

                    rc = string_printf (archive_name, sizeof (archive_name), & size,
                                        "%s/%s%s", pb.dst_path, name, pb.lite ? ".lite.sra" : ".sra");
                    if ( rc != 0 )
                    {
                        rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcInsufficient);
                        PLOGERR (klogFatal, (klogFatal, rc, "Failure building archive name $(P)", "P=%s", archive_name));
                        break;
                    }
                    pb.dst_path = archive_name;
                    break;
                }
                default:
                    rc = RC (rcExe, rcArgv, rcParsing, rcPath, rcInvalid);
                    break;
                }
                if (rc == 0)
                {
                    STSMSG (1,("Creating archive (%s) from table (%s)\n", pb.dst_path, pb.src_path));

                    rc = run (&pb);
                    STSMSG (5, ("Run exits with %d %R\n", rc, rc));
                }
            }

        } while (0);
    }
    STSMSG (1, ("Exit status %u %R", rc, rc));
    return rc;
}
예제 #2
0
파일: proxy.c 프로젝트: ncbi/ncbi-vdb
rc_t KNSProxiesVSetHTTPProxyPath ( KNSProxies * self,
    const char * fmt, va_list args, bool clear )
{
    rc_t rc = 0;

    if ( clear )
        rc = KNSProxiesHttpProxyClear ( self );

    if ( rc == 0 && fmt != NULL && fmt [ 0 ] != 0 ) {
        size_t psize;
        char path [ 4096 * 2 ];
        const char * p = path;
        rc = string_vprintf ( path, sizeof path, & psize, fmt, args );
        if ( rc == 0 ) {
            while ( psize != 0 ) {
                size_t s = psize;
                uint16_t proxy_port = 0;
                const char * colon = NULL;
                const char * comma = string_chr ( p, psize, ',' );
                if ( comma != NULL ) {
#ifdef MULTIPLE_PROXIES
                    s = comma - p;
#else
                    rc = RC ( rcNS, rcMgr, rcUpdating, rcPath, rcInvalid );
                    break;
#endif
                }

                colon = string_chr ( p, s, ':' );
                if ( colon != NULL ) {
                    char * end = NULL;
                    const char * port_spec = NULL;
                    long port_num = 0;

                    int have = colon - p;
                    int remains = s - have;
                    if ( remains > 2 ) {
                        assert ( colon [ 0 ] == ':' );
                        if ( colon [ 1 ] == '/' && colon [ 2 ] == '/' ) {
          /* strip off the scheme from proxy specification: it is ignored now */
                            psize -= have + 3;
                            p = colon + 3;
                            if ( psize == 0 )
                                return RC ( rcNS, rcMgr, rcUpdating,
                                            rcPath, rcInvalid );
                            continue;
                        }
                    }

                    port_spec = colon + 1;
             /* it is true that some day we might read symbolic port names... */
                    port_num = strtol ( port_spec, & end, 10 );
                    if ( port_num <= 0 || port_num >= 0x10000) {
                        PLOGERR ( klogErr, ( klogErr, rc,
                            "Proxy '$(proxy)' was ignored",
                            "proxy=%.*s", ( int ) s, p ) );
                        rc = RC ( rcNS, rcMgr, rcUpdating, rcPath, rcInvalid );
                    }
                    else if ( end [ 0 ] != 0 && comma == NULL ) {
                        if ( * end != '/' && * end != '?' ) {
                            /* skip everyting after '/' or '?' */
                            PLOGERR ( klogErr, ( klogErr, rc,
                                "Proxy '$(proxy)' was ignored",
                                "proxy=%.*s", ( int ) s, p ) );
                                rc = RC ( rcNS, rcMgr, rcUpdating,
                                          rcPath, rcInvalid );
                        }
                    }
                    if ( rc == 0 ) {
                        proxy_port = ( uint16_t ) port_num;
                        s = colon - p;
                    }
                }

                if ( rc == 0 )
                    rc = KNSProxiesAddHttpProxyPath ( self, p, s, proxy_port );

                if ( comma == NULL)
                    psize = 0;
                else {
                    s = comma - p + 1;
                    if ( s > psize )
                        psize = 0;
                    else {
                        psize -= s;
                        p += s;
                    }
                }
            }
        }
    }

    return rc;
}
예제 #3
0
static rc_t read_from_ref_node( ref_node * node, 
                                int32_t ref_offset, uint32_t ref_len,
                                uint8_t *exclude_vector,
                                uint32_t *active )
{
    rc_t rc = 0;
    uint64_t row_id = ( ref_offset / node->read_len ) + 1;
    uint8_t *dst = exclude_vector;
    uint32_t remaining = ref_len;
    uint32_t src_ofs = ref_offset % node->read_len;

    while ( remaining > 0 && rc == 0 )
    {
        uint32_t elem_bits, boff, rlen;
        const uint8_t *src;
        rc = VCursorCellDataDirect ( node->cur, row_id, node->hits_idx,
                                     &elem_bits, (const void**)&src, &boff, &rlen );
        if ( rc != 0 )
        {
            PLOGERR( klogInt, ( klogInt, rc, 
                 "error to read $(col_name) from 1st row in table $(db_name).$(tab_name)",
                 "col_name=%s,db_name=%S,tab_name=%s",
                 HITS_COLUMN, node->name, HITMAP_TAB ) );
        }
        else
        {
            if ( src_ofs >= rlen )
            {
                rc = RC( rcApp, rcNoTarg, rcReading, rcParam, rcInvalid );
                PLOGERR( klogInt, ( klogInt, rc, 
                     "error: try to read more data than are in var-loc $(tab_name)",
                     "tab_name=%S", node->name ) );
            }
            else
            {
                uint32_t to_copy = ( rlen - src_ofs );
                if ( to_copy > remaining )
                {
                    to_copy = remaining;
                }
                src += src_ofs;

                memmove( dst, src, to_copy );
                dst += to_copy;
                remaining -= to_copy;
                src_ofs = 0;
                row_id ++;

                node->bytes_requested += to_copy;
            }
        }
    }
    *active = 0;
    if ( rc == 0 )
    {
        for ( src_ofs = 0; src_ofs < ref_len; ++src_ofs )
        {
            if ( exclude_vector[ src_ofs ] > 0 )
            {
                ( *active )++;
            }
        }
    }

    return rc;
}
예제 #4
0
파일: sra-kar.c 프로젝트: ncbi/sra-tools
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;
}
예제 #5
0
static rc_t detect_read_len( ref_node *node )
{
    const VCursor *temp_cursor;
    rc_t rc = VTableCreateCursorRead ( node->tab, &temp_cursor );
    if ( rc != 0 )
    {
        PLOGERR( klogInt, ( klogInt, rc, 
             "error to create cursor on table $(tab_name)",
             "tab_name=%S", node->name ) );
    }
    else
    {
        uint32_t idx;
        rc = VCursorAddColumn ( temp_cursor, &idx, MAXLEN_COLUMN );
        if ( rc != 0 )
        {
            PLOGERR( klogInt, ( klogInt, rc, 
                 "error to add column $(col_name) to cursor for table $(db_name).$(tab_name)",
                 "col_name=%s,db_name=%S,tab_name=%s",
                 MAXLEN_COLUMN, node->name, HITMAP_TAB ) );
        }
        else
        {
            rc = VCursorOpen( temp_cursor );
            if ( rc != 0 )
            {
                PLOGERR( klogInt, ( klogInt, rc, 
                     "error to open cursor for table $(db_name).$(tab_name to read $(col_name)",
                     "col_name=%s,db_name=%S,tab_name=%s",
                     MAXLEN_COLUMN, node->name, HITMAP_TAB ) );
            }
            else
            {
                uint32_t elem_bits, boff, row_len;
                const void *base;
                rc = VCursorCellDataDirect ( temp_cursor, 1, idx,
                                             &elem_bits, &base, &boff, &row_len );
                if ( rc != 0 )
                {
                    PLOGERR( klogInt, ( klogInt, rc, 
                         "error to read $(col_name) from 1st row in table $(db_name).$(tab_name)",
                         "col_name=%s,db_name=%S,tab_name=%s",
                         MAXLEN_COLUMN, node->name, HITMAP_TAB ) );
                }
                else
                {
                    node->read_len = *((uint32_t *)base);
                    if ( node->read_len == 0 )
                    {
                        rc = RC( rcApp, rcNoTarg, rcReading, rcParam, rcInvalid );
                        PLOGERR( klogInt, ( klogInt, rc, 
                             "$(col_name) == 0 discoverd in table $(db_name).$(tab_name)",
                             "col_name=%s,db_name=%S,tab_name=%s",
                             MAXLEN_COLUMN, node->name, HITMAP_TAB ) );
                    }
                }
            }
        }
        VCursorRelease( temp_cursor );
    }
    return rc;
}
예제 #6
0
static ref_node * make_ref_node( ref_exclude *exclude, const String * s )
{
    ref_node * res = calloc( 1, sizeof * res );
    if ( res != NULL )
    {
        if ( StringCopy ( &res->name, s ) != 0 )
        {
            free( res );
            res = NULL;
        }
    }
    if ( res != NULL && exclude->path != NULL )
    {
        char buf[ 1024 ];
        size_t num_writ;
        rc_t rc = string_printf ( buf, sizeof buf, &num_writ, 
                                  "%s/%S", exclude->path, s );
        if ( rc != 0 )
        {
            PLOGERR( klogInt, ( klogInt, rc, 
                 "error to assemble path to exclude-table $(tab_name)",
                 "tab_name=%S", s ) );
        }
        else
        {
            rc = VDBManagerOpenDBRead ( exclude->mgr, &res->db, NULL, "%s", buf );
            if ( rc != 0 )
            {
                PLOGERR( klogInt, ( klogInt, rc, 
                     "error to open exclude-table $(db_name)",
                     "db_name=%s", buf ) );
                /*
                it can be OK if the database/table cannot be found!
                */
                rc = 0;
            }
            else
            {
                rc = VDatabaseOpenTableRead ( res->db, &res->tab, "HITMAP" );
                if ( rc != 0 )
                {
                    PLOGERR( klogInt, ( klogInt, rc, 
                         "error to open exclude-table 'HITMAP' in $(db_name)",
                         "db_name=%s", buf ) );
                    /*
                    it can be OK if the database/table cannot be found!
                    */
                    rc = 0;
                }
                else
                {
                    rc = detect_read_len( res );
                    if ( rc == 0 )
                    {
                        rc = prepare_ref_node( res );
                    }
                }
            }
        }
    }
    return res;
}
예제 #7
0
static
rc_t nenctool (const char * srcstr, const char * dststr, bool force)
{
    VFSManager * mgr;
    rc_t rc;

    rc = VFSManagerMake (&mgr);
    if (rc)
        LOGERR (klogInt, rc, "failed to create file system manager");
    else
    {
        VPath * srcpath;

        rc = VFSManagerMakePath (mgr, &srcpath, "%s", srcstr);
        if (rc)
            PLOGERR (klogErr,
                     (klogErr, rc, "Failed to parse source path '$(path)'",
                      "path=%s", srcstr));
        else
        {
            VPath * dstpath;

            rc = VFSManagerMakePath (mgr, &dstpath, "%s", dststr);
            if (rc)
                PLOGERR (klogErr,
                         (klogErr, rc, "Failed to parse destination path '$(path)'",
                          "path=%s", dststr));
            else
            {
                const KFile * srcfile;

                rc = VFSManagerOpenFileRead (mgr, &srcfile, srcpath);
                if (rc)
                    PLOGERR (klogErr,
                             (klogErr, rc, "Failed to open source path '$(path)'",
                              "path=%s", srcstr));
                else
                {
                    KFile * dstfile;

                    rc = VFSManagerCreateFile (mgr, &dstfile, false, 0666,
                                               kcmParents | (force ? kcmInit : kcmCreate),
                                               dstpath);
                    if (rc)
                        PLOGERR (klogErr,
                                 (klogErr, rc, "failed to open destination path '$(path)'",
                                  "path=%s", dststr));
                    else
                    {
                        rc = copy_file (srcstr, dststr, srcfile, dstfile);
                        if (rc)
                        {
                            PLOGERR (klogErr,
                                     (klogErr, rc, "failed to copy '$(S)' to '$(D)'",
                                      "S=%s,D=%s", srcstr, dststr));

                            VFSManagerRemove (mgr, true, dstpath);
                        }

                        KFileRelease (dstfile);
                    }
                    KFileRelease (srcfile);
                }
                VPathRelease (dstpath);
            }
            VPathRelease (srcpath);
        }
        VFSManagerRelease (mgr);
    }
    return rc;
}
예제 #8
0
static
rc_t VCursorFlushPageInt ( VCursor *self )
{
    rc_t rc;

    if ( self == NULL )
        rc = RC ( rcVDB, rcCursor, rcFlushing, rcSelf, rcNull );
    else if ( self -> read_only )
        rc = RC ( rcVDB, rcCursor, rcFlushing, rcCursor, rcReadonly );
    else
    {
        int64_t end_id;
#if ! VCURSOR_FLUSH_THREAD
        run_trigger_prod_data pb;
#endif
        switch ( self -> state )
        {
        case vcConstruct:
            rc = RC ( rcVDB, rcCursor, rcFlushing, rcCursor, rcNotOpen );
            break;
        case vcFailed:
            rc = RC ( rcVDB, rcCursor, rcFlushing, rcCursor, rcInvalid );
            break;
        case vcRowOpen:
            rc = RC ( rcVDB, rcCursor, rcFlushing, rcCursor, rcBusy );
            break;
        default:

            /* ignore request if there is no page to commit */
            if ( self -> start_id == self -> end_id )
            {
                /* the cursor should be in unwritten state,
                   where the row_id can be reset but drags
                   along the other markers. */
                assert ( self -> end_id == self -> row_id );
                return 0;
            }

#if VCURSOR_FLUSH_THREAD
            MTCURSOR_DBG (( "VCursorFlushPageInt: going to acquire lock\n" ));
            /* get lock */
            rc = KLockAcquire ( self -> flush_lock );
            if ( rc != 0 )
                return rc;

            MTCURSOR_DBG (( "VCursorFlushPageInt: have lock\n" ));

            /* make sure that background thread is ready */
            while ( self -> flush_state == vfBusy )
            {
                MTCURSOR_DBG (( "VCursorFlushPageInt: waiting for background thread\n" ));
                rc = KConditionWait ( self -> flush_cond, self -> flush_lock );
                if ( rc != 0 )
                {
                    LOGERR ( klogSys, rc, "VCursorFlushPageInt: wait failed - exiting" );
                    KLockUnlock ( self -> flush_lock );
                    return rc;
                }
            }

            if ( self -> flush_state != vfReady )
            {
                if ( self -> flush_state != vfBgErr )
                    rc = RC ( rcVDB, rcCursor, rcFlushing, rcCursor, rcInconsistent );
                else
                {
                    rc_t rc2;
                    MTCURSOR_DBG (( "VCursorFlushPageInt: waiting on thread to exit\n" ));
                    rc = KThreadWait ( self -> flush_thread, & rc2 );
                    if ( rc == 0 )
                    {
                        rc = rc2;
                        MTCURSOR_DBG (( "VCursorFlushPageInt: releasing thread\n" ));
                        KThreadRelease ( self -> flush_thread );
                        self -> flush_thread = NULL;
                    }
                }

                PLOGERR ( klogInt, (klogInt, rc, "VCursorFlushPageInt: not in ready state[$(state)] - exiting","state=%hu",self -> flush_state ));
                KLockUnlock ( self -> flush_lock );
                return rc;
            }

            MTCURSOR_DBG (( "VCursorFlushPageInt: running buffer page\n" ));
#endif

            /* first, tell all columns to bundle up their pages into buffers */
            end_id = self -> end_id;
            rc = RC ( rcVDB, rcCursor, rcFlushing, rcMemory, rcExhausted );
            if ( VectorDoUntil ( & self -> row, false, WColumnBufferPage, & end_id ) )
            {
                VectorForEach ( & self -> row, false, WColumnDropPage, NULL );
                self -> flush_state = vfFgErr;
            }
            else
            {
                /* supposed to be constant */
                assert ( end_id == self -> end_id );
#if VCURSOR_FLUSH_THREAD
                MTCURSOR_DBG (( "VCursorFlushPageInt: pages buffered - capturing id and count\n" ));
                self -> flush_id = self -> start_id;
                self -> flush_cnt = self -> end_id - self -> start_id;

                self -> start_id = self -> end_id;
                self -> end_id = self -> row_id + 1;
                self -> state = vcReady;

                MTCURSOR_DBG (( "VCursorFlushPageInt: state set to busy - signaling bg thread\n" ));
                self -> flush_state = vfBusy;
                rc = KConditionSignal ( self -> flush_cond );
                if ( rc != 0 )
                    LOGERR ( klogSys, rc, "VCursorFlushPageInt: condition returned error on signal" );
#else
                /* run all validation and trigger productions */
                pb . id = self -> start_id;
                pb . cnt = self -> end_id - self -> start_id;
                pb . rc = 0;
                if ( ! VectorDoUntil ( & self -> trig, false, run_trigger_prods, & pb ) )
                {
                    self -> start_id = self -> end_id;
                    self -> end_id = self -> row_id + 1;
                    self -> state = vcReady;
                }

                rc = pb . rc;

                /* drop page buffers */
                VectorForEach ( & self -> row, false, WColumnDropPage, NULL );
#endif
            }

#if VCURSOR_FLUSH_THREAD
            MTCURSOR_DBG (( "VCursorFlushPageInt: unlocking\n" ));
            KLockUnlock ( self -> flush_lock );
#endif
        }
    }

    return rc;
}
예제 #9
0
static
rc_t copy_file (const char * src, const char * dst, const KFile * fin, KFile *fout)
{
    rc_t rc;
    uint8_t	buff	[64 * 1024];
    size_t	num_read;
    uint64_t	inpos;
    uint64_t	outpos;

    assert (src);
    assert (dst);
    assert (fin);
    assert (fout);

    inpos = 0;
    outpos = 0;

#if 1
    for (inpos = 0; ; inpos += num_read)
    {
        rc = Quitting ();
        if (rc)
        {
            LOGMSG (klogFatal, "Received quit");
            break;
        }
        else
        {
            rc = KFileReadAll (fin, inpos, buff, sizeof (buff), &num_read);
            if (rc)
            {
                PLOGERR (klogErr,
                         (klogErr, rc,
                          "Failed to read from file $(F) at $(P)",
                          "F=%s,P=%lu", src, inpos));
                break;
            }
            else if (num_read)
            {
                size_t num_writ;

                rc = KFileWriteAll (fout, inpos, buff, num_read, &num_writ);
                if (rc)
                {
                    PLOGERR (klogErr,
                             (klogErr, rc,
                              "Failed to write to file $(F) at $(P)",
                              "F=%s,P=%lu", dst, outpos));
                    break;
                }
                else if (num_writ != num_read)
                {
                    rc = RC (rcExe, rcFile, rcWriting, rcFile, rcInsufficient);
                    PLOGERR (klogErr,
                             (klogErr, rc,
                              "Failed to write all to file $(F) at $(P)",
                              "F=%s,P=%lu", dst, outpos));
                    break;
                }
            }
            else 
                break;
        }
    }
#else
    do
    {
        rc = Quitting ();
        if (rc)
        {
            LOGMSG (klogFatal, "Received quit");
            break;
        }
        rc = KFileRead (fin, inpos, buff, sizeof (buff), &num_read);
        if (rc)
        {
            PLOGERR (klogErr,
                     (klogErr, rc,
                      "Failed to read from file $(F) at $(P)",
                      "F=%s,P=%lu", src, inpos));
            break;
        }
        else if (num_read > 0)
        {
            size_t to_write;

            inpos += (uint64_t)num_read;

            STSMSG (2, ("Read %zu bytes to %lu", num_read, inpos));

            to_write = num_read;
            while (to_write > 0)
            {
                size_t num_writ;
                rc = KFileWrite (fout, outpos, buff, num_read, &num_writ);
                if (rc)
                {
                    PLOGERR (klogErr,
                             (klogErr, rc,
                              "Failed to write to file $(F) at $(P)",
                              "F=%s,P=%lu", dst, outpos));
                    break;
                }
                outpos += num_writ;
                to_write -= num_writ;
            }
        }
        if (rc)
            break;
    } while (num_read != 0);
#endif
    return rc;
}
예제 #10
0
파일: syspath.c 프로젝트: mariux/sratoolkit
rc_t CC VPathTransformSysPath (VPath * self)
{
    char * pc;
    size_t lim;
    size_t ix;
    rc_t rc = 0;

    pc = (char *)(self->path.addr);
    lim = StringSize (&self->path);

    if (lim == 0)
        return 0;

    /* -----
     * toss out windows/dos device names
     */
    for (ix = 0; reserved_device_names[ix] != NULL; ++ix)
        if (strcmp (reserved_device_names[ix], pc) == 0)
            return RC (rcFS, rcPath, rcConstructing, rcPath, rcIncorrect);

    /* -----
     * look for for mingw and cygwin full paths and make them windowish
     * NOTE we could screw things up here with bad strings like
     * /cygdrive/haha/fooled-you
     */

    if (pc[0] == '/')
    {
        static const char cygdrive [] = "/cygdrive/";

        if (strncmp (pc, cygdrive, sizeof cygdrive - 1) == 0)
        {
            pc += sizeof cygdrive - 2;
            lim -= sizeof cygdrive - 2;
            /* NOTE: if the macro changes this could fail! */
            StringInit (&self->path, pc,
                        StringSize(&self->path) - sizeof cygdrive - 2,
                        StringLength(&self->path) - sizeof cygdrive - 2);
        }

        /* if here we had a mingw or cygwin specification */
        if (isalpha (pc[1]) && (pc[2] == '/'))
        {
            /* change it back to a windows specification */
            pc[0] = pc[1];
            pc[1] = ':';
        }
    }

    /* change from  Windows '/' to posix '/' segment dividers */
    for (ix = 0; ix < lim; ++ix)
        if (pc [ix] == '\\')
            pc [ix] = '/';

    /* look for stray ':' characters */
    for (ix = 0; ix < lim; ++ix)
    {
        if (pc[ix] == ':')
        {
            rc = RC (rcFS, rcPath, rcConstructing, rcPath, rcInvalid);
            PLOGERR (klogErr,
                     (klogErr, rc, "incorrect use of ':' in path '$(path)'",
                      "path=%s", pc));
            return rc;
        }
        if (ix == 0)
            ++ix;
    }

    if ((lim > 1) && (pc[1] == ':'))
    {
        if (!isalpha (pc[0]))
        {
            rc = RC (rcFS, rcPath, rcConstructing, rcPath, rcInvalid);
            PLOGERR (klogErr,
                     (klogErr, rc, "invalid character as drive letter '$(c)' ($(d)) in path '$(S)'",
                      "c=%c,d=%d,s=%s",pc[0],pc[0],pc));
            return rc;
        }
        /* we don't support drive relative addressing at this point
         * we should correct this in the future */
        if (pc[2] != '/')
        {
            rc = RC (rcFS, rcPath, rcConstructing, rcPath, rcIncorrect);
            PLOGERR (klogErr,
                     (klogErr, rc, "drive relative addressing not currently supported '$(path)'",
                      "path=%s", pc));
            return rc;
        }
    }

    if ((pc[0] == '/') && (pc[1] == '/')) /* UNC form */
    {
        if (pc[2] == '.') /* device space name - we don't handle these */
            return RC (rcFS, rcPath, rcConstructing, rcPath, rcIncorrect);

        if (pc[2] == '?') /* special space name - we don't handle these */
            return RC (rcFS, rcPath, rcConstructing, rcPath, rcIncorrect);
    }

/* possible future stuff? */
#if DO_MORE_STUFF_TO_VALIDATE_PATH
    /* -----
     * this size thing is turned off to allow paths into archives
     */
    ix = string_size (ipc = inpath);
    if (ix > MAX_PATH)
        return RC (rcFS, rcPath, rcConstructing, rcPath, rcTooLong);
    {
        DWORD insize = ilen;
        DWORD outlen;

        outlen = GetFullPathNameA (ipc, (DWORD)sizeof (temp), temp, NULL);

        if (outlen == 0)
            return RC (rcFS, rcPath, rcConstructing, rcPath, rcInvalid);
        if (outlen > sizeof temp)
            return RC (rcFS, rcPath, rcConstructing, rcPath, rcTooShort);

        ipc = temp;

        opc[0] = '/';
        opc[1] = ipc [0];

        ix = jx = 2;
    }

    do
    {
        switch (ipc[ix])
        {
        case '\0':
            normalize_done = true;
            /* fall through */
        default:
            opc[jx++] = ipc[ix++];
            break;

        case '\\':
            opc[jx++] = '/';
            ++ix;
            break;

        case '\x1': /* never allowed */
        case '\x2':
        case '\x3':
        case '\x4':
        case '\x5':
        case '\x6':
        case '\x7':
        case '\x8':
        case '\x9':
        case '\xA':
        case '\xB':
        case '\xC':
        case '\xD':
        case '\xE':
        case '\xF':
        case '\x10':
        case '\x11':
        case '\x12':
        case '\x13':
        case '\x14':
        case '\x15':
        case '\x16':
        case '\x17':
        case '\x18':
        case '\x19':
        case '\x1A':
        case '\x1B':
        case '\x1C':
        case '\x1D':
        case '\x1E':
        case '\x1F':
        case ':':
        case '<':
        case '>':
        case '"':
        case '|':
        case '*':
        case '?':
            rc = RC (rcFS, rcPath, rcConstructing, rcPath, rcInvalid);
            PLOGERR (klogErr,
                     (klogErr, rc, "invalid character '$(c)' ($(d)) in path '$(s)'",
                      "c=%c,d=%d,s=%s",ipc[ix],ipc[ix],ipc));
            return rc;
        }
    } while (!normalize_done);

    self->asciz_size = ix - 1;

#endif
    /* put drive based paths (back?) into our form */
    if ((lim > 1) && (pc[1] == ':'))
    {
        pc[1] = pc[0];
        pc[0] = '/';
        self->scheme = vpuri_ncbi_vfs;
    }
    return 0;
}
예제 #11
0
rc_t CC KMain(int argc, char* argv[])
{
    const char table[] = "/home/klymenka/REDACT-IN";
    const char name[] = "READ_FILTER";

    rc_t rc = 0;

    bool locked = false;

    VDBManager* mgr;
    VTable *tbl;
    const VCursor *rCursor = NULL;

    int i;

    LogLevelSet("info");

    for (i = 1; i < argc; ++i) {
        if (!strcmp(argv[i], "-+")) {
            if (++i <= argc) {
#if _DEBUGGING
	        KDbgSetString(argv[i]);
#endif
	    }
        }
    }

  /*KDbgSetString("VDB");*/

    if (rc == 0) {
/* +01: ManagerMake */
        LOGMSG(klogInfo, "VDBManagerMakeUpdate");
        rc = VDBManagerMakeUpdate(&mgr, NULL);
        DISP_RC_INT(rc, "while calling VDBManagerMakeUpdate");
    }

    if (rc == 0) {
        rc = VDBManagerWritable(mgr, "%s", table);
        if (GetRCState(rc) == rcLocked) {
            LOGMSG(klogInfo, "VDBManagerUnlock");
            rc = VDBManagerUnlock(mgr, "%s", table);
            DISP_RC_INT(rc, "while calling VDBManagerUnlock");
            locked = true;
        }
    }

    if (rc == 0) {
/* +02: OpenTable */
        PLOGMSG(klogInfo, (klogInfo,
            "VDBManagerOpenTableUpdate(\"$(t)\")", "t=%s", table));
        rc = VDBManagerOpenTableUpdate
            (mgr, &tbl, NULL, "%s", table);
        if (rc != 0) {
            PLOGERR(klogErr, (klogErr, rc,
                "while opening VTable '$(path)'", "path=%s", table));
        }
    }

    if (rc == 0) {
/* +03: CreateCursorRead */
        LOGMSG(klogInfo, "VDBManagerUnlock");
        rc = VTableCreateCursorRead(tbl, &rCursor);
        DISP_RC_INT(rc, "while creating read cursor");

#if 1
        if (rc == 0) {
            uint32_t idx;
            PLOGMSG(klogInfo, (klogInfo,
                "VCursorAddColumn(read cursor, \"$(n)\")", "n=%s", name));
            rc = VCursorAddColumn(rCursor, &idx, "%s", name);
            if (rc != 0) {
                PLOGERR(klogErr, (klogErr, rc,
                    "while adding $(name) to read cursor", "name=%s", name));
            }
        }
#endif
        if (rc == 0) {
            LOGMSG(klogInfo, "VCursorOpen(read cursor)");
            rc = VCursorOpen(rCursor);
            DISP_RC_INT(rc, "while opening read cursor");
        }
    }

    if (rc == 0) {
        VCursor *cursor;
        uint32_t idx;
/* +04: CreateCursorWrite */
        LOGMSG(klogInfo, "VTableCreateCursorWrite");
        rc = VTableCreateCursorWrite(tbl, &cursor, kcmInsert);
        DISP_RC_INT(rc, "while creating write cursor");
        if (rc == 0) {
            PLOGMSG(klogInfo, (klogInfo,
                "VCursorAddColumn(write cursor, \"$(n)\")", "n=%s", name));
            rc = VCursorAddColumn(cursor, &idx, "%s", name);
            if (rc != 0) {
                PLOGERR(klogErr, (klogErr, rc,
                    "while adding $(name) to write cursor", "name=%s", name));
            }
        }
        if (rc == 0) {
            LOGMSG(klogInfo, "VCursorOpen(write cursor)");
            rc = VCursorOpen(cursor);
            DISP_RC_INT(rc, "while opening write cursor");
        }
#if 1
        for (i = 0; i < 3 && rc == 0; ++i) {
            if (rc == 0) {
                PLOGMSG(klogInfo, (klogInfo,
                    "VCursorOpenRow(write cursor) $(i)", "i=%d", i));
                rc = VCursorOpenRow(cursor);
                DISP_RC_INT(rc, "while opening row to write");
            }
            if (rc == 0) {
                char buffer[1];
                char b;
                switch (i) {
                    case 0:
                        buffer[0] = SRA_READ_FILTER_CRITERIA;
                        buffer[0] = SRA_READ_FILTER_REJECT;
                        break;
                    case 1:
                        buffer[0] = SRA_READ_FILTER_REJECT;
                        buffer[0] = SRA_READ_FILTER_CRITERIA;
                        break;
                    case 2:
                        buffer[0] = SRA_READ_FILTER_REDACTED;
                        break;
                }
                buffer[0] = SRA_READ_FILTER_PASS;
                b = buffer[0];
                PLOGMSG(klogInfo, (klogInfo,
                    "VCursorWrite('$(v)') $(i)", "v=%s,i=%d",
                    b == SRA_READ_FILTER_REDACTED ? "SRA_READ_FILTER_REDACTED" :
                        "?",
                    i));
                rc = VCursorWrite(cursor, idx, 8, buffer, 0, 1);
                DISP_RC_INT(rc, "while writing");
            }
            if (rc == 0) {
                PLOGMSG(klogInfo, (klogInfo,
                    "VCursorCommitRow(write cursor) $(i)", "i=%d", i));
                rc = VCursorCommitRow(cursor);
                DISP_RC_INT(rc, "while committing row");
            }
            PLOGMSG(klogInfo, (klogInfo,
                "VCursorCloseRow(write cursor) $(i)", "i=%d", i));
            {
                rc_t rc2 = VCursorCloseRow(cursor);
                DISP_RC_INT(rc2, "while closing row");
                if (rc == 0)
                {   rc = rc2; }
            }
        }
#endif
        LOGMSG(klogInfo, "VCursorRelease(read cursor)");
/* -03: CreateCursorRead */
        VCursorRelease(rCursor);
        if (rc == 0) {
            LOGMSG(klogInfo, "VCursorCommit(write cursor)");
            rc = VCursorCommit(cursor);
            DISP_RC_INT(rc, "while committing cursor");
        }
        LOGMSG(klogInfo, "VCursorRelease(write cursor)");
/* -04: CreateCursorWrite */
        VCursorRelease(cursor);
    }

    LOGMSG(klogInfo, "VTableRelease");
/* -02: OpenTable */
    VTableRelease(tbl);
    LOGMSG(klogInfo, "VDBManagerLock");
    if (locked) {
        rc_t rc2 = VDBManagerLock(mgr, "%s", table);
        DISP_RC_INT(rc2, "while VDBManagerLock");
    }

/* -01: ManagerMake */
    LOGMSG(klogInfo, "VDBManagerRelease");
    VDBManagerRelease(mgr);

    if (rc == 0) {
        LOGMSG(klogInfo, "SUCCESS");
    }
    else { LOGMSG(klogInfo, "FAILURE"); }

    return rc;
}
예제 #12
0
static
rc_t run()
{
    KRng * rng;
    rc_t rc;

    rc = KCSPRngMake (&rng);
    if (rc)
        LOGERR (klogErr, rc, "failed to make RNG");
    else
    {
        uint32_t ix;

        OUTMSG (("KCSPRng with no seed\n"));
        
        for (ix = 0; ix < 10; ++ix)
        {
            uint8_t buff [8];
            uint32_t num_read;

            rc = KRngRead (rng, buff, sizeof (buff), &num_read);
            if (rc)
                PLOGERR (klogErr,
                         (klogErr, rc, "error in read '$u' from KRng",
                          "u=%lu", ix));
            else
            {
                OUTMSG (( "%4.4d: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
                          ix,
                          buff[0], buff[1], buff[2], buff[3],
                          buff[4], buff[5], buff[6], buff[7]));
            }
        }

        KRngRelease (rng);

        rc = KCSPRngMake (&rng);
        if (rc)
            LOGERR (klogErr, rc, "failed to make RNG");
        else
        {
            uint32_t ix;


            /* this is using system seed from us */
            rc = KRngSeed (rng);
            if (rc)
                LOGERR (klogErr, rc, "error running KRngSeed");
            else
            {
                OUTMSG (("KCSPRng with seed\n"));
        
                for (ix = 0; ix < 10; ++ix)
                {
                    uint8_t buff [8];
                    uint32_t num_read;

                    rc = KRngRead (rng, buff, sizeof (buff), &num_read);
                    if (rc)
                        PLOGERR (klogErr,
                                 (klogErr, rc, "error in read '$u' from KRng",
                                  "u=%lu", ix));
                    else
                    {
                        OUTMSG (( "%4.4d: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
                                  ix,
                                  buff[0], buff[1], buff[2], buff[3],
                                  buff[4], buff[5], buff[6], buff[7]));
                    }
                }
                {
                    uint8_t reseed [1024];

                    reseed[0] = 0xAA;

                    for (ix = 1; ix < sizeof (reseed); ++ix)
                        reseed[ix] = reseed[ix-1] ^ (uint8_t)ix;

                    /* this is using reseed from us */
                    rc = KRngReseed (rng, reseed, sizeof (reseed));
                    if (rc)
                        LOGERR (klogErr, rc, "error running KRngReseed");
                    else
                    {
                        OUTMSG (("KCSPRng with reseed\n"));

                        for (ix = 0; ix < 10; ++ix)
                        {
                            uint8_t buff [8];
                            uint32_t num_read;

                            rc = KRngRead (rng, buff, sizeof (buff), &num_read);
                            if (rc)
                                PLOGERR (klogErr,
                                         (klogErr, rc, "error in read '$u' from KRng",
                                          "u=%lu", ix));
                            else
                            {
                                OUTMSG (( "%4.4d: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
                                          ix,
                                          buff[0], buff[1], buff[2], buff[3],
                                          buff[4], buff[5], buff[6], buff[7]));
                            }
                        }
                    }        
                }
            }        
            KRngRelease (rng);
        }
    }
    return 0;
}
예제 #13
0
파일: bzip.c 프로젝트: mariux/sratoolkit
static
rc_t KBZipFileReadInt (KBZipFile * self, void * buffer, size_t bsize, size_t * pnumread)
{
    bz_stream temp;     /* store some values here during a reinit after stream end */
    bz_stream * strm;   /* alias for the object's bzip stream object */
    size_t bleft = bsize;
    size_t num_read;
    size_t tot_read = 0;
    rc_t rc = 0;

    BZIP_DEBUG (("---------------\n%s: Enter requesting bsize %lu\n", __func__, bsize));

    strm = &self->strm;

    for (tot_read = 0; tot_read < bsize ; )
    {
        char * this_out;
        size_t src_read;
        int zret;
        bool bin;
        bool bout;
        bool end;

        bin = (strm->avail_in != 0);

        BZIP_DEBUG (("%s: loop start tot_read %zu\n", __func__, tot_read));

        strm->next_out = this_out = (char*)buffer + tot_read;
        strm->avail_out = bsize - tot_read;

        BZIP_DEBUG(("%s: call Decompress\n", __func__));

        BZIP_DBGSTREAM (strm, "before BZ2_bzDecompress");

        zret = BZ2_bzDecompress(strm);

        BZIP_DBGSTREAM (strm, "after BZ2_bzDecompress");

        switch (zret)
        {
            /* unexpected error returns from zlib */
        default:
            BZIP_DEBUG (("%s: undocumented error return in bzip Decompress\n", __func__));
            rc = RC (rcFS, rcFile, rcReading, rcFile, rcUnknown);
            PLOGERR (klogErr,
                     (klogErr, rc, "unknown error decompressing BZip2 file "
                      "error code '$(EC)'", "EC=%d", zret));
            return rc;

            /* known unfixable errors */
        case BZ_PARAM_ERROR:
            BZIP_DEBUG (("%s: internal programming error - bad parameters\n", __func__));
            rc = RC (rcFS, rcFile, rcReading, rcSelf, rcInvalid);
            if (strm == NULL)
                BZIP_DEBUG (("%s: strm is NULL\n", __func__));
            else
            {
                if (strm->state == NULL)
                    BZIP_DEBUG (("%s: strm->state is NULL\n", __func__));
                if (strm->avail_out < 1)
                    BZIP_DEBUG (("%s: strm->avail_out < 1\n", __func__));
            }
            LOGERR (klogInt, rc, "bzip strm structure bad");
            return rc;

        case BZ_DATA_ERROR:
            BZIP_DEBUG (("%s: data integrity error in bzip stream\n", __func__));
            rc = RC (rcFS, rcFile, rcReading, rcData, rcCorrupt);
            LOGERR (klogErr, rc, "bzip stream data error");
            return rc;

        case BZ_DATA_ERROR_MAGIC:
            BZIP_DEBUG (("%s: data magic bytes error in bzip stream\n", __func__));
            rc = RC (rcFS, rcFile, rcReading, rcData, rcIncorrect);
            LOGERR (klogErr, rc, "bzip stream not a bzip stream");
            return rc;

        case BZ_MEM_ERROR:
            BZIP_DEBUG (("%s: memory exhausted during BZip decompress\n", __func__));
            rc = RC (rcFS, rcFile, rcReading, rcMemory, rcExhausted);
            LOGERR (klogErr, rc, "not enough memory available during bzip decompress");
            return rc;

        case BZ_STREAM_END:
            BZIP_DEBUG (("%s: BZ_STREAM_END\n", __func__));
            end = true;
            num_read = strm->next_out - this_out;
            bout = (num_read != 0);
            BZIP_DEBUG (("%s: num_read %zu\n", __func__, num_read));
            self->completed = true;
            BZIP_DBGSTREAM (strm, "before BZ2_bzDecompressEnd");
            zret = BZ2_bzDecompressEnd(strm);
            BZIP_DBGSTREAM (strm, "after BZ2_bzDecompressEnd");
            if (zret == BZ_OK)
            {
                temp = *strm;
                memset (strm, 0, sizeof *strm);
                zret = BZ2_bzDecompressInit (strm, 1, 0);
                BZIP_DBGSTREAM (strm, "after BZ2_bzDecompressInit");
                strm->next_in = temp.next_in;
                strm->avail_in = temp.avail_in;
/*                 strm->next_out = temp.next_out; */
/*                 strm->avail_out = temp.avail_out; */
                strm->total_in_lo32 = temp.total_in_lo32;
                strm->total_in_hi32 = temp.total_in_hi32;
                strm->total_out_lo32 = temp.total_out_lo32;
                strm->total_out_hi32 = temp.total_out_hi32;
                BZIP_DBGSTREAM (strm, "after restore");
            }
            switch (zret)
            {
            case BZ_OK:
                break;
            default:
                return RC (rcFS, rcFile, rcReading, rcFile, rcUnknown);
            }
            goto read_more;

        case BZ_OK:
            BZIP_DEBUG (("%s: BZ_OK\n", __func__));
            end = false;
            num_read = strm->next_out - this_out;
            bout = (num_read != 0);
            BZIP_DEBUG (("%s: num_read %zu\n", __func__, num_read));

        read_more:
            /* if we wanted more on this read, read some more compressed */
            tot_read += num_read;
            if (strm->avail_in == 0)
            {
                rc = KFileRead (self->file, self->filePosition, self->buff, 
                                sizeof self->buff, &src_read);
                if (rc)
                    return rc;

                BZIP_DEBUG (("%s: KFileRead read %u\n", __func__, src_read));

                if (src_read == 0)
                {
                    BZIP_DEBUG (("%s: end %u in %u out %u\n", __func__, end, bin, bout));
                
                    if (!end && bin && !bout)
                        rc = RC (rcFS, rcFile, rcReading, rcData, rcInsufficient);
                    goto done;
                }
                strm->avail_in = src_read;
                self->filePosition += src_read;
                strm->next_in = self->buff;

                /* if src_read == 0 but we are not at BZ_STREAM_END
                 * we have an error
                 */
                if (src_read == 0)
                    bleft = 0;
            }
            break;
        }
        if (rc)
            break;
        BZIP_DEBUG (("%s: loop end tot_read %zu\n", __func__, tot_read));
    }
done:
    BZIP_DEBUG (("%s: returning tot_read %zu\n\n\n", __func__, tot_read));
    *pnumread = tot_read;
    return rc;
}