00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00114
00115
00116
00117
00118
00119
00120
00121
00122 #include "jsoc_main.h"
00123 #include "drms.h"
00124 #include "drms_names.h"
00125 #include <unistd.h>
00126 #include <strings.h>
00127
00128 #define NOTSPECIFIED "***NOTSPECIFIED***"
00129
00130 ModuleArgs_t module_args[] = {
00131 {ARG_STRING, "ds", NOTSPECIFIED, "", ""},
00132 {ARG_STRING, "in", NOTSPECIFIED, "", ""},
00133 {ARG_STRING, "sel", NOTSPECIFIED, "", ""},
00134 {ARG_STRING, "note", NOTSPECIFIED, "", ""},
00135 {ARG_INT, "perm", "1", "", ""},
00136 {ARG_FLAG, "c", "0", "", ""},
00137 {ARG_FLAG, "h", "0", "", ""},
00138 {ARG_FLAG, "v", "0", "", ""},
00139 {ARG_END}
00140 };
00141
00142
00143 char *module_name = "store_file";
00144
00145
00146 int verbose = 0;
00147
00148
00149
00150 int nice_intro () {
00151 int usage = cmdparams_get_int (&cmdparams, "h", NULL);
00152 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00153 if (usage) {
00154 printf ("store_file ds=<series> {-c} {-h} {-v} in=<file> {sel=<desc>} {note=<comment>} {perm=1}\n"
00155 " -c: Quietly create series if does not exist\n"
00156 " -h: print this message and exit\n"
00157 " -v: verbose\n"
00158 "ds= - data series to store file into\n"
00159 "in= - file to store\n"
00160 "sel= - optional description for retrieval key\n"
00161 "note= - optional description for file\n"
00162 "perm=1 - optional access permissions, use 0 for private.\n");
00163 return (1);
00164 }
00165 if (verbose) cmdparams_printall (&cmdparams);
00166 return (0);
00167 }
00168
00169
00170 int DoIt (void) {
00171 int status = 0;
00172
00173 char *in, *series, *note, *filename, *sel, *rsp;
00174 DRMS_Record_t *rec, *template;
00175 int yes_create = cmdparams_get_int (&cmdparams, "c", NULL);
00176
00177 if (nice_intro())
00178 return (0);
00179
00180 series = strdup(cmdparams_get_str(&cmdparams, "ds", NULL));
00181 note = cmdparams_get_str(&cmdparams, "note", NULL);
00182 sel = cmdparams_get_str(&cmdparams, "sel", NULL);
00183 in = cmdparams_get_str(&cmdparams, "in", NULL);
00184 if (strcmp(series, NOTSPECIFIED) == 0)
00185 {
00186 fprintf(stderr, "'ds' series must be specified. Abort\n");
00187 return(1);
00188 }
00189 if (strcmp(in, NOTSPECIFIED) == 0)
00190 {
00191 fprintf(stderr, "'in' file must be specified. Abort\n");
00192 return(1);
00193 }
00194 if (strcmp(sel, NOTSPECIFIED) == 0)
00195 sel = " ";
00196 if (strcmp(note, NOTSPECIFIED) == 0)
00197 note = " ";
00198
00199
00200 if (access(in, R_OK) != 0)
00201 {
00202 printf("The requested file can not be accessed. %s\n", in);
00203 return(1);
00204 }
00205
00206
00207 filename = rindex(in, '/');
00208 if (filename)
00209 filename++;
00210 else
00211 filename = in;
00212
00213 rsp = index(series, '[');
00214 if (rsp)
00215 {
00216
00217 *rsp++ = '\0';
00218 printf("WARNING: Destination series %s was given a record spec: %s, which will be ignored.\n",series,rsp);
00219 }
00220
00221
00222 template = drms_template_record(drms_env, series, &status);
00223 if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
00224 {
00225 int user_says_yes();
00226 if (yes_create || user_says_yes(series))
00227 {
00228 int create_series(char *series);
00229 if (create_series(series))
00230 {
00231 printf("Series '%s' does not exist. Create_series failed. Give up.\n", series);
00232 return(1);
00233 }
00234 }
00235 else
00236 {
00237 printf("Series '%s' does not exist. Give up.\n", series);
00238 return(1);
00239 }
00240 }
00241 else
00242 if(status)
00243 {
00244 fprintf(stderr, "DRMS problem looking up series.\n");
00245 return(status);
00246 }
00247
00248
00249 rec = drms_create_record(drms_env, series, DRMS_PERMANENT, &status);
00250 if (!rec || status)
00251 {
00252 printf("drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
00253 return(status);
00254 }
00255 if ((status = drms_setkey_string(rec, "file", filename)))
00256 {
00257 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00258 printf("ERROR: series %s does not have a keyword named 'file'\n", series);
00259 else
00260 printf("ERROR: drms_setkey_string failed for 'file'\n");
00261 return(1);
00262 }
00263 if ((status = drms_setkey_string(rec, "sel", sel)))
00264 {
00265 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00266 printf("ERROR: series %s does not have a keyword named 'sel'\n", series);
00267 else
00268 printf("ERROR: drms_setkey_string failed for 'sel'\n");
00269 return(1);
00270 }
00271 if ((status = drms_setkey_string(rec, "note", note)))
00272 {
00273 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00274 printf("WARNING: series %s does not have a keyword named 'note'\n", series);
00275 else
00276 printf("WARNING: drms_setkey_string failed for 'note'\n");
00277 printf("Your note was not saved: %s\n",note);
00278 }
00279 if ((status = drms_segment_write_from_file(drms_segment_lookup(rec, "file_seg"), in)))
00280 {
00281 printf("drms_segment_write_file failed, status=%d\n",status);
00282 return(status);
00283 }
00284 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD)))
00285 printf("drms_close_record failed!\n");
00286
00287 return(status);
00288 }
00289
00290 int user_says_yes(char *series)
00291 {
00292 char resp[DRMS_MAXPATHLEN];
00293 printf("Series '%s' does not exist. Do you want a standard store_file series to be created?\n", series);
00294 printf("Answer yes or no: ");
00295 fflush(stdout);
00296 gets(resp);
00297 return(strcasecmp(resp,"yes")==0);
00298 }
00299
00300 char * make_series_jsd(char *series)
00301 {
00302 char *user = getenv("USER");
00303 char jsd[1024];
00304 sprintf(jsd,
00305 "Seriesname: %s\n"
00306 "Author: %s\n"
00307 "Owners: %s\n"
00308 "Unitsize: 1\n"
00309 "Archive: 1\n"
00310 "Retention: 7\n"
00311 "Tapegroup: 1\n"
00312 "Index: file,sel\n"
00313 "Description: \"File storage\"\n"
00314 "Keyword: file, string, variable, record, \" \", %%s, none, \"Original filename\"\n"
00315 "Keyword: sel, string, variable, record, \" \", %%s, none, \"Version\"\n"
00316 "Keyword: note, string, variable, record, \" \", %%s, none, \" \"\n"
00317 "Data: file_seg, variable, int, 0, none, generic, \" \"\n",
00318 series, user, user);
00319 return (strdup(jsd));
00320 }
00321
00322 int create_series(char *series)
00323 {
00324 char *jsd;
00325 int perm = cmdparams_get_int(&cmdparams, "perm", NULL);
00326 DRMS_Record_t *template;
00327 jsd = make_series_jsd(series);
00328 template = drms_parse_description(drms_env, jsd);
00329 if (template==NULL)
00330 {
00331 fprintf(stderr, "Failed to parse series description. JSD was:\n%s\n",jsd);
00332 return(1);
00333 }
00334 if (drms_create_series(template, perm))
00335 {
00336 fprintf(stderr, "Failed to create series. JSD was:\n%s\n", jsd);
00337 return(1);
00338 }
00339 if (verbose)
00340 printf("Create_series successful, JSD was:\n%s\n", jsd);
00341 drms_free_record_struct(template);
00342 free(template);
00343 free(jsd);
00344 return(0);
00345 }
00346