7
#include "fileRepository.h"
9
#include "objectImpl.h"
13
#define VERSION "0.8.0"
15
static int opt_verbose = 0;
16
static int opt_version = 0;
17
static int opt_help = 0;
18
static int opt_from_mof = 0;
19
static int opt_only_from_mof = 0;
20
static int opt_truncate = 0;
21
static char outfilepath[3000] = {0};
22
static char sfcbcfg[1024] = SFCB_CONFDIR"/sfcb.cfg";
23
static char namespace[600] = {0};
24
static char classname[600] = {0};
25
static char altRepositoryDir[1024] = {0};
26
static char * valid_options = "n:c:g:o:r:tphvVmM";
27
#define dbg(x) if(opt_verbose) {x;}
30
extern char *sfcb_pathToChars(CMPIObjectPath * cop, CMPIStatus * rc, char *str);
31
extern CMPIObjectPath *getObjectPath(char *path, char **msg);
32
extern CMPIData __ift_getPropertyAt(const CMPIInstance * ci, CMPICount i, CMPIString ** name,
35
static char *datatypeToString(CMPIData * d, int *isarray)
38
*isarray = (d->type & CMPI_ARRAY);
40
CMPIType type = d->type & ~CMPI_ARRAY;
75
dbg(printf("unknown type: %04x : %d\n", type, type));
80
char *escapeQuotes(char *in)
88
out = (char *) malloc((l * 2) + 1); // worst case scenario - it's all quotes
90
for (i = 0, o = 0; i < l; i++) {
103
UtilStringBuffer * dataValueToStringBuf(CMPIData d, CMPIInstance *inst)
105
UtilStringBuffer *sb = UtilFactory->newStrinBuffer(64);
106
char str[256] = {0};// for string, we'll redirect the sp pointer,
107
// all other types should fit in this char-array
112
if (d.type & CMPI_ARRAY)
114
sb->ft->appendChars(sb, "{");
115
struct native_array * na = (struct native_array *)d.value.array;
118
for (i=0; i<na->size; i++)
121
sb->ft->appendChars(sb,",");
123
dd.type = d.type & ~CMPI_ARRAY;
124
dd.state= na->data[i].state;
125
dd.value = na->data[i].value;
126
UtilStringBuffer *b = dataValueToStringBuf(dd, inst);
127
sb->ft->appendChars(sb, b->ft->getCharPtr(b));
129
sb->ft->appendChars(sb, "}");
133
if((d.type & (CMPI_UINT|CMPI_SINT)) == CMPI_UINT) {
134
unsigned long long ul = 0LL;
149
sprintf(str, "%llu", ul);
151
else if((d.type & (CMPI_UINT|CMPI_SINT)) == CMPI_SINT) {
167
sprintf(str, "%lld", sl);
169
else if (d.type & CMPI_REAL) {
172
sprintf(str, "%.7e", d.value.real32);
175
sprintf(str, "%.16e", d.value.real64);
179
else if (d.type == CMPI_boolean) {
180
sprintf(str, "%s", d.value.boolean ? "TRUE" : "FALSE");
182
else if (d.type == CMPI_char16) {
183
sprintf(str, "%c", d.value.char16);
185
else if (d.type == CMPI_chars) {
187
sp = (char *) d.value.string->hdl;
189
else if (d.type == CMPI_string) {
191
sp = (char *) d.value.string->hdl;
193
else if (d.type == CMPI_dateTime) {
194
if (!(d.state & CMPI_nullValue)) {
196
dateTime2chars(d.value.dateTime, NULL, str);
199
else if (d.type == CMPI_ref) {
201
sfcb_pathToChars(d.value.ref, NULL, str);
203
else if (d.type == CMPI_instance) {
205
sp = (char *) d.value.string->hdl;
213
//also needs escaping
218
sb->ft->appendChars(sb, "\"");
219
sb->ft->appendChars(sb, sp);
221
sb->ft->appendChars(sb, "\"");
228
UtilStringBuffer *instanceToMofWithType(CMPIInstance * ci, int withType)
232
CMPIStatus rc = {0, NULL};
233
UtilStringBuffer *sb = UtilFactory->newStrinBuffer(64);
234
ClInstance *cli = (ClInstance *)ci->hdl;
238
SFCB_APPENDCHARS_BLOCK(sb, "Instance of ");
239
sb->ft->appendChars(sb, instGetClassName(ci));
240
SFCB_APPENDCHARS_BLOCK(sb, "\n{\n");
241
for (i = 0, m = ClInstanceGetPropertyCount(cli); i < m; i++)
243
CMPIData d = __ift_getPropertyAt(ci, i, &name, &rc);
244
if (rc.rc == CMPI_RC_OK)
246
if (!(d.state & CMPI_nullValue))
248
SFCB_APPENDCHARS_BLOCK(sb, " ");
252
sb->ft->appendChars(sb, datatypeToString(&d, &isarray));
254
SFCB_APPENDCHARS_BLOCK(sb, "[]");
255
SFCB_APPENDCHARS_BLOCK(sb, " ");
257
sb->ft->appendChars(sb, name->hdl);
258
SFCB_APPENDCHARS_BLOCK(sb, " = ");
259
UtilStringBuffer *buff = dataValueToStringBuf(d, ci);
260
sb->ft->appendChars(sb, buff->ft->getCharPtr(buff));
261
SFCB_APPENDCHARS_BLOCK(sb, ";\n");
266
SFCB_APPENDCHARS_BLOCK(sb, "};\n");
271
UtilStringBuffer *instanceToMof(CMPIInstance * ci)
273
return instanceToMofWithType(ci, 0);
277
static CMPIInstance *instifyBlob(void * blob) {
285
inst=relocateSerializedInstance(blob);
292
static BlobIndex *_getIndex(const char *ns, const char *cn)
295
if (getIndex(ns,cn,strlen(ns)+strlen(cn)+64,0,&bi))
301
static CMPIInstance* ipGetFirst(BlobIndex *bi, int *len, char** keyb, size_t *keybl) {
302
void *blob=getFirst(bi, len, keyb, keybl);
303
return instifyBlob(blob);
306
static CMPIInstance* ipGetNext(BlobIndex *bi, int *len, char** keyb, size_t *keybl) {
307
void *blob=getNext(bi, len, keyb, keybl);
308
return instifyBlob(blob);
313
static int parse_options(int argc, char * argv[])
316
while ((opt=getopt(argc,argv,valid_options)) != -1)
320
case 'n': // namespace, required
321
strncpy(namespace,optarg,sizeof(namespace));
323
case 'c': // classname, required
324
strncpy(classname,optarg,sizeof(classname));
326
case 'g': // sfcb configuration - default = SFCB_CONFIR = (/usr/local)/etc/sfcb/sfcb.cfg
327
// this configuration specifies the repository directory
328
strncpy(sfcbcfg,optarg,sizeof(sfcbcfg));
331
strncpy(outfilepath,optarg,sizeof(outfilepath));
334
strncpy(altRepositoryDir,optarg,sizeof(altRepositoryDir));
336
case 't': // default is append, not truncate
343
opt_only_from_mof = 1;
362
static void version(char *name)
364
printf("%s - Version %s - Dump static instances to mof\n", name, VERSION);
367
static void usage(char *name)
369
printf("%s <options> [-n namespace] [-c classname]\n", name);
372
static void help(char *name)
376
printf(" Allowed options are\n");
377
printf(" -h display this message\n");
378
printf(" -v verbose: print some extra processing information to stdout\n");
379
printf(" -V print version information\n");
380
printf(" -n <namespace> [REQUIRED]\n");
381
printf(" -c <classname> [REQUIRED] - actual class name instrumented - not a parent class\n");
382
printf(" -g <sfcb cfg file> (default=%s/sfcb.cfg)\n", SFCB_CONFDIR);
383
printf(" -o <output file> full path to output file. Path must exist, with write rights. (default = stdout)\n");
384
printf(" -t truncate output file before writing to it (default = append)\n");
385
printf(" -m output instances that were created in MOF, in addition to instances created interactively\n");
386
printf(" -M output ONLY instances that were created in MOF (don't include instances created interactively)\n");
387
printf(" -r <repository dir> use alternate repository (override sfcb cfg file). (default = %s/registration/repository)\n", SFCB_STATEDIR);
391
int main(int argc, char *argv[])
394
if ((argidx=parse_options(argc,argv)) == -1)
412
printf("Parsing %s for instances, output to %s\n", classname, *outfilepath?outfilepath:"stdout");
413
if (opt_only_from_mof)
415
printf(" Dumping only instances created in MOF\n");
417
else if (opt_from_mof)
419
printf(" Dump includes instances created from MOF\n");
423
printf(" Dumping only instances not created from MOF\n");
427
if ( *classname==0 || *namespace==0)
429
printf("--> You must provide a namespace and a classname\n");
435
// now let's get to work
436
char *ns = namespace;
437
char *clsname = classname;
443
CMPIObjectPath *cop = NULL;
444
CMPIInstance *inst = NULL;
445
char copKey[8192]={0};
446
BlobIndex *bi = NULL;
449
if (*altRepositoryDir)
451
// must have trailing '/'... if not there, add it
452
int len = strlen(altRepositoryDir);
453
if (altRepositoryDir[len-1] != '/')
454
strcat(altRepositoryDir, "/");
455
useAlternateRepository(altRepositoryDir);
456
dbg(printf("> Using alternate repository dir: %s\n", altRepositoryDir));
459
dbg(printf("> NS: %s\n", ns));
460
dbg(printf("> CN: %s\n", clsname));
462
if ((bi=_getIndex(ns,clsname))!=NULL)
464
inst = ipGetFirst(bi,NULL,&kp,&ekl);
471
fp = fopen(outfilepath, "w");
473
fp = fopen(outfilepath, "a");
478
dbg(printf("> got instance\n"));
482
strcat(copKey,clsname);
484
strncat(copKey,kp,ekl);
486
cop=(CMPIObjectPath *)getObjectPath(copKey,&msg);
488
dbg(printf(" ! got error msg getting cop: %s\n", msg));
492
char copstr[4096] = { 0 };
493
dbg(printf("> > got cop: %s\n", sfcb_pathToChars(cop, NULL, copstr)));
494
ClInstance *cli = (ClInstance *) (inst->hdl);
495
dbg(showClHdr((void *)&cli));
496
if (cli->hdr.flags & HDR_FromMof)
499
dbg(printf("> > Instance is from Mof\n"));
504
dbg(printf("> > Instance is Not from Mof\n"));
507
if ((!fromMof && (!opt_only_from_mof)) ||
508
(fromMof && (opt_from_mof || opt_only_from_mof)))
510
dbg(printf("> > Including instance in output.\n"));
511
UtilStringBuffer *sb = instanceToMof(inst);
513
char *str=(char *)sb->ft->getCharPtr(sb);
516
fputs((const char *)str, fp);
521
printf("%s",(const char *) str);
530
if ((bi->next < bi->dSize) && (inst=ipGetNext(bi,NULL,&kp,&ekl)))
540
freeBlobIndex(&bi,1);