00001
00049 #include "drms.h"
00050 #include "jsoc_main.h"
00051
00052
00053 ModuleArgs_t module_args[] = {
00054 {ARG_END}
00055 };
00056
00057 char *module_name = "create_series";
00058 int DoIt(void) {
00059 int force, len, perms;
00060 const char *filename;
00061 FILE *fp;
00062 char *buf;
00063 const char *series = NULL;
00064 struct stat file_stat;
00065 DRMS_Record_t *template;
00066 char yesno[10];
00067 int usestdin = 0;
00068
00069
00070 if (cmdparams_numargs (&cmdparams) < 1)
00071 {
00072 fprintf(stderr, "Insufficient number of args.\n");
00073 goto usage;
00074 }
00075
00076 force = cmdparams_exists(&cmdparams, "f");
00077 usestdin = cmdparams_isflagset(&cmdparams, "i");
00078
00079
00080
00081
00082 perms = DB_PRIV_SELECT;
00083
00084 filename = cmdparams_getarg(&cmdparams, 1);
00085
00086 if (filename)
00087 {
00088
00089 if (stat(filename, &file_stat))
00090 {
00091 printf("Can't stat file %s\n", filename);
00092 return 1;
00093 }
00094 buf = (char *)malloc( file_stat.st_size+1 );
00095 XASSERT(buf);
00096 fp = fopen(filename,"r");
00097 fread(buf,file_stat.st_size,1,fp);
00098 buf[file_stat.st_size] = 0;
00099 }
00100 else if (usestdin)
00101 {
00102 char bufin[80];
00103 char *bufout = NULL;
00104 size_t szbuf = 1024;
00105
00106 bufout = malloc(szbuf * sizeof(char));
00107 *bufout = '\0';
00108
00109 if (!bufout)
00110 {
00111 fprintf(stderr, "No memory!\n");
00112 goto bailout;
00113 }
00114
00115 while (fgets(bufin, sizeof(bufin), stdin) != NULL)
00116 {
00117 bufout = base_strcatalloc(bufout, bufin, &szbuf);
00118 }
00119
00120 buf = bufout;
00121 }
00122 else
00123 {
00124
00125 fprintf(stderr, "No jsd provided?\n");
00126 goto usage;
00127 }
00128
00129
00130 template = drms_parse_description(drms_env, buf);
00131 free(buf);
00132 if (template==NULL)
00133 {
00134 if (!usestdin)
00135 {
00136 printf("Failed to parse series description in file '%s'.\n",filename);
00137 }
00138 else
00139 {
00140 printf("Failed to parse series description from stdin.\n");
00141 }
00142 fflush(stdout);
00143 fflush(stderr);
00144 return 1;
00145 }
00146
00147 if (series = cmdparams_get_str(&cmdparams, "name", NULL)) {
00148 strcpy(template->seriesinfo->seriesname, series);
00149 } else {
00150 if (!strncmp(template->seriesinfo->seriesname, "XXX", 3)) {
00151 fprintf(stderr, "This JSD requires user supplied series name.\n");
00152 goto usage;
00153 }
00154 series = template->seriesinfo->seriesname;
00155 }
00156
00157 if (force) {
00158 int status;
00159
00160 printf ("You are about to permanently erase all metadata for the series "
00161 "'%s'.\nAre you sure you want to do this (yes/no)? ",series);
00162 fgets (yesno, 10, stdin);
00163 len = strlen (yesno);
00164 if (yesno[len-1] == '\n') yesno[len-1] = 0;
00165 if (strcmp(yesno,"yes")) goto bailout;
00166
00167 printf ("Removing existing series '%s'...\n", series);
00168 status = drms_delete_series(drms_env, series, 1, 0);
00169 if (status) {
00170 if (status != DRMS_ERROR_UNKNOWNSERIES) {
00171 goto bailout;
00172 }
00173 }
00174 }
00175
00176
00177
00178 printf ("Creating new series '%s'...\n",series);
00179 if (drms_create_series (template, perms)) {
00180 printf ("Failed to create series.\n");
00181 goto bailout;
00182 }
00183 drms_free_record_struct (template);
00184 free (template);
00185
00186 fflush(stdout);
00187 fflush(stderr);
00188 return 0;
00189 bailout:
00190 fflush(stdout);
00191 fflush(stderr);
00192 return 1;
00193 usage:
00194 printf ("Usage: %s [-f] [perm=[s|i|u]] [name=seriesname] (file.jsd | -i)\n",
00195 cmdparams_getarg (&cmdparams, 0));
00196 fflush(stdout);
00197 fflush(stderr);
00198 return 1;
00199 }