-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
H5Lint.c
2176 lines (1830 loc) · 80.8 KB
/
H5Lint.c
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* 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 COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/****************/
/* Module Setup */
/****************/
#include "H5Lmodule.h" /* This source code file is part of the H5L module */
/***********/
/* Headers */
/***********/
#include "H5private.h" /* Generic Functions */
#include "H5CXprivate.h" /* API Contexts */
#include "H5Eprivate.h" /* Error handling */
#include "H5Fprivate.h" /* File access */
#include "H5Gprivate.h" /* Groups */
#include "H5Iprivate.h" /* IDs */
#include "H5Lpkg.h" /* Links */
#include "H5MMprivate.h" /* Memory management */
#include "H5Oprivate.h" /* File objects */
#include "H5Pprivate.h" /* Property lists */
#include "H5VLprivate.h" /* Virtual Object Layer */
/****************/
/* Local Macros */
/****************/
#define H5L_MIN_TABLE_SIZE 32 /* Minimum size of the user-defined link type table if it is allocated */
/******************/
/* Local Typedefs */
/******************/
/* User data for path traversal routine for getting link info by name */
typedef struct {
H5L_info2_t *linfo; /* Buffer to return to user */
} H5L_trav_gi_t;
/* User data for path traversal routine for getting link value by index */
typedef struct {
/* In */
H5_index_t idx_type; /* Index to use */
H5_iter_order_t order; /* Order to iterate in index */
hsize_t n; /* Offset of link within index */
size_t size; /* Size of user buffer */
/* Out */
void *buf; /* User buffer */
} H5L_trav_gvbi_t;
/* User data for path traversal routine for getting link info by index */
typedef struct {
/* In */
H5_index_t idx_type; /* Index to use */
H5_iter_order_t order; /* Order to iterate in index */
hsize_t n; /* Offset of link within index */
/* Out */
H5L_info2_t *linfo; /* Buffer to return to user */
} H5L_trav_gibi_t;
/* User data for path traversal routine for removing link by index */
typedef struct {
/* In */
H5_index_t idx_type; /* Index to use */
H5_iter_order_t order; /* Order to iterate in index */
hsize_t n; /* Offset of link within index */
} H5L_trav_rmbi_t;
/* User data for path traversal routine for getting name by index */
typedef struct {
/* In */
H5_index_t idx_type; /* Index to use */
H5_iter_order_t order; /* Order to iterate in index */
hsize_t n; /* Offset of link within index */
size_t size; /* Size of name buffer */
/* Out */
char *name; /* Buffer to return name to user */
size_t name_len; /* Length of full name */
} H5L_trav_gnbi_t;
/* User data for path traversal callback to creating a link */
struct H5L_trav_cr_t {
H5F_t *file; /* Pointer to the file */
H5P_genplist_t *lc_plist; /* Link creation property list */
H5G_name_t *path; /* Path to object being linked */
H5O_obj_create_t *ocrt_info; /* Pointer to object creation info */
H5O_link_t *lnk; /* Pointer to link information to insert */
};
/* User data for path traversal routine for moving and renaming a link */
typedef struct {
const char *dst_name; /* Destination name for moving object */
H5T_cset_t cset; /* Char set for new name */
const H5G_loc_t *dst_loc; /* Destination location for moving object */
unsigned dst_target_flags; /* Target flags for destination object */
bool copy; /* true if this is a copy operation */
size_t orig_nlinks; /* The original value for the # of soft / UD links that can be traversed */
} H5L_trav_mv_t;
/* User data for path traversal routine for moving and renaming an object */
typedef struct {
H5F_t *file; /* Pointer to the file */
H5O_link_t *lnk; /* Pointer to link information to insert */
bool copy; /* true if this is a copy operation */
} H5L_trav_mv2_t;
/* User data for path traversal routine for checking if a link exists */
typedef struct {
/* Down */
char *sep; /* Pointer to next separator in the string */
/* Up */
bool *exists; /* Whether the link exists or not */
} H5L_trav_le_t;
/* User data for path traversal routine for getting link value */
typedef struct {
size_t size; /* Size of user buffer */
void *buf; /* User buffer */
} H5L_trav_gv_t;
/********************/
/* Local Prototypes */
/********************/
static int H5L__find_class_idx(H5L_type_t id);
static herr_t H5L__link_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__create_real(const H5G_loc_t *link_loc, const char *link_name, H5G_name_t *obj_path,
H5F_t *obj_file, H5O_link_t *lnk, H5O_obj_create_t *ocrt_info, hid_t lcpl_id);
static herr_t H5L__get_val_real(const H5O_link_t *lnk, void *buf, size_t size);
static herr_t H5L__get_val_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__get_val_by_idx_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__delete_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__delete_by_idx_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__move_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__move_dest_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__exists_final_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__exists_inter_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__get_info_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__get_info_by_idx_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
static herr_t H5L__get_name_by_idx_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/,
H5G_own_loc_t *own_loc /*out*/);
/*********************/
/* Package Variables */
/*********************/
/*****************************/
/* Library Private Variables */
/*****************************/
/*******************/
/* Local Variables */
/*******************/
/* Information about user-defined links */
static size_t H5L_table_alloc_g = 0;
static size_t H5L_table_used_g = 0;
static H5L_class_t *H5L_table_g = NULL;
/*-------------------------------------------------------------------------
* Function: H5L_init
*
* Purpose: Initialize the interface from some other package.
*
* Return: Success: non-negative
*
* Failure: negative
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_init(void)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
/* Initialize user-defined link classes */
if (H5L_register_external() < 0)
HGOTO_ERROR(H5E_LINK, H5E_NOTREGISTERED, FAIL, "unable to register external link class");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_init() */
/*-------------------------------------------------------------------------
* Function: H5L_term_package
*
* Purpose: Terminate any resources allocated in H5L_init.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
int
H5L_term_package(void)
{
int n = 0;
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* Free the table of link types */
if (H5L_table_g) {
H5L_table_g = (H5L_class_t *)H5MM_xfree(H5L_table_g);
H5L_table_used_g = H5L_table_alloc_g = 0;
n++;
} /* end if */
FUNC_LEAVE_NOAPI(n)
} /* H5L_term_package() */
/*-------------------------------------------------------------------------
* Function: H5L__find_class_idx
*
* Purpose: Given a link class ID, return the offset in the global array
* that holds all the registered link classes.
*
* Return: Success: Non-negative index of entry in global
* link class table.
* Failure: Negative
*
*-------------------------------------------------------------------------
*/
static int
H5L__find_class_idx(H5L_type_t id)
{
size_t i; /* Local index variable */
int ret_value = FAIL; /* Return value */
FUNC_ENTER_PACKAGE_NOERR
for (i = 0; i < H5L_table_used_g; i++)
if (H5L_table_g[i].id == id)
HGOTO_DONE((int)i);
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__find_class_idx */
/*-------------------------------------------------------------------------
* Function: H5L_find_class
*
* Purpose: Given a link class ID return a pointer to a global struct that
* defines the link class.
*
* Return: Success: Ptr to entry in global link class table.
* Failure: NULL
*
*-------------------------------------------------------------------------
*/
const H5L_class_t *
H5L_find_class(H5L_type_t id)
{
int idx; /* Filter index in global table */
H5L_class_t *ret_value = NULL; /* Return value */
FUNC_ENTER_NOAPI(NULL)
/* Get the index in the global table */
if ((idx = H5L__find_class_idx(id)) < 0)
HGOTO_ERROR(H5E_LINK, H5E_NOTREGISTERED, NULL, "unable to find link class");
/* Set return value */
ret_value = H5L_table_g + idx;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_find_class */
/*-------------------------------------------------------------------------
* Function: H5L_register
*
* Purpose: Registers a class of user-defined links, or changes the
* behavior of an existing class.
*
* See H5Lregister for full documentation.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_register(const H5L_class_t *cls)
{
size_t i; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
assert(cls);
assert(cls->id >= 0 && cls->id <= H5L_TYPE_MAX);
/* Is the link type already registered? */
for (i = 0; i < H5L_table_used_g; i++)
if (H5L_table_g[i].id == cls->id)
break;
/* Filter not already registered */
if (i >= H5L_table_used_g) {
if (H5L_table_used_g >= H5L_table_alloc_g) {
size_t n = MAX(H5L_MIN_TABLE_SIZE, (2 * H5L_table_alloc_g));
H5L_class_t *table = (H5L_class_t *)H5MM_realloc(H5L_table_g, (n * sizeof(H5L_class_t)));
if (!table)
HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "unable to extend link type table");
H5L_table_g = table;
H5L_table_alloc_g = n;
} /* end if */
/* Initialize */
i = H5L_table_used_g++;
} /* end if */
/* Copy link class info into table */
H5MM_memcpy(H5L_table_g + i, cls, sizeof(H5L_class_t));
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_register */
/*-------------------------------------------------------------------------
* Function: H5L_unregister
*
* Purpose: Unregisters a class of user-defined links.
*
* See H5Lunregister for full documentation.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_unregister(H5L_type_t id)
{
size_t i; /* Local index variable */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI(FAIL)
assert(id >= 0 && id <= H5L_TYPE_MAX);
/* Is the filter already registered? */
for (i = 0; i < H5L_table_used_g; i++)
if (H5L_table_g[i].id == id)
break;
/* Fail if filter not found */
if (i >= H5L_table_used_g)
HGOTO_ERROR(H5E_LINK, H5E_NOTREGISTERED, FAIL, "link class is not registered");
/* Remove filter from table */
/* Don't worry about shrinking table size (for now) */
memmove(&H5L_table_g[i], &H5L_table_g[i + 1], sizeof(H5L_class_t) * ((H5L_table_used_g - 1) - i));
H5L_table_used_g--;
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_unregister() */
/*-------------------------------------------------------------------------
* Function: H5L_is_registered
*
* Purpose: Tests whether a user-defined link class has been registered
* or not.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_is_registered(H5L_type_t id, bool *is_registered)
{
size_t i; /* Local index variable */
FUNC_ENTER_NOAPI_NOINIT_NOERR
/* Check args */
assert(is_registered);
/* Is the link class already registered? */
*is_registered = false;
for (i = 0; i < H5L_table_used_g; i++)
if (H5L_table_g[i].id == id) {
*is_registered = true;
break;
}
FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5L_is_registered() */
/*-------------------------------------------------------------------------
* Function: H5L_link
*
* Purpose: Creates a link from OBJ_ID to CUR_NAME. See H5Olink() for
* full documentation.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_link(const H5G_loc_t *new_loc, const char *new_name, H5G_loc_t *obj_loc, hid_t lcpl_id)
{
H5O_link_t lnk; /* Link to insert */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* Check args */
assert(new_loc);
assert(obj_loc);
assert(new_name && *new_name);
/* The link callback will check that the object isn't being hard linked
* into a different file, so we don't need to do it here (there could be
* external links along the path).
*/
/* Construct link information for eventual insertion */
lnk.type = H5L_TYPE_HARD;
lnk.u.hard.addr = obj_loc->oloc->addr;
/* Create the link */
if (H5L__create_real(new_loc, new_name, obj_loc->path, obj_loc->oloc->file, &lnk, NULL, lcpl_id) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create new link to object");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_link() */
/*-------------------------------------------------------------------------
* Function: H5L_link_object
*
* Purpose: Creates a new object and a link to it.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L_link_object(const H5G_loc_t *new_loc, const char *new_name, H5O_obj_create_t *ocrt_info, hid_t lcpl_id)
{
H5O_link_t lnk; /* Link to insert */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_NOAPI_NOINIT
/* Check args */
assert(new_loc);
assert(new_name && *new_name);
assert(ocrt_info);
/* The link callback will check that the object isn't being hard linked
* into a different file, so we don't need to do it here (there could be
* external links along the path).
*/
/* Construct link information for eventual insertion */
lnk.type = H5L_TYPE_HARD;
/* Create the link */
if (H5L__create_real(new_loc, new_name, NULL, NULL, &lnk, ocrt_info, lcpl_id) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create new link to object");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L_link_object() */
/*-------------------------------------------------------------------------
* Function: H5L__link_cb
*
* Purpose: Callback for creating a link to an object.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5L__link_cb(H5G_loc_t *grp_loc /*in*/, const char *name, const H5O_link_t H5_ATTR_UNUSED *lnk,
H5G_loc_t *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/)
{
H5L_trav_cr_t *udata = (H5L_trav_cr_t *)_udata; /* User data passed in */
H5G_t *grp = NULL; /* H5G_t for this group, opened to pass to user callback */
hid_t grp_id = FAIL; /* Id for this group (passed to user callback */
H5G_loc_t temp_loc; /* For UD callback */
bool temp_loc_init = false; /* Temporary location for UD callback (temp_loc) has been initialized */
bool obj_created = false; /* Whether an object was created (through a hard link) */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check if the name in this group resolved to a valid location */
/* (which is not what we want) */
if (obj_loc != NULL)
HGOTO_ERROR(H5E_LINK, H5E_EXISTS, FAIL, "name already exists");
/* Check for crossing file boundaries with a new hard link */
if (udata->lnk->type == H5L_TYPE_HARD) {
/* Check for creating an object */
/* (only for hard links) */
if (udata->ocrt_info) {
H5G_loc_t new_loc; /* Group location for new object */
/* Create new object at this location */
if (NULL ==
(udata->ocrt_info->new_obj = H5O_obj_create(grp_loc->oloc->file, udata->ocrt_info->obj_type,
udata->ocrt_info->crt_info, &new_loc)))
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create object");
/* Set address for hard link */
udata->lnk->u.hard.addr = new_loc.oloc->addr;
/* Set object path to use for setting object name (below) */
udata->path = new_loc.path;
/* Indicate that an object was created */
obj_created = true;
} /* end if */
else {
/* Check that both objects are in same file */
if (!H5F_SAME_SHARED(grp_loc->oloc->file, udata->file))
HGOTO_ERROR(H5E_LINK, H5E_BADVALUE, FAIL, "interfile hard links are not allowed");
} /* end else */
} /* end if */
/* Set 'standard' aspects of link */
udata->lnk->corder =
0; /* Will be re-written during group insertion, if the group is tracking creation order */
udata->lnk->corder_valid = false; /* Creation order not valid (yet) */
/* Check for non-default link creation properties */
if (udata->lc_plist) {
/* Get character encoding property */
if (H5CX_get_encoding(&udata->lnk->cset) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTGET, FAIL, "can't get 'character set' property");
} /* end if */
else
udata->lnk->cset = H5F_DEFAULT_CSET; /* Default character encoding for link */
/* Set the link's name correctly */
H5_GCC_CLANG_DIAG_OFF("cast-qual")
udata->lnk->name = (char *)name;
H5_GCC_CLANG_DIAG_ON("cast-qual")
/* Insert link into group */
if (H5G_obj_insert(grp_loc->oloc, udata->lnk, true,
udata->ocrt_info ? udata->ocrt_info->obj_type : H5O_TYPE_UNKNOWN,
udata->ocrt_info ? udata->ocrt_info->crt_info : NULL) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create new link for object");
/* Set object's path if it has been passed in and is not set */
if (udata->path != NULL && udata->path->user_path_r == NULL)
if (H5G_name_set(grp_loc->path, udata->path, name) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "cannot set name");
/* If link is a user-defined link, trigger its creation callback if it has one */
if (udata->lnk->type >= H5L_TYPE_UD_MIN) {
const H5L_class_t *link_class; /* User-defined link class */
/* Get the link class for this type of link. */
if (NULL == (link_class = H5L_find_class(udata->lnk->type)))
HGOTO_ERROR(H5E_LINK, H5E_NOTREGISTERED, FAIL, "unable to get class of UD link");
if (link_class->create_func != NULL) {
H5O_loc_t temp_oloc;
H5G_name_t temp_path;
/* Create a temporary location (or else H5G_open will do a shallow
* copy and wipe out grp_loc)
*/
H5G_name_reset(&temp_path);
if (H5O_loc_copy_deep(&temp_oloc, grp_loc->oloc) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTCOPY, FAIL, "unable to copy object location");
temp_loc.oloc = &temp_oloc;
temp_loc.path = &temp_path;
temp_loc_init = true;
/* Set up location for user-defined callback */
if (NULL == (grp = H5G_open(&temp_loc)))
HGOTO_ERROR(H5E_LINK, H5E_CANTOPENOBJ, FAIL, "unable to open group");
if ((grp_id = H5VL_wrap_register(H5I_GROUP, grp, true)) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTREGISTER, FAIL, "unable to register ID for group");
/* Make callback */
if ((link_class->create_func)(name, grp_id, udata->lnk->u.ud.udata, udata->lnk->u.ud.size,
H5P_DEFAULT) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CALLBACK, FAIL, "link creation callback failed");
} /* end if */
} /* end if */
done:
/* Check if an object was created */
if (obj_created) {
H5O_loc_t oloc; /* Object location for created object */
/* Set up object location */
memset(&oloc, 0, sizeof(oloc));
oloc.file = grp_loc->oloc->file;
oloc.addr = udata->lnk->u.hard.addr;
/* Decrement refcount on new object's object header in memory */
if (H5O_dec_rc_by_loc(&oloc) < 0)
HDONE_ERROR(H5E_LINK, H5E_CANTDEC, FAIL, "unable to decrement refcount on newly created object");
} /* end if */
/* Close the location given to the user callback if it was created */
if (grp_id >= 0) {
if (H5I_dec_app_ref(grp_id) < 0)
HDONE_ERROR(H5E_LINK, H5E_CANTRELEASE, FAIL, "unable to close ID from UD callback");
} /* end if */
else if (grp != NULL) {
if (H5G_close(grp) < 0)
HDONE_ERROR(H5E_LINK, H5E_CANTRELEASE, FAIL, "unable to close group given to UD callback");
} /* end if */
else if (temp_loc_init)
H5G_loc_free(&temp_loc);
/* Indicate that this callback didn't take ownership of the group *
* location for the object */
*own_loc = H5G_OWN_NONE;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__link_cb() */
/*-------------------------------------------------------------------------
* Function: H5L__create_real
*
* Purpose: Creates a link at a path location
*
* lnk should have linkclass-specific information already
* set, but this function will take care of setting name.
*
* obj_path can be NULL if the object's path doesn't need to
* be set, and obj_file can be NULL if the object is not a
* hard link.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5L__create_real(const H5G_loc_t *link_loc, const char *link_name, H5G_name_t *obj_path, H5F_t *obj_file,
H5O_link_t *lnk, H5O_obj_create_t *ocrt_info, hid_t lcpl_id)
{
char *norm_link_name = NULL; /* Pointer to normalized link name */
unsigned target_flags = H5G_TARGET_NORMAL; /* Flags to pass to group traversal function */
H5P_genplist_t *lc_plist = NULL; /* Link creation property list */
H5L_trav_cr_t udata; /* User data for callback */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args */
assert(link_loc);
assert(link_name && *link_name);
assert(lnk);
assert(lnk->type >= H5L_TYPE_HARD && lnk->type <= H5L_TYPE_MAX);
/* Get normalized link name */
if ((norm_link_name = H5G_normalize(link_name)) == NULL)
HGOTO_ERROR(H5E_LINK, H5E_BADVALUE, FAIL, "can't normalize name");
/* Check for flags present in creation property list */
if (lcpl_id != H5P_DEFAULT) {
unsigned crt_intmd_group;
/* Get link creation property list */
if (NULL == (lc_plist = (H5P_genplist_t *)H5I_object(lcpl_id)))
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a property list");
/* Get intermediate group creation property */
if (H5CX_get_intermediate_group(&crt_intmd_group) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTGET, FAIL, "can't get 'create intermediate group' property");
if (crt_intmd_group > 0)
target_flags |= H5G_CRT_INTMD_GROUP;
} /* end if */
if (ocrt_info != NULL)
target_flags |= H5G_CRT_OBJ;
/* Set up user data
* FILE is used to make sure that hard links don't cross files, and
* should be NULL for other link types.
* LC_PLIST is a pointer to the link creation property list.
* PATH is a pointer to the path of the object being inserted if this is
* a hard link; this is used to set the paths to objects when they are
* created. For other link types, this is NULL.
* OCRT_INFO is a pointer to the structure for object creation.
* LNK is the link struct passed into this function. At this point all
* of its fields should be populated except for name, which is set when
* inserting it in the callback.
*/
udata.file = obj_file;
udata.lc_plist = lc_plist;
udata.path = obj_path;
udata.ocrt_info = ocrt_info;
udata.lnk = lnk;
/* Traverse the destination path & create new link */
if (H5G_traverse(link_loc, link_name, target_flags, H5L__link_cb, &udata) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINSERT, FAIL, "can't insert link");
done:
/* Free the normalized path name */
if (norm_link_name)
H5MM_xfree(norm_link_name);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__create_real() */
/*-------------------------------------------------------------------------
* Function: H5L__create_hard
*
* Purpose: Creates a hard link from NEW_NAME to CUR_NAME.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L__create_hard(H5G_loc_t *cur_loc, const char *cur_name, const H5G_loc_t *link_loc, const char *link_name,
hid_t lcpl_id)
{
char *norm_cur_name = NULL; /* Pointer to normalized current name */
H5F_t *link_file = NULL; /* Pointer to file to link to */
H5O_link_t lnk; /* Link to insert */
H5G_loc_t obj_loc; /* Location of object to link to */
H5G_name_t path; /* obj_loc's path*/
H5O_loc_t oloc; /* obj_loc's oloc */
bool loc_valid = false;
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args */
assert(cur_loc);
assert(cur_name && *cur_name);
assert(link_loc);
assert(link_name && *link_name);
/* Get normalized copy of the current name */
if ((norm_cur_name = H5G_normalize(cur_name)) == NULL)
HGOTO_ERROR(H5E_LINK, H5E_BADVALUE, FAIL, "can't normalize name");
/* Set up link data specific to hard links */
lnk.type = H5L_TYPE_HARD;
/* Get object location for object pointed to */
obj_loc.path = &path;
obj_loc.oloc = &oloc;
H5G_loc_reset(&obj_loc);
if (H5G_loc_find(cur_loc, norm_cur_name, &obj_loc) < 0)
HGOTO_ERROR(H5E_LINK, H5E_NOTFOUND, FAIL, "source object not found");
loc_valid = true;
/* Construct link information for eventual insertion */
lnk.u.hard.addr = obj_loc.oloc->addr;
/* Set destination's file information */
link_file = obj_loc.oloc->file;
/* Create actual link to the object. Pass in NULL for the path, since this
* function shouldn't change an object's user path. */
if (H5L__create_real(link_loc, link_name, NULL, link_file, &lnk, NULL, lcpl_id) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create new link to object");
done:
/* Free the object header location */
if (loc_valid)
if (H5G_loc_free(&obj_loc) < 0)
HDONE_ERROR(H5E_LINK, H5E_CANTRELEASE, FAIL, "unable to free location");
/* Free the normalized path name */
if (norm_cur_name)
H5MM_xfree(norm_cur_name);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__create_hard() */
/*-------------------------------------------------------------------------
* Function: H5L__create_soft
*
* Purpose: Creates a soft link from LINK_NAME to TARGET_PATH.
*
* Return: SUCCEED/FAIL
*
*-------------------------------------------------------------------------
*/
herr_t
H5L__create_soft(const char *target_path, const H5G_loc_t *link_loc, const char *link_name, hid_t lcpl_id)
{
char *norm_target = NULL; /* Pointer to normalized current name */
H5O_link_t lnk; /* Link to insert */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args */
assert(link_loc);
assert(target_path && *target_path);
assert(link_name && *link_name);
/* Get normalized copy of the link target */
if ((norm_target = H5G_normalize(target_path)) == NULL)
HGOTO_ERROR(H5E_LINK, H5E_BADVALUE, FAIL, "can't normalize name");
/* Set up link data specific to soft links */
lnk.type = H5L_TYPE_SOFT;
lnk.u.soft.name = norm_target;
/* Create actual link to the object */
if (H5L__create_real(link_loc, link_name, NULL, NULL, &lnk, NULL, lcpl_id) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to create new link to object");
done:
/* Free the normalized target name */
if (norm_target)
H5MM_xfree(norm_target);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__create_soft() */
/*-------------------------------------------------------------------------
* Function: H5L__create_ud
*
* Purpose: Creates a user-defined link. See H5Lcreate_ud for
* full documentation.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
herr_t
H5L__create_ud(const H5G_loc_t *link_loc, const char *link_name, const void *ud_data, size_t ud_data_size,
H5L_type_t type, hid_t lcpl_id)
{
H5O_link_t lnk; /* Link to insert */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check args */
assert(type >= H5L_TYPE_UD_MIN && type <= H5L_TYPE_MAX);
assert(link_loc);
assert(link_name && *link_name);
assert(ud_data_size == 0 || ud_data);
/* Initialize the link struct's pointer to its udata buffer */
lnk.u.ud.udata = NULL;
/* Make sure that this link class is registered */
if (H5L__find_class_idx(type) < 0)
HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "link class has not been registered with library");
/* Fill in UD link-specific information in the link struct*/
if (ud_data_size > 0) {
lnk.u.ud.udata = H5MM_malloc((size_t)ud_data_size);
H5MM_memcpy(lnk.u.ud.udata, ud_data, (size_t)ud_data_size);
} /* end if */
else
lnk.u.ud.udata = NULL;
lnk.u.ud.size = ud_data_size;
lnk.type = type;
/* Create actual link to the object */
if (H5L__create_real(link_loc, link_name, NULL, NULL, &lnk, NULL, lcpl_id) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTINIT, FAIL, "unable to register new name for object");
done:
/* Free the link's udata buffer if it's been allocated */
H5MM_xfree(lnk.u.ud.udata);
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__create_ud() */
/*-------------------------------------------------------------------------
* Function: H5L__get_val_real
*
* Purpose: Retrieve link value from a link object
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5L__get_val_real(const H5O_link_t *lnk, void *buf, size_t size)
{
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Sanity check */
assert(lnk);
/* Check for soft link */
if (H5L_TYPE_SOFT == lnk->type) {
/* Copy to output buffer */
if (size > 0 && buf) {
strncpy((char *)buf, lnk->u.soft.name, size);
if (strlen(lnk->u.soft.name) >= size)
((char *)buf)[size - 1] = '\0';
} /* end if */
} /* end if */
/* Check for user-defined link */
else if (lnk->type >= H5L_TYPE_UD_MIN) {
const H5L_class_t *link_class; /* User-defined link class */
/* Get the link class for this type of link. It's okay if the class
* isn't registered, though--we just can't give any more information
* about it
*/
link_class = H5L_find_class(lnk->type);
if (link_class != NULL && link_class->query_func != NULL) {
if ((link_class->query_func)(lnk->name, lnk->u.ud.udata, lnk->u.ud.size, buf, size) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CALLBACK, FAIL, "query callback returned failure");
} /* end if */
else if (buf && size > 0)
((char *)buf)[0] = '\0';
} /* end if */
else
HGOTO_ERROR(H5E_LINK, H5E_BADTYPE, FAIL, "object is not a symbolic or user-defined link");
done:
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__get_val_real() */
/*-------------------------------------------------------------------------
* Function: H5L__get_val_cb
*
* Purpose: Callback for retrieving link value or udata.
*
* Return: Non-negative on success/Negative on failure
*
*-------------------------------------------------------------------------
*/
static herr_t
H5L__get_val_cb(H5G_loc_t H5_ATTR_UNUSED *grp_loc /*in*/, const char *name, const H5O_link_t *lnk,
H5G_loc_t H5_ATTR_UNUSED *obj_loc, void *_udata /*in,out*/, H5G_own_loc_t *own_loc /*out*/)
{
H5L_trav_gv_t *udata = (H5L_trav_gv_t *)_udata; /* User data passed in */
herr_t ret_value = SUCCEED; /* Return value */
FUNC_ENTER_PACKAGE
/* Check if the name in this group resolved to a valid link */
if (lnk == NULL)
HGOTO_ERROR(H5E_LINK, H5E_NOTFOUND, FAIL, "'%s' doesn't exist", name);
/* Retrieve the value for the link */
if (H5L__get_val_real(lnk, udata->buf, udata->size) < 0)
HGOTO_ERROR(H5E_LINK, H5E_CANTGET, FAIL, "can't retrieve link value");
done:
/* Indicate that this callback didn't take ownership of the group *
* location for the object */
*own_loc = H5G_OWN_NONE;
FUNC_LEAVE_NOAPI(ret_value)
} /* end H5L__get_val_cb() */
/*-------------------------------------------------------------------------
* Function: H5L__get_val
*
* Purpose: Returns the value of a symbolic link or the udata for a
* user-defined link.
*
* Return: Success: Non-negative, with at most SIZE bytes of the