Ejemplo n.º 1
0
/* Test
 *  returns true if path appears to be accession
 *  the test is a heuristic, and may return false positives
 *
 *  "path" [ IN ] - NUL terminated path to run
 */
static
bool SRAPathTestTable ( const KDirectory *dir, uint32_t type, const char *path )
{
    /* differentiate between legacy and current structure */
    switch ( KDirectoryPathType ( dir, "%s/idx", path ) )
    {
    case kptNotFound:
        switch ( KDirectoryPathType ( dir, "%s/meta", path ) )
        {
        case kptFile:
        case kptFile | kptAlias:
            switch ( KDirectoryPathType ( dir, "%s/skey", path ) )
            {
            case kptFile:
            case kptFile | kptAlias:
                return true;
            }
            break;
        }
        break;

    case kptDir:
    case kptDir | kptAlias:
        switch ( KDirectoryPathType ( dir, "%s/md/cur", path ) )
        {
        case kptFile:
        case kptFile | kptAlias:
            return true;
        }
        break;
    }

    /* not a table */
    return false;
}
Ejemplo n.º 2
0
/* Writable
 *  examines a directory structure for any "lock" files
 */
rc_t KDBWritable ( const KDirectory *dir, const char *path )
{
    uint32_t access;
    rc_t rc;

    /* protect us from bad parameters */
    if (dir == NULL)
        return RC (rcDB, rcPath, rcAccessing, rcDirectory, rcNull);
    if (path == NULL)
        return RC (rcDB, rcPath, rcAccessing, rcPath, rcNull);

    /* we have to be able to check the access if it is to be writable */
    rc = KDirectoryVAccess ( dir, & access, path, NULL );
    if ( rc == 0 )
    {
        /* if there is a lock (or deprecated sealed) file in this directory */
        switch ( KDirectoryPathType ( dir, "%s/lock", path ) )
        {
        case kptFile:
        case kptFile | kptAlias:
            rc = RC ( rcDB, rcPath, rcAccessing, rcLock, rcLocked );
            break;
        case kptNotFound:
            /* much simpler handling for the sealed file */
            switch ( KDirectoryPathType ( dir, "%s/sealed", path ) )
            {
            case kptFile:
            case kptFile | kptAlias:
                rc = RC ( rcDB, rcPath, rcAccessing, rcLock, rcLocked );
                break;
            case kptNotFound:
                /* check if there are no write permissions */
                if ( ( access & 0222 ) == 0 )
                    rc = RC ( rcDB, rcPath, rcAccessing, rcPath, rcReadonly );
                /* else rc is still 0 from VAccess */
            }
            break;
        case kptBadPath:
            /* likely to be a non-driectory or something */
            rc = RC ( rcDB, rcPath, rcAccessing, rcPath, rcInvalid);
            break;
        default:
            /* an illegal type of object named "lock" is in this directory
             * which will block the ability to lock it
             */
            rc = RC ( rcDB, rcPath, rcAccessing, rcPath, rcIncorrect );
        }
    }
    return rc;
}
Ejemplo n.º 3
0
static
rc_t open_file_as_dir (const char * path, const KDirectory ** dir)
{
    rc_t rc;
    KPathType kpt;
    const KDirectory * d;

    rc = 0;
    kpt = KDirectoryPathType (kdir, path);
    switch (kpt & ~kptAlias)
    {
    case kptNotFound:
        rc = RC (rcExe, rcNoTarg, rcParsing, rcParam, rcNotFound);
        PLOGERR (klogFatal, (klogFatal, rc, "Archive [$(A)] must exist", PLOG_S(A), path));
        return rc;
    case kptDir:
    default:
        rc = RC (rcExe, rcNoTarg, rcParsing, rcParam, rcInvalid);
        PLOGERR (klogFatal, (klogFatal, rc, "Archive [$(A)] must be a file", PLOG_S(A), path));
        return rc;
    case kptFile:
        break;
    }
    if (rc == 0)
    {
        STSMSG (1, ("Opening %s\n", path));
        rc = KDirectoryOpenArcDirRead (kdir, &d, /*chroot*/false, path, 
                                       tocKFile, KArcParseSRA,
                                       /*filter*/pnamesFilter, /*filt.param*/NULL);
        *dir = (rc == 0) ? d : NULL;
    }
    return rc;
}
Ejemplo n.º 4
0
static rc_t CC scan_mod_dir(const KDirectory* dir,
    uint32_t type, const char* name, void* data)
{
    rc_t rc = 0;
    const char ext[] = SHLX;

    assert(data);

    if (strlen(name) > strlen(ext) + 1 &&
        name[strlen(name) - strlen(ext) - 1] == '.')
    {
        char buf[PATH_MAX + 1];
        rc = KDirectoryResolvePath
            (dir, true, buf, sizeof buf, "%s/%s", data, name);

        while (rc == 0) {
            uint32_t type = KDirectoryPathType(dir, buf);
            if (type & kptAlias) {
                rc = KDirectoryResolveAlias
                    (dir, true, buf, sizeof buf, buf);
                DISP_RC(rc, name);
            }
            else if (rc == 0) {
                if (type == kptNotFound || type == kptBadPath) {
                    OUTMSG(("%s: %s\n", buf,
                        type == kptNotFound ? "not found" : "bad path"));
                }
                else { OUTMSG(("%s\n", buf)); }
                break;
            }
        }
    }

    return rc;
}
Ejemplo n.º 5
0
static rc_t cg_dump_prepare_output( cg_dump_opts * opts, cg_dump_ctx * cg_ctx )
{
    rc_t rc = 0;

    uint32_t pt = ( KDirectoryPathType ( cg_ctx->dir, "%s", cg_ctx->dst ) & ~ kptAlias );
    switch ( pt )
    {
        case kptNotFound :  rc = cg_dump_create_output_dir( cg_ctx ); break;

        case kptDir  :  rc = cg_dump_clear_output_dir( opts, cg_ctx ); break;

        case kptFile :  rc = cg_dump_remove_file( opts, cg_ctx ); 
                        if ( rc == 0 )
                            rc = cg_dump_create_output_dir( cg_ctx );
                        break;

        default :   rc = RC( rcExe, rcNoTarg, rcReading, rcParam, rcInvalid );
                    (void)PLOGERR( klogErr, ( klogErr, rc, "invalid output-directory-type '$(dt)'",
                                              "dt=%s", pathtype_2_pchar( pt ) ) );
                    break;
    }
    if ( rc == 0 )
    {
        rc = KDirectoryOpenDirUpdate ( cg_ctx->dir, &cg_ctx->out_dir, false, "%s", cg_ctx->dst );
        if ( rc != 0 )
        {
            (void)LOGERR( klogErr, rc, "cannot open output-directory for update" );
        }
        else
        {
            BSTreeInit( &cg_ctx->lanes );
        }
    }
    return rc;
}
Ejemplo n.º 6
0
rc_t ProcessOneDo (ProcessOne * self)
{
    static const char F[] = PLOG_2(PLOG_S(p),PLOG_S(t));
    enum KPathType type;
    rc_t rc = 0;
    
    type = KDirectoryPathType (self->dir, self->path);

    switch (type)
    {
    case kptFile:
	rc = ProcessOneDoFile (self);
 	break;
/*     case kptDir: */
/* 	break; */
/*     case kptAlias|kptFile: */
/* 	break; */
/*     case kptAlias|kptDir: */
/* 	break; */
    default:
	PLOGMSG (klogInfo, "+ Skipping $(p) of type $(t)", F, self->path, typeToString(type));
	break;
    }
    return rc;
}
Ejemplo n.º 7
0
static rc_t pacbio_check_sourcefile( context *ctx, ld_context *lctx )
{
    rc_t rc = 0;
    uint32_t src_path_type = KDirectoryPathType ( lctx->wd, ctx->src_path );
    if ( ( src_path_type & kptFile ) == 0 )
    {
        rc = RC ( rcExe, rcFile, rcValidating, rcItem, rcNotFound );
        LOGERR( klogErr, rc, "source-file not found" );
    }
    else
    {
        if ( ( src_path_type & kptAlias ) != 0 )
        {
            char resolved[ 2048 ];
            rc = KDirectoryResolveAlias ( lctx->wd, true, resolved,
                                          2084, ctx->src_path );
            if ( rc != 0 )
            {
                LOGERR( klogErr, rc, "cannot resolve srcfile-link" );
            }
            else
            {
                free( ctx->src_path );
                ctx->src_path = string_dup_measure ( resolved, NULL );
            }
        }
    }
    return rc;
}
Ejemplo n.º 8
0
int KDBPathType ( const KDirectory *dir, bool *pHasZombies, const char *path )
{
    const char *leaf, *parent;


    rc_t rc;
    int type = KDirectoryPathType ( dir, "%s", path );
    
    if (pHasZombies)
        *pHasZombies = false;

    switch ( type )
    {
    case kptDir:
    case kptDir | kptAlias:
        type = KDBPathTypeDir (dir, type, pHasZombies, path);
        break;

    case kptFile:
    case kptFile | kptAlias:
    {
        /* if we hit a file first try it as an archive */
        const KDirectory * ldir;

        rc = KDirectoryOpenSraArchiveRead_silent ( dir, &ldir, false, "%s", path );
#if SUPPORT_KDB_TAR
        if ( rc != 0 )
            rc = KDirectoryOpenTarArchiveRead_silent ( dir, &ldir, false, "%s", path );
#endif
        /* it was an archive so recur */
        if ( rc == 0 )
        {
            /* recheck this newly opened directory for KDB/KFS type */
            int type2;

            type2 = KDBPathType ( ldir, NULL, "." );
            if ((type2 != kptDir) || (type != (kptDir|kptAlias)))
                type = type2;

            KDirectoryRelease (ldir);
        }
        /* it was not an archive so see if it it's an idx file */
        else
        {
            leaf = strrchr ( path, '/' );
            if ( leaf != NULL )
            {
                parent = string_rchr ( path, leaf - path, '/' );
                if ( parent ++ == NULL )
                    parent = path;
                if ( memcmp ( parent, "idx/", 4 ) == 0 )
                    type += kptIndex - kptFile;
            }
        }
        break;
    }
    }
    return type;
}
Ejemplo n.º 9
0
    DepFixture(const char *path)
        : mgr(NULL)
        , vmgr(NULL)
        , resolver(NULL)
        , siteless(false)
    {
        rc_t rc = 0;

        KDirectory *wd = NULL;
        if (KDirectoryNativeDir(&wd)) {
            FAIL("failed to KDirectoryNativeDir");
        }

        const KDirectory *dir = NULL;
        KConfig *cfg = NULL;

        if (KDirectoryOpenDirRead(wd, &dir, false, path)) {
            FAIL("failed to KDirectoryOpenDirRead()");
        }
        if (KConfigMake(&cfg, dir)) {
            FAIL("failed to KConfigMake()");
        }
        RELEASE(KDirectory, dir);
        if (VFSManagerMakeFromKfg(&vmgr, cfg)) {
            FAIL("failed to VFSManagerMakeFromKfg()");
        }
        if (VFSManagerGetResolver(vmgr, &resolver)) {
            FAIL("failed to VFSManagerGetResolver");
        }

        String *result = NULL;
        rc = KConfigReadString(cfg, "repository/site/main/tracearc/root",
            &result);
        if (rc != 0) {
            if (rc == SILENT_RC(rcKFG, rcNode, rcOpening, rcPath, rcNotFound)) {
                rc = 0;
                siteless = true;
            }
            else {
                FAIL(
              "failed to KConfigReadString(repository/site/main/tracearc/root)");
            }
        }
        else {
            assert(result);
            KPathType t = KDirectoryPathType(wd, result->addr);
            if (t != kptDir) {
                siteless = true;
            }
        }
        RELEASE(String, result);

        RELEASE(KConfig, cfg);
        if (VDBManagerMakeReadWithVFSManager(&mgr, NULL, vmgr)) {
            FAIL("failed to VDBManagerMakeReadWithVFSManager()");
        }

        RELEASE(KDirectory, wd);
    }
Ejemplo n.º 10
0
static
uint32_t CC KLoaderFile_Type(const KLoaderFile *self)
{
    if( self && self->kfile ) {
        return KFileType(self->kfile);
    }
    return KDirectoryPathType(self ? self->dir : NULL, "%s", self->realname);
}
Ejemplo n.º 11
0
static rc_t perform_file_load( const p_context ctx, KDirectory *dir, const TableWriterRefSeq* wr )
{
    rc_t rc = 0;
    uint32_t pt = KDirectoryPathType ( dir, "%s", ctx->src );
    if ( pt != kptFile )
    {
        rc = RC( rcExe, rcFile, rcConstructing, rcParam, rcInvalid );
        LOGERR( klogErr, rc, "input-file not found" );
    }
    else
    {
        const KFile * f;
        rc = KDirectoryOpenFileRead ( dir, &f, "%s", ctx->src );
        if ( rc != 0 )
        {
            LOGERR( klogErr, rc, "cannot open input-file for read" );
        }
        else
        {
            data_ctx dctx;
            rc = init_data_context( ctx, wr, &dctx );
            if ( rc == 0 )
            {
                uint64_t pos = 0;
                bool done = false;
                while ( !done )
                {
                    size_t num_read;
                    char buffer[ 8192 ];
                    rc = KFileReadAll ( f, pos, buffer, sizeof buffer, &num_read );
                    if ( rc != 0 )
                    {
                        PLOGERR( klogErr, ( klogErr, rc, "$(path) at $(pos)", "path=%s,pos=%lu", ctx->src, pos ) );
                        done = true;
                    }
                    else
                    {
                        on_curl_data( buffer, 1, num_read, &dctx );

                        if ( num_read < sizeof( buffer ) )
                            done = true;
                        else
                            pos += num_read;
                    }
                }
                finish_data_context( ctx, &dctx );
                if ( dctx.rc == 0 )
                {
                    dctx.rc = rc;
                }
            }
            KFileRelease( f );
        }
    }
    return rc;
}
Ejemplo n.º 12
0
rc_t KeyRingOpen(KKeyRing** self, const char* path, const struct KFile* pwd_in, struct KFile* pwd_out)
{
    rc_t rc; 
    assert(self && path && pwd_in && pwd_out);

    *self = (KKeyRing*) malloc(sizeof(**self));
    if (*self)
    { 
        rc = KeyRingInit(*self, path);
        if (rc == 0)
        {
            rc = KeyRingAddRef(*self);
            if (rc == 0)
            {
                KDirectory* wd; 
                rc = KDirectoryNativeDir(&wd);
                if (rc == 0)
                {   /* open the database */
                    if (KDirectoryPathType(wd, "%s", (*self)->path) == kptFile)
                        rc = GetPassword(pwd_in, pwd_out, (*self)->passwd);
                    else /* does not exist; create first */
                    {   
                        rc = GetNewPassword(pwd_in, pwd_out, (*self)->passwd);
                        if (rc == 0)
                            rc = CreateDatabase(*self);
                    }
                    if (rc == 0)
                        rc = OpenDatabase(*self);
                        
                    {
                        rc_t rc2;
                        rc2 = KDirectoryRelease(wd);
                        if (rc == 0)
                            rc = rc2;
                    }
                }
            }
            
            if (rc != 0)
            {
                KeyRingWhack(*self);
                *self = NULL;
            }
        }
        else
        {
            free(*self);
            *self = NULL;
        }
    }
    else
        rc = RC ( rcApp, rcDatabase, rcOpening, rcMemory, rcExhausted );

    return rc;
}
Ejemplo n.º 13
0
/* IsAlias
 *  returns true if object name is an alias
 *  returns path to fundamental name if it was aliased
 *
 *  "type" [ IN ] - a KDBPathType
 *  valid values are kptIndex and kptColumn
 *
 *  "resolved" [ OUT ] and "rsize" [ IN ] - optional output buffer
 *  for fundamenta object name if "alias" is not a fundamental name, 
 *
 *  "name" [ IN ] - NUL terminated object name
 */
LIB_EXPORT bool CC KTableIsAlias ( const KTable *self, uint32_t type,
    char *resolved, size_t rsize, const char *name )
{
    if ( self != NULL && name != NULL && name [ 0 ] != 0 )
    {
        rc_t rc;
        const char *ns;
        char path [ 256 ];

        switch ( type )
        {
        case kptIndex:
            ns = "idx";
            break;

        case kptColumn:
            ns = "col";
            break;

        default:
            return false;
        }

        rc = KDBMakeSubPath ( self -> dir, path, sizeof path, ns, 3, name );
        if ( rc == 0 )
        {
            switch ( KDirectoryPathType ( self -> dir, "%s", path ) )
            {
            case kptFile | kptAlias:
            case kptDir | kptAlias:
                if ( resolved != NULL && rsize != 0 )
                {
                    const KDirectory *sub;
                    rc = KDirectoryOpenDirRead ( self -> dir, & sub, false, "%s", ns );
                    * resolved = 0;
                    if ( rc != 0 )
                        PLOGERR ( klogWarn, ( klogWarn, rc, "failed to open subdirectory '$(sub)'", "sub=%s", ns ) );
                    else
                    {
                        rc = KDirectoryResolveAlias ( sub, false, resolved, rsize, "%s", name );
                        KDirectoryRelease ( sub );
                        if ( rc != 0 )
                            PLOGERR ( klogWarn, ( klogWarn, rc, "failed to resolve alias '$(alias)'", "alias=%s", name ) );
                    }
                }
                return true;
            }
        }
    }

    if ( resolved != NULL && rsize != 0 )
        * resolved = 0;

    return false;
}
Ejemplo n.º 14
0
rc_t CopyMode( const KDirectory *source, const char *sourcename,
	       KDirectory *target, char *targetname )
{
  /* Make sure they both exist and are the same type */
  uint32_t src_pathtype;
  uint32_t dest_pathtype;
  uint32_t mode;
  rc_t rc;

  src_pathtype = KDirectoryPathType( source, "%s", sourcename );
  dest_pathtype = KDirectoryPathType( target, "%s", targetname );
  if ((src_pathtype & ~kptAlias) != (dest_pathtype & ~kptAlias)) {
    return -1;
  }
  rc = KDirectoryAccess( source, &mode, "%s", sourcename );
  if (rc != 0)
    {
      LOGERR ( klogInt, rc, sourcename );
      return rc;
    }
  KDirectorySetAccess( target, false, mode, 0777, "%s", targetname );
  return 0;
}
Ejemplo n.º 15
0
static
bool SRAPathTestInt ( const KDirectory *dir, uint32_t type, const char *path )
{
    switch ( type )
    {
    case kptDir:
    case kptDir | kptAlias:
        break;

    case kptFile:
    case kptFile | kptAlias:
        return SRAPathTestFile ( dir, type, path );

    default:
        return false;
    }

    /* detect apparent tables */
    switch ( KDirectoryPathType ( dir, "%s/col", path ) )
    {
    case kptDir:
    case kptDir | kptAlias:
        return SRAPathTestTable ( dir, type, path );
    }

    /* detect apparent databases */
    switch ( KDirectoryPathType ( dir, "%s/tbl", path ) )
    {
    case kptDir:
    case kptDir | kptAlias:
        return SRAPathTestDatabase ( dir, type, path );
    }

    /* not a recognized accession */
    return false;
}
Ejemplo n.º 16
0
/* Exists
 *  returns true if requested object exists
 *
 *  "type" [ IN ] - a KDBPathType
 *  valid values are kptTable
 *
 *  "path" [ IN ] - NUL terminated path
 */
LIB_EXPORT bool CC KDatabaseVExists ( const KDatabase *self, uint32_t type, const char *name, va_list args )
{
    if ( self != NULL && name != NULL && name [ 0 ] != 0 )
    {
        rc_t rc;
        size_t len;
        const char *ns;
        char path [ 256 ];

        switch ( type )
        {
        case kptDatabase:
            ns = "db";
            len = 2;
            break;

        case kptTable:
            ns = "tbl";
            len = 3;
            break;

        case kptIndex:
            ns = "idx";
            len = 3;
            break;

        default:
            return false;
        }

        rc = KDBVMakeSubPath ( self -> dir, path, sizeof path, ns, len, name, args );
        if ( rc == 0 )
        {
            switch ( KDirectoryPathType ( self -> dir, "%s", path ) )
            {
            case kptFile:
            case kptDir:
            case kptFile | kptAlias:
            case kptDir | kptAlias:
                return true;
            }
        }
    }
    return false;
}
Ejemplo n.º 17
0
static
rc_t ReportAlias(const ReportFuncs *f, uint32_t indent, const char* alias, const KDirectory* dir)
{
    char resolved[PATH_MAX + 1];
    rc_t rc
        = KDirectoryResolveAlias(dir, false, resolved, sizeof resolved, "%s", alias);
    if (rc == 0) {
        const char tag[] = "Alias";
        uint32_t type = KDirectoryPathType(dir, "%s", resolved);
        if (type & kptAlias) {
            reportOpen(indent, tag, 1, "resolved", 's', resolved);
            rc = ReportAlias(f, indent + 1, resolved, dir);
            reportClose(indent, tag);
        }
        else
        {   report(indent, tag, 1, "resolved", 's', resolved); }
    }
    return rc;
}
Ejemplo n.º 18
0
/* this could be modified to accept stdout */
static
rc_t open_out_file (const char * path, KFile ** fout)
{
    rc_t rc;
    KPathType kpt;
    KCreateMode mode;
    rc = 0;
    mode = kcmParents|kcmCreate;

    kpt = KDirectoryPathType (kdir, path);
    switch (kpt & ~kptAlias)
    {
    case kptDir:
        /* -----
         * this version will fail but perhaps with directory
         * take last facet <F> from input path and use that as a name
         * to be <F>.sra in this directory
         */
    default:
        rc = RC (rcExe, rcNoTarg, rcParsing, rcParam, rcInvalid);
        PLOGERR (klogFatal, (klogFatal, rc, "archive [$(A)] must be a file", PLOG_S(A), path));
        break;
    case kptFile:
        if (force)
            mode = kcmParents|kcmInit;
        else
        {
            rc = RC (rcExe, rcNoTarg, rcParsing, rcFile, rcExists);
            PLOGERR (klogFatal, (klogFatal, rc, "archive [$(A)] already exists", PLOG_S(A), path));
        }
        break;
    case kptNotFound:
        break;
    }
    if (rc == 0)
    {
        rc = KDirectoryVCreateFile (kdir, fout, false, 0664,
                                    mode, path, NULL);
        if (rc)
            PLOGERR (klogFatal, (klogFatal, rc, "unable to create archive [$(A)]", PLOG_S(A), path));
    }
    return rc;
}
Ejemplo n.º 19
0
static
rc_t ApplyAlg( const NCBISRAPath *self, const char *rep, const char *vol, 
    const char *accession, char *path, size_t path_max, NCBIRepository *repo, bool* found)
{
    SRAPathFindInfo pb;
    /* see if we recognize volume */
    pb . path = vol;
    pb . size = strlen ( vol );
    pb . found = 0;

    DLListDoUntil ( & repo -> vols, 0, SRAPathStringFind, & pb );
    if ( pb . found )
    {
        *found = true;
        switch (pb . alg)
        {
        case alg_ebi:
            return SRAPathFullEBI ( self, rep, vol, accession, path, path_max );
        case alg_refseq:
        {
            rc_t rc = SRAPathFullREFSEQ ( self, rep, vol, accession, path, path_max );
            if ( rc == 0) /* check for existence of accession at the root of the volume, and if not found try to apply the default path-building scheme */
            {
                switch ( KDirectoryPathType ( self -> dir, path ) )
                {
                case kptNotFound:
                case kptBadPath:
                    return SRAPathFullREFSEQArchive( self, rep, vol, accession, path, path_max );
                default:
                	return 0;
                }
            }
		}            
        case alg_wgs:
            return SRAPathFullWGS ( self, rep, vol, accession, path, path_max );
        default:
            break;
        }
    }
    *found = false;
    return 0;
}
Ejemplo n.º 20
0
static rc_t check_if_schema_exists( KDirectory * dir, const char * schema_name )
{
    KConfig * cfg;
    rc_t rc = KConfigMake ( &cfg, NULL );
    if ( rc != 0 )
    {
        LOGERR( klogErr, rc, "check_if_schema_exists:KConfigMake() failed" );
    }
    else
    {
        const KConfigNode *node;
        rc = KConfigOpenNodeRead ( cfg, &node, "vdb/schema/paths" );
        if ( rc !=  0 )
        {
            LOGERR( klogErr, rc, "check_if_schema_exists:KConfigOpenNodeRead( 'vdb/schema/paths' ) failed" );
        }
        else
        {
            char buffer[ 1024 ];
            size_t num_read;
            rc = KConfigNodeRead ( node, 0, buffer, sizeof buffer, &num_read, NULL );
            if ( rc != 0 )
            {
                LOGERR( klogErr, rc, "check_if_schema_exists:KConfigNodeRead() failed" );
            }
            else
            {
                uint32_t path_type;
                buffer[ num_read ] = 0;
                path_type = KDirectoryPathType ( dir, "%s/%s", buffer, schema_name );
                if ( ( path_type & kptFile ) == 0 )
                {
                    rc = RC( rcExe, rcFile, rcConstructing, rcParam, rcInvalid );
                }
            }
            KConfigNodeRelease ( node );
        }
        KConfigRelease ( cfg );
    }
    return rc;
}
Ejemplo n.º 21
0
static bool _KDirectoryFileFound(const KDirectory *self,
    const char *path, bool status)
{
    KPathType type = kptNotFound;
    if (status) {
        STSMSG(STS_DBG, ("Checking '%s'", path));
    }
    type = KDirectoryPathType(self, "%s", path);
    if ((type & ~kptAlias) == kptFile) {
        if (status) {
            STSMSG(STS_DBG, ("'%s': found", path));
        }
        return true;
    }
    else {
        if (status) {
            STSMSG(STS_DBG, ("'%s': not found", path));
        }
        return false;
    }
}
Ejemplo n.º 22
0
static
rc_t open_dir_as_archive (const char * path, const KFile ** file)
{
    rc_t rc;
    KPathType kpt;
    const KDirectory * d;

    assert (path != NULL);
    assert (path[0] != '\0');
    assert (file != NULL);

    rc = 0;
    kpt = KDirectoryPathType (kdir, path);
    switch (kpt)
    {
    case kptFile:
        STSMSG (1, ("Opening as archive %s\n", path));

        rc = KDirectoryOpenSraArchiveRead (kdir, &d, false, path);
        if (rc != 0)
            rc = KDirectoryOpenTarArchiveRead (kdir, &d, false, path);
        if (rc != 0)
            rc = RC (rcExe, rcParam, rcOpening, rcDirectory, rcInvalid);
        break;
    default:
        rc = RC (rcExe, rcNoTarg, rcParsing, rcParam, rcInvalid);
        PLOGERR (klogFatal, (klogFatal, rc, "Parameter [$(P)] must be a directory", PLOG_S(P), path));
        return rc;
    case kptDir:
/*         KOutMsg ("%s: opening dir\n",__func__); */
        rc = KDirectoryVOpenDirRead (kdir, &d, false, path, NULL);
    }
    if (rc == 0)
    {
/*         KOutMsg ("%s: dir to archive\n",__func__); */
        rc = KDirectoryOpenTocFileRead (d, file, alignment, pnamesFilter, NULL, sort_size_then_rel_path );
        KDirectoryRelease (d);
    }
    return rc;
}
Ejemplo n.º 23
0
rc_t CopyDirectoryDirectories( const KDirectory *source, KDirectory *dest ) {
  rc_t rc;
  KNamelist *list;
  const char *name;
  int i;
  uint32_t count;
  uint32_t mode;
  uint32_t pathtype;

  KDirectoryList( source, &list, PathIsDir, NULL, ".");
  KNamelistCount(list, &count);
  for (i=0; i<count; i++) {
    KNamelistGet(list, i, &name);
    /* fprintf(stderr, "Creating directory %s\n", name); */
    mode = DEFAULT_DIR_MODE;
    rc = KDirectoryAccess( source, &mode, "%s", name);
    if (rc != 0)
      {
	LOGERR ( klogInt, rc, name );
	return rc;
      }
    pathtype = KDirectoryPathType( dest, "%s", name );
    if ((pathtype & ~kptAlias) == kptNotFound) {
        rc = KDirectoryCreateDir( dest, mode, kcmOpen, "%s", name );
      if (rc != 0)
	{
	  LOGERR ( klogInt, rc, name );
	  return rc;
	}
    } else if ((pathtype & ~kptAlias) == kptDir) {
      if (clobber_protections) {
          KDirectorySetAccess( dest, false, mode, 0777, "%s", name);
      }
    }
    CopyDirectoryToExistingDirectory( source, name, dest, (char *)name);
  }
  return 0;
}
Ejemplo n.º 24
0
static
rc_t DoDir (const KDirectory * sd, KDirectory * dd)
{
    KNamelist * names;
    rc_t rc;

    rc = KDirectoryList (sd, &names, NULL, NULL, ".");
    if (rc)
        ;
    else
    {
        uint32_t count;

        rc = KNamelistCount (names, &count);
        if (rc)
            ;
        else
        {
            uint32_t idx;

            for (idx = 0; idx < count; ++idx)
            {
                const char * name;

                rc = KNamelistGet (names, idx, &name);
                if (rc)
                    ;
                else
                {
                    const KDirectory * nsd;
                    KDirectory * ndd;
                    KPathType kpt;

                    kpt = KDirectoryPathType (sd, name);

                    switch (kpt)
                    {
                    default:
                        break;

                    case kptFile:
                        if (sd == dd)
                            rc = FileInPlace (dd, name, true);
                        else
                            rc = FileToFile (sd, name, dd, name, true, NULL);
                        break;

                    case kptDir:
                        if (sd == dd)
                        {
                            rc = KDirectoryOpenDirUpdate (dd, &ndd, false, "%s", name);
                            if (rc)
                                ;
                            else
                            {
                                /* RECURSION */
                                STSMSG (1, ("%scrypting directory %s", De, name));
                                rc = DoDir (ndd, ndd);
                                STSMSG (1, ("done with directory %s", name));
                                KDirectoryRelease (ndd);
                            }
                        }
                        else
                        {
                            rc = KDirectoryOpenDirRead (sd, &nsd, false, name);
                            if (rc)
                                ;
                            else
                            {
                                rc = KDirectoryCreateDir (dd, 0600, kcmOpen, "%s", name);
                                if (rc)
                                    ;
                                else
                                {
                                    rc = KDirectoryOpenDirUpdate (dd, &ndd, false, "%s", name);
                                    if (rc)
                                        ;
                                    else
                                    {
                                        /* RECURSION */
                                        STSMSG (1, ("%scrypting directory %s", De, name));
                                        rc = DoDir (nsd, ndd);
                                        STSMSG (1, ("done with directory %s", name));

                                        KDirectoryRelease (ndd);
                                    }
                                }
                                KDirectoryRelease (nsd);
                            }
                        }
                        break;
                    }
                }
            }
        }
        KNamelistRelease (names);
    }
    return rc;
}
Ejemplo n.º 25
0
static
rc_t FileInPlace (KDirectory * cwd, const char * leaf, bool try_rename)
{
    rc_t rc;
    bool is_tmp;

    STSMSG (1, ("%scrypting file in place %s",De,leaf));

    rc = 0;
    is_tmp = IsTmpFile (leaf);

    if (is_tmp)
    {
        STSMSG (1, ("%s is a vdb-decrypt/vdb-encrypt temporary file and will "
                    "be ignored", leaf));
        TmpFoundFlag = true;
        if (ForceFlag)
            ; /* LOG OVERWRITE */
        else
            ; /* LOG TMP */
    }
    if (!is_tmp || ForceFlag)
    {
        char temp [MY_MAX_PATH];


        rc = KDirectoryResolvePath (cwd, false, temp, sizeof temp, ".%s%s",
                                    leaf, TmpExt);

        if (rc)
            PLOGERR (klogErr, (klogErr, rc, "unable to resolve '.$(S)$(E)'",
                               "S=%s,E=%s",leaf,TmpExt));
        else
        {
            KPathType kpt;
            uint32_t kcm;

            kcm = kcmCreate|kcmParents;
            kpt = KDirectoryPathType (cwd, temp);
            if (kpt != kptNotFound)
            {
                /* log busy */
                if (ForceFlag)
                {
                    kcm = kcmInit|kcmParents;
                    /* log force */
                    kpt = kptNotFound;
                }
            }

            if (kpt == kptNotFound)
            {
                const KFile * infile;

                rc = KDirectoryOpenFileRead (cwd, &infile, "%s", leaf);
                if (rc)
                    PLOGERR (klogErr, (klogErr, rc, "Unable to resolve '$(F)'",
                                       "F=%s",leaf));
                else
                {
                    EncScheme scheme;

                    rc = EncryptionTypeCheck (infile, leaf, &scheme);
                    if (rc == 0)
                    {
                        ArcScheme ascheme;
                        bool changed;
                        bool do_this_file;
                        char new_name [MY_MAX_PATH + sizeof EncExt];

                        do_this_file = DoThisFile (infile, scheme, &ascheme);
                        strcpy (new_name, leaf);
                        if (try_rename)
                            changed = NameFixUp (new_name);
                        else
                            changed = false;
                        /*                         KOutMsg ("### %d \n", changed); */

                        if (!do_this_file)
                        {
                            if (changed)
                            {
                                STSMSG (1, ("renaming %s to %s", leaf, new_name));
                                rc = KDirectoryRename (cwd, false, leaf, new_name);
                            }
                            else
                                STSMSG (1, ("skipping %s",leaf));
                        }
                        else
                        {
                            KFile * outfile;

                            rc = KDirectoryCreateExclusiveAccessFile (cwd, &outfile,
                                    false, 0600, kcm,
                                    temp);
                            if (rc)
                                ;
                            else
                            {
                                const KFile * Infile;
                                KFile * Outfile;

                                rc = CryptFile (infile, &Infile, outfile, &Outfile, scheme);

                                if (rc == 0)
                                {
                                    STSMSG (1, ("copying %s to %s", leaf, temp));

                                    rc = CopyFile (Infile, Outfile, leaf, temp);

                                    if (rc == 0)
                                    {
                                        uint32_t access;
                                        KTime_t date;

                                        rc = KDirectoryAccess (cwd, &access, "%s", leaf);
                                        if (rc == 0)
                                            rc = KDirectoryDate (cwd, &date, "%s", leaf);

                                        KFileRelease (infile);
                                        KFileRelease (outfile);
                                        KFileRelease (Infile);
                                        KFileRelease (Outfile);

                                        if (rc == 0)
                                        {
                                            STSMSG (1, ("renaming %s to %s", temp, new_name));

                                            rc = KDirectoryRename (cwd, true, temp, new_name);
                                            if (rc)
                                                LOGERR (klogErr, rc, "error renaming");
                                            else
                                            {
                                                if (changed)
                                                    KDirectoryRemove (cwd, false, "%s", leaf);

                                                /*rc =*/
                                                KDirectorySetAccess (cwd, false, access,
                                                                     0777, "%s", new_name);
                                                KDirectorySetDate (cwd, false, date, "%s", new_name);
                                                /* gonna ignore an error here I think */
                                                return rc;
                                            }
                                        }
                                    }
                                }
                                KFileRelease (outfile);
                            }
                        }
                    }
                    KFileRelease (infile);
                }
            }
        }
    }
    return rc;
}
Ejemplo n.º 26
0
static rc_t ReportBinary(const ReportFuncs *f, uint32_t indent, const char* argv0) {
    rc_t rc = 0;
    KDyld *dyld = NULL;
    assert(argv0);
    rc = KDyldMake(&dyld);
    if (rc != 0) {
        reportError(indent + 1, rc, "KDyldMake");
    }
    else {
        const KDirectory* dir = NULL;
        rc = KDyldHomeDirectory(dyld, &dir, (fptr_t) ReportFinalize);
        if (rc != 0) {
            reportError(indent + 1, rc, "KDyldHomeDirectory");
        }
        else {
            char binary[PATH_MAX + 1];
            const char* name = strpbrk(argv0, "/\\");
            const char* last_name = name;
            if (last_name)
            {   ++last_name; }
            while (name) {
                name = strpbrk(last_name, "/\\");
                if (name) {
                    last_name = name;
                    if (last_name)
                    {   ++last_name; }
                }
            }
            name = last_name ? last_name : argv0;
            rc = KDirectoryResolvePath(dir, true, binary, sizeof binary, "%s", name);
            if (rc != 0) {
                reportErrorStr(indent + 1, rc, "KDirectoryResolvePath",
                    "origin", "KDyldHomeDirectory");
            }
            else {
                bool found = false;
                const char tag[] = "Binary";
                const char* sType = NULL;
                uint8_t digest[16];
                uint32_t type = KDirectoryPathType(dir, "%s", binary);
                switch (type & ~kptAlias) {
                    case kptFile:
                        sType = type & kptAlias ? "alias" : "file";
                        found = true;
                        break;
                    case kptNotFound:
                        sType = "not found";
                        break;
                    default:
                        sType = "unknown";
                        break;
                }
                if (found)
                {   rc = md5(name, digest, dir); }
                if (type & kptAlias) {
                    if (found && rc == 0)  {
                        reportOpen(indent, tag, 3, "path", 's', binary,
                            "type", 's', sType, "md5", 'M', digest);
                    }
                    else {
                        reportOpen(indent, tag, 2, "path", 's', binary,
                            "type", 's', sType);
                    }
                    if (rc == 0 && type & kptAlias)
                    {   rc = ReportAlias(f, indent + 1, name, dir); }
                    reportClose(indent, tag);
                }
                else {
                    if (found && rc == 0)  {
                        report(indent, tag, 3, "path", 's', binary,
                            "type", 's', sType, "md5", 'M', digest);
                    }
                    else {
                        report(indent, tag, 2, "path", 's', binary,
                            "type", 's', sType);
                    }
                }
            }
        }
        RELEASE(KDirectory, dir);
    }
    RELEASE(KDyld, dyld);
    return rc;
}
Ejemplo n.º 27
0
static
rc_t Start (KDirectory * cwd, const char * src, const char * dst)
{
    KPathType dtype;
    KPathType stype;
    char dpath [MY_MAX_PATH];
    char spath [MY_MAX_PATH];
    rc_t rc;
    bool using_stdin, using_stdout, try_rename;

    /* limited anti oops checks */
    try_rename = (dst == NULL);
    if (!try_rename)
    {
        /* try to prevent file to file clash */
        if (strcmp (src,dst) == 0)
            dst = NULL;

        /* try to prevent file to dir clash */
        else
        {
            size_t s,d;

            s = string_size (src);
            d = string_size (dst);

            if (s > d)
            {
                if (string_cmp (src, s, dst, d, d) == 0)
                {
                    if ((strchr (src+d+1, '/') == NULL) &&
                            ((src[d] == '/') ||
                             (src[d-1] == '/')))
                    {
                        try_rename = true;
                        dst = NULL;
                    }
                }
            }
        }
    }

    /*
     * This is a quick fix "hack"
     * A fully built out VFS should replace the KFS in use and eliminate this
     */
    using_stdin = (strcmp (src, "/dev/stdin") == 0);

    if (using_stdin)
    {
        if (dst == NULL)
        {
            rc = RC (rcExe, rcArgv, rcParsing, rcParam, rcNull);
            LOGERR (klogErr, rc, "Unable to handle stdin in place");
            return rc;
        }
        stype = kptFile;
        strcpy (spath, src);
        UseStdin = true;
        STSMSG (1, ("reading console / stdin as input"));
        goto stdin_shortcut;
    }

    rc = KDirectoryResolvePath (cwd, false, spath, sizeof spath, "%s", src);
    if (rc)
    {
        LOGERR (klogErr, rc, "can't resolve source");
        return rc;
    }

    stype = KDirectoryPathType (cwd, spath);

    switch (stype)
    {
    case kptNotFound:
        rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcNotFound);
        break;

    default:
    case kptBadPath:
        rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcInvalid);
        break;

    case kptCharDev:
    case kptBlockDev:
    case kptFIFO:
    case kptZombieFile:
    case kptDataset:
    case kptDatatype:
        rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcIncorrect);
        break;

    case kptFile:
    case kptDir:
        break;
    }
    if (rc)
    {
        PLOGERR (klogErr, (klogErr, rc, "can not use source '$(S)'", "S=%s", src));
        return rc;
    }

    /*
     * In Place Operation
     */
    if (dst == NULL)
    {

        /*
         * Input is a file
         */
        if (stype == kptFile)
        {
            KDirectory * ndir;
            char * pc;

            pc = strrchr (spath, '/');
            if (pc == NULL)
            {
                pc = spath;
                ndir = cwd;
                rc = KDirectoryAddRef (cwd);
            }
            else if (pc == spath)
            {
                ++pc;
                ndir = cwd;
                rc = KDirectoryAddRef (cwd);
            }
            else
            {
                *pc++ = '\0';
                rc = KDirectoryOpenDirUpdate (cwd, &ndir, false, spath);
            }

            if (rc == 0)
            {
                rc = FileInPlace (ndir, pc, try_rename);
                KDirectoryRelease (ndir);
            }
        }
        /*
         * Input is a directory
         */
        else
        {
            KDirectory * ndir;

            rc = KDirectoryOpenDirUpdate (cwd, &ndir, false, spath);
            if (rc)
                ;
            else
            {
                STSMSG (1, ("%scrypting directory %s", De, spath));
                rc = DoDir (ndir, ndir);
                STSMSG (1, ("done with directory %s", spath));
                KDirectoryRelease (ndir);
            }
        }
    }

    /*
     * 'Copy' Operation
     */
    else
    {
stdin_shortcut:
        using_stdout = (strcmp (dst, "/dev/stdout") == 0);
        if (using_stdout == true)
        {
            dtype = kptFile;
            strcpy (dpath, dst);
            UseStdout = true;
            STSMSG (1, ("writing console / stdout as output"));
            goto do_file;
        }
        rc = KDirectoryResolvePath (cwd, false, dpath, sizeof dpath, "%s", dst);
        if (rc)
        {
            LOGERR (klogErr, rc, "can't resolve destination");
            return rc;
        }
        dtype = KDirectoryPathType (cwd, dpath);
        switch (dtype)
        {
        default:
        case kptBadPath:
            rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcInvalid);
            PLOGERR (klogErr, (klogErr, rc, "can not use destination  '$(S)'", "S=%s", dst));
            break;

        case kptCharDev:
        case kptBlockDev:
        case kptFIFO:
        case kptZombieFile:
        case kptDataset:
        case kptDatatype:
            rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcIncorrect);
            PLOGERR (klogErr, (klogErr, rc, "can not use destination parameter '$(S)'", "S=%s", dst));
            break;

        case kptNotFound:
        {
            size_t z;

            z = strlen (dst) - 1;
            if ((dst[z] == '/') || (stype == kptDir))
                goto do_dir;
            else
                goto do_file;
        }

        case kptFile:
            if (!ForceFlag)
            {
                rc = RC (rcExe, rcArgv, rcParsing, rcFile, rcExists);
                PLOGERR (klogErr, (klogErr, rc, "can not over-write '$(F)' without --force",
                                   "F=%s", dpath));
                break;
            }
do_file:
            if (stype == kptFile)
            {
                rc = FileToFile (cwd, spath, cwd, dpath, try_rename, NULL);
            }
            else
            {
                rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcIncorrect);
                LOGERR (klogErr, rc, "Can't do directory to file");
            }
            break;

do_dir:
        case kptDir:
            /*
             * Input is a directory
             */
            if (stype == kptDir)
            {
#if DIRECTORY_TO_DIRECTORY_SUPPORTED
                const KDirectory * sdir;
                KDirectory * ddir;

                rc = KDirectoryOpenDirRead (cwd, &sdir, false, spath);
                if (rc)
                    ;
                else
                {
                    if (dtype == kptNotFound)
                    {
                        STSMSG (1, ("creating output directory %s", dpath));
                        rc = KDirectoryCreateDir (cwd, 0775, kcmCreate|kcmParents,
                                                  "%s", dpath);
                    }
                    if (rc == 0)
                    {
                        rc = KDirectoryOpenDirUpdate (cwd, &ddir, false, dpath);
                        if (rc)
                            ;
                        else
                        {
                            STSMSG (1, ("%scrypting directory %s to %s", De, spath, dpath));
                            rc = DoDir (sdir, ddir);
                            STSMSG (1, ("done with directory %s to %s", spath, dpath));
                            KDirectoryRelease (ddir);
                        }
                    }
                    KDirectoryRelease (sdir);
                }
#else
                rc = RC (rcExe, rcArgv, rcResolving, rcPath, rcIncorrect);
                LOGERR (klogErr, rc, "Can't do directory to directory");
#endif
            }
            /*
             * Input is a file
             */
            else
            {
                KDirectory * ndir;
                const char * pc;

                if (dtype == kptNotFound)
                {
                    STSMSG (1, ("creating output directory %s", dpath));
                    rc = KDirectoryCreateDir (cwd, 0775, kcmCreate|kcmParents,
                                              "%s", dpath);
                }
                if (rc == 0)
                {

                    STSMSG (1, ("opening output directory %s", dpath));
                    rc = KDirectoryOpenDirUpdate (cwd, &ndir, false, dpath);
                    if (rc)
                        ;
                    else
                    {
                        pc = strrchr (spath, '/');
                        if (pc == NULL)
                            pc = spath;
                        else
                            ++pc;

                        rc = FileToFile (cwd, spath, ndir, pc, true, dpath);

                        KDirectoryRelease (ndir);
                    }
                }
            }
            break;
        }
    }
    return rc;
}
Ejemplo n.º 28
0
static rc_t CC ReportObj(const ReportFuncs *f, uint32_t indent,
    const char *object, bool *wasDbOrTableSet)
{
    Report* self = NULL;
    const char* fullpath = NULL;
    const KDatabase* kdb = NULL;
    const KTable* ktbl = NULL;
    const VDatabase* db = NULL;
    KPathType type = kptNotFound;
    KPathType file_type = kptNotFound;
    bool alias = false;
    uint64_t size = 0;
    bool size_unknown = true;

    rc_t rc = ReportGet(&self);
    assert(self);

    if (wasDbOrTableSet != NULL) {
        *wasDbOrTableSet = self->db != NULL || self->table != NULL;
        return 0;
    }

    if (self->db != NULL) {
        type = kptDatabase;
        db = self->db;
    }
    else if (self->table != NULL) {
        rc_t rc2 = VTableOpenParentRead(self->table, &db);
        if (rc2)
        {
            if (rc == 0)
            {
                rc = rc2;
            }
        }
        else if (!db)
        {
            type = kptTable;
            rc2 = VTableGetKTableRead(self->table, &ktbl);
            if (rc2)
            {
                if (rc == 0)
                {
                    rc = rc2;
                }
            }
            else
            {
                rc2 = KTableGetPath(ktbl, &fullpath);
            }
        }
    }

    if (db) {
        rc_t rc2 = VDatabaseOpenKDatabaseRead(db, &kdb);
        type = kptDatabase;
        if (rc2) {
            if (rc == 0)
            {   rc = rc2; }
        }
        else {
            rc2 = KDatabaseGetPath(kdb, &fullpath);
            if (rc2) {
                if (rc == 0)
                {   rc = rc2; }
            }
        }
    }

    if (fullpath) {
        KDirectory* dir = NULL;
        rc_t rc2 = KDirectoryNativeDir(&dir);
        if (rc2) {
            if (rc == 0)
            {   rc = rc2; }
        }
        else {
            file_type = KDirectoryPathType(dir, "%s", fullpath);
            alias = file_type & kptAlias;
            file_type &= ~kptAlias;
            if (file_type == kptFile) {
                rc2 = KDirectoryFileSize(dir, &size, "%s", fullpath);
                if (rc2) {
                    if (rc == 0)
                    {   rc = rc2; }
                }
                else {  size_unknown = false; }
            }
        }
        RELEASE(KDirectory, dir);
    }

    if (object || type != kptNotFound) {
        const char* path
            = fullpath ? fullpath : object ? object : "not set";
        const char* stype = type == kptTable ? "table" : 
            type == kptDatabase ? "database" : "unknown";
        const char* sfile_type = file_type == kptFile ? "archive" : 
            file_type == kptDir ? "dir" : "unexpected";

        if (fullpath && !size_unknown) {
            if (alias)
            { OBJ_P_S_A(indent, path, stype, sfile_type, size); }
            else
            { OBJ_P_S  (indent, path, stype, sfile_type, size); }
        }
        else if (fullpath && size_unknown) {
            if (alias)
            { OBJ_P_A  (indent, path, stype, sfile_type); }
            else
            { OBJ_P    (indent, path, stype, sfile_type); }
        }
        else
        {     OBJ      (indent, path, stype); }

        if (!db)
        {   db = self->db; }

        if (db) {
            rc_t rc2 = ReportDepend(f, indent + 1, db);
            if (rc == 0)
            {   rc = rc2; }
        }
        if (file_type == kptDir) {
            rc_t rc2 = ReportDir(f, indent + 1, ktbl);
            if (rc == 0)
            {   rc = rc2; }
        }

        reportClose(indent, "Object");
    }

    if (db != self->db)
    {   RELEASE(VDatabase, db); }
    RELEASE(KTable, ktbl);
    RELEASE(KDatabase, kdb);

    return rc;
}
Ejemplo n.º 29
0
/* KMain
 */
rc_t CC KMain ( int argc, char *argv [] )
{
    Args * args;
    rc_t rc;

    rc = ArgsMakeAndHandle (&args, argc, argv, 0);
    if (rc)
        LOGERR (klogInt, rc, "failed to parse arguments");
    else do
    {
        uint32_t acount;
        rc = ArgsParamCount (args, &acount);
        if (rc)
        {
            LOGERR (klogInt, rc, "failed to count parameters");
            break;
        }

        if (acount == 0)
        {
            rc = MiniUsage (args);
            break;
        }
        else
        {
            VFSManager* mgr;
            rc = VFSManagerMake(&mgr);
            if (rc)
                LOGERR ( klogErr, rc, "failed to create VFSManager object" );
            else
            {
                VResolver * resolver;

                rc = VFSManagerGetResolver (mgr, &resolver);
                if (rc == 0)
                {
                    uint32_t ix;
                    for ( ix = 0; ix < acount; ++ ix )
                    {
                        const char * pc;
                        rc = ArgsParamValue (args, ix, &pc );
                        if (rc)
                            LOGERR (klogInt, rc,
                                    "failed to retrieve parameter value");
                        else
                        {
                            const VPath * upath = NULL;
                            rc = VFSManagerMakePath ( mgr, (VPath**)&upath, "%s", pc);
                            if (rc == 0)
                            {
                                const VPath * local;
                                const VPath * remote;

                                rc = VResolverQuery (resolver, eProtocolHttp, upath, &local, &remote, NULL);

                                if (rc == 0)
                                {
                                    const String * s;

                                    if (local != NULL)
                                        rc = VPathMakeString (local, &s);
                                    else 
                                        rc = VPathMakeString (remote, &s);
                                    if (rc == 0)
                                    {
                                        OUTMSG (("%S\n", s));
                                        free ((void*)s);
                                    }
                                    VPathRelease (local);
                                    VPathRelease (remote);
                                }
                                else
                                {
                                    KDirectory * cwd;
                                    rc_t orc = VFSManagerGetCWD (mgr, &cwd);
                                    if (orc == 0)
                                    {
                                        KPathType kpt
                                            = KDirectoryPathType(cwd, "%s", pc);
                                        switch (kpt &= ~kptAlias)
                                        {
                                        case kptNotFound:
                                            STSMSG(1, ("'%s': not found while "
                                                "searching the file system",
                                                pc));
                                            break;
                                        case kptBadPath:
                                            STSMSG(1, ("'%s': bad path while "
                                                "searching the file system",
                                                pc));
                                            break;
                                        default:
                                            STSMSG(1, ("'%s': "
                                                "found in the file system",
                                                pc));
                                            rc = 0;
                                            break;
                                        }
                                    }
                                    if (orc == 0 && rc == 0) {
                                        if (rc != 0) {
                                            PLOGMSG(klogErr, (klogErr,
                                                              "'$(name)': not found",
                                                              "name=%s", pc));
                                        }
                                        else {
                                            char resolved[PATH_MAX] = "";
                                            rc = KDirectoryResolvePath(cwd, true,
                                                                       resolved, sizeof resolved, "%s", pc);
                                            if (rc == 0) {
                                                STSMSG(1, ("'%s': found in "
                                                           "the current directory at '%s'",
                                                           pc, resolved));
                                                OUTMSG (("%s\n", resolved));
                                            }
                                            else {
                                                STSMSG(1, ("'%s': cannot resolve "
                                                           "in the current directory",
                                                           pc));
                                                OUTMSG (("./%s\n", pc));
                                            }
                                        }
                                    }
                                    KDirectoryRelease(cwd);
                                }
                            }

                            RELEASE(VPath, upath);
                        }
                    }
                    VResolverRelease (resolver);
                }
                VFSManagerRelease(mgr);
            }
        }
        ArgsWhack (args);

    } while (0);

    return rc;
}
Ejemplo n.º 30
0
static
rc_t CC
_SFTestMake ( const struct _SFTest ** Test, const char * Path )
{
    rc_t RCt;
    struct _SFTest * Ret;
    struct KDirectory * Dir;
    uint32_t PathType;
    uint64_t Size;

    RCt = 0;
    Ret = NULL;
    Dir = NULL;
    PathType = kptNotFound;
    Size = 0;

    if ( Test != NULL ) {
        * Test = NULL;
    }

    if ( Test == NULL || Path == NULL ) {
        return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcNull );
    }

        /* First we shoud check that file exist
         */
    RCt = KDirectoryNativeDir ( & Dir );
    if ( RCt == 0 ) {
        PathType = KDirectoryPathType ( Dir, Path );
        if ( PathType == kptFile ) {
            RCt = KDirectoryFileSize ( Dir, & Size, Path );
            if ( Size == 0 ) {
                return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcInvalid );
            }
        }
        else {
            return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcInvalid );
        }
        KDirectoryRelease ( Dir );
    }

    if ( RCt == 0 ) {
        Ret = calloc ( 1, sizeof ( struct _SFTest ) );
        if ( Ret == NULL ) {
            RCt = RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcExhausted );
        }
        else {
            Ret -> path = string_dup_measure ( Path, NULL );
            if ( Ret -> path == NULL ) {
                RCt = RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcExhausted );
            }
            else {
                Ret -> size = Size;

                * Test = Ret;
            }
        }
    }

    if ( RCt != 0 ) {
        * Test = NULL;

        _SFTestDispose ( Ret );
    }

    return RCt;
}   /* _SFTestMake () */