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
00179
00180 snprintf (configFilePath, MAX_PATH_ALLOWED, "%s/config/%s", getenv("irodsHomeDir"), "irods.config");
00181 configFile = fopen(configFilePath, "r");
00182 if (configFile == NULL)
00183 {
00184 rodsLog (LOG_ERROR, "getDefaultLocalRescInfo: Cannot open configuration file %s",
00185 configFilePath);
00186 return NULL;
00187 }
00188
00189
00190 while (fgets (buf, LONG_NAME_LEN * 5, configFile) != NULL)
00191 {
00192
00193 if (strstr(buf,"$DATABASE_HOME") == buf)
00194 {
00195
00196 dbPath = strchr(buf,'\'') + 1;
00197
00198
00199 strchr(dbPath,'\'')[0] = '\0';
00200
00201 break;
00202 }
00203 }
00204
00205 fclose(configFile);
00206
00207 if (dbPath != NULL)
00208 {
00209 return (strdup(dbPath));
00210 }
00211 else
00212 {
00213 return NULL;
00214 }
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 int getDefaultLocalRescInfo(rescInfo_t **rescInfo)
00227 {
00228 char configFilePath[MAX_PATH_ALLOWED + 1];
00229 char buf[LONG_NAME_LEN * 5];
00230 char *rescName = NULL;
00231 FILE *configFile;
00232 int status = 0;
00233
00234
00235
00236 snprintf (configFilePath, MAX_PATH_ALLOWED, "%s/config/%s", getenv("irodsHomeDir"), "irods.config");
00237 configFile = fopen(configFilePath, "r");
00238 if (configFile == NULL)
00239 {
00240 rodsLog (LOG_ERROR, "getDefaultLocalRescInfo: Cannot open configuration file %s",
00241 configFilePath);
00242 return FILE_OPEN_ERR;
00243 }
00244
00245
00246 while (fgets (buf, LONG_NAME_LEN * 5, configFile) != NULL)
00247 {
00248
00249 if (strstr(buf,"$RESOURCE_NAME") == buf)
00250 {
00251
00252 rescName = strchr(buf,'\'') + 1;
00253
00254
00255 strchr(rescName,'\'')[0] = '\0';
00256
00257 break;
00258 }
00259 }
00260
00261 fclose(configFile);
00262
00263
00264 if (rescName != NULL)
00265 {
00266
00267 if( !(*rescInfo ) ) {
00268 *rescInfo = new rescInfo_t;
00269 }
00270 eirods::resource_ptr resc;
00271 eirods::error err = eirods::get_resc_info( rescName, **rescInfo );
00272 if( !err.ok() ) {
00273 std::stringstream msg;
00274 msg << "getDefaultLocalRescInfo - failed to resolve resource ";
00275 msg << rescName;
00276 eirods::log( PASS( false, -1, msg.str(), err ) );
00277 }
00278 }
00279 else
00280 {
00281 rodsLog (LOG_ERROR,
00282 "getDefaultLocalRescInfo: Local resource not found in configuration file.");
00283 status = SYS_CONFIG_FILE_ERR;
00284 }
00285
00286 return status;
00287 }
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 int
00331 msiServerBackup(msParam_t *options, msParam_t *keyValOut, ruleExecInfo_t *rei)
00332 {
00333 keyValPair_t *myKeyVal;
00334 collInp_t collInp;
00335
00336 dataObjInp_t dataObjInp;
00337
00338 char tStr0[TIME_LEN], tStr[TIME_LEN];
00339
00340 char newDirPath[MAX_NAME_LEN];
00341
00342 rescInfo_t *rescInfo;
00343 char *dbPath;
00344
00345 char *rodsDirPath, *subPath;
00346 size_t offset;
00347
00348 int fileCount, status;
00349 char fileCountStr[21];
00350
00351
00352
00353
00354 RE_TEST_MACRO (" Calling msiServerBackup")
00355
00356
00357 if (rei == NULL || rei->rsComm == NULL)
00358 {
00359 rodsLog (LOG_ERROR, "msiServerBackup: input rei or rsComm is NULL.");
00360 return (SYS_INTERNAL_NULL_INPUT_ERR);
00361 }
00362
00363
00364
00365 if (rei->uoic->authInfo.authFlag < LOCAL_PRIV_USER_AUTH)
00366 {
00367 status = CAT_INSUFFICIENT_PRIVILEGE_LEVEL;
00368 rodsLog (LOG_ERROR, "msiServerBackup: User %s is not local admin. Status = %d",
00369 rei->uoic->userName, status);
00370 return(status);
00371 }
00372
00373
00374
00375 #ifdef RODS_CAT
00376 dbPath = getDBHomeDir();
00377 #else
00378 dbPath = NULL;
00379 #endif
00380
00381
00382 status = getDefaultLocalRescInfo(&rescInfo);
00383 if (status < 0)
00384 {
00385 rodsLog (LOG_ERROR, "msiServerBackup: Could not resolve local resource, status = %d",
00386 status);
00387 free( dbPath );
00388 return (status);
00389 }
00390
00391
00392
00393 if ((rodsDirPath = getenv("irodsHomeDir")) == NULL)
00394 {
00395 rodsLog (LOG_ERROR, "msiServerBackup: Cannot find directory to back up.");
00396 free( dbPath );
00397 return (USER_INPUT_PATH_ERR);
00398 }
00399
00400
00401
00402
00403
00404
00405 getNowStr (tStr0);
00406 getLocalTimeFromRodsTime (tStr0,tStr);
00407
00408
00409
00410
00411
00412
00413 myKeyVal = (keyValPair_t*) malloc (sizeof(keyValPair_t));
00414 memset (myKeyVal, 0, sizeof(keyValPair_t));
00415 keyValOut->type = strdup(KeyValPair_MS_T);
00416
00417
00418
00419
00420
00421
00422 offset = strrchr(rodsDirPath,'/') - rodsDirPath + 1;
00423
00424
00425
00426
00427
00428
00429
00430 fileCount = loadDirToLocalResc(rei, rodsDirPath, offset, rescInfo->rescVaultPath, tStr, dbPath);
00431
00432 if (rei->status < 0)
00433 {
00434 rodsLog (LOG_ERROR, "msiServerBackup: loadDirToLocalResc() error, status = %d",
00435 rei->status);
00436 free( myKeyVal );
00437 free( dbPath );
00438 return rei->status;
00439 }
00440
00441
00442
00443
00444
00445
00446 memset (&collInp, 0, sizeof(collInp_t));
00447 addKeyVal (&collInp.condInput, RECURSIVE_OPR__KW, "");
00448
00449
00450 snprintf(collInp.collName, MAX_NAME_LEN, "%s/%s/%s_%s", rei->rsComm->myEnv.rodsHome,
00451 BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr);
00452
00453
00454 rei->status = rsCollCreate (rei->rsComm, &collInp);
00455 if (rei->status < 0)
00456 {
00457 rodsLog (LOG_ERROR, "msiServerBackup: rsCollCreate failed for %s, status = %d",
00458 collInp.collName, rei->status);
00459 free( myKeyVal );
00460 free( dbPath );
00461 return (rei->status);
00462 }
00463
00464
00465
00466
00467
00468 memset(&dataObjInp, 0, sizeof(dataObjInp_t));
00469 addKeyVal (&dataObjInp.condInput, COLLECTION_KW, "");
00470 addKeyVal (&dataObjInp.condInput, DEST_RESC_NAME_KW, rescInfo->rescName);
00471
00472
00473 subPath = rei->rsComm->myEnv.rodsHome + strlen(rei->rsComm->myEnv.rodsZone) + 2;
00474
00475
00476 snprintf(newDirPath, MAX_NAME_LEN, "%s/%s/%s/%s_%s/%s", rescInfo->rescVaultPath,
00477 subPath, BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr, rodsDirPath + offset);
00478
00479 addKeyVal (&dataObjInp.condInput, FILE_PATH_KW, newDirPath);
00480
00481
00482 snprintf(dataObjInp.objPath, MAX_NAME_LEN, "%s/%s/%s_%s/%s", rei->rsComm->myEnv.rodsHome,
00483 BCKP_COLL_NAME, rei->rsComm->myEnv.rodsHost, tStr, rodsDirPath + offset);
00484
00485
00486
00487 rei->status = rsPhyPathReg (rei->rsComm, &dataObjInp);
00488 if (rei->status < 0)
00489 {
00490 rodsLog (LOG_ERROR, "msiServerBackup: rsPhyPathReg() failed with status %d", rei->status);
00491 free( myKeyVal );
00492 free( dbPath );
00493 return rei->status;
00494 }
00495
00496
00497
00498 snprintf(fileCountStr, 21, "%d", fileCount);
00499 addKeyVal(myKeyVal, "object_count", fileCountStr);
00500
00501
00502 keyValOut->inOutStruct = (void*) myKeyVal;
00503
00504 free( myKeyVal );
00505 free( dbPath );
00506
00507
00508 return 0;
00509 }