00001
00002
00003
00004 #include "jsoc_main.h"
00005
00006
00007 char *module_name = "drms_addkeys";
00008
00009 typedef enum
00010 {
00011 kAkErr_Success = 0,
00012 kAkErr_FileIO,
00013 kAkErr_NoMemory,
00014 kAkErr_CantModSeries,
00015 kAkErr_AddKeys
00016 } AkError_t;
00017
00018 #define kSeries "series"
00019 #define kSpec "spec"
00020
00021 ModuleArgs_t module_args[] =
00022 {
00023 {ARG_STRING, kSeries, NULL, "Series to add keywords to."},
00024 {ARG_STRING, kSpec, NULL, "Keyword specification(s). This can be a JSD keyword description, or a path to a file that contains JSD keyword descriptions, one per line."},
00025 {ARG_END}
00026 };
00027
00028
00029 int DoIt(void)
00030 {
00031 AkError_t rv = kAkErr_Success;
00032
00033 const char *series = NULL;
00034 const char *spec = NULL;
00035 struct stat stBuf;
00036 FILE *fptr = NULL;
00037 char *specin = NULL;
00038
00039 series = cmdparams_get_str(&cmdparams, kSeries, NULL);
00040 spec = cmdparams_get_str(&cmdparams, kSpec, NULL);
00041
00042 if (!stat(spec, &stBuf) && S_ISREG(stBuf.st_mode))
00043 {
00044
00045 char rbuf[512];
00046 size_t nbytes;
00047 size_t ntot = 0;
00048 size_t szspec;
00049
00050 fptr = fopen(spec, "r");
00051 if (fptr)
00052 {
00053 szspec = 1024;
00054 specin = calloc(1, szspec);
00055
00056 if (specin)
00057 {
00058 while ((nbytes = fread(rbuf, sizeof(char), sizeof(rbuf) - 1, fptr)) > 0)
00059 {
00060 rbuf[nbytes] = '\0';
00061 ntot += nbytes;
00062 specin = base_strcatalloc(specin, rbuf, &szspec);
00063 }
00064
00065 if (ntot != stBuf.st_size)
00066 {
00067 fprintf(stderr, "Error reading file %s.\n", spec);
00068 rv = kAkErr_FileIO;
00069 }
00070 }
00071 else
00072 {
00073 rv = kAkErr_NoMemory;
00074 }
00075
00076 fclose(fptr);
00077 }
00078 else
00079 {
00080 fprintf(stderr, "Unable to open file %s for reading.\n", spec);
00081 rv = kAkErr_FileIO;
00082 }
00083 }
00084 else
00085 {
00086
00087 specin = strdup(spec);
00088 }
00089
00090 if (rv == kAkErr_Success)
00091 {
00092
00093
00094 char *sql = NULL;
00095 int ret = drms_addkeys_toseries(drms_env, series, specin, &sql);
00096
00097 if (ret == DRMS_ERROR_CANTMODPUBSERIES)
00098 {
00099 fprintf(stderr, "Unable to modify replicated series.\n");
00100
00101 if (sql)
00102 {
00103 fprintf(stderr, "SQL to issue with slonik to add columns to %s:\n\n", series);
00104 fprintf(stderr, "%s", sql);
00105 }
00106
00107 rv = kAkErr_CantModSeries;
00108 }
00109 else if (ret != DRMS_SUCCESS)
00110 {
00111 rv = kAkErr_AddKeys;
00112 }
00113
00114 if (sql)
00115 {
00116 free(sql);
00117 }
00118 }
00119
00120 if (specin)
00121 {
00122 free(specin);
00123 }
00124
00125 return rv;
00126 }