コード例 #1
0
ファイル: rsExecCmd.c プロジェクト: UPPMAX/irods
int
remoteExecCmd (rsComm_t *rsComm, execCmd_t *execCmdInp, 
execCmdOut_t **execCmdOut, rodsServerHost_t *rodsServerHost)
{
    int status;

    if (rodsServerHost == NULL) {
        rodsLog (LOG_NOTICE,
          "remoteExecCmd: Invalid rodsServerHost");
        return SYS_INVALID_SERVER_HOST;
    }

    if ((status = svrToSvrConnect (rsComm, rodsServerHost)) < 0) {
        return status;
    }

    status = rcExecCmd (rodsServerHost->conn, execCmdInp, execCmdOut);

    if (status < 0) {
        rodsLog (LOG_ERROR,
         "remoteExecCmd: rcExecCmd failed for %s. status = %d",
          execCmdInp->cmd, status);
    } else if (status > 0 &&
      getValByKey (&execCmdInp->condInput, STREAM_STDOUT_KW) != NULL) {
	int fileInx = status;
        (*execCmdOut)->status = bindStreamToIRods (rodsServerHost, fileInx);
        if ((*execCmdOut)->status < 0) {
	    fileCloseInp_t remFileCloseInp;
            rodsLog (LOG_ERROR,
             "remoteExecCmd: bindStreamToIRods failed. status = %d",
              (*execCmdOut)->status);
            memset (&remFileCloseInp, 0, sizeof (remFileCloseInp));
            remFileCloseInp.fileInx = fileInx;
            rcFileClose (rodsServerHost->conn, &remFileCloseInp);
	}
	status = (*execCmdOut)->status;
    } else {
	status = 0;
    }
    return status;
}
コード例 #2
0
ファイル: iexecmd.c プロジェクト: jcnelson/iRODS-FUSE-Mod
int
main(int argc, char **argv) {
    int status;
    rodsEnv myEnv;
    rErrMsg_t errMsg;
    rcComm_t *conn;
    rodsArguments_t myRodsArgs;
    char *optStr;
    execCmd_t execCmd;
    execCmdOut_t *execCmdOut = NULL;
    int nArg;
    char *tmpPtr;

    optStr = "hvH:p:P:";

    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);
    }

    nArg = argc - optind;

    if (nArg <= 0 || nArg > 1) {
        rodsLog (LOG_ERROR, "iexecmd: no input or too many input");
        printf("Use -h for help.\n");
        exit (2);
    }

    if (myRodsArgs.physicalPath == True && myRodsArgs.logicalPath == True) {
        rodsLog (LOG_ERROR,
                 "iexecmd: -p and -P options cannot be used together");
        exit (3);
    }

    memset (&execCmd, 0, sizeof (execCmd));

    if ((tmpPtr = strchr (argv[optind], ' ')) != NULL &&
            tmpPtr != argv[optind]) {
        /* have argv in the command */
        *tmpPtr = '\0';
        rstrcpy (execCmd.cmd, argv[optind], LONG_NAME_LEN);
        tmpPtr ++;
        rstrcpy (execCmd.cmdArgv, tmpPtr, HUGE_NAME_LEN);
    } else {
        rstrcpy (execCmd.cmd, argv[optind], LONG_NAME_LEN);
    }

    if (myRodsArgs.hostAddr == True) {
        rstrcpy (execCmd.execAddr, myRodsArgs.hostAddrString, LONG_NAME_LEN);
    }

    if (myRodsArgs.physicalPath == True) {
        rstrcpy (execCmd.hintPath, myRodsArgs.physicalPathString,
                 LONG_NAME_LEN);
        execCmd.addPathToArgv = 0;
    } else if (myRodsArgs.logicalPath == True) {
        rstrcpy (execCmd.hintPath, myRodsArgs.logicalPathString,
                 LONG_NAME_LEN);
        execCmd.addPathToArgv = 1;
    }
    addKeyVal (&execCmd.condInput, STREAM_STDOUT_KW, "");

    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);
    }

    status = rcExecCmd (conn, &execCmd, &execCmdOut);

    if (status == SYS_UNMATCHED_API_NUM) {
        /* try older version */
        execCmd241_t execCmd241Inp;

        rstrcpy (execCmd241Inp.cmd, execCmd.cmd, LONG_NAME_LEN);
        rstrcpy (execCmd241Inp.cmdArgv, execCmd.cmdArgv, HUGE_NAME_LEN);
        rstrcpy (execCmd241Inp.execAddr, execCmd.execAddr, LONG_NAME_LEN);
        rstrcpy (execCmd241Inp.hintPath, execCmd.hintPath, LONG_NAME_LEN);
        execCmd241Inp.addPathToArgv = execCmd.addPathToArgv;
        bzero (&execCmd241Inp.condInput, sizeof (keyValPair_t));

        status = rcExecCmd241 (conn, &execCmd241Inp, &execCmdOut);
    }

    if (status < 0) {
        rodsLogError (LOG_ERROR, status, "rcExecCmd error. ");
        printErrorStack (conn->rError);
        if (execCmdOut != NULL && execCmdOut->stderrBuf.buf != NULL) {
            fprintf (stderr, "%s", (char *) execCmdOut->stderrBuf.buf);
        }

        rcDisconnect(conn);
        exit (4);
    }


    if (myRodsArgs.verbose == True) {
        printf ("rcExecCmd completed successfully.    Output \n\n");
    }

    if (execCmdOut != NULL) {
        int i;
        char *tmpPtr;
        fileReadInp_t streamReadInp;
        fileCloseInp_t streamCloseInp;
        bytesBuf_t *streamReadOutBBuf;
        int bytesRead;

        if (execCmdOut->stdoutBuf.buf != NULL) {
            tmpPtr = (char*)execCmdOut->stdoutBuf.buf;
            for (i = 0; i < execCmdOut->stdoutBuf.len; i++) {
                fputc ((int)(*tmpPtr), stdout);
                tmpPtr++;
            }
        }
        if (execCmdOut->stderrBuf.buf != NULL) {
            tmpPtr = (char*)execCmdOut->stderrBuf.buf;
            for (i = 0; i < execCmdOut->stderrBuf.len; i++) {
                fputc ((int)(*tmpPtr), stderr);
                tmpPtr++;
            }
        }
        if (execCmdOut->status > 0) {
            /* (execCmdOut->status is a stream descriptor */
            bzero (&streamReadInp, sizeof (streamReadInp));
            streamReadOutBBuf = &execCmdOut->stdoutBuf;
            if (streamReadOutBBuf->len < MAX_SZ_FOR_EXECMD_BUF) {
                if (streamReadOutBBuf->buf != NULL) {
                    free (streamReadOutBBuf->buf);
                }
                streamReadOutBBuf->buf = malloc (MAX_SZ_FOR_EXECMD_BUF);
            }
            streamReadOutBBuf->len = streamReadInp.len = MAX_SZ_FOR_EXECMD_BUF;
            streamReadInp.fileInx = execCmdOut->status;
            while ((bytesRead = rcStreamRead (conn, &streamReadInp,
                                              streamReadOutBBuf)) > 0) {
                tmpPtr = (char*)streamReadOutBBuf->buf;
                for (i = 0; i < bytesRead; i++) {
                    fputc ((int)(*tmpPtr), stdout);
                    tmpPtr++;
                }
            }
            streamCloseInp.fileInx = execCmdOut->status;
            rcStreamClose (conn, &streamCloseInp);
        }
    }
    rcDisconnect(conn);

    exit(0);
}