00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00093 #include "jsoc_main.h"
00094 #include "drms.h"
00095 #include "drms_names.h"
00096 #include <unistd.h>
00097 #include <strings.h>
00098
00099
00100
00101
00102
00103
00104
00105 #define NOTSPECIFIED "###NOTSPECIFIED###"
00106
00107 ModuleArgs_t module_args[] = {
00108 {ARG_STRING, "segment", NOTSPECIFIED, "", ""},
00109 {ARG_STRING, "ds", NOTSPECIFIED, "", ""},
00110 {ARG_STRING, "out", NOTSPECIFIED, "", ""},
00111 {ARG_FLAG, "f", "0", "", ""},
00112 {ARG_FLAG, "h", "0", "", ""},
00113 {ARG_FLAG, "l", "0", "", ""},
00114 {ARG_FLAG, "v", "0", "", ""},
00115 {ARG_END, NULL, NULL}
00116 };
00117
00118 char *module_name = "retrieve_file";
00119
00120
00121 int verbose = 0;
00122
00123
00124
00125 int nice_intro (int do_usage) {
00126 int help = cmdparams_get_int (&cmdparams, "h", NULL);
00127 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00128 if (help || do_usage) {
00129 printf("retrieve_file {-f} {-h} {-l} {-v} <seriesname>{[<record_spec>]} to={dest} {segment=<segment_name>}\n"
00130 "-f force retreived file to overwrite existing file\n"
00131 "-h print this message and exit\n"
00132 "-l create symbolic link to SUMS archive instead of copying file.\n"
00133 "-v verbose flag.\n"
00134 "ds=<seriesname> - series containing file to retrieve.\n"
00135 "<record_spec> - query to identify chosen record.\n"
00136 "out= destination directory (required).\n"
00137 "segment=file - segment in series to use for getting file to retrieve.\n"
00138 " Not used if series has only one segment.\n");
00139 return(1);
00140 }
00141 if (verbose) cmdparams_printall (&cmdparams);
00142 return(0);
00143 }
00144
00145
00146 int DoIt(void)
00147 {
00148 int status = 0;
00149
00150 char *segname, *series;
00151 DRMS_RecordSet_t *recordset;
00152 int irec;
00153 int wantlink = cmdparams_get_int(&cmdparams, "l", NULL);
00154 int force = cmdparams_get_int(&cmdparams, "f", NULL);
00155 char *destination;
00156
00157 if (nice_intro(0))
00158 return(0);
00159
00160 destination = cmdparams_get_str(&cmdparams, "out", NULL);
00161 series = strdup(cmdparams_get_str(&cmdparams, "ds", NULL));
00162 segname = cmdparams_get_str(&cmdparams, "segment", NULL);
00163
00164 if (strcmp(series, NOTSPECIFIED) == 0)
00165 {
00166 printf("\n$$$ NO ACTION - A record query must be specified as 'ds=xxx'.\n");
00167 return nice_intro(1);
00168 }
00169 if (strcmp(destination, NOTSPECIFIED) == 0)
00170 {
00171 printf("\n$$$ NO ACTION - A destination directory must be specified as 'out=<dir>'.\n");
00172 return nice_intro(1);
00173 }
00174
00175 recordset = drms_open_records(drms_env, series, &status);
00176 if (status)
00177 {
00178 fprintf(stderr,"retrieve_file target NOT_FOUND, drms_open_records failed, series=%s, status=%d.\n"
00179 "Aborting.\n", series,status);
00180 return(1);
00181 }
00182 if (recordset->n > 1)
00183 printf("%d files matched.\n", recordset->n);
00184
00185 for (irec = 0; irec< recordset->n; irec++)
00186 {
00187 DRMS_Record_t *rec = recordset->records[irec];
00188 DRMS_Segment_t *seg;
00189 char path[DRMS_MAXPATHLEN];
00190 int nsegs = hcon_size(&rec->segments);
00191 if (nsegs == 0)
00192 {
00193 fprintf(stderr,"retrieve_file series has no segments. series=%s.\n",series);
00194 return(1);
00195 }
00196
00197 if (nsegs > 1)
00198 {
00199 if (strcmp(segname, NOTSPECIFIED) == 0)
00200 {
00201 fprintf(stderr,"retrieve_file series has %d segments. desired segment must be specified. series=%s.\n",
00202 nsegs, series);
00203 return(1);
00204 }
00205 seg = drms_segment_lookup(rec, segname);
00206 }
00207 else
00208 seg = drms_segment_lookupnum(rec, 0);
00209 drms_record_directory(rec,path,1);
00210 drms_segment_filename(seg, path);
00211 if (*path)
00212 {
00213 char cmd[DRMS_MAXPATHLEN+1024];
00214 sprintf(cmd, "cp %s %s %s %s", (wantlink ? "-s" : ""), (force ? "-f" : "-b" ), path, destination);
00215 if (system(cmd))
00216 {
00217 fprintf(stderr, "retrieve_file failed on command: %s\n",cmd);
00218 return(1);
00219 }
00220 if (verbose)
00221 {
00222 char *note = drms_getkey_string(rec, "note", &status);
00223 printf("%s %s to %s",path, (wantlink ? "linked" : "retrieved"), destination);
00224 if (note && *note)
00225 printf(", note = %s\n", note);
00226 else
00227 printf(".\n");
00228 }
00229 }
00230 else
00231 {
00232 printf("retrieve_file target file NOT_FOUND, path not found in %s, abort.\n", series);
00233 status = 1;
00234 }
00235 }
00236
00237 return(status);
00238 }