00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <stdlib.h>
00014 #include <dirent.h>
00015 #include "rodsDef.h"
00016 #include "sysBackupMS.h"
00017 #include "resource.h"
00018 #include "fileOpr.h"
00019 #include "physPath.h"
00020 #include "objMetaOpr.h"
00021 #include "apiHeaderAll.h"
00022
00023
00024
00025 #include "eirods_resource_backport.h"
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 int loadDirToLocalResc(ruleExecInfo_t *rei, char *dirPath, size_t offset,
00037 char *resDirPath, char *timestamp, char *dbPath)
00038 {
00039 DIR *myDir;
00040 struct dirent *de;
00041 struct stat s;
00042 char absPath[MAX_NAME_LEN], *subPath;
00043 char sysCopyCmd[2*MAX_NAME_LEN];
00044 int filecount = 0, status;
00045 char *dirname;
00046
00047
00048
00049
00050 if ((dirname = strrchr(dirPath, '/') + 1) == NULL)
00051 {
00052 rei->status = SYS_INVALID_FILE_PATH;
00053 return 0;
00054 }
00055
00056
00057
00058
00059
00060 if (!strcmp(dirname, ".")
00061 || !strcmp(dirname, "..")
00062
00063 || !strcmp(dirPath, resDirPath) )
00064 {
00065 return 0;
00066 }
00067
00068
00069
00070 if (dbPath && !strcmp(dirPath,dbPath))
00071 {
00072 return 0;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 subPath = rei->rsComm->myEnv.rodsHome + strlen(rei->rsComm->myEnv.rodsZone) + 2;
00086
00087
00088
00089 snprintf(sysCopyCmd, 2*MAX_NAME_LEN, "mkdir -p \"%s/%s/%s/%s_%s/%s\"",
00090 resDirPath, subPath, BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, timestamp,
00091 dirPath + offset);
00092
00093
00094
00095 status = system(sysCopyCmd);
00096 if (status < 0)
00097 {
00098 rodsLog (LOG_ERROR, "loadDirToLocalResc: mkdir error %d.", status);
00099 rei->status = UNIX_FILE_MKDIR_ERR;
00100 }
00101
00102
00103
00104 myDir = opendir(dirPath);
00105
00106 while ((de = readdir(myDir)) != NULL)
00107 {
00108
00109 if (!strcmp(de->d_name, ".DS_Store"))
00110 {
00111 continue;
00112 }
00113
00114
00115 snprintf(absPath, MAX_NAME_LEN, "%s/%s", dirPath, de->d_name);
00116 if (lstat(absPath, &s) != 0)
00117 {
00118 rodsLog(LOG_ERROR, "putDir error: cannot lstat %s, %s", absPath, strerror(errno));
00119 rei->status = UNIX_FILE_STAT_ERR;
00120 continue;
00121 }
00122
00123
00124 if (S_ISDIR(s.st_mode))
00125 {
00126
00127
00128 filecount += loadDirToLocalResc(rei, absPath, offset,
00129 resDirPath, timestamp, dbPath);
00130
00131 }
00132 else
00133 {
00134
00135 memset(sysCopyCmd, 0, 2*MAX_NAME_LEN);
00136
00137 snprintf(sysCopyCmd, 2*MAX_NAME_LEN, "cp \"%s\" \"%s/%s/%s/%s_%s/%s/%s\"",
00138 absPath, resDirPath, subPath, BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost,
00139 timestamp, dirPath + offset, de->d_name);
00140
00141 status = system(sysCopyCmd);
00142 if (status < 0)
00143 {
00144 rodsLog (LOG_ERROR, "loadDirToLocalResc: cp error, status = %d.", status);
00145 rei->status = status + FILE_OPEN_ERR;
00146 }
00147
00148 filecount++;
00149 }
00150
00151
00152
00153 }
00154
00155
00156
00157 closedir(myDir);
00158
00159 return filecount;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 char *getDBHomeDir()
00172 {
00173 char configFilePath[MAX_PATH_ALLOWED + 1];
00174 char buf[LONG_NAME_LEN * 5];
00175 char *dbPath = NULL;
00176 FILE *configFile;
00177
00178 #ifndef RODS_CAT
00179 return NULL;
00180 #endif
00181
00182
00183 snprintf (configFilePath, MAX_PATH_ALLOWED, "%s/config/%s", getenv("irodsHomeDir"), "irods.config");
00184 configFile = fopen(configFilePath, "r");
00185 if (configFile == NULL)
00186 {
00187 rodsLog (LOG_ERROR, "getDefaultLocalRescInfo: Cannot open configuration file %s",
00188 configFilePath);
00189 return NULL;
00190 }
00191
00192
00193 while (fgets (buf, LONG_NAME_LEN * 5, configFile) != NULL)
00194 {
00195
00196 if (strstr(buf,"$DATABASE_HOME") == buf)
00197 {
00198
00199 dbPath = strchr(buf,'\'') + 1;
00200
00201
00202 strchr(dbPath,'\'')[0] = '\0';
00203
00204 break;
00205 }
00206 }
00207
00208 fclose(configFile);
00209
00210 if (dbPath != NULL)
00211 {
00212 return (strdup(dbPath));
00213 }
00214 else
00215 {
00216 return NULL;
00217 }
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 int getDefaultLocalRescInfo(rescInfo_t **rescInfo)
00230 {
00231 char configFilePath[MAX_PATH_ALLOWED + 1];
00232 char buf[LONG_NAME_LEN * 5];
00233 char *rescName = NULL;
00234 FILE *configFile;
00235
00236
00237 snprintf (configFilePath, MAX_PATH_ALLOWED, "%s/config/%s", getenv("irodsHomeDir"), "irods.config");
00238 configFile = fopen(configFilePath, "r");
00239 if (configFile == NULL)
00240 {
00241 rodsLog (LOG_ERROR, "getDefaultLocalRescInfo: Cannot open configuration file %s",
00242 configFilePath);
00243 return FILE_OPEN_ERR;
00244 }
00245
00246
00247 while (fgets (buf, LONG_NAME_LEN * 5, configFile) != NULL)
00248 {
00249
00250 if (strstr(buf,"$RESOURCE_NAME") == buf)
00251 {
00252
00253 rescName = strchr(buf,'\'') + 1;
00254
00255
00256 strchr(rescName,'\'')[0] = '\0';
00257
00258 break;
00259 }
00260 }
00261
00262 fclose(configFile);
00263
00264 if (rescName == NULL)
00265 {
00266 rodsLog (LOG_ERROR,
00267 "getDefaultLocalRescInfo: Local resource not found in configuration file.");
00268 return SYS_CONFIG_FILE_ERR;
00269 }
00270
00271
00272
00273 if( !(*rescInfo ) ) {
00274 *rescInfo = new rescInfo_t;
00275 }
00276
00277 eirods::resource_ptr resc;
00278 eirods::error err = eirods::get_resc_info( rescName, **rescInfo );
00279 if( !err.ok() ) {
00280 std::stringstream msg;
00281 msg << "failed to resolve resource [";
00282 msg << rescName << "]";
00283 eirods::log( PASSMSG( msg.str(), err ) );
00284 return err.code();
00285 }
00286
00287 return 0;
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 int
00332 msiServerBackup(msParam_t *options, msParam_t *keyValOut, ruleExecInfo_t *rei)
00333 {
00334 keyValPair_t *myKeyVal;
00335 collInp_t collInp;
00336
00337 dataObjInp_t dataObjInp;
00338
00339 char tStr0[TIME_LEN], tStr[TIME_LEN];
00340
00341 char newDirPath[MAX_NAME_LEN];
00342
00343 rescInfo_t *rescInfo;
00344 char *dbPath;
00345
00346 char *rodsDirPath, *subPath;
00347 size_t offset;
00348
00349 int fileCount, status;
00350 char fileCountStr[21];
00351
00352
00353
00354
00355 RE_TEST_MACRO (" Calling msiServerBackup")
00356
00357
00358 if (rei == NULL || rei->rsComm == NULL)
00359 {
00360 rodsLog (LOG_ERROR, "msiServerBackup: input rei or rsComm is NULL.");
00361 return (SYS_INTERNAL_NULL_INPUT_ERR);
00362 }
00363
00364
00365
00366 if (rei->uoic->authInfo.authFlag < LOCAL_PRIV_USER_AUTH)
00367 {
00368 status = CAT_INSUFFICIENT_PRIVILEGE_LEVEL;
00369 rodsLog (LOG_ERROR, "msiServerBackup: User %s is not local admin. Status = %d",
00370 rei->uoic->userName, status);
00371 return(status);
00372 }
00373
00374
00375
00376 dbPath = getDBHomeDir();
00377
00378
00379 status = getDefaultLocalRescInfo(&rescInfo);
00380 if (status < 0)
00381 {
00382 rodsLog (LOG_ERROR, "msiServerBackup: Could not resolve local resource, status = %d",
00383 status);
00384 if (dbPath) {
00385 free(dbPath);
00386 }
00387 return (status);
00388 }
00389
00390
00391
00392 if ((rodsDirPath = getenv("irodsHomeDir")) == NULL)
00393 {
00394 rodsLog (LOG_ERROR, "msiServerBackup: Cannot find directory to back up.");
00395 if (dbPath) {
00396 free(dbPath);
00397 }
00398 return (USER_INPUT_PATH_ERR);
00399 }
00400
00401
00402
00403
00404
00405
00406 getNowStr (tStr0);
00407 getLocalTimeFromRodsTime (tStr0,tStr);
00408
00409
00410
00411
00412
00413
00414 myKeyVal = (keyValPair_t*) malloc (sizeof(keyValPair_t));
00415 memset (myKeyVal, 0, sizeof(keyValPair_t));
00416 keyValOut->type = strdup(KeyValPair_MS_T);
00417
00418
00419
00420
00421
00422
00423 offset = strrchr(rodsDirPath,'/') - rodsDirPath + 1;
00424
00425
00426
00427
00428
00429
00430
00431 fileCount = loadDirToLocalResc(rei, rodsDirPath, offset, rescInfo->rescVaultPath, tStr, dbPath);
00432
00433
00434 if (dbPath) {
00435 free(dbPath);
00436 }
00437
00438 if (rei->status < 0)
00439 {
00440 rodsLog (LOG_ERROR, "msiServerBackup: loadDirToLocalResc() error, status = %d",
00441 rei->status);
00442 free(myKeyVal);
00443 return rei->status;
00444 }
00445
00446
00447
00448
00449
00450
00451 memset (&collInp, 0, sizeof(collInp_t));
00452 addKeyVal (&collInp.condInput, RECURSIVE_OPR__KW, "");
00453
00454
00455 snprintf(collInp.collName, MAX_NAME_LEN, "%s/%s/%s_%s", rei->rsComm->myEnv.rodsHome,
00456 BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr);
00457
00458
00459 rei->status = rsCollCreate (rei->rsComm, &collInp);
00460 if (rei->status < 0)
00461 {
00462 rodsLog (LOG_ERROR, "msiServerBackup: rsCollCreate failed for %s, status = %d",
00463 collInp.collName, rei->status);
00464 free(myKeyVal);
00465 return (rei->status);
00466 }
00467
00468
00469
00470
00471
00472 memset(&dataObjInp, 0, sizeof(dataObjInp_t));
00473 addKeyVal (&dataObjInp.condInput, COLLECTION_KW, "");
00474 addKeyVal (&dataObjInp.condInput, DEST_RESC_NAME_KW, rescInfo->rescName);
00475
00476
00477 subPath = rei->rsComm->myEnv.rodsHome + strlen(rei->rsComm->myEnv.rodsZone) + 2;
00478
00479
00480 snprintf(newDirPath, MAX_NAME_LEN, "%s/%s/%s/%s_%s/%s", rescInfo->rescVaultPath,
00481 subPath, BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr, rodsDirPath + offset);
00482
00483 addKeyVal (&dataObjInp.condInput, FILE_PATH_KW, newDirPath);
00484
00485
00486 snprintf(dataObjInp.objPath, MAX_NAME_LEN, "%s/%s/%s_%s/%s", rei->rsComm->myEnv.rodsHome,
00487 BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr, rodsDirPath + offset);
00488
00489
00490
00491 rei->status = rsPhyPathReg (rei->rsComm, &dataObjInp);
00492 if (rei->status < 0)
00493 {
00494 rodsLog (LOG_ERROR, "msiServerBackup: rsPhyPathReg() failed with status %d", rei->status);
00495 free(myKeyVal);
00496 return rei->status;
00497 }
00498
00499
00500
00501 snprintf(fileCountStr, 21, "%d", fileCount);
00502 addKeyVal(myKeyVal, "object_count", fileCountStr);
00503
00504
00505 keyValOut->inOutStruct = (void*) myKeyVal;
00506
00507
00508
00509 return 0;
00510 }