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
00109 #include "jsoc_main.h"
00110 #include "drms.h"
00111 #include "drms_names.h"
00112 #include <unistd.h>
00113 #include <strings.h>
00114
00115 #define NOTSPECIFIED "***NOTSPECIFIED***"
00116
00117 ModuleArgs_t module_args[] = {
00118 {ARG_STRING, "series", "", "Series name to store the dir into"},
00119 {ARG_STRING, "dirname", "", "Dir to store. prime key"},
00120 {ARG_STRING, "sel", "", "selection name. prime key"},
00121 {ARG_STRING, "note", "N/A", "comment field"},
00122 {ARG_FLAG, "c", "0", "create new series if needed"},
00123 {ARG_FLAG, "v", "0", "verbose flag"},
00124 {ARG_END}
00125 };
00126
00127
00128 char *module_name = "store_dir";
00129
00130
00131 int verbose = 0;
00132
00133
00134 int nice_intro () {
00135 verbose = cmdparams_get_int (&cmdparams, "v", NULL);
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 if (verbose) cmdparams_printall (&cmdparams);
00151 return(0);
00152 }
00153
00154
00155 int DoIt (void) {
00156 int status = 0;
00157
00158 const char *in, *series, *note, *sel;
00159 char *dirname, *rsp;
00160 char path[DRMS_MAXPATHLEN];
00161 char cmd[DRMS_MAXPATHLEN+1024];
00162 DRMS_Record_t *rec, *template;
00163 int yes_create = cmdparams_get_int(&cmdparams, "c", NULL);
00164
00165 if (nice_intro())
00166 return(0);
00167
00168
00169 series = cmdparams_get_str(&cmdparams, "series", NULL);
00170 note = cmdparams_get_str(&cmdparams, "note", NULL);
00171 sel = cmdparams_get_str(&cmdparams, "sel", NULL);
00172 in = cmdparams_get_str(&cmdparams, "dirname", NULL);
00173
00174 if (access(in, R_OK) != 0)
00175 {
00176 printf("The requested dir can not be accessed. %s\n", in);
00177 return(1);
00178 }
00179 dirname = (char *)in;
00180
00181
00182 rsp = index(series, '[');
00183 if (rsp)
00184 {
00185
00186 *rsp++ = '\0';
00187 printf("WARNING: Destination series %s was given a record spec: %s, which will be ignored.\n",series,rsp);
00188 }
00189
00190
00191 template = drms_template_record(drms_env, series, &status);
00192 if (template==NULL && status == DRMS_ERROR_UNKNOWNSERIES)
00193 {
00194 int user_says_yes();
00195 if (yes_create || user_says_yes(series))
00196 {
00197 int create_series(const char *series);
00198 if (create_series(series))
00199 {
00200 printf("Series '%s' does not exist. Create_series failed. Give up.\n", series);
00201 return(1);
00202 }
00203 }
00204 else
00205 {
00206 printf("Series '%s' does not exist. Give up.\n", series);
00207 return(1);
00208 }
00209 }
00210 else
00211 if(status)
00212 {
00213 fprintf(stderr, "DRMS problem looking up series.\n");
00214 return(status);
00215 }
00216
00217
00218 rec = drms_create_record(drms_env, (char *)series, DRMS_PERMANENT, &status);
00219 if (!rec || status)
00220 {
00221 printf("drms_create_record failed, series=%s, status=%d. Aborting.\n", series,status);
00222 return(status);
00223 }
00224 if ((status = drms_setkey_string(rec, "dirname", dirname)))
00225 {
00226 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00227 printf("ERROR: series %s does not have a keyword named 'dirname'\n", series);
00228 else
00229 printf("ERROR: drms_setkey_string failed for 'dirname'\n");
00230 return(1);
00231 }
00232 if ((status = drms_setkey_string(rec, "sel", sel)))
00233 {
00234 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00235 printf("ERROR: series %s does not have a keyword named 'sel'\n", series);
00236 else
00237 printf("ERROR: drms_setkey_string failed for 'sel'\n");
00238 return(1);
00239 }
00240 if ((status = drms_setkey_string(rec, "note", note)))
00241 {
00242 if (status == DRMS_ERROR_UNKNOWNKEYWORD)
00243 printf("WARNING: series %s does not have a keyword named 'note'\n", series);
00244 else
00245 printf("WARNING: drms_setkey_string failed for 'note'\n");
00246 printf("Your note was not saved: %s\n",note);
00247 }
00248 drms_setkey_time(rec, "DATE", time(0) + UNIX_EPOCH);
00249
00250
00251 drms_record_directory(rec, path, 1);
00252 if (! *path)
00253 {
00254 fprintf(stderr,"no path found\n");
00255 return(1);
00256 }
00257 sprintf(cmd, "cp -rp %s %s", in, path);
00258 status = system(cmd);
00259 if (status)
00260 {
00261 printf("WARNING: Copy failed: %s, Your data is NOT stored in SUMS\n", cmd);
00262 return(status);
00263 }
00264 else
00265 printf("Command success: %s\n", cmd);
00266 if ((status = drms_close_record(rec, DRMS_INSERT_RECORD)))
00267 printf("drms_close_record failed!\n");
00268
00269 return(status);
00270 }
00271
00272 int user_says_yes(const char *series)
00273 {
00274 char resp[DRMS_MAXPATHLEN];
00275 printf("Series '%s' does not exist. Do you want a standard store_dir series to be created?\n", series);
00276 printf("Answer yes or no: ");
00277 fflush(stdout);
00278 gets(resp);
00279 return(strcasecmp(resp,"yes")==0);
00280 }
00281
00282 char * make_series_jsd(const char *series)
00283 {
00284 char *user = getenv("USER");
00285 char jsd[1024];
00286 sprintf(jsd,
00287 "Seriesname: %s\n"
00288 "Author: %s\n"
00289 "Owners: %s\n"
00290 "Unitsize: 1\n"
00291 "Archive: 1\n"
00292 "Retention: 7\n"
00293 "Tapegroup: 1\n"
00294 "Index: dirname,sel\n"
00295 "Description: \"Dir storage\"\n"
00296 "Keyword: dirname, string, variable, record, \" \", %%s, none, \"Original dirname, source of files stored\"\n"
00297 "Keyword: sel, string, variable, record, \" \", %%s, none, \"Version for dirname save\"\n"
00298 "Keyword: DATE, time, variable, record, \"JD_0\", 0, \"UTC\", \"Time of store_dir call\"\n" "Keyword: note, string, variable, record, \" \", %%s, none, \"Optional deacription\"\n"
00299 "Data: dir, variable, int, 0, none, generic, \"Stored files\"\n",
00300 series, user, user);
00301 return (strdup(jsd));
00302 }
00303
00304 int create_series(const char *series)
00305 {
00306 char *jsd;
00307 int perm = 1;
00308 DRMS_Record_t *template;
00309 jsd = make_series_jsd(series);
00310 template = drms_parse_description(drms_env, jsd);
00311 if (template==NULL)
00312 {
00313 fprintf(stderr, "Failed to parse series description. JSD was:\n%s\n",jsd);
00314 return(1);
00315 }
00316 if (drms_create_series(template, perm))
00317 {
00318 fprintf(stderr, "Failed to create series. JSD was:\n%s\n", jsd);
00319 return(1);
00320 }
00321 if (verbose)
00322 printf("Create_series successful, JSD was:\n%s\n", jsd);
00323 drms_free_record_struct(template);
00324 free(template);
00325 free(jsd);
00326 return(0);
00327 }
00328