00001
00002
00003
00004 #include "drms.h"
00005 #include "jsoc_main.h"
00006 #include "exputil.h"
00007
00008
00009 #define SPECIFICATION "spec"
00010 #define FILENAME_FMT_STRING "format"
00011 #define UNDEFINED "undefined"
00012 #define DEFAULT_FORMAT "defformat"
00013
00014 typedef enum
00015 {
00016 kSFN_STATUS_SUCCESS = 0,
00017 kSFN_STATUS_BADARGS = 1,
00018 kSFN_STATUS_RETRIEVE_RECORDS = 2
00019 } kSFNStatus_t;
00020
00021 char *module_name = "segment-file-name";
00022
00023
00024 ModuleArgs_t module_args[] =
00025 {
00026 { ARG_STRING, SPECIFICATION, "", "The record-set specification." },
00027 { ARG_STRING, FILENAME_FMT_STRING, DEFAULT_FORMAT, "OPTIONAL: The file-name format string." },
00028 { ARG_END }
00029 };
00030
00031
00032 int DoIt(void)
00033 {
00034 kSFNStatus_t rv = kSFN_STATUS_SUCCESS;
00035 int drmsStat = DRMS_SUCCESS;
00036 DRMS_RecordSet_t *recSet = NULL;
00037 DRMS_Record_t *rec = NULL;
00038 DRMS_Segment_t *seg = NULL;
00039 DRMS_Segment_t *segLinked = NULL;
00040 DRMS_RecChunking_t cstat;
00041 int newchunk = 0;
00042 HIterator_t *iter = NULL;
00043 const char *spec = NULL;
00044 const char *ffmt = NULL;
00045
00046 spec = cmdparams_get_str(&cmdparams, SPECIFICATION, &drmsStat);
00047
00048 if (drmsStat != DRMS_SUCCESS)
00049 {
00050 rv = kSFN_STATUS_BADARGS;
00051 fprintf(stderr, "Failure parsing required argument '%s'.\n", SPECIFICATION);
00052 }
00053 else
00054 {
00055 ffmt = cmdparams_get_str(&cmdparams, FILENAME_FMT_STRING, &drmsStat);
00056
00057 if (strcmp(ffmt, DEFAULT_FORMAT) == 0)
00058 {
00059 ffmt = NULL;
00060 }
00061
00062 if (drmsStat != DRMS_SUCCESS)
00063 {
00064 rv = kSFN_STATUS_BADARGS;
00065 fprintf(stderr, "Failure parsing required argument '%s'.\n", FILENAME_FMT_STRING);
00066 }
00067 else
00068 {
00069
00070 recSet = drms_open_recordset(drms_env, spec, &drmsStat);
00071 if (drmsStat != DRMS_SUCCESS)
00072 {
00073 fprintf(stderr, "Invalid record-set specification %s.\n", spec);
00074 rv = kSFN_STATUS_BADARGS;
00075 }
00076 else
00077 {
00078 ExpUtlStat_t expfn = kExpUtlStat_Success;
00079 char fileIn[DRMS_MAXPATHLEN];
00080 char basename[DRMS_MAXPATHLEN];
00081
00082 while ((rec = drms_recordset_fetchnext(drms_env, recSet, &drmsStat, &cstat, &newchunk)) != NULL)
00083 {
00084 if (drmsStat != DRMS_SUCCESS)
00085 {
00086 fprintf(stderr, "Unable to fetch next input record.\n");
00087 rv = kSFN_STATUS_RETRIEVE_RECORDS;
00088 break;
00089 }
00090
00091 while ((seg = drms_record_nextseg(rec, &iter, 0)) != NULL)
00092 {
00093 if (seg->info->islink)
00094 {
00095 if ((segLinked = drms_segment_lookup(rec, seg->info->name)) == NULL)
00096 {
00097 fprintf(stderr, "Unable to locate target segment %s.\n", seg->info->name);
00098 continue;
00099 }
00100
00101
00102 drms_segment_filename(segLinked, fileIn);
00103 }
00104 else
00105 {
00106 segLinked = seg;
00107
00108 drms_segment_filename(seg, fileIn);
00109 }
00110
00111 if ((expfn = exputl_mk_expfilename(seg, segLinked, ffmt, basename)) == kExpUtlStat_Success)
00112 {
00113 fprintf(stdout, "%lld\t%s\n", rec->recnum, basename);
00114 }
00115 else if (expfn == kExpUtlStat_InvalidFmt)
00116 {
00117 fprintf(stderr, "Invalid file-format string %s.\n", ffmt);
00118 fprintf(stdout, "%lld\tN/A\n", rec->recnum);
00119 continue;
00120 }
00121 else if (expfn == kExpUtlStat_UnknownKey)
00122 {
00123 fprintf(stderr, "Unknown DRMS keyword in format string %s.\n", ffmt);
00124 fprintf(stdout, "%lld\tN/A\n", rec->recnum);
00125 continue;
00126 }
00127 else
00128 {
00129 fprintf(stderr, "Unable to successfully parse format string %s.\n", ffmt);
00130 fprintf(stdout, "%lld\tN/A\n", rec->recnum);
00131 continue;
00132 }
00133 }
00134
00135 hiter_destroy(&iter);
00136 }
00137 }
00138
00139 if (recSet)
00140 {
00141 drms_close_records(recSet, DRMS_FREE_RECORD);
00142 }
00143 }
00144 }
00145
00146 return rv;
00147 }