00001 #include "jsoc_main.h"
00002 #include "drms_types.h"
00003 #include "drms_storageunit.h"
00004 #include "exputil.h"
00005
00006 char *module_name = "export";
00007
00008 typedef enum
00009 {
00010 kMymodErr_Success = 0,
00011 kMymodErr_CantRegisterDefs,
00012 kMymodErr_MissingArg,
00013 kMymodErr_BadRequestID,
00014 kMymodErr_BadRecSetQuery,
00015 kMymodErr_BadFilenameFmt,
00016 kMymodErr_ExportFailed,
00017 kMymodErr_CantOpenPackfile,
00018 kMymodErr_PackfileFailure,
00019 kMymodErr_UnsupportedPLRecType,
00020 kMymodErr_DRMS
00021 } MymodError_t;
00022
00023 typedef enum
00024 {
00025 kPL_metadata,
00026 kPL_content
00027 } PLRecType_t;
00028
00029 #define kNotSpecified "NOT SPECIFIED"
00030 #define kArg_reqid "reqid"
00031 #define kArg_version "version"
00032 #define kArg_method "method"
00033 #define kArg_protocol "protocol"
00034 #define kArg_rsquery "rsquery"
00035 #define kArg_expSeries "expseries"
00036 #define kArg_ffmt "ffmt"
00037 #define kArg_path "path"
00038 #define kArg_clname "kmclass"
00039 #define kArg_kmfile "kmfile"
00040
00041
00042 #define kDef_expSeries "jsoc.exports"
00043
00044 #define kPWD "PWD"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 ModuleArgs_t module_args[] =
00061 {
00062 {ARG_STRING, kArg_version, "", "jsoc export version."},
00063 {ARG_STRING, kArg_reqid, "",
00064 "Export series primary key value that identifies the output record."},
00065 {ARG_STRING, kArg_method, "", "jsoc export method (eg, url or ftp)."},
00066 {ARG_STRING, kArg_protocol, "", "file conversion method (eg, convert to fits)."},
00067 {ARG_STRING, kArg_rsquery, kNotSpecified,
00068 "Record-set query that specifies data to be exported."},
00069 {ARG_STRING, kArg_expSeries, kDef_expSeries, "Series to which exported data are saved."},
00070 {ARG_STRING, kArg_ffmt, kNotSpecified, "Export filename template."},
00071 {ARG_STRING, kArg_path, kNotSpecified, "Path to which fits files are output."},
00072 {ARG_STRING, kArg_clname, kNotSpecified, "Export key map class."},
00073 {ARG_STRING, kArg_kmfile, kNotSpecified, "Export key map file."},
00074 {ARG_END}
00075 };
00076
00077 char gDefBuf[PATH_MAX] = {0};
00078
00079 MymodError_t WritePListRecord(PLRecType_t rectype, FILE *pkfile, const char *f1, const char *f2)
00080 {
00081 MymodError_t err = kMymodErr_Success;
00082
00083 switch (rectype)
00084 {
00085 case kPL_metadata:
00086 fprintf(pkfile, "%s=%s\n", f1, f2);
00087 break;
00088 case kPL_content:
00089 fprintf(pkfile, "%s\t%s\n", f1, f2);
00090 break;
00091 default:
00092 fprintf(stderr, "Unsupported packing-list record type '%d'.\n", (int)rectype);
00093 err = kMymodErr_UnsupportedPLRecType;
00094 }
00095
00096 return err;
00097 }
00098
00099
00100
00101 static int MapexportRecordToDir(DRMS_Record_t *recin,
00102 const char *ffmt,
00103 const char *outpath,
00104 FILE *pklist,
00105 const char *classname,
00106 const char *mapfile,
00107 int *tcount,
00108 MymodError_t *status)
00109 {
00110 int drmsstat = DRMS_SUCCESS;
00111 MymodError_t modstat = kMymodErr_Success;
00112 HIterator_t *hit = NULL;
00113 DRMS_Segment_t *segin = NULL;
00114 unsigned long long tsize = 0;
00115 char dir[DRMS_MAXPATHLEN];
00116 char fmtname[DRMS_MAXPATHLEN];
00117 char fullfname[DRMS_MAXPATHLEN];
00118 char query[DRMS_MAXQUERYLEN];
00119 struct stat filestat;
00120
00121 drms_record_directory(recin, dir, 1);
00122
00123
00124 char buf[1028];
00125 int nkeys = recin->seriesinfo->dbidx_num;
00126 int snext = 0;
00127 int ikey = 0;
00128
00129 snprintf(query, sizeof(query), "%s", recin->seriesinfo->seriesname);
00130 snext = strlen(query);
00131
00132 for (ikey = 0; ikey < nkeys; ikey++)
00133 {
00134 drms_keyword_snprintfval(recin->seriesinfo->dbidx_keywords[ikey], buf, sizeof(buf));
00135 snprintf(query + snext, sizeof(query) - snext, "[%s]", buf);
00136 snext += strlen(buf) + 2;
00137 }
00138
00139
00140
00141 hit = hiter_create(&(recin->segments));
00142 while ((segin = hiter_getnext(hit)) != NULL)
00143 {
00144 if (exputl_mk_expfilename(segin, ffmt, fmtname) == kExpUtlStat_Success)
00145 {
00146 snprintf(fullfname, sizeof(fullfname), "%s/%s", outpath, fmtname);
00147 }
00148 else
00149 {
00150 modstat = kMymodErr_BadFilenameFmt;
00151 break;
00152 }
00153
00154
00155 DRMS_Array_t *arrout = drms_segment_read(segin, DRMS_TYPE_RAW, &drmsstat);
00156 drmsstat = drms_mapexport_tofitsfile(arrout,
00157 &segin->record->keywords,
00158 segin->cparms,
00159 classname,
00160 mapfile,
00161 fullfname);
00162 if (drmsstat != DRMS_SUCCESS || stat(fullfname, &filestat))
00163 {
00164 modstat = kMymodErr_ExportFailed;
00165 fprintf(stderr, "Failure exporting segment '%s'.\n", segin->info->name);
00166 break;
00167 }
00168 else
00169 {
00170 if (tcount)
00171 {
00172 ++*tcount;
00173 }
00174
00175 tsize += filestat.st_size;
00176 WritePListRecord(kPL_content, pklist, query, fmtname);
00177 }
00178 }
00179
00180 hiter_destroy(&hit);
00181
00182 if (status)
00183 {
00184 *status = modstat;
00185 }
00186
00187 return tsize;
00188 }
00189
00190 static int MapexportToDir(DRMS_Env_t *env,
00191 const char *rsinquery,
00192 const char *ffmt,
00193 const char *outpath,
00194 FILE *pklist,
00195 const char *classname,
00196 const char *mapfile,
00197 int *tcount,
00198 TIME *exptime,
00199 MymodError_t *status)
00200 {
00201 int stat = DRMS_SUCCESS;
00202 MymodError_t modstat = kMymodErr_Success;
00203 DRMS_RecordSet_t *rsin = NULL;
00204 DRMS_Record_t *recin = NULL;
00205 int iRec = 0;
00206 int nSets = 0;
00207 int iSet = 0;
00208 int nRecs = 0;
00209 unsigned long long tsize = 0;
00210
00211
00212 char *tmpq = NULL;
00213
00214 if (rsinquery)
00215 {
00216 tmpq = strdup(rsinquery);
00217 }
00218
00219 if (tmpq && (rsin = drms_open_records(env, tmpq, &stat)))
00220 {
00221 nSets = rsin->ss_n;
00222
00223 for (iSet = 0;
00224 stat == DRMS_SUCCESS && modstat == kMymodErr_Success && iSet < nSets;
00225 iSet++)
00226 {
00227 nRecs = drms_recordset_getssnrecs(rsin, iSet, &stat);
00228
00229 for (iRec = 0; modstat == kMymodErr_Success && iRec < nRecs; iRec++)
00230 {
00231 recin = rsin->records[(rsin->ss_starts)[iSet] + iRec];
00232 tsize += MapexportRecordToDir(recin,
00233 ffmt,
00234 outpath,
00235 pklist,
00236 classname,
00237 mapfile,
00238 tcount,
00239 &modstat);
00240 }
00241 }
00242
00243 if (stat != DRMS_SUCCESS || modstat != kMymodErr_Success)
00244 {
00245 fprintf(stderr, "Export halted due to DRMS failure.\n");
00246 }
00247 else if (exptime)
00248 {
00249 *exptime = CURRENT_SYSTEM_TIME;
00250 }
00251 }
00252 else
00253 {
00254 fprintf(stderr, "Record-set query '%s' is not valid.\n", rsinquery);
00255 modstat = kMymodErr_BadRecSetQuery;
00256 }
00257
00258 if (status)
00259 {
00260 *status = modstat;
00261 }
00262
00263 return tsize;
00264 }
00265
00266 static MymodError_t CallExportToFile(DRMS_Segment_t *segout,
00267 DRMS_Segment_t *segin,
00268 const char *clname,
00269 const char *mapfile,
00270 const char *ffmt,
00271 unsigned long long *szout,
00272 char *filewritten)
00273 {
00274 int status = DRMS_SUCCESS;
00275 MymodError_t err = kMymodErr_Success;
00276 char fileout[DRMS_MAXPATHLEN];
00277 char filein[DRMS_MAXPATHLEN];
00278 char basename[DRMS_MAXPATHLEN];
00279 unsigned long long size = 0;
00280 struct stat filestat;
00281
00282 if (segout)
00283 {
00284 drms_segment_filename(segin, filein);
00285 if (!stat(filein, &filestat))
00286 {
00287 size = filestat.st_size;
00288
00289 if (exputl_mk_expfilename(segin, ffmt, basename) == kExpUtlStat_Success)
00290 {
00291 CHECKSNPRINTF(snprintf(segout->filename, DRMS_MAXSEGFILENAME, "%s", basename), DRMS_MAXSEGFILENAME);
00292 drms_segment_filename(segout, fileout);
00293
00294 status = drms_segment_mapexport_tofile(segin, clname, mapfile, fileout);
00295 if (status != DRMS_SUCCESS)
00296 {
00297 err = kMymodErr_ExportFailed;
00298 fprintf(stderr, "Failed to export segment '%s' to '%s'.\n", segin->info->name, fileout);
00299 }
00300 }
00301 else
00302 {
00303 err = kMymodErr_BadFilenameFmt;
00304 }
00305 }
00306 else
00307 {
00308 fprintf(stderr, "Unable to open source file '%s'.\n", filein);
00309 err = kMymodErr_ExportFailed;
00310 }
00311 }
00312
00313 *szout = 0;
00314 if (err == kMymodErr_Success)
00315 {
00316 *szout = size;
00317 snprintf(filewritten, DRMS_MAXPATHLEN, "%s", basename);
00318 }
00319
00320 return err;
00321 }
00322
00323
00324 static int MapexportRecord(DRMS_Record_t *recout,
00325 DRMS_Record_t *recin,
00326 const char *classname,
00327 const char *mapfile,
00328 const char *pklfilename,
00329 int *tcount,
00330 const char *ffmt,
00331 char **outpath,
00332 FILE **pklist,
00333 MymodError_t *status)
00334 {
00335 MymodError_t err = kMymodErr_Success;
00336 HIterator_t *hit = NULL;
00337 DRMS_Segment_t *segout = NULL;
00338 DRMS_Segment_t *segin = NULL;
00339 unsigned long long size = 0;
00340 unsigned long long tsize = 0;
00341 char dir[DRMS_MAXPATHLEN];
00342
00343 segout = drms_segment_lookupnum(recout, 0);
00344
00345 drms_record_directory(recin, dir, 1);
00346
00347 if (segout)
00348 {
00349 char glom[128];
00350 snprintf(glom, sizeof(glom), "%%s/%s", DRMS_SLOTDIR_FORMAT);
00351 char path[DRMS_MAXPATHLEN];
00352 snprintf(path, sizeof(path), glom, segout->record->su->sudir, segout->record->slotnum);
00353
00354 if (outpath)
00355 {
00356 *outpath = strdup(path);
00357 }
00358
00359 if (pklist)
00360 {
00361 char pkpath[DRMS_MAXPATHLEN];
00362 snprintf(pkpath, sizeof(pkpath), "%s/%s", path, pklfilename);
00363 *pklist = fopen(pkpath, "w+");
00364 }
00365
00366 char fname[DRMS_MAXPATHLEN];
00367 char query[DRMS_MAXQUERYLEN];
00368
00369
00370 char buf[1028];
00371 int nkeys = recin->seriesinfo->dbidx_num;
00372 int snext = 0;
00373 int ikey = 0;
00374
00375 snprintf(query, sizeof(query), "%s", recin->seriesinfo->seriesname);
00376 snext = strlen(query);
00377
00378 for (ikey = 0; ikey < nkeys; ikey++)
00379 {
00380 drms_keyword_snprintfval(recin->seriesinfo->dbidx_keywords[ikey], buf, sizeof(buf));
00381 snprintf(query + snext, sizeof(query) - snext, "[%s]", buf);
00382 snext += strlen(buf) + 2;
00383 }
00384
00385
00386
00387 hit = hiter_create(&(recin->segments));
00388 while ((segin = hiter_getnext(hit)) != NULL)
00389 {
00390 size = 0;
00391 err = CallExportToFile(segout, segin, classname, mapfile, ffmt, &size, fname);
00392 if (err != kMymodErr_Success)
00393 {
00394 fprintf(stderr, "Failure exporting segment '%s'.\n", segin->info->name);
00395 break;
00396 }
00397 else
00398 {
00399 if (tcount)
00400 {
00401 ++*tcount;
00402 }
00403
00404 tsize += size;
00405 if (pklist && *pklist)
00406 {
00407 WritePListRecord(kPL_content, *pklist, query, fname);
00408 }
00409 }
00410 }
00411
00412 hiter_destroy(&hit);
00413 }
00414 else
00415 {
00416 fprintf(stderr, "Export series contains no segment!\n");
00417 err = kMymodErr_ExportFailed;
00418 }
00419
00420 if (status)
00421 {
00422 *status = err;
00423 }
00424
00425 return tsize;
00426 }
00427
00428
00429
00430
00431 static int Mapexport(DRMS_Env_t *env,
00432 const char *reqid,
00433 const char *classname,
00434 const char *mapfile,
00435 const char *expseries,
00436 const char *pklfilename,
00437 int *tcount,
00438 char **outpath,
00439 TIME *exptime,
00440 FILE **pklist,
00441 MymodError_t *status)
00442 {
00443 int stat;
00444 MymodError_t err = kMymodErr_Success;
00445 int nSets = 0;
00446 int iSet = 0;
00447 int nRecs = 0;
00448 int iRec = 0;
00449 unsigned long long tsize = 0;
00450 DRMS_Record_t *recout = NULL;
00451 DRMS_Record_t *recin = NULL;
00452 DRMS_RecordSet_t *rs = NULL;
00453 DRMS_RecordSet_t *rsout = NULL;
00454 DRMS_RecordSet_t *rsin = NULL;
00455 char rsoutquery[DRMS_MAXQUERYLEN];
00456 char *rsinquery = NULL;
00457
00458 snprintf(rsoutquery, sizeof(rsoutquery), "%s[%s]", expseries, reqid);
00459
00460 rs = drms_open_records(env, rsoutquery, &stat);
00461
00462 if (rs)
00463 {
00464
00465 rsout = drms_clone_records(rs, DRMS_PERMANENT, DRMS_COPY_SEGMENTS, &stat);
00466 drms_close_records(rs, DRMS_FREE_RECORD);
00467 }
00468
00469 if (rsout && rsout->n == 1)
00470 {
00471
00472 char *ffmt = NULL;
00473 char *kval = NULL;
00474
00475 recout = rsout->records[0];
00476 rsinquery = drms_getkey_string(recout, drms_defs_getval("kExportKW_Request"), &stat);
00477
00478 kval = drms_getkey_string(recout, drms_defs_getval("kExportKW_FileNameFormat"), &stat);
00479 if (kval)
00480 {
00481 if (*kval == '\0')
00482 {
00483 ffmt = NULL;
00484 }
00485 else
00486 {
00487 ffmt = strdup(kval);
00488 }
00489
00490 free(kval);
00491 }
00492
00493 if (rsinquery && (rsin = drms_open_records(env, rsinquery, &stat)))
00494 {
00495 nSets = rsin->ss_n;
00496
00497 for (iSet = 0;
00498 stat == DRMS_SUCCESS && err == kMymodErr_Success && iSet < nSets;
00499 iSet++)
00500 {
00501
00502
00503
00504 nRecs = drms_recordset_getssnrecs(rsin, iSet, &stat);
00505
00506 for (iRec = 0;
00507 stat == DRMS_SUCCESS && err == kMymodErr_Success &&iRec < nRecs;
00508 iRec++)
00509 {
00510 recin = rsin->records[(rsin->ss_starts)[iSet] + iRec];
00511 tsize += MapexportRecord(recout,
00512 recin,
00513 classname,
00514 mapfile,
00515 pklfilename,
00516 tcount,
00517 ffmt,
00518 outpath,
00519 pklist,
00520 &err);
00521 }
00522 }
00523
00524 if (stat != DRMS_SUCCESS || err != kMymodErr_Success)
00525 {
00526 fprintf(stderr, "Export halted due to DRMS failure.\n");
00527 }
00528 else if (exptime)
00529 {
00530 *exptime = CURRENT_SYSTEM_TIME;
00531 }
00532 }
00533 else
00534 {
00535 fprintf(stderr,
00536 "Export series keyword '%s' did not contain a valid recordset query.\n",
00537 drms_defs_getval("kExportKW_Request"));
00538 err = kMymodErr_BadRecSetQuery;
00539 }
00540
00541 if (ffmt)
00542 {
00543 free(ffmt);
00544 }
00545 }
00546 else
00547 {
00548 fprintf(stderr, "Could not open export destination record set with request id '%s'.\n", reqid);
00549 err = kMymodErr_BadRequestID;
00550 }
00551
00552
00553 if (err == kMymodErr_Success)
00554 {
00555 drms_setkey_time(recout, drms_defs_getval("kExportKW_ExpTime"), CURRENT_SYSTEM_TIME);
00556 drms_setkey_int(recout, drms_defs_getval("kExportKW_DataSize"), tsize);
00557 }
00558
00559 if (rsout)
00560 {
00561 drms_close_records(rsout, DRMS_INSERT_RECORD);
00562 }
00563
00564 if (rsinquery)
00565 {
00566 free(rsinquery);
00567 }
00568
00569 if (status)
00570 {
00571 *status = err;
00572 }
00573
00574 return tsize;
00575 }
00576
00577 static char *GenErrMsg(const char *fmt, ...)
00578 {
00579 char *msgout = NULL;
00580 char errmsg[4096];
00581
00582 va_list ap;
00583 va_start(ap, fmt);
00584 vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
00585
00586 msgout = strdup(errmsg);
00587 fprintf(stderr, errmsg);
00588
00589 va_end (ap);
00590
00591 return msgout;
00592 }
00593
00594 static MymodError_t AppendContent(FILE *dst, FILE *src)
00595 {
00596 MymodError_t err = kMymodErr_Success;
00597 char buf[32768];
00598 int nread = 0;
00599 int dstfd;
00600 int srcfd;
00601
00602 if (dst && src)
00603 {
00604 dstfd = fileno(dst);
00605 srcfd = fileno(src);
00606
00607 while(1)
00608 {
00609 nread = read(srcfd, buf, sizeof(buf));
00610 if (nread > 0)
00611 {
00612 if (write(dstfd, buf, nread) == -1)
00613 {
00614 err = kMymodErr_PackfileFailure;
00615 fprintf(stderr, "Failure writing packing-list file.\n");
00616 break;
00617 }
00618 }
00619 else
00620 {
00621 if (nread == -1)
00622 {
00623 err = kMymodErr_PackfileFailure;
00624 fprintf(stderr, "Failure reading packing-list file.\n");
00625 }
00626
00627 break;
00628 }
00629 }
00630 }
00631
00632 return err;
00633 }
00634
00635
00636
00637
00638
00639
00640 int DoIt(void)
00641 {
00642 MymodError_t err = kMymodErr_Success;
00643 int drmsstat = DRMS_SUCCESS;
00644 long long tsize = 0;
00645 int tcount = 0;
00646 TIME exptime = DRMS_MISSING_TIME;
00647 FILE *pklist = NULL;
00648 FILE *pklistTMP = NULL;
00649 char pklistfname[PATH_MAX];
00650 char pklistfnameTMP[PATH_MAX];
00651 char pklistpath[PATH_MAX];
00652 char pklistpathTMP[PATH_MAX];
00653
00654 const char *version = NULL;
00655 const char *reqid = NULL;
00656 const char *method = NULL;
00657 const char *protocol = NULL;
00658 const char *rsquery = NULL;
00659 const char *clname = NULL;
00660 const char *mapfile = NULL;
00661
00662
00663 char *md_version = NULL;
00664 char *md_reqid = NULL;
00665 char *md_method = NULL;
00666 char *md_protocol = NULL;
00667 char *md_count = NULL;
00668 char *md_size = NULL;
00669 char *md_exptime = NULL;
00670 char *md_dir = NULL;
00671
00672 char *md_status = NULL;
00673 char *md_error = NULL;
00674
00675 if (drms_defs_register("/home/arta/Dev/cvs/JSOC/proj/export/apps/data/export.defs"))
00676 {
00677 md_error = GenErrMsg("jsoc_export failure - missing definition file.\n");
00678 err = kMymodErr_CantRegisterDefs;
00679 }
00680 else
00681 {
00682 snprintf(pklistfname, sizeof(pklistfname), "%s", drms_defs_getval("kPackListFileName"));
00683 snprintf(pklistfnameTMP, sizeof(pklistfnameTMP), "%s.tmp", pklistfname);
00684
00685 version = cmdparams_get_str(&cmdparams, kArg_version, &drmsstat);
00686 reqid = cmdparams_get_str(&cmdparams, kArg_reqid, &drmsstat);
00687 method = cmdparams_get_str(&cmdparams, kArg_method, &drmsstat);
00688 protocol = cmdparams_get_str(&cmdparams, kArg_protocol, &drmsstat);
00689
00690 rsquery = cmdparams_get_str(&cmdparams, kArg_rsquery, &drmsstat);
00691
00692 clname = cmdparams_get_str(&cmdparams, kArg_clname, &drmsstat);
00693 if (drmsstat != DRMS_SUCCESS || !strcmp(clname, kNotSpecified))
00694 {
00695 clname = NULL;
00696 }
00697
00698 mapfile = cmdparams_get_str(&cmdparams, kArg_kmfile, &drmsstat);
00699 if (drmsstat != DRMS_SUCCESS || !strcmp(mapfile, kNotSpecified))
00700 {
00701 mapfile = NULL;
00702 }
00703
00704 md_version = strdup(version);
00705 md_reqid = strdup(reqid);
00706 md_method = strdup(method);
00707 md_protocol = strdup(protocol);
00708
00709 if (strcmp(rsquery, kNotSpecified) == 0)
00710 {
00711
00712 char *outpath = NULL;
00713
00714
00715 const char *expseries = NULL;
00716
00717 expseries = cmdparams_get_str(&cmdparams, kArg_expSeries, &drmsstat);
00718
00719
00720 tsize = Mapexport(drms_env,
00721 reqid,
00722 clname,
00723 mapfile,
00724 expseries,
00725 pklistfnameTMP,
00726 &tcount,
00727 &outpath,
00728 &exptime,
00729 &pklistTMP,
00730 &err);
00731
00732 if (err != kMymodErr_Success)
00733 {
00734 md_error = GenErrMsg("Failure occurred while processing export Request ID '%s'.\n", reqid);
00735 err = kMymodErr_ExportFailed;
00736 }
00737 else
00738 {
00739 snprintf(pklistpathTMP, sizeof(pklistpathTMP), "%s/%s", outpath, pklistfnameTMP);
00740
00741
00742 md_dir = strdup(outpath);
00743 }
00744
00745 if (outpath)
00746 {
00747 free(outpath);
00748 }
00749 }
00750 else
00751 {
00752
00753 const char *outpath = NULL;
00754
00755
00756 const char *ffmt = NULL;
00757
00758 outpath = cmdparams_get_str(&cmdparams, kArg_path, &drmsstat);
00759 if (strcmp(outpath, kNotSpecified) == 0)
00760 {
00761
00762 outpath = getenv(kPWD);
00763 }
00764
00765 ffmt = cmdparams_get_str(&cmdparams, kArg_ffmt, &drmsstat);
00766 if (strcmp(ffmt, kNotSpecified) == 0 || *ffmt == '\0')
00767 {
00768
00769 ffmt = NULL;
00770 }
00771
00772
00773 snprintf(pklistpathTMP, sizeof(pklistpathTMP), "%s/%s", outpath, pklistfnameTMP);
00774
00775 pklistTMP = fopen(pklistpathTMP, "w+");
00776 if (pklistTMP)
00777 {
00778
00779 tsize = MapexportToDir(drms_env,
00780 rsquery,
00781 ffmt,
00782 outpath,
00783 pklistTMP,
00784 clname,
00785 mapfile,
00786 &tcount,
00787 &exptime,
00788 &err);
00789 }
00790 else
00791 {
00792 err = kMymodErr_CantOpenPackfile;
00793 md_error = GenErrMsg("Couldn't open packing-list file '%s'.\n", pklistpathTMP);
00794 }
00795
00796 if (err != kMymodErr_Success)
00797 {
00798 md_error = GenErrMsg("Failure occurred while processing export Request ID '%s'.\n", reqid);
00799 err = kMymodErr_ExportFailed;
00800 }
00801 else
00802 {
00803
00804 md_dir = strdup(outpath);
00805 }
00806 }
00807
00808 if (drmsstat != DRMS_SUCCESS)
00809 {
00810 md_error = GenErrMsg("DRMS error '%d'.\n", drmsstat);
00811 }
00812 else if (err == kMymodErr_Success)
00813 {
00814 char tstr[64];
00815 int strsize = 0;
00816 sprint_time(tstr, exptime, "UT", 0);
00817
00818
00819 strsize = 64;
00820 md_size = malloc(strsize);
00821 snprintf(md_size, strsize, "%lld", tsize);
00822 md_count = malloc(strsize);
00823 snprintf(md_count, strsize, "%d", tcount);
00824 md_exptime = strdup(tstr);
00825 }
00826
00827 fprintf(stdout, "'%lld' bytes exported.\n", tsize);
00828 }
00829
00830
00831 if (md_dir)
00832 {
00833 snprintf(pklistpath, sizeof(pklistpath), "%s/%s", md_dir, pklistfname);
00834 pklist = fopen(pklistpath, "w+");
00835 }
00836
00837 if (pklist)
00838 {
00839 if (fseek(pklistTMP, 0, SEEK_SET))
00840 {
00841 md_error = GenErrMsg("Failure accessing packing-list file '%s'.\n", pklistfnameTMP);
00842 err = kMymodErr_PackfileFailure;
00843 }
00844 }
00845 else
00846 {
00847 md_error = GenErrMsg("Failure opening packing-list file '%s'.\n", pklistfname);
00848 err = kMymodErr_PackfileFailure;
00849 }
00850
00851
00852 if (err == kMymodErr_Success)
00853 {
00854 md_status = strdup(drms_defs_getval("kMDStatus_Good"));
00855 }
00856 else
00857 {
00858 md_status = strdup(drms_defs_getval("kMDStatus_Bad"));
00859 }
00860
00861 if (pklist)
00862 {
00863
00864 fprintf(pklist, "# JSOC \n");
00865 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Version"), md_version);
00866 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_RequestID"), md_reqid);
00867 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Method"), md_method);
00868 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Protocol"), md_protocol);
00869 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Count"), md_count ? md_count : "-1");
00870 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Size"), md_size ? md_size : "-1");
00871 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_ExpTime"), md_exptime ? md_exptime : "");
00872 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Dir"), md_dir ? md_dir : "");
00873 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Status"), md_status);
00874
00875 fflush(pklist);
00876
00877 if (err == kMymodErr_Success)
00878 {
00879
00880 fprintf(pklist, "# DATA \n");
00881 fflush(pklist);
00882 err = AppendContent(pklist, pklistTMP);
00883 }
00884
00885 if (err != kMymodErr_Success)
00886 {
00887 fflush(pklist);
00888
00889
00890 WritePListRecord(kPL_metadata, pklist, drms_defs_getval("kMD_Error"), md_error ? md_error : "");
00891 }
00892 }
00893
00894
00895
00896 if (pklistTMP)
00897 {
00898
00899 fclose(pklistTMP);
00900 pklistTMP = NULL;
00901
00902
00903 unlink(pklistpathTMP);
00904 }
00905
00906 if (pklist)
00907 {
00908
00909 fclose(pklist);
00910 pklist = NULL;
00911 }
00912
00913 if (md_version)
00914 {
00915 free(md_version);
00916 }
00917 if (md_reqid)
00918 {
00919 free(md_reqid);
00920 }
00921 if (md_method)
00922 {
00923 free(md_method);
00924 }
00925 if (md_protocol)
00926 {
00927 free(md_protocol);
00928 }
00929 if (md_count)
00930 {
00931 free(md_count);
00932 }
00933 if (md_size)
00934 {
00935 free(md_size);
00936 }
00937 if (md_exptime)
00938 {
00939 free(md_exptime);
00940 }
00941 if (md_dir)
00942 {
00943 free(md_dir);
00944 }
00945 if (md_status)
00946 {
00947 free(md_status);
00948 }
00949 if (md_error)
00950 {
00951 free(md_error);
00952 }
00953
00954 return err;
00955 }