Exemple #1
0
    void Grub::releasing_idle()
    {
	DBGENTER(_SERVER_DBG_LEV);
	if (double(SIMUL.getTime()) > vtime.get_value()) {
	    Tick extra = Tick(getUtil() * (double(SIMUL.getTime()) - vtime.get_value()));
	    supervisor->set_capacity(extra);
	}
	status = IDLE;
	supervisor->set_idle(this);
    }
Exemple #2
0
    void Grub::startAccounting()
    {
	if (status == EXECUTING) {
	    vtime.start(supervisor->getActiveUtilization()/getUtil());
	    cap.start(-supervisor->getActiveUtilization());
	    Tick delta = cap.get_intercept(0);
	    if (delta < 0) {
		cout << "Task: " << dynamic_cast<Task *>(tasks[0])->getName() << endl;
		cout << "Time: " << SIMUL.getTime() << endl;
		cout << "Status: " << status << " -- intercept: " << delta << endl;
		cout << "capacity: " << cap.get_value() << endl;
		cout << "Supervisor utilization: " << supervisor->getActiveUtilization() << endl;
		assert(0);
	    }
	    _bandExEvt.post(SIMUL.getTime() + cap.get_intercept(0));
	}
    }
Exemple #3
0
int
getUtil (rcComm_t **myConn, rodsEnv *myRodsEnv, rodsArguments_t *myRodsArgs,
rodsPathInp_t *rodsPathInp)
{
    int i;
    int status; 
    int savedStatus = 0;
    rodsPath_t *targPath;
    dataObjInp_t dataObjOprInp;
    rodsRestart_t rodsRestart;
    rcComm_t *conn = *myConn;

    if (rodsPathInp == NULL) {
	return (USER__NULL_INPUT_ERR);
    }

    if (myRodsArgs->ticket == True) {
       if (myRodsArgs->ticketString == NULL) {
	  rodsLog (LOG_ERROR,
		   "initCondForPut: NULL ticketString error");
	  return (USER__NULL_INPUT_ERR);
       } else {
	  setSessionTicket(conn, myRodsArgs->ticketString);
       }
    }

    initCondForGet (conn, myRodsEnv, myRodsArgs, &dataObjOprInp, &rodsRestart);

    if (rodsPathInp->resolved == False) {
        status = resolveRodsTarget (conn, myRodsEnv, rodsPathInp, 1);
        if (status < 0) {
            rodsLogError (LOG_ERROR, status,
              "getUtil: resolveRodsTarget");
            return (status);
	}
	rodsPathInp->resolved = True;
    }

    /* initialize the progress struct */
    if (gGuiProgressCB != NULL) {
        bzero (&conn->operProgress, sizeof (conn->operProgress));
	for (i = 0; i < rodsPathInp->numSrc; i++) {
	    targPath = &rodsPathInp->targPath[i];
            if (targPath->objType == LOCAL_FILE_T) {
                conn->operProgress.totalNumFiles++;
		if (rodsPathInp->srcPath[i].size > 0) {
                    conn->operProgress.totalFileSize += 
		      rodsPathInp->srcPath[i].size;
		}
	    } else {
		getCollSizeForProgStat (conn, rodsPathInp->srcPath[i].outPath,
		  &conn->operProgress);
	    }
	}
    }

    if (conn->fileRestart.flags == FILE_RESTART_ON) {
        fileRestartInfo_t *info;
        status = readLfRestartFile (conn->fileRestart.infoFile, &info);
        if (status >= 0) {
            status = lfRestartGetWithInfo (conn, info);
            if (status >= 0) {
                /* save info so we know what got restarted and what not to
                 * delete in setStateForResume */
                rstrcpy (conn->fileRestart.info.objPath, info->objPath,
                  MAX_NAME_LEN);
                rstrcpy (conn->fileRestart.info.fileName, info->fileName,
                  MAX_NAME_LEN);
                conn->fileRestart.info.status = FILE_RESTARTED;
                printf ("%s was restarted successfully\n",
                  conn->fileRestart.info.objPath);
                unlink (conn->fileRestart.infoFile);
            }
            if (info != NULL) free (info);
        }
    }

    for (i = 0; i < rodsPathInp->numSrc; i++) {
        targPath = &rodsPathInp->targPath[i];

	if (rodsPathInp->srcPath[i].rodsObjStat != NULL &&
	  rodsPathInp->srcPath[i].rodsObjStat->specColl != NULL) {
	    dataObjOprInp.specColl = 
	      rodsPathInp->srcPath[i].rodsObjStat->specColl;
	} else {
	    dataObjOprInp.specColl = NULL;
	}
	if (targPath->objType == LOCAL_FILE_T) {
	    rmKeyVal (&dataObjOprInp.condInput, TRANSLATED_PATH_KW);
	    status = getDataObjUtil (conn, rodsPathInp->srcPath[i].outPath, 
	     targPath->outPath, rodsPathInp->srcPath[i].size,
	      rodsPathInp->srcPath[i].objMode, myRodsEnv, 
	      myRodsArgs, &dataObjOprInp);
	} else if (targPath->objType ==  LOCAL_DIR_T) {
            setStateForRestart (conn, &rodsRestart, targPath, myRodsArgs);
            /* The path given by collEnt.collName from rclReadCollection 
             * has already been translated */
	    addKeyVal (&dataObjOprInp.condInput, TRANSLATED_PATH_KW, "");
	    status = getCollUtil (myConn, rodsPathInp->srcPath[i].outPath,
              targPath->outPath, myRodsEnv, myRodsArgs, &dataObjOprInp,
	      &rodsRestart);
#if 0
            if (rodsRestart.fd > 0 && status < 0) {
                close (rodsRestart.fd);
                return (status);
            }
#endif
	} else {
	    /* should not be here */
	    rodsLog (LOG_ERROR,
	     "getUtil: invalid get dest objType %d for %s", 
	      targPath->objType, targPath->outPath);
	    return (USER_INPUT_PATH_ERR);
	}
	/* XXXX may need to return a global status */
	if (status < 0) {
            rodsLogError (LOG_ERROR, status,
             "getUtil: get error for %s", 
	      targPath->outPath);
	    savedStatus = status;
	    break;
	} 
    }

    if (rodsRestart.fd > 0) {
        close (rodsRestart.fd);
    }
    if (savedStatus < 0) {
        status = savedStatus;
    } else if (status == CAT_NO_ROWS_FOUND) {
        status = 0;
    }

    if (status < 0 && myRodsArgs->retries == True) {
        int reconnFlag;
        /* this is recursive. Only do it the first time */
        myRodsArgs->retries = False;
        if (myRodsArgs->reconnect == True) {
            reconnFlag = RECONN_TIMEOUT;
        } else {
            reconnFlag = NO_RECONN;
        }
        while (myRodsArgs->retriesValue > 0) {
            rErrMsg_t errMsg;
            bzero (&errMsg, sizeof (errMsg));
            status = rcReconnect (myConn, myRodsEnv->rodsHost, myRodsEnv,
              reconnFlag);
            if (status < 0) {
                rodsLogError (LOG_ERROR, status,
                 "getUtil: rcReconnect error for %s", targPath->outPath);
                return status;
            }
            status = getUtil (myConn,  myRodsEnv, myRodsArgs, rodsPathInp);
            if (status >= 0) {
                printf ("Retry get successful\n");
                break;
            } else {
                rodsLogError (LOG_ERROR, status,
                 "getUtil: retry getUtil error");
            }
            myRodsArgs->retriesValue--;
        }
    }
    return status;
}
static int
_download(const char *path, struct stat *stbufIn) {
    int status;
    rcComm_t *conn;
    rodsPathInp_t rodsPathInp;
    rErrMsg_t errMsg;
    char preloadCachePath[MAX_NAME_LEN];
    char preloadCacheWorkPath[MAX_NAME_LEN];

    // set path for getUtil
    status = _getCachePath(path, preloadCachePath);
    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: _getCachePath error.");
        rodsLog (LOG_ERROR, "_download: failed to get cache path - %s", path);
        return status;
    }

    status = _getCacheWorkPath(path, preloadCacheWorkPath);
    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: _getCacheWorkPath error.");
        rodsLog (LOG_ERROR, "_download: failed to get cache work path - %s", path);
        return status;
    }

    rodsLog (LOG_DEBUG, "_download: download %s to %s", path, preloadCachePath);

    // set src path
    memset( &rodsPathInp, 0, sizeof( rodsPathInp_t ) );
    addSrcInPath( &rodsPathInp, (char*)path );
    status = parseRodsPath (&rodsPathInp.srcPath[0], PreloadRodsEnv);
    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: parseRodsPath error.");
        return status;
    }

    // set dest path
    rodsPathInp.destPath = ( rodsPath_t* )malloc( sizeof( rodsPath_t ) );
    memset( rodsPathInp.destPath, 0, sizeof( rodsPath_t ) );
    rstrcpy( rodsPathInp.destPath->inPath, preloadCacheWorkPath, MAX_NAME_LEN );
    status = parseLocalPath (rodsPathInp.destPath);
    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: parseLocalPath error.");
        return status;
    }

    // Connect
    conn = rcConnect (PreloadRodsEnv->rodsHost, PreloadRodsEnv->rodsPort, PreloadRodsEnv->rodsUserName, PreloadRodsEnv->rodsZone, RECONN_TIMEOUT, &errMsg);
    if (conn == NULL) {
        rodsLog (LOG_ERROR, "_download: error occurred while connecting to irods");
        return -EPIPE;
    }

    // Login
    if (strcmp (PreloadRodsEnv->rodsUserName, PUBLIC_USER_NAME) != 0) { 
        status = clientLogin(conn);
        if (status != 0) {
            rodsLogError(LOG_ERROR, status, "_download: ClientLogin error.");
            rcDisconnect(conn);
            return status;
        }
    }

    // make dir
    prepareDir(preloadCachePath);

    // Preload
    rodsLog (LOG_DEBUG, "_download: download %s", path);
    status = getUtil (&conn, PreloadRodsEnv, PreloadRodsArgs, &rodsPathInp);
    rodsLog (LOG_DEBUG, "_download: complete downloading %s", path);

    // Disconnect 
    rcDisconnect(conn);

    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: getUtil error.");
        return status;
    }

    // be careful when using Lock
    LOCK(PreloadLock);
    status = _completeDownload(preloadCacheWorkPath, preloadCachePath, stbufIn);
    if(status < 0) {
        rodsLogError(LOG_ERROR, status, "_download: _completeDownload error.");
        UNLOCK(PreloadLock);
        return status;
    }

    UNLOCK(PreloadLock);
    return 0;
}
int
main( int argc, char **argv ) {

    signal( SIGPIPE, SIG_IGN );

    int status;
    rodsEnv myEnv;
    rErrMsg_t errMsg;
    rcComm_t *conn;
    boost::program_options::variables_map argsMap;
    irods::error err;
    bool useSaveFile = false;
    execMyRuleInp_t execMyRuleInp;
    msParamArray_t *outParamArray = NULL;
    msParamArray_t msParamArray;
    int rulegen;

    int connFlag = 0;
    char saveFile[MAX_NAME_LEN];
    char ruleFile[MAX_NAME_LEN];
    char cmdLineInput[MAX_NAME_LEN];
    err = parseProgramOptions( argc, argv, argsMap ); 

    if ( !err.ok() ) {
        std::cerr << "Error in parsing command line arguments" << std::endl;
        exit( 1 );
    }

    if ( argsMap.count( "help" ) ) {
        usage(/*std::cout*/);
        exit( 0 );
    }

    /* init data structures */
    memset( &execMyRuleInp, 0, sizeof( execMyRuleInp ) );
    memset( &msParamArray, 0, sizeof( msParamArray ) );
    execMyRuleInp.inpParamArray = &msParamArray;
    execMyRuleInp.condInput.len = 0;

    /* add key val for test mode */
    if ( argsMap.count( "test" ) ) {
        addKeyVal( &execMyRuleInp.condInput, "looptest", "true" );
    }
    /* add key val for specifying instance on which to run rule */
    if ( argsMap.count( "rule-engine-plugin-instance" ) ) {
        addKeyVal( &execMyRuleInp.condInput, irods::CFG_INSTANCE_NAME_KW.c_str(), argsMap["rule-engine-plugin-instance"].as<std::string>().c_str() );
    }
    /* Don't need to parse parameters if just listing available rule_engine_instances */
    if ( argsMap.count( "available" ) ) {
        /* add key val for listing available rule engine instances */
        addKeyVal( &execMyRuleInp.condInput, "available", "true" );
    } else {
        /* read rules from the input file */
        if ( argsMap.count( "file" ) ) {
            FILE *fptr;
            int len;
            int gotRule = 0;
            char buf[META_STR_LEN];
            const char* fileName;

            try {
                fileName = argsMap["file"].as< std::string >().c_str();
            } catch ( boost::bad_any_cast& e ) {
                std::cerr << "Bad filename provided to --file option\n";
                std::cerr << "Use -h or --help for help\n";
                exit( 10 );
            } catch ( std::out_of_range& e ) {
                std::cerr << "No filename provided to --file option\n";
                std::cerr << "Use -h or --help for help\n";
                exit( 10 );
            }

            // =-=-=-=-=-=-=-
            // initialize pluggable api table
            irods::api_entry_table&  api_tbl = irods::get_client_api_table();
            irods::pack_entry_table& pk_tbl  = irods::get_pack_table();
            init_api_table( api_tbl, pk_tbl );

            /* if the input file name starts with "i:", the get the file from iRODS server */
            if ( !strncmp( fileName, "i:", 2 ) ) {
                status = getRodsEnv( &myEnv );

                if ( status < 0 ) {
                    rodsLogError( LOG_ERROR, status, "main: getRodsEnv error. " );
                    exit( 1 );
                }

                conn = rcConnect( myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
                        myEnv.rodsZone, 0, &errMsg );

                if ( conn == NULL ) {
                    exit( 2 );
                }

                status = clientLogin( conn );
                if ( status != 0 ) {
                    rcDisconnect( conn );
                    exit( 7 );
                }
                if ( status == 0 ) {
                    char *myargv[3];
                    int myargc, myoptind;
                    rodsPathInp_t rodsPathInp;
                    rodsArguments_t myRodsArgs;
                    connFlag = 1;

                    myargv[0] = strdup( fileName + 2 );
                    myargv[1] = saveFile;
                    myargc = 2;
                    myoptind = 0;
                    const char *fileType = strrchr( fileName, '.' );
                    if ( fileType == NULL ) {
                        printf( "Unsupported input file type\n" );
                        exit( 10 );
                    }
                    if ( strcmp( fileType, ".r" ) == 0 ) {
                        rulegen = 1;
                    }
                    else if ( strcmp( fileType, ".ir" ) == 0 || strcmp( fileType, ".irb" ) == 0 ) {
                        rulegen = 0;
                    }
                    else {
                        rodsLog( LOG_ERROR,
                                "Unsupported input file type %s\n", fileType );
                        exit( 10 );
                    }
                    snprintf( saveFile, MAX_NAME_LEN, "/tmp/tmpiruleFile.%i.%i.%s",
                            ( unsigned int ) time( 0 ), getpid(), rulegen ? "r" : "ir" );
                    status = parseCmdLinePath( myargc, myargv, myoptind, &myEnv,
                            UNKNOWN_OBJ_T, UNKNOWN_FILE_T, 0, &rodsPathInp );
                    status = getUtil( &conn, &myEnv, &myRodsArgs, &rodsPathInp );
                    if ( status < 0 ) {
                        rcDisconnect( conn );
                        exit( 3 );
                    }

                    useSaveFile = true;
                    connFlag = 1;
                }
            }

            if ( useSaveFile ) {
                rstrcpy( ruleFile, saveFile, MAX_NAME_LEN );
            } else {
                rstrcpy( ruleFile, fileName, MAX_NAME_LEN );
            }

            fptr = fopen( ruleFile, "r" );

            /* test if the file can be opened */
            if ( fptr == NULL ) {
                rodsLog( LOG_ERROR, "Cannot open input file %s. errno = %d\n",
                        ruleFile, errno );
                exit( 1 );
            }

            /* test if the file extension is supported */
            const char *fileType = strrchr( ruleFile, '.' );
            if ( fileType == NULL ) {
                printf( "Unsupported input file type\n" );
                exit( 10 );

            }
            else if ( strcmp( fileType, ".r" ) == 0 ) {
                rulegen = 1;
            }
            else if ( strcmp( fileType, ".ir" ) == 0 || strcmp( fileType, ".irb" ) == 0 ) {
                rulegen = 0;
            }
            else {
                rodsLog( LOG_ERROR,
                        "Unsupported input file type %s\n", fileType );
                exit( 10 );
            }

            /* add the @external directive in the rule if the input file is in the new rule engine syntax */
            if ( rulegen ) {
                rstrcpy( execMyRuleInp.myRule, "@external\n", META_STR_LEN );
            }

            while ( ( len = getLine( fptr, buf, META_STR_LEN ) ) > 0 ) {
                if ( argsMap.count( "list" ) ) {
                    puts( buf );
                }

                /* skip comments if the input file is in the old rule engine syntax */
                if ( !rulegen && buf[0] == '#' ) {
                    continue;
                }

                if ( rulegen ) {
                    if ( startsWith( buf, "INPUT" ) || startsWith( buf, "input" ) ) {
                        gotRule = 1;
                        trimSpaces( trimPrefix( buf ) );
                    }
                    else if ( startsWith( buf, "OUTPUT" ) || startsWith( buf, "output" ) ) {
                        gotRule = 2;
                        trimSpaces( trimPrefix( buf ) );
                    }
                }

                if ( gotRule == 0 ) {
                    if ( !rulegen ) {
                        /* the input is a rule */
                        snprintf( execMyRuleInp.myRule + strlen( execMyRuleInp.myRule ), META_STR_LEN - strlen( execMyRuleInp.myRule ), "%s\n", buf );
                    }
                    else {
                        snprintf( execMyRuleInp.myRule + strlen( execMyRuleInp.myRule ), META_STR_LEN - strlen( execMyRuleInp.myRule ), "%s\n", buf );
                    }
                }
                else if ( gotRule == 1 ) {
                    if ( rulegen ) {
                        if ( convertListToMultiString( buf, 1 ) != 0 ) {
                            rodsLog( LOG_ERROR,
                                "Input parameter list format error for %s\n", ruleFile );
                            exit( 10 );
                        }
                    }
                    parseParameters( argsMap, rulegen, &execMyRuleInp, buf );
                }
                else if ( gotRule == 2 ) {
                    if ( rulegen ) {
                        if ( convertListToMultiString( buf, 0 ) != 0 ) {
                            rodsLog( LOG_ERROR,
                                "Output parameter list format error for %s\n", ruleFile );
                            exit( 10 );
                        }
                    }
                    if ( strcmp( buf, "null" ) != 0 ) {
                        rstrcpy( execMyRuleInp.outParamDesc, buf, LONG_NAME_LEN );
                    }
                    break;
                }
                else {
                    break;
                }
                if ( !rulegen ) {
                    gotRule++;
                }
            }

            if ( argsMap.count( "list" ) ) {
                puts( "-----------------------------------------------------------------" );
            }

            if ( gotRule != 2 ) {
                rodsLog( LOG_ERROR, "Incomplete rule input for %s", ruleFile );
                //                     argsMap["file"].as<std::string>().c_str() );
                exit( 2 );
            }
            if ( connFlag == 1 ) {
                fclose( fptr );
                unlink( saveFile );
            }
        }
        else {	/* command line input */
            std::vector< std::string > parameters;
            try {
                parameters = argsMap["parameters"].as< std::vector< std::string> >();
            } catch ( boost::bad_any_cast& e ) {
                std::cerr << "Bad parameter list provided\n";
                std::cerr << "Use -h or --help for help\n";
                exit( 10 );
            } catch ( std::out_of_range& e ) {
                std::cerr << "No parameters list provided\n";
                std::cerr << "Use -h or --help for help\n";
                exit( 10 );
            }

            rulegen = 1;
            if ( parameters.size() < 3 ) {
                rodsLog( LOG_ERROR, "incomplete input" );
                fprintf(stderr, "Use -h for help.\n" );
                exit( 3 );
            }
            
            snprintf( execMyRuleInp.myRule, META_STR_LEN, "@external rule { %s }", parameters.at(0).c_str() );
            rstrcpy( cmdLineInput, parameters.at(1).c_str(), MAX_NAME_LEN );

            if (0 != parseParameters( argsMap, 1, &execMyRuleInp, cmdLineInput )) {
                rodsLog (LOG_ERROR, "Invalid input parameter list specification");
                fprintf( stderr, "Use -h for help.\n" );
                exit(10);
            }

            if ( parameters.at(2) != "null") {
                rstrcpy( execMyRuleInp.outParamDesc, parameters.at(2).c_str(), LONG_NAME_LEN );
            }
        }
    }

    if ( connFlag == 0 ) {
        status = getRodsEnv( &myEnv );

        if ( status < 0 ) {
            rodsLogError( LOG_ERROR, status, "main: getRodsEnv error. " );
            exit( 1 );
        }

        conn = rcConnect( myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
                          myEnv.rodsZone, 0, &errMsg );

        if ( conn == NULL ) {
            rodsLogError( LOG_ERROR, errMsg.status, "rcConnect failure %s",
                          errMsg.msg );
            exit( 2 );
        }

        status = clientLogin( conn );
        if ( status != 0 ) {
            rcDisconnect( conn );
            exit( 7 );
        }
    }

    if ( argsMap.count( "verbose" ) ) {
        printf( "rcExecMyRule: %s\n", rulegen ? execMyRuleInp.myRule + 10 : execMyRuleInp.myRule );
        printf( "outParamDesc: %s\n", execMyRuleInp.outParamDesc );
    }

    status = rcExecMyRule( conn, &execMyRuleInp, &outParamArray );

    if ( argsMap.count( "test" ) ) {
        printErrorStack( conn->rError );
    }

    if ( status < 0 ) {
        msParam_t *mP;
        execCmdOut_t *execCmdOut;

        if ( !rulegen ) {
            rodsLogError( LOG_ERROR, status, "rcExecMyRule error. The rule engine is running under backward compatible mode. To run the rule(s) under normal mode, try renaming the file extension to \".r\". " );

        }
        else {
            rodsLogError( LOG_ERROR, status, "rcExecMyRule error. " );

        }
        printErrorStack( conn->rError );
        if ( ( mP = getMsParamByType( outParamArray, ExecCmdOut_MS_T ) ) != NULL ) {
            execCmdOut = ( execCmdOut_t * ) mP->inOutStruct;
            if ( execCmdOut->stdoutBuf.buf != NULL ) {
                fprintf( stdout, "%s", ( char * ) execCmdOut->stdoutBuf.buf );
            }
            if ( execCmdOut->stderrBuf.buf != NULL ) {
                fprintf( stderr, "%s", ( char * ) execCmdOut->stderrBuf.buf );
            }
        }
        rcDisconnect( conn );
        exit( 4 );
    }

    if ( argsMap.count( "verbose" ) ) {
        printf( "ExecMyRule completed successfully.    Output \n\n" );
        printMsParamNew( outParamArray, 1 );
    }
    else {
        printMsParamNew( outParamArray, 0 );
        msParam_t *mP;
        execCmdOut_t *execCmdOut;
        if ( ( mP = getMsParamByType( outParamArray, ExecCmdOut_MS_T ) ) != NULL ) {
            execCmdOut = ( execCmdOut_t * ) mP->inOutStruct;
            if ( execCmdOut->stdoutBuf.buf != NULL ) {
                fprintf( stdout, "%s", ( char * ) execCmdOut->stdoutBuf.buf );
            }
            if ( execCmdOut->stderrBuf.buf != NULL ) {
                fprintf( stderr, "%s", ( char * ) execCmdOut->stderrBuf.buf );
            }
        }
    }
    if ( argsMap.count( "verbose" ) && conn->rError != NULL ) {
        int i, len;
        rErrMsg_t *errMsg;
        len = conn->rError->len;
        for ( i = 0; i < len; i++ ) {
            errMsg = conn->rError->errMsg[i];
            printf( "%s\n", errMsg->msg );
        }
    }

    printErrorStack( conn->rError );
    rcDisconnect( conn );
    exit( 0 );

}
Exemple #6
0
int
main( int argc, char **argv ) {

    signal( SIGPIPE, SIG_IGN );

    int status;
    rodsEnv myEnv;
    rErrMsg_t errMsg;
    rcComm_t *conn;
    rodsArguments_t myRodsArgs;
    char *optStr;
    rodsPathInp_t rodsPathInp;
    int reconnFlag;


    optStr = "hfIKN:n:PQrt:vVX:R:TZ";

    status = parseCmdLineOpt( argc, argv, optStr, 1, &myRodsArgs );

    if ( status < 0 ) {
        printf( "Use -h for help.\n" );
        exit( 1 );
    }
    if ( myRodsArgs.help == True ) {
        usage();
        exit( 0 );
    }

    status = getRodsEnv( &myEnv );

    if ( status < 0 ) {
        rodsLogError( LOG_ERROR, status, "main: getRodsEnv error. " );
        exit( 1 );
    }

    status = parseCmdLinePath( argc, argv, optind, &myEnv,
                               UNKNOWN_OBJ_T, UNKNOWN_FILE_T, 0, &rodsPathInp );

    if ( status < 0 ) {
        rodsLogError( LOG_ERROR, status, "main: parseCmdLinePath error. " );
        printf( "Use -h for help.\n" );
        exit( 1 );
    }

    if ( myRodsArgs.reconnect == True ) {
        reconnFlag = RECONN_TIMEOUT;
    }
    else {
        reconnFlag = NO_RECONN;
    }

    // =-=-=-=-=-=-=-
    // initialize pluggable api table
    irods::api_entry_table& api_tbl = irods::get_client_api_table();
    irods::pack_entry_table& pk_tbl = irods::get_pack_table();
    init_api_table( api_tbl, pk_tbl );

    conn = rcConnect( myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
                      myEnv.rodsZone, reconnFlag, &errMsg );

    if ( conn == NULL ) {
        exit( 2 );
    }

    if ( strcmp( myEnv.rodsUserName, PUBLIC_USER_NAME ) != 0 ) {
        status = clientLogin( conn );
        if ( status != 0 ) {
            rcDisconnect( conn );
            exit( 7 );
        }
    }

    if ( myRodsArgs.progressFlag == True ) {
        gGuiProgressCB = ( guiProgressCallback ) iCommandProgStat;
    }

    status = getUtil( &conn, &myEnv, &myRodsArgs, &rodsPathInp );

    printErrorStack( conn->rError );
    rcDisconnect( conn );

    if ( status < 0 ) {
        exit( 3 );
    }
    else {
        exit( 0 );
    }

}
Exemple #7
0
int
main( int argc, char **argv ) {
    int status;
    rodsEnv myEnv;
    rErrMsg_t errMsg;
    rcComm_t *conn;
    rodsArguments_t myRodsArgs;
    char *optStr;
    execMyRuleInp_t execMyRuleInp;
    msParamArray_t *outParamArray = NULL;
    msParamArray_t msParamArray;
    int rulegen;

    int connFlag = 0;
    char saveFile[MAX_NAME_LEN];

    optStr = "ZhlvF:s";

    status = parseCmdLineOpt( argc, argv, optStr, 1, &myRodsArgs );

    if ( status < 0 ) {
        printf( "Use -h for help.\n" );
        exit( 1 );
    }

    if ( myRodsArgs.help == True ) {
        usage();
        exit( 0 );
    }

    /* init data structures */
    memset( &execMyRuleInp, 0, sizeof( execMyRuleInp ) );
    memset( &msParamArray, 0, sizeof( msParamArray ) );
    execMyRuleInp.inpParamArray = &msParamArray;
    execMyRuleInp.condInput.len = 0;

    /* add key val for test mode */
    if ( myRodsArgs.test == True ) {
        addKeyVal( &execMyRuleInp.condInput, "looptest", "true" );
    }

    /* read rules from the input file */
    if ( myRodsArgs.file == True ) {
        FILE *fptr;
        int len;
        int gotRule = 0;
        char buf[META_STR_LEN];
        char *inpParamNames[1024];
        char *outParamNames[1024];

        // =-=-=-=-=-=-=-
        // initialize pluggable api table
        irods::api_entry_table&  api_tbl = irods::get_client_api_table();
        irods::pack_entry_table& pk_tbl  = irods::get_pack_table();
        init_api_table( api_tbl, pk_tbl );

        /* if the input file name starts with "i:", the get the file from iRODS server */
        if ( !strncmp( myRodsArgs.fileString, "i:", 2 ) ) {
            status = getRodsEnv( &myEnv );

            if ( status < 0 ) {
                rodsLogError( LOG_ERROR, status, "main: getRodsEnv error. " );
                exit( 1 );
            }

            conn = rcConnect( myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
                              myEnv.rodsZone, 0, &errMsg );

            if ( conn == NULL ) {
                exit( 2 );
            }

            status = clientLogin( conn );
            if ( status != 0 ) {
                rcDisconnect( conn );
                exit( 7 );
            }
            if ( status == 0 ) {
                char *myargv[3];
                int myargc, myoptind;
                rodsPathInp_t rodsPathInp;
                connFlag = 1;

                myargv[0] = strdup( myRodsArgs.fileString + 2 );
                myargv[1] = saveFile;
                myargc = 2;
                myoptind = 0;
                char *fileType = strrchr( myRodsArgs.fileString, '.' );
                if ( fileType == NULL ) {
                    printf( "Unsupported input file type\n" );
                    exit( 10 );
                }
                if ( strcmp( fileType, ".r" ) == 0 ) {
                    rulegen = 1;
                }
                else if ( strcmp( fileType, ".ir" ) == 0 || strcmp( fileType, ".irb" ) == 0 ) {
                    rulegen = 0;
                }
                else {
                    rodsLog( LOG_ERROR,
                             "Unsupported input file type %s\n", fileType );
                    exit( 10 );
                }
                snprintf( saveFile, MAX_NAME_LEN, "/tmp/tmpiruleFile.%i.%i.%s",
                          ( unsigned int ) time( 0 ), getpid(), rulegen ? "r" : "ir" );
                status = parseCmdLinePath( myargc, myargv, myoptind, &myEnv,
                                           UNKNOWN_OBJ_T, UNKNOWN_FILE_T, 0, &rodsPathInp );
                status = getUtil( &conn, &myEnv, &myRodsArgs, &rodsPathInp );
                if ( status < 0 ) {
                    rcDisconnect( conn );
                    exit( 3 );
                }
                myRodsArgs.fileString = saveFile;
                connFlag = 1;
            }
        }

        fptr = fopen( myRodsArgs.fileString, "r" );

        /* test if the file can be opened */
        if ( fptr == NULL ) {
            rodsLog( LOG_ERROR, "Cannot open input file %s. errno = %d\n",
                     myRodsArgs.fileString, errno );
            exit( 1 );
        }

        /* test if the file extension is supported */
        char *fileType = strrchr( myRodsArgs.fileString, '.' );
        if ( fileType == NULL ) {
            printf( "Unsupported input file type\n" );
            exit( 10 );

        }
        else if ( strcmp( fileType, ".r" ) == 0 ) {
            rulegen = 1;
        }
        else if ( strcmp( fileType, ".ir" ) == 0 || strcmp( fileType, ".irb" ) == 0 ) {
            rulegen = 0;
        }
        else {
            rodsLog( LOG_ERROR,
                     "Unsupported input file type %s\n", fileType );
            exit( 10 );
        }

        /* add the @external directive in the rule if the input file is in the new rule engine syntax */
        if ( rulegen ) {
            rstrcpy( execMyRuleInp.myRule, "@external\n", META_STR_LEN );
        }

        while ( ( len = getLine( fptr, buf, META_STR_LEN ) ) > 0 ) {
            if ( myRodsArgs.longOption == True ) {
                puts( buf );
            }

            /* skip comments if the input file is in the old rule engine syntax */
            if ( !rulegen && buf[0] == '#' ) {
                continue;
            }

            if ( rulegen ) {
                if ( startsWith( buf, "INPUT" ) || startsWith( buf, "input" ) ) {
                    gotRule = 1;
                    trimSpaces( trimPrefix( buf ) );
                }
                else if ( startsWith( buf, "OUTPUT" ) || startsWith( buf, "output" ) ) {
                    gotRule = 2;
                    trimSpaces( trimPrefix( buf ) );
                }
            }

            if ( gotRule == 0 ) {
                if ( !rulegen ) {
                    /* the input is a rule */
                    snprintf( execMyRuleInp.myRule + strlen( execMyRuleInp.myRule ), META_STR_LEN - strlen( execMyRuleInp.myRule ), "%s\n", buf );
                }
                else {
                    snprintf( execMyRuleInp.myRule + strlen( execMyRuleInp.myRule ), META_STR_LEN - strlen( execMyRuleInp.myRule ), "%s\n", buf );
                }
            }
            else if ( gotRule == 1 ) {
                if ( rulegen ) {
                    if ( convertListToMultiString( buf, 1 ) != 0 ) {
                        rodsLog( LOG_ERROR,
                                 "Input parameter list format error for %s\n", myRodsArgs.fileString );
                        exit( 10 );
                    }
                }
                extractVarNames( inpParamNames, buf );
                parseMsInputParam( argc, argv, optind, rulegen, myRodsArgs.string, &execMyRuleInp, buf );
            }
            else if ( gotRule == 2 ) {
                if ( rulegen ) {
                    if ( convertListToMultiString( buf, 0 ) != 0 ) {
                        rodsLog( LOG_ERROR,
                                 "Output parameter list format error for %s\n", myRodsArgs.fileString );
                        exit( 10 );
                    }
                }
                extractVarNames( outParamNames, buf );
                if ( strcmp( buf, "null" ) != 0 ) {
                    rstrcpy( execMyRuleInp.outParamDesc, buf, LONG_NAME_LEN );
                }
                break;
            }
            else {
                break;
            }
            if ( !rulegen ) {
                gotRule++;
            }
        }

        if ( myRodsArgs.longOption == True ) {
            puts( "-----------------------------------------------------------------" );
        }

        if ( gotRule != 2 ) {
            rodsLog( LOG_ERROR, "Incomplete rule input for %s",
                     myRodsArgs.fileString );
            exit( 2 );
        }
        if ( connFlag == 1 ) {
            fclose( fptr );
            unlink( saveFile );
        }
    }
    else {	/* command line input */
        rulegen = 1;
        int nArg = argc - optind; /* number of rule arguments */
        if ( nArg < 3 ) {
            rodsLog( LOG_ERROR, "no input" );
            printf( "Use -h for help.\n" );
            exit( 3 );
        }

        snprintf( execMyRuleInp.myRule, META_STR_LEN, "@external rule { %s }", argv[optind] );
        parseMsInputParam( 0, NULL, 0, 1, myRodsArgs.string, &execMyRuleInp, argv[optind + 1] );
        if ( strcmp( argv[optind + 2], "null" ) != 0 ) {
            rstrcpy( execMyRuleInp.outParamDesc, argv[optind + 2],
                     LONG_NAME_LEN );
        }
    }


    if ( connFlag == 0 ) {
        status = getRodsEnv( &myEnv );

        if ( status < 0 ) {
            rodsLogError( LOG_ERROR, status, "main: getRodsEnv error. " );
            exit( 1 );
        }

        conn = rcConnect( myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName,
                          myEnv.rodsZone, 0, &errMsg );

        if ( conn == NULL ) {
            rodsLogError( LOG_ERROR, errMsg.status, "rcConnect failure %s",
                          errMsg.msg );
            exit( 2 );
        }

        status = clientLogin( conn );
        if ( status != 0 ) {
            rcDisconnect( conn );
            exit( 7 );
        }
    }

    if ( myRodsArgs.verbose == True ) {
        printf( "rcExecMyRule: %s\n", rulegen ? execMyRuleInp.myRule + 10 : execMyRuleInp.myRule );
        printf( "outParamDesc: %s\n", execMyRuleInp.outParamDesc );
    }

    status = rcExecMyRule( conn, &execMyRuleInp, &outParamArray );

    if ( myRodsArgs.test == True ) {
        printErrorStack( conn->rError );
    }

    if ( status < 0 ) {
        msParam_t *mP;
        execCmdOut_t *execCmdOut;

        if ( !rulegen ) {
            rodsLogError( LOG_ERROR, status, "rcExecMyRule error. The rule engine is running under backward compatible mode. To run the rule(s) under normal mode, try renaming the file extension to \".r\". " );

        }
        else {
            rodsLogError( LOG_ERROR, status, "rcExecMyRule error. " );

        }
        printErrorStack( conn->rError );
        if ( ( mP = getMsParamByType( outParamArray, ExecCmdOut_MS_T ) ) != NULL ) {
            execCmdOut = ( execCmdOut_t * ) mP->inOutStruct;
            if ( execCmdOut->stdoutBuf.buf != NULL ) {
                fprintf( stdout, "%s", ( char * ) execCmdOut->stdoutBuf.buf );
            }
            if ( execCmdOut->stderrBuf.buf != NULL ) {
                fprintf( stderr, "%s", ( char * ) execCmdOut->stderrBuf.buf );
            }
        }
        rcDisconnect( conn );
        exit( 4 );
    }

    if ( myRodsArgs.verbose == True ) {
        printf( "ExecMyRule completed successfully.    Output \n\n" );
        printMsParamNew( outParamArray, 1 );
    }
    else {
        printMsParamNew( outParamArray, 0 );
        msParam_t *mP;
        execCmdOut_t *execCmdOut;
        if ( ( mP = getMsParamByType( outParamArray, ExecCmdOut_MS_T ) ) != NULL ) {
            execCmdOut = ( execCmdOut_t * ) mP->inOutStruct;
            if ( execCmdOut->stdoutBuf.buf != NULL ) {
                fprintf( stdout, "%s", ( char * ) execCmdOut->stdoutBuf.buf );
            }
            if ( execCmdOut->stderrBuf.buf != NULL ) {
                fprintf( stderr, "%s", ( char * ) execCmdOut->stderrBuf.buf );
            }
        }
    }
    if ( myRodsArgs.verbose == True && conn->rError != NULL ) {
        int i, len;
        rErrMsg_t *errMsg;
        len = conn->rError->len;
        for ( i = 0; i < len; i++ ) {
            errMsg = conn->rError->errMsg[i];
            printf( "%s\n", errMsg->msg );
        }
    }

    printErrorStack( conn->rError );
    rcDisconnect( conn );
    exit( 0 );

}