00001
00002
00003
00004
00005 #include "jsoc_main.h"
00006 #include "drms.h"
00007 #include "drms_names.h"
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <unistd.h>
00011 #include <string.h>
00012 #include "printk.h"
00013
00014 ModuleArgs_t module_args[] =
00015 {
00016 {ARG_STRING, "op", "Not Specified", "<Operation>"},
00017 {ARG_STRING, "ds", "Not Specified", "<list of sunums>"},
00018 {ARG_STRING, "requestid", "Not Specified", "RequestID string for export management"},
00019 {ARG_STRING, "method", "url", "Export method"},
00020 {ARG_STRING, "protocol", "as-is", "export file protocol"},
00021 {ARG_STRING, "format", "json", "export communication protocol"},
00022 {ARG_FLAG, "h", "0", "help - show usage"},
00023 {ARG_FLAG, "z", "0", "emit JSON output"},
00024 {ARG_STRING, "QUERY_STRING", "Not Specified", "AJAX query from the web"},
00025 {ARG_END}
00026 };
00027
00028 char *module_name = "jsoc_export_SU_as_is";
00029 int nice_intro ()
00030 {
00031 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00032 if (usage)
00033 {
00034 printf ("Usage:\njsoc_info {-h} {ds=<sunum list>}\n"
00035 " details are:\n"
00036 "ds=<sunum list> comma delimited list of storage units\n"
00037 "requestid= RequestID string for export management\n"
00038 "method = Export method, default to url\n"
00039 "protocol = export file protocol, default to as-is\n"
00040 "format = export communication protocol, default to json\n"
00041 );
00042 return(1);
00043 }
00044 return (0);
00045 }
00046
00047 #define DIE(msg) \
00048 { \
00049 fprintf(index_txt,"status=1\n"); \
00050 fprintf(index_txt, "error='%s'\n", msg); \
00051 fclose(index_txt); \
00052 if (my_sum) \
00053 SUM_close(my_sum,printkerr); \
00054 return(1); \
00055 }
00056
00057 TIME timenow()
00058 {
00059 TIME UNIX_epoch = -220924792.000;
00060 TIME now = (double)time(NULL) + UNIX_epoch;
00061 return(now);
00062 }
00063
00064
00065 void freeInfostructs(SUM_info_t ***infostructs, int count)
00066 {
00067 int iinfo;
00068
00069 if (infostructs && *infostructs)
00070 {
00071 for (iinfo = 0; iinfo < count; iinfo++)
00072 {
00073 if ((*infostructs)[iinfo])
00074 {
00075 free((*infostructs)[iinfo]);
00076 (*infostructs)[iinfo] = NULL;
00077 }
00078 }
00079
00080 free(*infostructs);
00081 *infostructs = NULL;
00082 }
00083 }
00084
00085 void freeOutList(char ***outList, int num)
00086 {
00087 int iinfo;
00088
00089 for (iinfo = 0; iinfo < num; iinfo++)
00090 {
00091 if ((*outList)[iinfo])
00092 {
00093 free((*outList)[iinfo]);
00094 (*outList)[iinfo] = NULL;
00095 }
00096 }
00097
00098 free(*outList);
00099 *outList = NULL;
00100 }
00101
00102 static int nextInfoIndex(char **list)
00103 {
00104 static char **current = NULL;
00105
00106 if (!current)
00107 {
00108 current = list;
00109 }
00110
00111 while (current)
00112 {
00113 current++;
00114 }
00115
00116 return current - list;
00117 }
00118
00119
00120 int DoIt(void)
00121 {
00122 char *sunumlist;
00123 char *this_sunum;
00124 const char *requestid;
00125 const char *method;
00126 const char *protocol;
00127 const char *format;
00128
00129 int count;
00130 int status = DRMS_SUCCESS;
00131 long long size;
00132 FILE *index_txt = NULL;
00133 char buf[2*DRMS_MAXPATHLEN];
00134 char *cwd;
00135 TIME now = timenow();
00136
00137 int64_t *sunumList = NULL;
00138 int y,m,d,hr,mn;
00139 char sutime[64];
00140 int online;
00141 int ioff;
00142 int noff;
00143 SUM_info_t **infostructs = NULL;
00144 int iinfo;
00145 SUM_info_t *suinfo = NULL;
00146 int retention;
00147 char **outList = NULL;
00148 DRMS_SuAndSeries_t *offlineSUs = NULL;
00149 int nextIInfo;
00150 long long *offlineSUList = NULL;
00151
00152 if (nice_intro ()) return (0);
00153
00154 count = cmdparams_get_int64arr(&cmdparams, "ds", &sunumList, &status);
00155
00156 if (status != CMDPARAMS_SUCCESS || !sunumList)
00157 {
00158 fprintf(stderr, "Invalid argument 'sunum=%s'.\n", cmdparams_get_str(&cmdparams, "ds", NULL));
00159 return 1;
00160 }
00161
00162 requestid = cmdparams_get_str(&cmdparams, "requestid", NULL);
00163 format = cmdparams_get_str(&cmdparams, "format", NULL);
00164 method = cmdparams_get_str(&cmdparams, "method", NULL);
00165 protocol = cmdparams_get_str(&cmdparams, "protocol", NULL);
00166
00167
00168 size = 0;
00169
00170
00171
00172 if (count > 0)
00173 {
00174 infostructs = (SUM_info_t **)calloc(count, sizeof(SUM_info_t *));
00175
00176 if (!infostructs)
00177 {
00178 fprintf(stderr, "Out of memory.\n");
00179 free(sunumList);
00180
00181 return 1;
00182 }
00183
00184 status = drms_getsuinfo(drms_env, (long long *)sunumList, count, infostructs);
00185
00186 if (status != DRMS_SUCCESS)
00187 {
00188 fprintf(stderr, "Unable to get SUMS information for specified SUs.\n");
00189
00190 freeInfostructs(&infostructs, count);
00191 free(sunumList);
00192
00193 return 1;
00194 }
00195
00196 outList = calloc(count, sizeof(char *));
00197 if (!outList)
00198 {
00199 fprintf(stderr, "Out of memory.\n");
00200 freeInfostructs(&infostructs, count);
00201 free(sunumList);
00202
00203 return 1;
00204 }
00205
00206 for (iinfo = 0, ioff = 0; iinfo < count; iinfo++)
00207 {
00208 suinfo = infostructs[iinfo];
00209 if (suinfo && *suinfo->online_loc != '\0')
00210 {
00211 size += (long long)suinfo->bytes;
00212
00213
00214 if (strcmp(suinfo->online_status, "Y") == 0)
00215 {
00216
00217 sscanf(suinfo->effective_date, "%4d%2d%2d%2d%2d", &y, &m, &d, &hr, &mn);
00218 snprintf(sutime, sizeof(sutime), "%4d.%02d.%02d_%02d:%02d", y, m, d, hr, mn);
00219 retention = (sscan_time(sutime) - now)/86400.0;
00220
00221 if (retention >= 3)
00222 {
00223
00224 snprintf(buf, sizeof(buf), "%lld\t%s\t%s\t%s\t%d\n", suinfo->sunum, suinfo->owning_series, suinfo->online_loc, "Y", (int)suinfo->bytes);
00225 outList[iinfo] = strdup(buf);
00226 online = 1;
00227 }
00228 else
00229 {
00230 online = 0;
00231 }
00232 }
00233 else
00234 {
00235 online = 0;
00236 }
00237
00238 if (!online)
00239 {
00240 if (strcmp(suinfo->archive_status, "N") == 0)
00241 {
00242 snprintf(buf, sizeof(buf), "%lld\t%s\t%s\t%s\t%d\n", suinfo->sunum, suinfo->owning_series, "NA", "X", (int)suinfo->bytes);
00243 outList[iinfo] = strdup(buf);
00244 }
00245 else
00246 {
00247
00248
00249 if (!offlineSUs)
00250 {
00251 offlineSUs = calloc(count, sizeof(DRMS_SuAndSeries_t));
00252 if (!offlineSUs)
00253 {
00254 fprintf(stderr, "Out of memory.\n");
00255 freeInfostructs(&infostructs, count);
00256 free(sunumList);
00257
00258 return 1;
00259 }
00260 }
00261
00262 offlineSUs[ioff].sunum = suinfo->sunum;
00263 offlineSUs[ioff].series = suinfo->owning_series;
00264 ioff++;
00265 }
00266 }
00267 }
00268 else
00269 {
00270
00271 snprintf(buf, sizeof(buf), "%lld\t%s\t%s\t%s\t%d\n", sunumList[iinfo], "NA", "NA", "I", 0);
00272 outList[iinfo] = strdup(buf);
00273 }
00274 }
00275
00276 free(sunumList);
00277 sunumList = NULL;
00278
00279 noff = ioff;
00280
00281 if (noff > 0)
00282 {
00283
00284
00285
00286
00287
00288
00289
00290
00291 int bail = 0;
00292
00293
00294 status = drms_getunits_ex(drms_env, noff, offlineSUs, 1, 0);
00295
00296
00297 freeInfostructs(&infostructs, count);
00298
00299 if (status != DRMS_SUCCESS)
00300 {
00301 fprintf(stderr, "Out of memory.\n");
00302 free(offlineSUs);
00303 freeOutList(&outList, count);
00304
00305 return 1;
00306 }
00307
00308
00309
00310
00311
00312 infostructs = (SUM_info_t **)calloc(noff, sizeof(SUM_info_t *));
00313
00314 if (!infostructs)
00315 {
00316 fprintf(stderr, "Out of memory.\n");
00317 free(offlineSUs);
00318 freeOutList(&outList, count);
00319
00320 return 1;
00321 }
00322
00323
00324 offlineSUList = calloc(noff, sizeof(long long));
00325
00326 if (!offlineSUList)
00327 {
00328 fprintf(stderr, "Out of memory.\n");
00329 freeInfostructs(&infostructs, noff);
00330 free(offlineSUs);
00331 freeOutList(&outList, count);
00332
00333 return 1;
00334 }
00335
00336 for (ioff = 0; ioff < noff; ioff++)
00337 {
00338 offlineSUList[ioff] = offlineSUs[ioff].sunum;
00339 }
00340
00341 free(offlineSUs);
00342 offlineSUs = NULL;
00343
00344 status = drms_getsuinfo(drms_env, (long long *)offlineSUList, noff, infostructs);
00345
00346 free(offlineSUList);
00347 offlineSUList = NULL;
00348
00349 if (status != DRMS_SUCCESS)
00350 {
00351 fprintf(stderr, "Unable to get SUMS information for specified SUs.\n");
00352 freeInfostructs(&infostructs, noff);
00353 freeOutList(&outList, count);
00354
00355 return 1;
00356 }
00357
00358 for (ioff = 0; iinfo < noff; ioff++)
00359 {
00360 suinfo = infostructs[ioff];
00361
00362 if (suinfo && *suinfo->online_loc != '\0')
00363 {
00364
00365 if (strcmp(suinfo->online_status, "Y") == 0)
00366 {
00367
00368 sscanf(suinfo->effective_date, "%4d%2d%2d%2d%2d", &y, &m, &d, &hr, &mn);
00369 snprintf(sutime, sizeof(sutime), "%4d.%02d.%02d_%02d:%02d", y, m, d, hr, mn);
00370 retention = (sscan_time(sutime) - now)/86400.0;
00371
00372 if (retention < 3)
00373 {
00374
00375 bail = 1;
00376 }
00377 }
00378 else
00379 {
00380
00381 bail = 1;
00382 }
00383 }
00384 else
00385 {
00386
00387 bail = 1;
00388 }
00389
00390 if (bail)
00391 {
00392 fprintf(stderr, "Unable to stage offline SUs.\n");
00393 freeInfostructs(&infostructs, noff);
00394 freeOutList(&outList, count);
00395
00396 return 1;
00397 }
00398 else
00399 {
00400 snprintf(buf, sizeof(buf), "%lld\t%s\t%s\t%s\t%d\n", suinfo->sunum, suinfo->owning_series, suinfo->online_loc, "Y", (int)suinfo->bytes);
00401 nextIInfo = nextInfoIndex(outList);
00402 outList[nextIInfo] = strdup(buf);
00403 }
00404 }
00405
00406 freeInfostructs(&infostructs, noff);
00407 }
00408 }
00409
00410 if (noff < 1)
00411 {
00412 freeInfostructs(&infostructs, count);
00413 }
00414
00415 index_txt = fopen("index.txt", "w");
00416
00417 if (!index_txt)
00418 {
00419 fprintf(stderr, "Unable to open index.txt for writing.\n");
00420
00421 return 1;
00422 }
00423
00424 fprintf(index_txt, "# JSOC Export SU List\n");
00425 fprintf(index_txt, "version=1\n");
00426 fprintf(index_txt, "requestid=%s\n", requestid);
00427 fprintf(index_txt, "method=%s\n", method);
00428 fprintf(index_txt, "protocol=%s\n", protocol);
00429 fprintf(index_txt, "wait=0\n");
00430 fprintf(index_txt, "count=%d\n", count);
00431 fprintf(index_txt, "size=%lld\n", size);
00432 fprintf(index_txt, "status=0\n");
00433
00434 cwd = getcwd(NULL, 0);
00435 fprintf(index_txt,"dir=%s\n", ((strncmp("/auto", cwd, 5) == 0) ? cwd + 5 : cwd));
00436 free(cwd);
00437 fprintf(index_txt, "# DATA SU\n");
00438
00439
00440 for (iinfo = 0; iinfo < count; iinfo++)
00441 {
00442 fprintf(index_txt, outList[iinfo]);
00443 free(outList[iinfo]);
00444 outList[iinfo] = NULL;
00445 }
00446
00447 free(outList);
00448 outList = NULL;
00449
00450 fclose(index_txt);
00451
00452 return(0);
00453 }