00001
00002
00003
00004
00005
00006 #include "structFileBundle.h"
00007 #include "apiHeaderAll.h"
00008 #include "objMetaOpr.h"
00009 #include "dataObjOpr.h"
00010 #include "physPath.h"
00011 #include "miscServerFunct.h"
00012 #include "rcGlobalExtern.h"
00013 #include "reGlobalsExtern.h"
00014
00015 int
00016 rsStructFileBundle (rsComm_t *rsComm,
00017 structFileExtAndRegInp_t *structFileBundleInp)
00018 {
00019 #if 0
00020 char *destRescName = NULL;
00021 #endif
00022 int status;
00023 rodsServerHost_t *rodsServerHost;
00024 int remoteFlag;
00025 rodsHostAddr_t rescAddr;
00026 dataObjInp_t dataObjInp;
00027 rescGrpInfo_t *rescGrpInfo = NULL;
00028
00029 memset (&dataObjInp, 0, sizeof (dataObjInp));
00030 rstrcpy (dataObjInp.objPath, structFileBundleInp->objPath,
00031 MAX_NAME_LEN);
00032
00033 remoteFlag = getAndConnRemoteZone (rsComm, &dataObjInp, &rodsServerHost,
00034 REMOTE_CREATE);
00035
00036 if (remoteFlag < 0) {
00037 return (remoteFlag);
00038 } else if (remoteFlag == REMOTE_HOST) {
00039 status = rcStructFileBundle (rodsServerHost->conn,
00040 structFileBundleInp);
00041 return status;
00042 }
00043
00044 #if 0
00045 if ((destRescName =
00046 getValByKey (&structFileBundleInp->condInput, DEST_RESC_NAME_KW)) == NULL
00047 && (destRescName =
00048 getValByKey (&structFileBundleInp->condInput, DEF_RESC_NAME_KW)) == NULL) {
00049 return USER_NO_RESC_INPUT_ERR;
00050 }
00051
00052 status = _getRescInfo (rsComm, destRescName, &rescGrpInfo);
00053 if (status < 0) {
00054 rodsLog (LOG_ERROR,
00055 "rsStructFileBundle: _getRescInfo of %s error for %s. stat = %d",
00056 destRescName, structFileBundleInp->collection, status);
00057 return status;
00058 }
00059 #else
00060
00061 dataObjInp.condInput = structFileBundleInp->condInput;
00062 status = getRescGrpForCreate (rsComm, &dataObjInp, &rescGrpInfo);
00063 if (status < 0 || NULL == rescGrpInfo ) return status;
00064 #endif
00065
00066 bzero (&rescAddr, sizeof (rescAddr));
00067 rstrcpy (rescAddr.hostAddr, rescGrpInfo->rescInfo->rescLoc, NAME_LEN);
00068 remoteFlag = resolveHost (&rescAddr, &rodsServerHost);
00069
00070 if (remoteFlag == LOCAL_HOST) {
00071 status = _rsStructFileBundle (rsComm, structFileBundleInp);
00072 } else if (remoteFlag == REMOTE_HOST) {
00073 status = remoteStructFileBundle (rsComm, structFileBundleInp,
00074 rodsServerHost);
00075 } else if (remoteFlag < 0) {
00076 status = remoteFlag;
00077 }
00078 freeAllRescGrpInfo (rescGrpInfo);
00079 return status;
00080 }
00081
00082 int
00083 _rsStructFileBundle (rsComm_t *rsComm,
00084 structFileExtAndRegInp_t *structFileBundleInp)
00085 {
00086 int status;
00087 dataObjInp_t dataObjInp;
00088 openedDataObjInp_t dataObjCloseInp;
00089 collInp_t collInp;
00090 collEnt_t *collEnt = NULL;
00091 int handleInx;
00092 int collLen;
00093 char phyBunDir[MAX_NAME_LEN];
00094 char tmpPath[MAX_NAME_LEN];
00095 chkObjPermAndStat_t chkObjPermAndStatInp;
00096 int l1descInx;
00097 int savedStatus = 0;
00098 char* dataType = 0;
00099
00100
00101 memset (&dataObjInp, 0, sizeof (dataObjInp));
00102
00103
00104 dataType = getValByKey (&structFileBundleInp->condInput, DATA_TYPE_KW);
00105 if (dataType != NULL && strstr (dataType, ZIP_DT_STR) != NULL) {
00106
00107 int len = strlen (structFileBundleInp->objPath);
00108 if (strcmp (&structFileBundleInp->objPath[len - 4], ".zip") != 0) {
00109 strcat (structFileBundleInp->objPath, ".zip");
00110 }
00111 }
00112
00113 rstrcpy (dataObjInp.objPath, structFileBundleInp->objPath,
00114 MAX_NAME_LEN);
00115
00116
00117 replKeyVal (&structFileBundleInp->condInput, &dataObjInp.condInput);
00118
00119 dataObjInp.openFlags = O_WRONLY;
00120
00121 if ((structFileBundleInp->oprType & ADD_TO_TAR_OPR) != 0) {
00122 l1descInx = rsDataObjOpen (rsComm, &dataObjInp);
00123 } else {
00124 l1descInx = rsDataObjCreate (rsComm, &dataObjInp);
00125 }
00126 if (l1descInx < 0) {
00127 rodsLog (LOG_ERROR,
00128 "rsStructFileBundle: rsDataObjCreate of %s error. status = %d",
00129 dataObjInp.objPath, l1descInx);
00130 return (l1descInx);
00131 }
00132 clearKeyVal (&dataObjInp.condInput);
00133 l3Close (rsComm, l1descInx);
00134 L1desc[l1descInx].l3descInx = 0;
00135
00136 if ((structFileBundleInp->oprType & ADD_TO_TAR_OPR) == 0)
00137 l3Unlink (rsComm, L1desc[l1descInx].dataObjInfo);
00138
00139 memset (&chkObjPermAndStatInp, 0, sizeof (chkObjPermAndStatInp));
00140 rstrcpy (chkObjPermAndStatInp.objPath,
00141 structFileBundleInp->collection, MAX_NAME_LEN);
00142 chkObjPermAndStatInp.flags = CHK_COLL_FOR_BUNDLE_OPR;
00143 addKeyVal (&chkObjPermAndStatInp.condInput, RESC_NAME_KW,
00144 L1desc[l1descInx].dataObjInfo->rescName);
00145
00146 status = rsChkObjPermAndStat (rsComm, &chkObjPermAndStatInp);
00147
00148 clearKeyVal (&chkObjPermAndStatInp.condInput);
00149
00150 if (status < 0) {
00151 rodsLog (LOG_ERROR,
00152 "rsStructFileBundle: rsChkObjPermAndStat of %s error. stat = %d",
00153 chkObjPermAndStatInp.objPath, status);
00154 dataObjCloseInp.l1descInx = l1descInx;
00155 rsDataObjClose (rsComm, &dataObjCloseInp);
00156 return (status);
00157 }
00158
00159 createPhyBundleDir (rsComm, L1desc[l1descInx].dataObjInfo->filePath,
00160 phyBunDir);
00161
00162 bzero (&collInp, sizeof (collInp));
00163 rstrcpy (collInp.collName, structFileBundleInp->collection, MAX_NAME_LEN);
00164 collInp.flags = RECUR_QUERY_FG | VERY_LONG_METADATA_FG |
00165 NO_TRIM_REPL_FG | INCLUDE_CONDINPUT_IN_QUERY;
00166 addKeyVal (&collInp.condInput, RESC_NAME_KW,
00167 L1desc[l1descInx].dataObjInfo->rescName);
00168 handleInx = rsOpenCollection (rsComm, &collInp);
00169 if (handleInx < 0) {
00170 rodsLog (LOG_ERROR,
00171 "rsStructFileBundle: rsOpenCollection of %s error. status = %d",
00172 collInp.collName, handleInx);
00173 rmdir (phyBunDir);
00174 return (handleInx);
00175 }
00176
00177 if ((structFileBundleInp->oprType & PRESERVE_COLL_PATH) != 0) {
00178
00179 char *tmpPtr = collInp.collName;
00180 int tmpLen = 0;
00181 collLen = 0;
00182
00183 while (*tmpPtr != '\0') {
00184 if (*tmpPtr == '/') collLen = tmpLen;
00185 tmpLen++;
00186 tmpPtr++;
00187 }
00188 } else {
00189 collLen = strlen (collInp.collName);
00190 }
00191
00192
00193 while ((status = rsReadCollection (rsComm, &handleInx, &collEnt)) >= 0) {
00194 if( NULL == collEnt ) {
00195 rodsLog( LOG_ERROR, "rsStructFileBundle: collEnt is NULL" );
00196 continue;
00197 }
00198 if (collEnt->objType == DATA_OBJ_T) {
00199 if (collEnt->collName[collLen] == '\0') {
00200 snprintf (tmpPath, MAX_NAME_LEN, "%s/%s",
00201 phyBunDir, collEnt->dataName);
00202 } else {
00203 snprintf (tmpPath, MAX_NAME_LEN, "%s/%s/%s",
00204 phyBunDir, collEnt->collName + collLen + 1, collEnt->dataName);
00205 mkDirForFilePath (UNIX_FILE_TYPE, rsComm, phyBunDir,
00206 tmpPath, getDefDirMode ());
00207 }
00208
00209 status = link (collEnt->phyPath, tmpPath);
00210 if (status < 0) {
00211 rodsLog (LOG_ERROR,
00212 "rsStructFileBundle: link error %s to %s. errno = %d",
00213 collEnt->phyPath, tmpPath, errno);
00214 rmLinkedFilesInUnixDir (phyBunDir);
00215 rmdir (phyBunDir);
00216 return (UNIX_FILE_LINK_ERR - errno);
00217 }
00218 } else {
00219 if ((int) strlen (collEnt->collName) + 1 <= collLen) {
00220 free (collEnt);
00221 continue;
00222 }
00223 snprintf (tmpPath, MAX_NAME_LEN, "%s/%s",
00224 phyBunDir, collEnt->collName + collLen);
00225 mkdirR (phyBunDir, tmpPath, getDefDirMode ());
00226 }
00227 if (collEnt != NULL) {
00228 free (collEnt);
00229 collEnt = NULL;
00230 }
00231 }
00232 clearKeyVal (&collInp.condInput);
00233 rsCloseCollection (rsComm, &handleInx);
00234
00235 status = phyBundle( rsComm, L1desc[l1descInx].dataObjInfo, phyBunDir,
00236 collInp.collName, structFileBundleInp->oprType );
00237
00238 if (status < 0) {
00239 rodsLog (LOG_ERROR,
00240 "rsStructFileBundle: phyBundle of %s error. stat = %d",
00241 L1desc[l1descInx].dataObjInfo->objPath, status);
00242 L1desc[l1descInx].bytesWritten = 0;
00243 savedStatus = status;
00244 } else {
00245
00246 L1desc[l1descInx].bytesWritten = 1;
00247 }
00248
00249 rmLinkedFilesInUnixDir (phyBunDir);
00250 rmdir (phyBunDir);
00251
00252 dataObjCloseInp.l1descInx = l1descInx;
00253 status = rsDataObjClose (rsComm, &dataObjCloseInp);
00254
00255 if (status >= 0)
00256 return savedStatus;
00257 else
00258 return (status);
00259 }
00260
00261 int
00262 remoteStructFileBundle (rsComm_t *rsComm,
00263 structFileExtAndRegInp_t *structFileBundleInp, rodsServerHost_t *rodsServerHost)
00264 {
00265 int status;
00266
00267 if (rodsServerHost == NULL) {
00268 rodsLog (LOG_NOTICE,
00269 "remoteStructFileBundle: Invalid rodsServerHost");
00270 return SYS_INVALID_SERVER_HOST;
00271 }
00272
00273 if ((status = svrToSvrConnect (rsComm, rodsServerHost)) < 0) {
00274 return status;
00275 }
00276
00277 status = rcStructFileBundle (rodsServerHost->conn, structFileBundleInp);
00278 return status;
00279 }
00280