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