~ubuntu-branches/ubuntu/trusty/sblim-sfcb/trusty-proposed

« back to all changes in this revision

Viewing changes to sfcbinst2mof.c

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-06-08 12:04:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090608120449-byfplk09rqz8rtg6
Tags: 1.3.3-0ubuntu1
* New upstream release.
* debian/rules: Removed rpath hacks, SFCB default build handles that now.
* Removed 1934753-remove-assignment.diff, now upstream.
* Refreshed patch cim-schema-location.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string.h>
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
 
 
5
#include "cmpidt.h"
 
6
#include "cmpimacs.h"
 
7
#include "fileRepository.h"
 
8
#include "instance.h"
 
9
#include "objectImpl.h"
 
10
#include "control.h"
 
11
#include "array.h"
 
12
 
 
13
#define VERSION "0.8.0"
 
14
 
 
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;}
 
28
 
 
29
 
 
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,
 
33
                                     CMPIStatus * rc);
 
34
 
 
35
static char *datatypeToString(CMPIData * d, int *isarray)
 
36
{
 
37
   if (isarray != NULL)
 
38
       *isarray = (d->type & CMPI_ARRAY);
 
39
   
 
40
   CMPIType type = d->type & ~CMPI_ARRAY;
 
41
 
 
42
   switch (type) {
 
43
       case CMPI_chars:
 
44
       case CMPI_string:
 
45
          return "string";
 
46
       case CMPI_real64:
 
47
          return "real64";
 
48
       case CMPI_real32:
 
49
          return "real32";
 
50
       case CMPI_sint64:
 
51
          return "sint64";
 
52
       case CMPI_uint64:
 
53
          return "uint64";
 
54
       case CMPI_sint32:
 
55
          return "sint32";
 
56
       case CMPI_uint32:
 
57
          return "uint32";
 
58
       case CMPI_sint16:
 
59
          return "sint16";
 
60
       case CMPI_uint16:
 
61
          return "uint16";
 
62
       case CMPI_uint8:
 
63
          return "uint8";
 
64
       case CMPI_sint8:
 
65
          return "sint8";
 
66
       case CMPI_boolean:
 
67
          return "boolean";
 
68
       case CMPI_char16:
 
69
          return "char16";
 
70
       case CMPI_ref:
 
71
          return "ref";
 
72
       case CMPI_dateTime:
 
73
          return "dateTime";
 
74
       default:
 
75
          dbg(printf("unknown type: %04x : %d\n", type, type));
 
76
          return "unknownType";
 
77
   }
 
78
}
 
79
 
 
80
char *escapeQuotes(char *in)
 
81
{
 
82
   int i, l, o;
 
83
   char *out;
 
84
 
 
85
   if (in == NULL)
 
86
       return (NULL);
 
87
   l = strlen(in);
 
88
   out = (char *) malloc((l * 2) + 1); // worst case scenario - it's all quotes
 
89
 
 
90
   for (i = 0, o = 0; i < l; i++) {
 
91
       if (in[i] == '\"') {
 
92
           out[o++]='\\';
 
93
           out[o++]='\"';
 
94
       }
 
95
       else {
 
96
           out[o++] = in[i];
 
97
       }
 
98
   }
 
99
   out[o] = '\0';
 
100
   return out;
 
101
}
 
102
 
 
103
UtilStringBuffer * dataValueToStringBuf(CMPIData d, CMPIInstance *inst)
 
104
{
 
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
 
108
   char *sp = str;
 
109
   int needsQuotes = 0;
 
110
   int needsFree = 0;
 
111
 
 
112
   if (d.type & CMPI_ARRAY) 
 
113
   {
 
114
      sb->ft->appendChars(sb, "{");
 
115
      struct native_array * na = (struct native_array *)d.value.array;
 
116
     
 
117
      int i = 0; 
 
118
      for (i=0; i<na->size; i++)
 
119
      {
 
120
          if (i!=0)
 
121
              sb->ft->appendChars(sb,",");
 
122
          CMPIData dd;
 
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));
 
128
      }
 
129
      sb->ft->appendChars(sb, "}");
 
130
      return sb;
 
131
   }
 
132
 
 
133
   if((d.type & (CMPI_UINT|CMPI_SINT)) == CMPI_UINT) {
 
134
      unsigned long long ul = 0LL;
 
135
      switch (d.type) {
 
136
      case CMPI_uint8:
 
137
         ul = d.value.uint8;
 
138
         break;
 
139
      case CMPI_uint16:
 
140
         ul = d.value.uint16;
 
141
         break;
 
142
      case CMPI_uint32:
 
143
         ul = d.value.uint32;
 
144
         break;
 
145
      case CMPI_uint64:
 
146
         ul = d.value.uint64;
 
147
         break;
 
148
      }
 
149
      sprintf(str, "%llu", ul);
 
150
   }
 
151
   else if((d.type & (CMPI_UINT|CMPI_SINT)) == CMPI_SINT) {
 
152
      long long sl = 0LL;
 
153
      switch (d.type) {
 
154
      case CMPI_sint8:
 
155
         sl = d.value.sint8;
 
156
         break;
 
157
      case CMPI_sint16:
 
158
         sl = d.value.sint16;
 
159
         break;
 
160
      case CMPI_sint32:
 
161
         sl = d.value.sint32;
 
162
         break;
 
163
      case CMPI_sint64:
 
164
         sl = d.value.sint64;
 
165
         break;
 
166
      }
 
167
      sprintf(str, "%lld", sl);
 
168
   }
 
169
   else if (d.type & CMPI_REAL) {
 
170
      switch (d.type) {
 
171
      case CMPI_real32:
 
172
         sprintf(str, "%.7e", d.value.real32);
 
173
         break;
 
174
      case CMPI_real64:
 
175
         sprintf(str, "%.16e", d.value.real64);
 
176
         break;
 
177
      }
 
178
   }
 
179
   else if (d.type == CMPI_boolean) {
 
180
      sprintf(str, "%s", d.value.boolean ? "TRUE" : "FALSE");
 
181
   }
 
182
   else if (d.type == CMPI_char16) {
 
183
      sprintf(str, "%c", d.value.char16);
 
184
   }
 
185
   else if (d.type == CMPI_chars) {
 
186
      needsQuotes = 1;
 
187
      sp = (char *) d.value.string->hdl;
 
188
   }
 
189
   else if (d.type == CMPI_string) {
 
190
      needsQuotes = 1;
 
191
      sp = (char *) d.value.string->hdl;
 
192
   }
 
193
   else if (d.type == CMPI_dateTime) {
 
194
      if (!(d.state & CMPI_nullValue)) {
 
195
          needsQuotes = 1;
 
196
          dateTime2chars(d.value.dateTime, NULL, str);
 
197
      }
 
198
   }
 
199
   else if (d.type == CMPI_ref) {
 
200
      needsQuotes = 1;
 
201
      sfcb_pathToChars(d.value.ref, NULL, str);
 
202
   }
 
203
   else if (d.type == CMPI_instance) {
 
204
      needsQuotes = 1;
 
205
      sp = (char *) d.value.string->hdl;
 
206
   }
 
207
   else {
 
208
      sp = "Unknown type";
 
209
   }
 
210
 
 
211
   if (needsQuotes)
 
212
   {
 
213
      //also needs escaping
 
214
      sp=escapeQuotes(sp);
 
215
      needsFree = 1;
 
216
   }
 
217
   if (needsQuotes)
 
218
      sb->ft->appendChars(sb, "\"");
 
219
   sb->ft->appendChars(sb, sp);
 
220
   if (needsQuotes)
 
221
      sb->ft->appendChars(sb, "\"");
 
222
   if (needsFree)
 
223
      free(sp);
 
224
   return sb;
 
225
}
 
226
 
 
227
 
 
228
UtilStringBuffer *instanceToMofWithType(CMPIInstance * ci, int withType)
 
229
{
 
230
   unsigned int i, m;
 
231
   CMPIString *name;
 
232
   CMPIStatus rc = {0, NULL};
 
233
   UtilStringBuffer *sb = UtilFactory->newStrinBuffer(64);
 
234
   ClInstance *cli = (ClInstance *)ci->hdl;
 
235
   if (!cli)
 
236
       return NULL;
 
237
 
 
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++)
 
242
   {
 
243
       CMPIData d = __ift_getPropertyAt(ci, i, &name, &rc);
 
244
       if (rc.rc == CMPI_RC_OK)
 
245
       {
 
246
           if (!(d.state & CMPI_nullValue))
 
247
           {
 
248
                SFCB_APPENDCHARS_BLOCK(sb, "   ");
 
249
                if (withType)
 
250
                {
 
251
                    int isarray = 0;
 
252
                    sb->ft->appendChars(sb, datatypeToString(&d, &isarray));
 
253
                    if (isarray)
 
254
                        SFCB_APPENDCHARS_BLOCK(sb, "[]");
 
255
                    SFCB_APPENDCHARS_BLOCK(sb, " ");
 
256
                }
 
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");
 
262
           }
 
263
       }
 
264
   }
 
265
   
 
266
   SFCB_APPENDCHARS_BLOCK(sb, "};\n");
 
267
   return sb;
 
268
}
 
269
 
 
270
 
 
271
UtilStringBuffer *instanceToMof(CMPIInstance * ci)
 
272
{
 
273
    return instanceToMofWithType(ci, 0);
 
274
}
 
275
 
 
276
 
 
277
static CMPIInstance *instifyBlob(void * blob) {
 
278
    CMPIInstance *inst;
 
279
    int id;
 
280
 
 
281
    if (blob==NULL) {
 
282
        return NULL;
 
283
    }
 
284
    else {
 
285
        inst=relocateSerializedInstance(blob);
 
286
        memAdd(blob, &id);
 
287
        return inst;
 
288
    }
 
289
}
 
290
 
 
291
 
 
292
static BlobIndex *_getIndex(const char *ns, const char *cn)
 
293
{
 
294
    BlobIndex *bi;
 
295
    if (getIndex(ns,cn,strlen(ns)+strlen(cn)+64,0,&bi))
 
296
        return bi;
 
297
    else return NULL;
 
298
}
 
299
 
 
300
 
 
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);
 
304
}
 
305
 
 
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);
 
309
}
 
310
 
 
311
 
 
312
 
 
313
static int parse_options(int argc, char * argv[])
 
314
{
 
315
    int opt;
 
316
    while ((opt=getopt(argc,argv,valid_options)) != -1) 
 
317
    {
 
318
        switch (opt) 
 
319
        {
 
320
            case 'n': // namespace, required
 
321
                strncpy(namespace,optarg,sizeof(namespace));
 
322
                break;
 
323
            case 'c': // classname, required
 
324
                strncpy(classname,optarg,sizeof(classname));
 
325
                break;
 
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));
 
329
                break;
 
330
            case 'o':
 
331
                strncpy(outfilepath,optarg,sizeof(outfilepath));
 
332
                break;
 
333
            case 'r':
 
334
                strncpy(altRepositoryDir,optarg,sizeof(altRepositoryDir));
 
335
                break;
 
336
            case 't': // default is append, not truncate
 
337
                opt_truncate = 1;
 
338
                break;
 
339
            case 'm':
 
340
                opt_from_mof = 1;
 
341
                break;
 
342
            case 'M':
 
343
                opt_only_from_mof = 1;
 
344
                break;
 
345
            case 'v':
 
346
                opt_verbose = 1;
 
347
                break;
 
348
            case 'V':
 
349
                opt_version = 1;
 
350
                break;
 
351
            case 'h':
 
352
                opt_help = 1;
 
353
                break;
 
354
            default:
 
355
                return -1;
 
356
        }
 
357
    }
 
358
    return optind;
 
359
}
 
360
 
 
361
 
 
362
static void version(char *name)
 
363
{
 
364
    printf("%s - Version %s  -  Dump static instances to mof\n", name, VERSION);
 
365
}
 
366
 
 
367
static void usage(char *name)
 
368
{
 
369
    printf("%s <options> [-n namespace] [-c classname]\n", name);
 
370
}
 
371
 
 
372
static void help(char *name)
 
373
{
 
374
    usage(name);
 
375
    version(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);
 
388
}
 
389
 
 
390
 
 
391
int main(int argc, char *argv[])
 
392
{
 
393
    int argidx;
 
394
    if ((argidx=parse_options(argc,argv)) == -1) 
 
395
    {
 
396
        help(argv[0]);
 
397
        return 1;
 
398
    }
 
399
    if (opt_version) 
 
400
    {
 
401
        version(argv[0]);
 
402
        return 0;
 
403
    }
 
404
    if (opt_help) 
 
405
    {
 
406
        help(argv[0]);
 
407
        return 0;
 
408
    }
 
409
    if (opt_verbose) 
 
410
    {
 
411
        version(argv[0]);
 
412
        printf("Parsing %s for instances, output to %s\n", classname, *outfilepath?outfilepath:"stdout");
 
413
        if (opt_only_from_mof) 
 
414
        {
 
415
            printf("  Dumping only instances created in MOF\n");
 
416
        }
 
417
        else if (opt_from_mof)
 
418
        {
 
419
            printf("  Dump includes instances created from MOF\n");
 
420
        }
 
421
        else
 
422
        {
 
423
            printf("  Dumping only instances not created from MOF\n");
 
424
        }
 
425
    }
 
426
 
 
427
    if ( *classname==0 || *namespace==0)
 
428
    {
 
429
        printf("--> You must provide a namespace and a classname\n");
 
430
        help(argv[0]);
 
431
        return 0;
 
432
    }
 
433
 
 
434
 
 
435
    // now let's get to work
 
436
    char *ns = namespace;
 
437
    char *clsname = classname;
 
438
    char *cfg = sfcbcfg;
 
439
 
 
440
    char *msg = NULL;
 
441
    char *kp = NULL;
 
442
    size_t ekl = 0;
 
443
    CMPIObjectPath *cop = NULL;
 
444
    CMPIInstance *inst = NULL;
 
445
    char copKey[8192]={0};
 
446
    BlobIndex *bi = NULL;
 
447
   
 
448
    setupControl(cfg);
 
449
    if (*altRepositoryDir)
 
450
    {
 
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));
 
457
    }
 
458
    
 
459
    dbg(printf("> NS: %s\n", ns));
 
460
    dbg(printf("> CN: %s\n", clsname));
 
461
 
 
462
    if ((bi=_getIndex(ns,clsname))!=NULL) 
 
463
    {
 
464
        inst = ipGetFirst(bi,NULL,&kp,&ekl);
 
465
        if (inst) 
 
466
        {
 
467
            FILE *fp = NULL;
 
468
            if (*outfilepath)
 
469
            {
 
470
                if (opt_truncate)
 
471
                    fp = fopen(outfilepath, "w");
 
472
                else
 
473
                    fp = fopen(outfilepath, "a");
 
474
            }
 
475
 
 
476
            while(1) 
 
477
            {
 
478
                dbg(printf("> got instance\n"));
 
479
                
 
480
                strcpy(copKey,ns);
 
481
                strcat(copKey,":");
 
482
                strcat(copKey,clsname);
 
483
                strcat(copKey,".");
 
484
                strncat(copKey,kp,ekl);
 
485
 
 
486
                cop=(CMPIObjectPath *)getObjectPath(copKey,&msg);
 
487
                if (msg)
 
488
                    dbg(printf(" ! got error msg getting cop: %s\n", msg));
 
489
                if (cop)
 
490
                {
 
491
                    int fromMof = 0;
 
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)
 
497
                    {
 
498
                        fromMof = 1;
 
499
                        dbg(printf("> > Instance is from Mof\n"));
 
500
                    }
 
501
                    else
 
502
                    {
 
503
                        fromMof = 0;
 
504
                        dbg(printf("> > Instance is Not from Mof\n"));
 
505
                    }
 
506
                    
 
507
                    if ((!fromMof && (!opt_only_from_mof)) || 
 
508
                        (fromMof && (opt_from_mof || opt_only_from_mof)))
 
509
                    {
 
510
                        dbg(printf("> > Including instance in output.\n"));
 
511
                        UtilStringBuffer *sb = instanceToMof(inst);
 
512
                        
 
513
                        char *str=(char *)sb->ft->getCharPtr(sb);
 
514
                        if (fp)
 
515
                        {
 
516
                            fputs((const char *)str, fp);
 
517
                            fputs("\n\n", fp);
 
518
                        }
 
519
                        else
 
520
                        {
 
521
                            printf("%s",(const char *) str);
 
522
                            printf("\n\n");
 
523
                        }
 
524
                    }
 
525
                }
 
526
                else 
 
527
                {
 
528
                    return -1;
 
529
                }
 
530
                if ((bi->next < bi->dSize) && (inst=ipGetNext(bi,NULL,&kp,&ekl)))
 
531
                {
 
532
                    continue;
 
533
                }
 
534
                break;
 
535
            }
 
536
            if (fp)
 
537
                fclose(fp);
 
538
        }
 
539
    }
 
540
    freeBlobIndex(&bi,1);
 
541
    
 
542
    return 0;
 
543
}