summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5trav.c
blob: 8abdf55b476c1c12080441ca94a03c4613e1cfd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#include "h5trav.h"
#include "H5private.h"

/*-------------------------------------------------------------------------
 * local functions
 *-------------------------------------------------------------------------
 */
static int traverse( hid_t loc_id,
                     const char *group_name,
                     trav_table_t *table,
                     trav_info_t *info,
                     int *idx,
                     int print);

static hssize_t get_nnames( hid_t loc_id,
                            const char *group_name );

static herr_t get_name_type( hid_t loc_id,
                             const char *group_name,
                             int idx,
                             char **name,
                             H5G_obj_t *type );


/*-------------------------------------------------------------------------
 * "h5trav info" public functions. used in h5diff
 *-------------------------------------------------------------------------
 */

/*-------------------------------------------------------------------------
 * Function: h5trav_getinfo
 *
 * Purpose: get an array of "trav_info_t" , containing the name and type of
 *  objects in the file
 *
 * Return: number of object names in file
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: November 6, 2002
 *
 *-------------------------------------------------------------------------
 */

int h5trav_getinfo(hid_t file_id,
                   trav_info_t *info,
                   int print )
{

 trav_table_t  *table=NULL;
 int           nnames=0;

 /* init table */
 trav_table_init( &table );

 /* iterate starting on the root group */
 if (( nnames = traverse( file_id, "/", table, info, &nnames, print )) < 0 )
  return -1;

 /* free table */
 trav_table_free( table );

 return nnames;
}

/*-------------------------------------------------------------------------
 * Function: h5trav_getindex
 *
 * Purpose: get index of OBJ in list
 *
 * Return: index, -1 if not found
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: May 9, 2003
 *
 *-------------------------------------------------------------------------
 */

int h5trav_getindex( const char *obj, int nobjs, trav_info_t *info )
{
 char *pdest;
 int  result;
 int  i;

 for ( i = 0; i < nobjs; i++)
 {
  if ( strcmp(obj,info[i].name)==0 )
   return i;

  pdest  = strstr( info[i].name, obj );
  result = (int)(pdest - info[i].name);

  /* found at position 1, meaning without '/' */
  if( pdest != NULL && result==1 )
   return i;
 }
 return -1;
}

/*-------------------------------------------------------------------------
 * Function: h5trav_freeinfo
 *
 * Purpose: free info memory
 *
 *-------------------------------------------------------------------------
 */

void h5trav_freeinfo( trav_info_t *info, int nobjs )
{
 int i;
    if ( info )
    {
        for ( i = 0; i < nobjs; i++)
        {
            if (info[i].name)
            HDfree( info[i].name );
        }
        HDfree(info);
    }
}

/*-------------------------------------------------------------------------
 * Function: h5trav_printinfo
 *
 * Purpose: print list of names in file
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: May 9, 2003
 *
 *-------------------------------------------------------------------------
 */
void h5trav_printinfo(int nobjs, trav_info_t *travi)
{
 int i;
 for ( i = 0; i < nobjs; i++)
 {
  switch ( travi[i].type )
  {
  case H5G_GROUP:
   printf(" %-10s %s\n", "group", travi[i].name  );
   break;
  case H5G_DATASET:
   printf(" %-10s %s\n", "dataset", travi[i].name );
   break;
  case H5G_TYPE:
   printf(" %-10s %s\n", "datatype", travi[i].name );
   break;
  case H5G_LINK:
   printf(" %-10s %s\n", "link", travi[i].name );
   break;
  case H5G_UDLINK:
   printf(" %-10s %s\n", "User defined link", travi[i].name );
   break;
  default:
   printf(" %-10s %s\n", "User defined object", travi[i].name );
   break;
  }
 }
}

/*-------------------------------------------------------------------------
 * "h5trav table" public functions. used in h5repack
 *-------------------------------------------------------------------------
 */

/*-------------------------------------------------------------------------
 * Function: h5trav_getindext
 *
 * Purpose: get index of NAME in list
 *
 * Return: index, -1 if not found
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: December 18, 2003
 *
 *-------------------------------------------------------------------------
 */

int h5trav_getindext(const char *name, trav_table_t *table)
{
 char *pdest;
 int  result;
 unsigned int  i, j;

 for ( i = 0; i < table->nobjs; i++)
 {
  if ( strcmp(name,table->objs[i].name)==0 )
   return i;

  pdest  = strstr( table->objs[i].name, name );
  result = (int)(pdest - table->objs[i].name);

  /* found at position 1, meaning without '/' */
  if( pdest != NULL && result==1 && strlen(table->objs[i].name)-1==strlen(name))
   return i;

  /* search also in the list of links */
  if (table->objs[i].nlinks)
  {
   for ( j=0; j<table->objs[i].nlinks; j++)
   {
    if ( strcmp(name,table->objs[i].links[j].new_name)==0 )
     return i;

    pdest  = strstr( table->objs[i].links[j].new_name, name );
    result = (int)(pdest - table->objs[i].links[j].new_name);

    /* found at position 1, meaning without '/' */
    if( pdest != NULL && result==1 )
     return i;

   }
  }

 }
 return -1;
}

/*-------------------------------------------------------------------------
 * Function: h5trav_gettable
 *
 * Purpose: get the trav_table_t struct
 *
 * Return: 0, -1 on error
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: December 17, 2003
 *
 *-------------------------------------------------------------------------
 */

int h5trav_gettable(hid_t fid, trav_table_t *travt)
{
 int nnames=0;

 /* iterate starting on the root group */
 if (( nnames = traverse(fid,"/",travt,NULL,&nnames,0))<0)
  return -1;

 return 0;

}

/*-------------------------------------------------------------------------
 * Function: h5trav_printtable
 *
 * Purpose: print list of objects in file
 *
 * Return: void
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: May 9, 2003
 *
 *-------------------------------------------------------------------------
 */
void h5trav_printtable(trav_table_t *table)
{
 unsigned int i, j;

 for ( i = 0; i < table->nobjs; i++)
 {
  switch ( table->objs[i].type )
  {
  case H5G_GROUP:
   printf(" %-10s %s\n", "group", table->objs[i].name  );
   break;
  case H5G_DATASET:
   printf(" %-10s %s\n", "dataset", table->objs[i].name );
   break;
  case H5G_TYPE:
   printf(" %-10s %s\n", "datatype", table->objs[i].name );
   break;
  case H5G_LINK:
   printf(" %-10s %s\n", "link", table->objs[i].name );
   break;
  default:
   printf(" %-10s %s\n", "User defined object", table->objs[i].name );
   break;
  }

  if (table->objs[i].nlinks)
  {
   for ( j=0; j<table->objs[i].nlinks; j++)
   {
    printf(" %-10s %s\n", "    hardlink", table->objs[i].links[j].new_name );
   }
  }

 }
}


/*-------------------------------------------------------------------------
 * Function: get_nnames
 *
 * Purpose:  Counts the number of names in the group GROUP_NAME
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: October 10, 2002
 *
 * Return:
 *     Success: The return value of the first operator that
 *              returns non-zero, or zero if all members were
 *              processed with no operator returning non-zero.
 *
 *     Failure: Negative if something goes wrong within the
 *              library, or the negative value returned by one
 *              of the operators.
 *
 *-------------------------------------------------------------------------
 */

static hssize_t get_nnames( hid_t loc_id, const char *group_name )
{
    hid_t gid;
    hsize_t nobjs = 0;

    /* Open the group */
    if((gid = H5Gopen(loc_id, group_name)) < 0)
        return(-1);

    /* Retrieve the number of objects in it */
    if(H5Gget_num_objs(gid, &nobjs) < 0)
        return(-1);

    /* Close the group */
    if(H5Gclose(gid) < 0)
        return(-1);

    return((hssize_t)nobjs);
}


/*-------------------------------------------------------------------------
 * Function: opget_info
 *
 * Purpose: operator function
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: October 10, 2002
 *
 * Comments:
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */

static herr_t opget_info( hid_t loc_id, const char *name, void *op_data)
{

 H5G_stat_t statbuf;

 if (H5Gget_objinfo( loc_id, name, FALSE, &statbuf) < 0 )
  return -1;

 ((trav_info_t *)op_data)->type = statbuf.type;
 ((trav_info_t *)op_data)->name = (char *)HDstrdup(name);

 /* Define 1 for return. This will cause the iterator to stop */
 return 1;
}


/*-------------------------------------------------------------------------
 * Function: get_name_type
 *
 * Purpose:
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: October 10, 2002
 *
 * Return:
 *     Success: The return value of the first operator that
 *              returns non-zero, or zero if all members were
 *              processed with no operator returning non-zero.
 *
 *     Failure: Negative if something goes wrong within the
 *              library, or the negative value returned by one
 *              of the operators.
 *
 *-------------------------------------------------------------------------
 */

static herr_t get_name_type( hid_t loc_id,
                             const char *group_name,
                             int idx,
                             char **name,
                             H5G_obj_t *type )
{

 trav_info_t info;

 if (H5Giterate( loc_id, group_name, &idx, opget_info, (void *)&info) < 0 )
  return -1;

 *name = info.name;
 *type = info.type;

 return 0;
}

/*-------------------------------------------------------------------------
 * Function: traverse
 *
 * Purpose: recursive function that searches HDF5 objects in LOC_ID
 *
 * Return: number of objects found in LOC_ID
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: November 4, 2002
 *
 *-------------------------------------------------------------------------
 */

static int traverse( hid_t loc_id,
                     const char *group_name,
                     trav_table_t *table,
                     trav_info_t *info,
                     int *idx,
                     int print)
{
 haddr_t       objno;              /* Compact form of object's location */
 char          *name=NULL;
 H5G_obj_t     type;
 int           n_names;
 char          *path=NULL;
 H5G_stat_t    statbuf;
 int           inserted_objs=0;
 int           i, j;

 /* get the number of names */
 if (( n_names = (int)get_nnames( loc_id, group_name )) < 0 )
  return -1;

 for ( i = 0; i < n_names; i++)
 {
  if (get_name_type( loc_id, group_name, i, &name, &type ) < 0 )
   return -1;

  /* allocate path buffer */
  path = (char*) HDmalloc(HDstrlen(group_name) + HDstrlen(name) + 2);

  /* initialize path */
  HDstrcpy( path, group_name );
  if ( HDstrcmp(group_name, "/") != 0 )
   HDstrcat( path, "/" );
  HDstrcat( path, name );

  /* disable error reporting */
  H5E_BEGIN_TRY {

  /* get info */
   H5Gget_objinfo( loc_id, path, FALSE, &statbuf);
  } H5E_END_TRY;
  objno = (haddr_t)statbuf.objno[0] | ((haddr_t)statbuf.objno[1] << (8 * sizeof(long)));

  /* add to array */
  if ( info )
  {
   info[*idx].name = (char *)HDstrdup(path);
   info[*idx].type = type;
   (*idx)++;
  }


  switch ( type )
  {

  /*-------------------------------------------------------------------------
   * H5G_GROUP
   *-------------------------------------------------------------------------
   */

  case H5G_GROUP:

    /* increment */
   inserted_objs++;

   /* nlink is number of hard links to object */
   if (statbuf.nlink > 0  && trav_table_search(objno, table ) == -1)
   {
    /* add object to table */
    trav_table_add(objno, path, H5G_GROUP, table );

    /* print it */
    if (print)
     printf(" %-10s %s\n", "group", path  );

    /* recurse with the absolute name */
    inserted_objs += traverse( loc_id, path, table, info, idx, print );
   }

     /* search table
       group with more than one link to it */
   if (statbuf.nlink > 1)
   {
    if ((j = trav_table_search(objno, table )) < 0 )
     return -1;

    trav_table_addlink(table,j,path);

    if ( table->objs[j].displayed == 0 )
    {
     table->objs[j].displayed = 1;
    }
    else
    {
     /* print it */
     if (print)
      printf(" %-10s %s %s %s\n", "group", path, "->", table->objs[j].name  );
    }

   }

   break;

  /*-------------------------------------------------------------------------
   * H5G_DATASET
   *-------------------------------------------------------------------------
   */

  case H5G_DATASET:

    /* increment */
   inserted_objs++;

   /* nlink is number of hard links to object */
   if (statbuf.nlink > 0  && trav_table_search(objno, table ) == -1)
   {
    /* add object to table */
    trav_table_add(objno, path, H5G_DATASET, table );

    /* print it */
    if (print)
     printf(" %-10s %s\n", "dataset", path  );
   }

   /* search table
       dataset with more than one link to it */
   if (statbuf.nlink > 1)
   {
    if ((j = trav_table_search(objno, table )) < 0 )
     return -1;

    trav_table_addlink(table,j,path);

    if ( table->objs[j].displayed == 0 )
    {
     table->objs[j].displayed = 1;
    }
    else
    {
     /* print it */
     if (print)
      printf(" %-10s %s %s %s\n", "dataset", path, "->", table->objs[j].name  );
    } /* displayed==1 */
   } /* nlink>1 */


   break;

  /*-------------------------------------------------------------------------
   * H5G_TYPE
   *-------------------------------------------------------------------------
   */

  case H5G_TYPE:

   /* increment */
   inserted_objs++;

   /* nlink is number of hard links to object */
   if (statbuf.nlink > 0  && trav_table_search(objno, table ) == -1)
   {
    /* add object to table */
    trav_table_add(objno, path, H5G_TYPE, table );

     /* print it */
    if (print)
     printf(" %-10s %s\n", "datatype", path  );
   }

   break;


  /*-------------------------------------------------------------------------
   * H5G_LINK
   *-------------------------------------------------------------------------
   */

  case H5G_LINK:
  {
    /* increment */
    inserted_objs++;

    /* add object to table */
    trav_table_add(HADDR_UNDEF, path, H5G_LINK, table );

    if (statbuf.linklen>0)
    {
     char *targbuf;

     targbuf = HDmalloc(statbuf.linklen);
     assert(targbuf);
     H5Gget_linkval(loc_id,path,statbuf.linklen,targbuf);
     if (print)
      printf(" %-10s %s -> %s\n", "link", path, targbuf);
     free(targbuf);
    }
    else
    {
     if (print)
      printf(" %-10s %s ->\n", "link", path);
    }
   }

   break;

  /*-------------------------------------------------------------------------
   * H5G_UDLINK
   *-------------------------------------------------------------------------
   */

  case H5G_UDLINK:
  {
    H5L_info_t linkbuf;

    /* increment */
    inserted_objs++;

    /* add object to table */
    trav_table_add(HADDR_UNDEF, path, H5G_UDLINK, table );

    /* Get type of link */
    H5E_BEGIN_TRY {
        /* get link class info */
        H5Lget_info( loc_id, path, &linkbuf, H5P_DEFAULT);
    } H5E_END_TRY;

    if(linkbuf.type == H5L_TYPE_EXTERNAL)
    {
      if (statbuf.linklen>0)
      {
      char *targbuf;
      char *objname;

      targbuf = HDmalloc(statbuf.linklen);
      assert(targbuf);
      H5Gget_linkval(loc_id,path,statbuf.linklen,targbuf);
      H5Lunpack_elink_val(targbuf, statbuf.linklen, NULL, &objname);
      if (print)
        printf(" %-10s %s -> %s %s\n", "ext link", path, targbuf, objname);
      free(targbuf);
      }
      else
      {
      if (print)
        printf(" %-10s %s ->\n", "udlink", path);
      }
    }
    else  /* Unknown user-defined type */
    {
      if (print)
        printf(" %-10s %s ->\n", "UD link type", path);
    }
  }
  break;


  default:
    HDfprintf(stderr, "traverse: Unknown object %d!\n", type);
    return (-1);
   break;

  }

  /*-------------------------------------------------------------------------
   * end switch
   *-------------------------------------------------------------------------
   */

  if ( name )
   HDfree( name );

  if ( path )
   HDfree( path );

 } /* i */

 return inserted_objs;
}