/* * Copyright (C) 2008-2011 High Energy Accelerator Research Organization (KEK) * Copyright (C) 2008-2011 National Institute of Informatics in Japan. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /*** Copyright (c), The Regents of the University of California *** *** For more information please refer to files in the COPYRIGHT directory ***/ /* This is an interface to the Attribute-Value-Units type of metadata. */ #include "irods_icommands.hpp" #define MAX_SQL 300 #define BIG_STR 200 //#define linux_platform namespace irods_file_adaptor { namespace api { char cwd[BIG_STR]; int debug=0; int testMode=0; /* some some particular internal tests */ char zoneArgument[MAX_NAME_LEN+2]=""; int lastCommandStatus=0; int printCount=0; int icommands::imeta(int argc, char **argv) { rcComm_t *Conn; rodsEnv myEnv; int status, i, j; rErrMsg_t errMsg; // rodsArguments_t myRodsArgs; char *mySubName; char *myName; int argOffset; int maxCmdTokens=20; char *cmdToken[20]; int keepGoing; int firstTime; rodsLogLevel(LOG_ERROR); // for(int i=0; i 1) { // if (argOffset > 2) { // if (*argv[1]=='-' && *(argv[1]+1)=='z') { // if (*(argv[1]+2)=='\0') { // argOffset=3; /* skip -z zone */ // } // else { // argOffset=2; /* skip -z zone */ // } // } // else { // argOffset=1; /* Ignore the parseCmdLineOpt parsing // as -d etc handled below*/ // } // } // else { argOffset=1; /* Ignore the parseCmdLineOpt parsing // as -d etc handled below*/ // } // } status = getRodsEnv (&myEnv); if (status < 0) { rodsLog (LOG_ERROR, "main: getRodsEnv error. status = %d", status); exit (1); } strncpy(cwd,myEnv.rodsCwd,BIG_STR); if (strlen(cwd)==0) { strcpy(cwd,"/"); } for (i=0;iMAX_PASSWORD_LEN-2) { printf("Password exceeds maximum length\n"); } else { obfEncodeByKey(cmdToken[1], cmdToken[2], scrambled); printf("Scrambled form is:%s\n", scrambled); } exit(0); } Conn = rcConnect (myEnv.rodsHost, myEnv.rodsPort, myEnv.rodsUserName, myEnv.rodsZone, 0, &errMsg); if (Conn == NULL) { myName = rodsErrorName(errMsg.status, &mySubName); rodsLog(LOG_ERROR, "rcConnect failure %s (%s) (%d) %s", myName, mySubName, errMsg.status, errMsg.msg); exit (2); } status = clientLogin(Conn); if (status != 0) { if (!debug) exit (3); } keepGoing=1; firstTime=1; while (keepGoing) { int status; status=doCommand(Conn, myEnv, cmdToken); if (status==-1) keepGoing=0; if (firstTime) { if (status==0) keepGoing=0; if (status==-2) { keepGoing=0; lastCommandStatus=-1; } firstTime=0; } if (keepGoing) { getInput(Conn, cmdToken, maxCmdTokens); } } rcDisconnect(Conn); if (lastCommandStatus != 0) exit(4); return 0; // exit(0); } /* print the results of a general query. */ void icommands::printGenQueryResults(rcComm_t *Conn, int status, genQueryOut_t *genQueryOut, char *descriptions[]) { int i, j; lastCommandStatus = status; if (status == CAT_NO_ROWS_FOUND) lastCommandStatus = 0; if (status!=0 && status != CAT_NO_ROWS_FOUND) { printError(Conn, status, "rcGenQuery"); } else { if (status == CAT_NO_ROWS_FOUND) { if (printCount==0) printf("No rows found\n"); } else { for (i=0;irowCnt;i++) { if (i>0) printf("---- \n"); for (j=0;jattriCnt;j++) { char *tResult; tResult = genQueryOut->sqlResult[j].value; tResult += i*genQueryOut->sqlResult[j].len; printf("%s: %s\n", descriptions[j], tResult); printCount++; } } } } } /* Via a general query and show the AVUs for a dataobject. */ int icommands::showDataObj(rcComm_t *Conn, char *name, char *attrName, int wild) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; char v3[BIG_STR]; char fullName[LONG_NAME_LEN]; char myDirName[LONG_NAME_LEN]; char myFileName[LONG_NAME_LEN]; int status; /* "id" only used in testMode :*/ char *columnNames[]={"attribute", "value", "units", "id"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); // printf("AVUs defined for dataObj %s:\n",name); printCount=0; i1a[0]=COL_META_DATA_ATTR_NAME; i1b[0]=0; i1a[1]=COL_META_DATA_ATTR_VALUE; i1b[1]=0; i1a[2]=COL_META_DATA_ATTR_UNITS; i1b[2]=0; if (testMode) { i1a[3]=COL_META_DATA_ATTR_ID; i1b[3]=0; } genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 3; if (testMode) { genQueryInp.selectInp.len = 4; } i2a[0]=COL_COLL_NAME; sprintf(v1,"='%s'",cwd); condVal[0]=v1; i2a[1]=COL_DATA_NAME; sprintf(v2,"='%s'",name); condVal[1]=v2; strncpy(fullName, cwd, LONG_NAME_LEN); rstrcat(fullName, "/", LONG_NAME_LEN); rstrcat(fullName, name, LONG_NAME_LEN); if (strstr(name, "/") != NULL) { /* reset v1 and v2 for when full path or relative path entered */ if (*name=='/') { strncpy(fullName, name, LONG_NAME_LEN); } status = splitPathByKey(fullName, myDirName, myFileName, '/'); sprintf(v1,"='%s'",myDirName); sprintf(v2,"='%s'",myFileName); } genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; if (attrName != NULL && *attrName!='\0') { i2a[2]=COL_META_DATA_ATTR_NAME; if (wild) { sprintf(v3,"like '%s'",attrName); } else { sprintf(v3,"= '%s'",attrName); } condVal[2]=v3; genQueryInp.sqlCondInp.len++; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status == CAT_NO_ROWS_FOUND) { i1a[0]=COL_D_DATA_PATH; genQueryInp.selectInp.len = 1; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status==0) { printf("None\n"); return(0); } if (status == CAT_NO_ROWS_FOUND) { printf("Dataobject %s does not exist.\n", fullName); return(0); } printGenQueryResults(Conn, status, genQueryOut, columnNames); } else { printGenQueryResults(Conn, status, genQueryOut, columnNames); } while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Via a general query, show the AVUs for a collection */ int icommands::showColl(rcComm_t *Conn, char *name, char *attrName, int wild) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; char fullName[LONG_NAME_LEN]; int status; char *columnNames[]={"attribute", "value", "units"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); // printf("AVUs defined for collection %s:\n",name); printCount=0; i1a[0]=COL_META_COLL_ATTR_NAME; i1b[0]=0; /* currently unused */ i1a[1]=COL_META_COLL_ATTR_VALUE; i1b[1]=0; i1a[2]=COL_META_COLL_ATTR_UNITS; i1b[2]=0; genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 3; strncpy(fullName, cwd, LONG_NAME_LEN); if (strlen(name)>0) { if (*name=='/') { strncpy(fullName, name, LONG_NAME_LEN); } else { rstrcat(fullName, "/", LONG_NAME_LEN); rstrcat(fullName, name, LONG_NAME_LEN); } } i2a[0]=COL_COLL_NAME; sprintf(v1,"='%s'",fullName); condVal[0]=v1; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=1; if (attrName != NULL && *attrName!='\0') { i2a[1]=COL_META_COLL_ATTR_NAME; if (wild) { sprintf(v2,"like '%s'",attrName); } else { sprintf(v2,"= '%s'",attrName); } condVal[1]=v2; genQueryInp.sqlCondInp.len++; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status == CAT_NO_ROWS_FOUND) { i1a[0]=COL_COLL_COMMENTS; genQueryInp.selectInp.len = 1; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status==0) { printf("None\n"); return(0); } if (status == CAT_NO_ROWS_FOUND) { printf("Collection %s does not exist.\n", fullName); return(0); } } printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Via a general query, show the AVUs for a resource */ int icommands::showResc(rcComm_t *Conn, char *name, char *attrName, int wild) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"attribute", "value", "units"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); // printf("AVUs defined for resource %s:\n",name); printCount=0; i1a[0]=COL_META_RESC_ATTR_NAME; i1b[0]=0; /* currently unused */ i1a[1]=COL_META_RESC_ATTR_VALUE; i1b[1]=0; i1a[2]=COL_META_RESC_ATTR_UNITS; i1b[2]=0; genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 3; i2a[0]=COL_R_RESC_NAME; sprintf(v1,"='%s'",name); condVal[0]=v1; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=1; if (attrName != NULL && *attrName!='\0') { i2a[1]=COL_META_RESC_ATTR_NAME; if (wild) { sprintf(v2,"like '%s'",attrName); } else { sprintf(v2,"= '%s'",attrName); } condVal[1]=v2; genQueryInp.sqlCondInp.len++; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status == CAT_NO_ROWS_FOUND) { i1a[0]=COL_R_RESC_INFO; genQueryInp.selectInp.len = 1; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status==0) { printf("None\n"); return(0); } if (status == CAT_NO_ROWS_FOUND) { printf("Resource %s does not exist.\n", name); return(0); } } printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Via a general query, show the AVUs for a resource group */ int icommands::showRescGroup(rcComm_t *Conn, char *name, char *attrName, int wild) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"attribute", "value", "units"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); // printf("AVUs defined for resource group %s:\n",name); printCount=0; i1a[0]=COL_META_RESC_GROUP_ATTR_NAME; i1b[0]=0; /* currently unused */ i1a[1]=COL_META_RESC_GROUP_ATTR_VALUE; i1b[1]=0; i1a[2]=COL_META_RESC_GROUP_ATTR_UNITS; i1b[2]=0; genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 3; i2a[0]=COL_RESC_GROUP_NAME; sprintf(v1,"='%s'",name); condVal[0]=v1; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=1; if (attrName != NULL && *attrName!='\0') { i2a[1]=COL_META_RESC_GROUP_ATTR_NAME; if (wild) { sprintf(v2,"like '%s'",attrName); } else { sprintf(v2,"= '%s'",attrName); } condVal[1]=v2; genQueryInp.sqlCondInp.len++; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; /* if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } */ status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status == CAT_NO_ROWS_FOUND) { /* When no AVU found, test the existence of the resource group itself */ i1a[0]=COL_RESC_GROUP_NAME; genQueryInp.selectInp.len = 1; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status==0) { printf("None\n"); return(0); } if (status == CAT_NO_ROWS_FOUND) { printf("Resource group %s does not exist.\n", name); return(0); } } printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Via a general query, show the AVUs for a user */ int icommands::showUser(rcComm_t *Conn, rodsEnv myEnv, char *name, char *attrName, int wild) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; char v3[BIG_STR]; int status; char *columnNames[]={"attribute", "value", "units"}; char userName[NAME_LEN]; char userZone[NAME_LEN]; status = parseUserName(name, userName, userZone); if (status) { printf("Invalid username format\n"); return(0); } if (userZone[0]=='\0') { strncpy(userZone, myEnv.rodsZone, NAME_LEN); } memset (&genQueryInp, 0, sizeof (genQueryInp_t)); // printf("AVUs defined for user %s#%s:\n",userName, userZone); printCount=0; i1a[0]=COL_META_USER_ATTR_NAME; i1b[0]=0; /* currently unused */ i1a[1]=COL_META_USER_ATTR_VALUE; i1b[1]=0; i1a[2]=COL_META_USER_ATTR_UNITS; i1b[2]=0; genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 3; i2a[0]=COL_USER_NAME; sprintf(v1,"='%s'",userName); condVal[0]=v1; i2a[1]=COL_USER_ZONE; sprintf(v2,"='%s'",userZone); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; if (attrName != NULL && *attrName!='\0') { i2a[2]=COL_META_USER_ATTR_NAME; if (wild) { sprintf(v3,"like '%s'",attrName); } else { sprintf(v3,"= '%s'",attrName); } condVal[2]=v3; genQueryInp.sqlCondInp.len++; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status == CAT_NO_ROWS_FOUND) { i1a[0]=COL_USER_COMMENT; genQueryInp.selectInp.len = 1; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (status==0) { printf("None\n"); return(0); } if (status == CAT_NO_ROWS_FOUND) { printf("User %s does not exist.\n", name); return(0); } } printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Do a query on AVUs for dataobjs and show the results attribute op value [AND attribute op value] [REPEAT] */ int icommands::queryDataObj(rcComm_t *Conn, char *cmdToken[]) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[20]; int i1b[20]; int i2a[20]; char *condVal[20]; char v1[BIG_STR]; char v2[BIG_STR]; char v3[BIG_STR]; int status; char *columnNames[]={"collection", "dataObj"}; int cmdIx; int condIx; char vstr[20] [BIG_STR]; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); printCount=0; i1a[0]=COL_COLL_NAME; i1b[0]=0; /* (unused) */ i1a[1]=COL_DATA_NAME; i1b[1]=0; genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 2; i2a[0]=COL_META_DATA_ATTR_NAME; sprintf(v1,"='%s'", cmdToken[2]); condVal[0]=v1; i2a[1]=COL_META_DATA_ATTR_VALUE; sprintf(v2, "%s '%s'", cmdToken[3], cmdToken[4]); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; if (strcmp(cmdToken[5], "or")==0) { sprintf(v3, "|| %s '%s'", cmdToken[6], cmdToken[7]); rstrcat(v2, v3, BIG_STR); } cmdIx = 5; condIx = 2; while (strcmp(cmdToken[cmdIx], "and")==0) { i2a[condIx]=COL_META_DATA_ATTR_NAME; cmdIx++; sprintf(vstr[condIx],"='%s'", cmdToken[cmdIx]); condVal[condIx]=vstr[condIx]; condIx++; i2a[condIx]=COL_META_DATA_ATTR_VALUE; sprintf(vstr[condIx], "%s '%s'", cmdToken[cmdIx+1], cmdToken[cmdIx+2]); cmdIx+=3; condVal[condIx]=vstr[condIx]; condIx++; genQueryInp.sqlCondInp.len+=2; } genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Do a query on AVUs for collections and show the results */ int icommands::queryCollection(rcComm_t *Conn, char *attribute, char *op, char *value) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"collection"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); printCount=0; i1a[0]=COL_COLL_NAME; i1b[0]=0; /* (unused) */ genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 1; i2a[0]=COL_META_COLL_ATTR_NAME; sprintf(v1,"='%s'",attribute); condVal[0]=v1; i2a[1]=COL_META_COLL_ATTR_VALUE; sprintf(v2, "%s '%s'", op, value); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Do a query on AVUs for resources and show the results */ int icommands::queryResc(rcComm_t *Conn, char *attribute, char *op, char *value) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"resource"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); printCount=0; i1a[0]=COL_R_RESC_NAME; i1b[0]=0; /* (unused) */ genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 1; i2a[0]=COL_META_RESC_ATTR_NAME; sprintf(v1,"='%s'",attribute); condVal[0]=v1; i2a[1]=COL_META_RESC_ATTR_VALUE; sprintf(v2, "%s '%s'", op, value); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Do a query on AVUs for resource groups and show the results */ int icommands::queryRescGroup(rcComm_t *Conn, char *attribute, char *op, char *value) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"resource group"}; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); printCount=0; i1a[0]=COL_RESC_GROUP_NAME; i1b[0]=0; /* (unused) */ genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 1; i2a[0]=COL_META_RESC_GROUP_ATTR_NAME; sprintf(v1,"='%s'",attribute); condVal[0]=v1; i2a[1]=COL_META_RESC_GROUP_ATTR_VALUE; sprintf(v2, "%s '%s'", op, value); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; /* if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } */ status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Do a query on AVUs for users and show the results */ int icommands::queryUser(rcComm_t *Conn, rodsEnv myEnv, char *attribute, char *op, char *value) { genQueryInp_t genQueryInp; genQueryOut_t *genQueryOut; int i1a[10]; int i1b[10]; int i2a[10]; char *condVal[10]; char v1[BIG_STR]; char v2[BIG_STR]; int status; char *columnNames[]={"user", "zone"}; printCount=0; memset (&genQueryInp, 0, sizeof (genQueryInp_t)); i1a[0]=COL_USER_NAME; i1b[0]=0; /* (unused) */ i1a[1]=COL_USER_ZONE; i1b[1]=0; /* (unused) */ genQueryInp.selectInp.inx = i1a; genQueryInp.selectInp.value = i1b; genQueryInp.selectInp.len = 2; i2a[0]=COL_META_USER_ATTR_NAME; sprintf(v1,"='%s'",attribute); condVal[0]=v1; i2a[1]=COL_META_USER_ATTR_VALUE; sprintf(v2, "%s '%s'", op, value); condVal[1]=v2; genQueryInp.sqlCondInp.inx = i2a; genQueryInp.sqlCondInp.value = condVal; genQueryInp.sqlCondInp.len=2; genQueryInp.maxRows=10; genQueryInp.continueInx=0; genQueryInp.condInput.len=0; if (zoneArgument[0]!='\0') { addKeyVal (&genQueryInp.condInput, ZONE_KW, zoneArgument); } status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); printGenQueryResults(Conn, status, genQueryOut, columnNames); while (status==0 && genQueryOut->continueInx > 0) { genQueryInp.continueInx=genQueryOut->continueInx; status = rcGenQuery(Conn, &genQueryInp, &genQueryOut); if (genQueryOut->rowCnt>0) printf("----\n"); printGenQueryResults(Conn, status, genQueryOut, columnNames); } return (0); } /* Modify (copy) AVUs */ int icommands::modCopyAVUMetadata(rcComm_t *Conn, rodsEnv myEnv, char *arg0, char *arg1, char *arg2, char *arg3, char *arg4, char *arg5, char *arg6, char *arg7) { modAVUMetadataInp_t modAVUMetadataInp; int status; char *mySubName; char *myName; char fullName1[LONG_NAME_LEN]; char fullName2[LONG_NAME_LEN]; strncpy(fullName1, cwd, LONG_NAME_LEN); if (strcmp(arg1,"-R")==0 || strcmp(arg1,"-r")==0 || strcmp(arg1,"-G")==0 || strcmp(arg1,"-g")==0 || strcmp(arg1,"-u")==0) { strncpy(fullName1, arg3, LONG_NAME_LEN); } else { if (strlen(arg3)>0) { if (*arg3=='/') { strncpy(fullName1, arg3, LONG_NAME_LEN); } else { rstrcat(fullName1, "/", LONG_NAME_LEN); rstrcat(fullName1, arg3, LONG_NAME_LEN); } } } strncpy(fullName2, cwd, LONG_NAME_LEN); if (strcmp(arg2,"-R")==0 || strcmp(arg2,"-r")==0 || strcmp(arg2,"-G")==0 || strcmp(arg2,"-g")==0 || strcmp(arg2,"-u")==0) { strncpy(fullName2, arg4, LONG_NAME_LEN); } else { if (strlen(arg4)>0) { if (*arg4=='/') { strncpy(fullName2, arg4, LONG_NAME_LEN); } else { rstrcat(fullName2, "/", LONG_NAME_LEN); rstrcat(fullName2, arg4, LONG_NAME_LEN); } } } modAVUMetadataInp.arg0 = arg0; modAVUMetadataInp.arg1 = arg1; modAVUMetadataInp.arg2 = arg2; modAVUMetadataInp.arg3 = fullName1; modAVUMetadataInp.arg4 = fullName2; modAVUMetadataInp.arg5 = arg5; modAVUMetadataInp.arg6 = arg6; modAVUMetadataInp.arg7 = arg7; modAVUMetadataInp.arg8 =""; modAVUMetadataInp.arg9 =""; status = rcModAVUMetadata(Conn, &modAVUMetadataInp); lastCommandStatus = status; if (status < 0 ) { if (Conn->rError) { rError_t *Err; rErrMsg_t *ErrMsg; int i, len; Err = Conn->rError; len = Err->len; for (i=0;ierrMsg[i]; rodsLog(LOG_ERROR, "Level %d: %s",i, ErrMsg->msg); } } myName = rodsErrorName(status, &mySubName); rodsLog (LOG_ERROR, "rcModAVUMetadata failed with error %d %s %s", status, myName, mySubName); } if (status == CAT_UNKNOWN_FILE) { char tempName[LONG_NAME_LEN]="/"; int len; int isRemote=0; strncat(tempName, myEnv.rodsZone, MAX_NAME_LEN); len = strlen(tempName); if (strncmp(tempName, fullName1, len) != 0) { printf("Cannot copy metadata from a remote zone.\n"); isRemote=1; } if (strncmp(tempName, fullName2, len) != 0) { printf("Cannot copy metadata to a remote zone.\n"); isRemote=1; } if (isRemote) { printf("Copying of metadata is done via SQL within each ICAT\n"); printf("for efficiency. Copying metadata between zones is\n"); printf("not implemented.\n"); } } return(status); } /* Modify (add or remove) AVUs */ int icommands::modAVUMetadata(rcComm_t *Conn, char *arg0, char *arg1, char *arg2, char *arg3, char *arg4, char *arg5, char *arg6, char *arg7, char *arg8) { modAVUMetadataInp_t modAVUMetadataInp; int status; char *mySubName; char *myName; char fullName[LONG_NAME_LEN]; strncpy(fullName, cwd, LONG_NAME_LEN); if (strcmp(arg1,"-R")==0 || strcmp(arg1,"-r")==0 || strcmp(arg1,"-G")==0 || strcmp(arg1,"-g")==0 || strcmp(arg1,"-u")==0) { strncpy(fullName, arg2, LONG_NAME_LEN); } else { if (strlen(arg2)>0) { if (*arg2=='/') { strncpy(fullName, arg2, LONG_NAME_LEN); } else { rstrcat(fullName, "/", LONG_NAME_LEN); rstrcat(fullName, arg2, LONG_NAME_LEN); } } } modAVUMetadataInp.arg0 = arg0; modAVUMetadataInp.arg1 = arg1; modAVUMetadataInp.arg2 = fullName; modAVUMetadataInp.arg3 = arg3; modAVUMetadataInp.arg4 = arg4; modAVUMetadataInp.arg5 = arg5; modAVUMetadataInp.arg6 = arg6; modAVUMetadataInp.arg7 = arg7; modAVUMetadataInp.arg8 = arg8; modAVUMetadataInp.arg9 =""; status = rcModAVUMetadata(Conn, &modAVUMetadataInp); lastCommandStatus = status; if (status < 0 ) { if (Conn->rError) { rError_t *Err; rErrMsg_t *ErrMsg; int i, len; Err = Conn->rError; len = Err->len; for (i=0;ierrMsg[i]; rodsLog(LOG_ERROR, "Level %d: %s",i, ErrMsg->msg); } } myName = rodsErrorName(status, &mySubName); rodsLog (LOG_ERROR, "rcModAVUMetadata failed with error %d %s %s", status, myName, mySubName); } return(status); } /* Prompt for input and parse into tokens */ void icommands::getInput(rcComm_t *Conn, char *cmdToken[], int maxTokens) { int lenstr, i; static char ttybuf[BIG_STR]; int nTokens; int tokenFlag; /* 1: start reg, 2: start ", 3: start ' */ char *cpTokenStart; char *stat; memset(ttybuf, 0, BIG_STR); fputs("imeta>",stdout); stat = fgets(ttybuf, BIG_STR, stdin); if (stat==0) { printf("\n"); rcDisconnect(Conn); if (lastCommandStatus != 0) exit(4); exit(0); } lenstr=strlen(ttybuf); for (i=0;i 0) { printf("AVU added to %d data-objects\n", myStat); lastCommandStatus = 0; } return(0); } if (strcmp(cmdToken[0],"rmw") == 0) { modAVUMetadata(Conn, "rmw", cmdToken[1], cmdToken[2], cmdToken[3], cmdToken[4], cmdToken[5], cmdToken[6], cmdToken[7], ""); return(0); } if (strcmp(cmdToken[0],"rm") == 0) { modAVUMetadata(Conn, "rm", cmdToken[1], cmdToken[2], cmdToken[3], cmdToken[4], cmdToken[5], cmdToken[6], cmdToken[7], ""); return(0); } if (testMode) { if (strcmp(cmdToken[0],"rmi") == 0) { modAVUMetadata(Conn, "rmi", cmdToken[1], cmdToken[2], cmdToken[3], cmdToken[4], cmdToken[5], cmdToken[6], cmdToken[7], ""); return(0); } } if (strcmp(cmdToken[0],"mod") == 0) { modAVUMetadata(Conn, "mod", cmdToken[1], cmdToken[2], cmdToken[3], cmdToken[4], cmdToken[5], cmdToken[6], cmdToken[7], cmdToken[8]); return(0); } doLs=0; if (strcmp(cmdToken[0],"lsw") == 0) { doLs=1; wild=1; } if (strcmp(cmdToken[0],"ls") == 0) { doLs=1; wild=0; } if (doLs) { if (strcmp(cmdToken[1],"-d")==0) { showDataObj(Conn, cmdToken[2], cmdToken[3], wild); for(int i=0; i<4; i++){ printf("cmdToken[%d] = %s \n", i, cmdToken[i]); } return(0); } if (strcmp(cmdToken[1],"-C")==0 || strcmp(cmdToken[1],"-c")==0) { showColl(Conn, cmdToken[2], cmdToken[3], wild); return(0); } if (strcmp(cmdToken[1],"-R")==0 || strcmp(cmdToken[1],"-r")==0) { showResc(Conn, cmdToken[2], cmdToken[3], wild); return(0); } if (strcmp(cmdToken[1],"-G")==0 || strcmp(cmdToken[1],"-g")==0) { showRescGroup(Conn, cmdToken[2], cmdToken[3], wild); return(0); } if (strcmp(cmdToken[1],"-u")==0) { showUser(Conn, myEnv, cmdToken[2], cmdToken[3], wild); return(0); } } if (strcmp(cmdToken[0],"qu") == 0) { if (strcmp(cmdToken[1],"-d")==0) { queryDataObj(Conn, cmdToken); return(0); } if (strcmp(cmdToken[1],"-C")==0 || strcmp(cmdToken[1],"-c")==0) { queryCollection(Conn, cmdToken[2], cmdToken[3], cmdToken[4]); return(0); } if (strcmp(cmdToken[1],"-R")==0 || strcmp(cmdToken[1],"-r")==0) { queryResc(Conn, cmdToken[2], cmdToken[3], cmdToken[4]); return(0); } if (strcmp(cmdToken[1],"-G")==0 || strcmp(cmdToken[1],"-g")==0) { queryRescGroup(Conn, cmdToken[2], cmdToken[3], cmdToken[4]); return(0); } if (strcmp(cmdToken[1],"-u")==0) { queryUser(Conn, myEnv, cmdToken[2], cmdToken[3], cmdToken[4]); return(0); } } if (strcmp(cmdToken[0],"cp") == 0) { modCopyAVUMetadata(Conn, myEnv, "cp", cmdToken[1], cmdToken[2], cmdToken[3], cmdToken[4], cmdToken[5], cmdToken[6], cmdToken[7]); return(0); } if (strcmp(cmdToken[0],"test") == 0) { if (testMode) { testMode=0; printf("testMode is now off\n"); } else { testMode=1; printf("testMode is now on\n"); } return(0); } if (*cmdToken[0] != '\0') { printf("unrecognized command, try 'help'\n"); return(-2); } return(-3); } }//namespace api }//namespace irods_file_adaptor // ///* //Print the main usage/help information. // */ //void usageMain() //{ // char *msgs[]={ //"Usage: imeta [-vVhz] [command]", //" -v verbose", //" -V Very verbose", //" -z Zonename work with the specified Zone", //" -h This help", //"Commands are:", //" add -d|C|R|G|u Name AttName AttValue [AttUnits] (Add new AVU triplet)", //" addw -d Name AttName AttValue [AttUnits] (Add new AVU triplet", //" using Wildcards in Name)", //" rm -d|C|R|G|u Name AttName AttValue [AttUnits] (Remove AVU)", //" rmw -d|C|R|G|u Name AttName AttValue [AttUnits] (Remove AVU, use Wildcards)", //" mod -d|C|R|G|u Name AttName AttValue [AttUnits] [n:Name] [v:Value] [u:Units]", //" (modify AVU; new name (n:), value(v:), and/or units(u:)", //" ls -d|C|R|G|u Name [AttName] (List existing AVUs for item Name)", //" lsw -d|C|R|G|u Name [AttName] (List existing AVUs, use Wildcards)", //" qu -d|C|R|G|u AttName Op AttVal [...] (Query objects with matching AVUs)", //" cp -d|C|R|G|u -d|C|R|G|u Name1 Name2 (Copy AVUs from item Name1 to Name2)", //" ", //"Metadata attribute-value-units triplets (AVUs) consist of an Attribute-Name,", //"Attribute-Value, and an optional Attribute-Units. They can be added", //"via the 'add' command (and in other ways), and", //"then queried to find matching objects.", //" ", //"For each command, -d, -C, -R, -G or -u is used to specify which type of", //"object to work with: dataobjs (irods files), collections, resources,", //" resource groups or users. (Within imeta -c, -r and -g can be used, but -C,", //" -R and -G are the", //"iRODS standard options for collections, resources and resource groups.)", //" ", //"Fields represented with upper case, such as Name, are entered values. For", //"example, 'Name' is the name of a dataobject, collection, resource,", //"or user.", //" ", //"For rmw and lsw, the % and _ wildcard characters (as defined for SQL) can", //"be used for matching attribute values.", //" ", //"For addw, the % and _ wildcard characters (as defined for SQL) can", //"be used for matching object names. This is currently implemented only", //"for data-objects (-d).", //" ", //"A blank execute line invokes the interactive mode, where imeta", //"prompts and executes commands until 'quit' or 'q' is entered.", //"Like other unix utilities, a series of commands can be piped into it:", //"'cat file1 | imeta' (maintaining one connection for all commands).", //" ", //"Single or double quotes can be used to enter items with blanks.", //" ", //"Entered usernames are of the form username[#zone]. If #zone is not", //"provided, the zone from your .irodsEnv is assumed.", //" ", //"The appropriate zone (local or remote) is determined from the path names", //"or via -z Zonename (for 'qu' and when working with resources).", //" ", //"Try 'help command' for more help on a specific command.", //"'help qu' will explain additional options on the query.", //""}; // int i; // for (i=0;;i++) { // if (strlen(msgs[i])==0) break; // printf("%s\n",msgs[i]); // } // printReleaseInfo("imeta"); //} // ///* //Print either main usage/help information, or some more specific //information on particular commands. // */ //int //usage(char *subOpt) //{ // int i; // if (*subOpt=='\0') { // usageMain(); // } // else { // if (strcmp(subOpt,"add")==0) { // char *msgs[]={ //" add -d|C|R|G|u Name AttName AttValue [AttUnits] (Add new AVU triplet)", //"Add an AVU to a dataobj (-d), collection(-C), resource(-R), ", //"resource group(-G) or user(-u)", //"Example: add -d file1 distance 12 miles", //" ", //"Admins can also use the command 'adda' (add as admin) to add metadata", //"to any collection or dataobj; syntax is the same as 'add'. Admins are", //"also allowed to add user and resource metadata.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"addw")==0) { // char *msgs[]={ //" addw -d Name AttName AttValue [AttUnits] (Add new AVU triplet)", //"Add an AVU to a set of data-objects using wildcards to match", //"the data-object names.", //" ", //"The character _ matches any single character and % matches any", //"number of any characters.", //" ", //"Example: addw -d file% distance 12 miles", //"would add the AVU to dataobjects in the current directory with names", //"that start with 'file'.", //" ", //"Example2: addw -d /tempZone/home/rods/test/%/% distance 12 miles", //"would add the AVU to all dataobjects in the 'test' collection or any", //"subcollections under 'test'.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"rm")==0) { // char *msgs[]={ //" rm -d|C|R|G|u Name AttName AttValue [AttUnits] (Remove AVU)", //"Remove an AVU from a dataobj (-d), collection(-C), resource(-R),", //"resource group(-G) or user(-u)", //"Example: rm -d file1 distance 12 miles", //"An AttUnits value must be included if it was when the AVU was added.", //"Also see rmw for use of wildcard characters.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"rmw")==0) { // char *msgs[]={ //" rmw -d|C|R|G|u Name AttName AttValue [AttUnits] (Remove AVU, use Wildcard)", //"Remove an AVU from a dataobj (-d), collection(-C), resource(-R), ", //"resource group(-G) or user(-u)", //"An AttUnits value must be included if it was when the AVU was added.", //"rmw is very similar to rm but using SQL wildcard characters, _ and %.", //"The _ matches any single character and % matches any number of any", //"characters. Examples:", //" rmw -d file1 distance % %", //" or ", //" rmw -d file1 distance % m% ", //" ", //"Note that if the attributes contain the characters '%' or '_', ", //"the rmw command still do matching using them as wildcards, so you may", //"need to use rm instead.", //"Also see lsw.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"mod")==0) { // char *msgs[]={ //" mod -d|C|R|G|u Name AttName AttValue [AttUnits] [n:Name] [v:Value] [u:Units]", //" (modify AVU; new name (n:), value(v:), and/or units(u:)", //"Modify a defined AVU for the specified item (object)", //"Example: mod -d file1 distance 14 miles v:27", //" ", //"Only the AVU associated with this object is modified. Internally, the system", //"removes the old AVU from the object and creates a new one (ensuring", //"consistency), and performs a single 'commit' if all is valid.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"ls")==0) { // char *msgs[]={ //" ls -d|C|R|G|u Name [AttName] (List existing AVUs for item Name)", //"List defined AVUs for the specified item", //"Example: ls -d file1", //"If the optional AttName is included, it is the attribute name", //"you wish to list and only those will be listed.", //"Also see lsw.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // // if (strcmp(subOpt,"lsw")==0) { // char *msgs[]={ //" lsw -d|C|R|G|u Name [AttName] (List existing AVUs, use Wildcards)", //"List defined AVUs for the specified item", //"Example: lsw -d file1", //"If the optional AttName is included, it is the attribute name", //"you wish to list, doing so using wildcard matching.", //"For example: ls -d file1 attr%", //"Also see rmw and ls.", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"qu")==0) { // char *msgs[]={ //" qu -d|C|R|G|u AttName Op AttVal [...] (Query objects with matching AVUs)", //"Query across AVUs for the specified type of item", //"Example: qu -d distance '<=' 12", //" ", //"When querying dataObjects (-d) additional conditions (AttName Op AttVal)", //"may be given separated by 'and', for example:", //" qu -d a = b and c '<' 10", //"Or a single 'or' can be given for the same AttName, for example", //" qu -d r '<' 5 or '>' 7", //" ", //"You can also query in numeric mode (instead of as strings) by adding 'n'", //"in front of the test condition, for example:", //" qu -d r 'n<' 123", //"which causes it to cast the AVU column to numeric (decimal) in the SQL.", //"In numeric mode, if any of the named AVU values are non-numeric, a SQL", //"error will occur but this avoids problems when comparing numeric strings", //"of different lengths.", //" ", //"Other examples:", //" qu -d a like b%", //"returns data-objects with attribute 'a' with a value that starts with 'b'.", //" qu -d a like %", //"returns data-objects with attribute 'a' defined (with any value).", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // if (strcmp(subOpt,"cp")==0) { // char *msgs[]={ //" cp -d|C|R|G|u -d|C|R|G|u Name1 Name2 (Copy AVUs from item Name1 to Name2)", //"Example: cp -d -C file1 dir1", //""}; // for (i=0;;i++) { // if (strlen(msgs[i])==0) return(0); // printf("%s\n",msgs[i]); // } // } // printf("Sorry, either %s is an invalid command or the help has not been written yet\n", // subOpt); // } // return(0); //}