File: inv_stobj.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (1495 lines) | stat: -rw-r--r-- 40,740 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <xfs/xfs.h>
#include <xfs/jdm.h>

#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <sys/mman.h>
#include <assert.h>
#include <string.h>
#include <uuid/uuid.h>

#include "config.h"

#include "types.h"
#include "timeutil.h"
#include "mlog.h"
#include "inv_priv.h"
#include "arch_xlate.h"



/*----------------------------------------------------------------------*/
/* stobj_insert_session                                                 */
/*                                                                      */
/* Used in reconstruction of the inventory. We add a session to this    */
/* storage object whether or not it has reached its maximum.            */
/*----------------------------------------------------------------------*/
int
stobj_insert_session(invt_idxinfo_t *idx,
		      int fd, /* kept locked EX by caller */
		      invt_sessinfo_t *s)
{
	invt_sescounter_t *sescnt = NULL;

	if (GET_SESCOUNTERS(fd, &sescnt) < 0) {
		INVLOCK(fd, LOCK_UN);
		return -1;
	}

	/* Check the existing sessions to make sure that we're not
	   duplicating this session */
	if (sescnt->ic_curnum > 0) {
		uint i;
		invt_session_t	*sessions = calloc(sescnt->ic_curnum,
				   sizeof(invt_session_t));
		if (GET_REC_NOLOCK(fd, sessions, sescnt->ic_curnum *
				     sizeof(invt_session_t),
				     (off64_t) (sizeof(*sescnt) +
         		sescnt->ic_maxnum * sizeof(invt_seshdr_t))) < 0) {
			free (sescnt);
			free (sessions);
			return -1;
		}

		for(i = 0; i <  sescnt->ic_curnum; i++) {
			if (uuid_compare(sessions[i].s_sesid,
					   s->ses->s_sesid) == 0)
				break;

		}
		free (sessions);
		if (i < sescnt->ic_curnum) {
			mlog(MLOG_DEBUG | MLOG_INV,
			      "INV: attempt to insert an "
			      "existing session.\n"
			     );
			free (sescnt);
			return 1;
		}

	}

	if (sescnt->ic_curnum >= sescnt->ic_maxnum) {
		if (stobj_split(idx, fd, sescnt, s) < 0) {
			free(sescnt);
			return -1;
		}
		free(sescnt);
		return 1;

	}

	if (stobj_put_session(fd, sescnt, s->ses, s->seshdr, s->strms,
			        s->mfiles) < 0){
		free (sescnt);
		return -1;
	}

	free (sescnt);
	return 1;
}


/*----------------------------------------------------------------------*/
/*  stobj_find_splitpoint						*/
/*                                                                      */
/* Load in all the session headers, and go thru them, one by one, 	*/
/* beginning with last seshdr. We return once we find one with a 	*/
/* different sh_time from the previous one. 				*/
/*----------------------------------------------------------------------*/


/* ARGSUSED */
uint
stobj_find_splitpoint(int fd, invt_seshdr_t *harr, uint ns, time32_t tm)
{
	uint i;

	if (harr[ns-1].sh_time < tm)
		return ns;
	/* since ns > 1, our split point > 0  */
	for (i = ns - 1; i > 0; i--) {
		/* these are ordered in ascending order */
		if (harr[i].sh_time > harr[i-1].sh_time)
			return i;
	}


	/* All the entries have the same sh_time. It's not clear if that's
	   really possible, but either way, we split at the last entry.
	   This will impact the guarantee that invindex tries to give - that
	   there's always a unique stobj for every session. */
	mlog(MLOG_VERBOSE | MLOG_INV, _(
	      "INV: failed to find a different sh_time to split\n")
	     );

	return ns - 1;



}



/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

int
stobj_split(invt_idxinfo_t *idx, int fd, invt_sescounter_t *sescnt,
	     invt_sessinfo_t *newsess)
{
	invt_seshdr_t 	*harr = NULL;
	uint          	i, ix, ns = sescnt->ic_curnum;
	void        	*bufpp;
	size_t        	bufszp;
	invt_sessinfo_t sesinfo;
	invt_entry_t 	ient;

	if (GET_SESHEADERS(fd, &harr, ns) < 0)
		return -1;

	assert(harr != NULL);

	if ((ix = stobj_find_splitpoint(fd, harr, ns,
				       newsess->seshdr->sh_time)) == 0)
		return -1;

	if (ix == ns) {
		ient.ie_timeperiod.tp_start = ient.ie_timeperiod.tp_end =
			 newsess->seshdr->sh_time;
	} else {
		ient.ie_timeperiod.tp_start = ient.ie_timeperiod.tp_end =
		harr[ix].sh_time;
		if (harr[ix - 1].sh_time >  newsess->seshdr->sh_time)
			idx->iarr[idx->index].ie_timeperiod.tp_end
				= harr[ix - 1].sh_time;
		else
			idx->iarr[idx->index].ie_timeperiod.tp_end
				= newsess->seshdr->sh_time;
	}

	/* Get the new stobj to put the 'spilling' sessinfo in. We know the
	   idx of the current stobj, and by definition, the new stobj
	   should come right afterwards. */
	newsess->stobjfd = idx_put_newentry(idx, &ient);
	if (newsess->stobjfd < 0)
		return -1;

	if (ix == ns) {
		invt_sescounter_t *scnt = NULL;
		off64_t rval;

		/* We dont need to split. So, just put the new session in
		   the new stobj, and rest in peace */
		idx->index++;

		if (GET_SESCOUNTERS(newsess->stobjfd, &scnt) < 0)
			return -1;

		rval = stobj_put_session(newsess->stobjfd, scnt,
				        newsess->ses,
				        newsess->seshdr, newsess->strms,
				        newsess->mfiles);
		free(scnt);
		return (rval < 0)? -1: 1;
	}



	for (i = ix; i < ns; i++) {
		invt_session_t session;
		bufpp = NULL;
		bufszp = 0;

		newsess->seshdr->sh_sess_off = harr[i].sh_sess_off;

		if (GET_REC_NOLOCK(fd, &session, sizeof(invt_session_t),
			     harr[i].sh_sess_off) < 0)
			return -1;
		if (!stobj_pack_sessinfo(fd, &session, &harr[i], &bufpp,
					   &bufszp))
			return -1;
		/* Now we need to put this in the new StObj. So, first
		   unpack it. */
		if (!stobj_unpack_sessinfo(bufpp, bufszp, &sesinfo))
			return -1;

		/* There is no chance of a recursion here */
		if (stobj_insert_session(idx, newsess->stobjfd, &sesinfo)
		     < 0)
			return -1;

		/* Now delete that session from this StObj */
		if (!stobj_delete_sessinfo(fd, sescnt, &session,
					     &harr[i]))
			return -1;
		free(bufpp);
	}
	/* Put the new session in the stobj that we just split. Make
	   sure that we use the updated sescnt and not the stale stuff
	   from disk. stobj_put_session() writes sescnt and sessinfo to
	   disk */
	if (stobj_put_session(fd, sescnt, newsess->ses, newsess->seshdr,
			        newsess->strms, newsess->mfiles) < 0) {
		free (sescnt);
		return -1;
	}


	return 1;
}




/* ARGSUSED */
int
stobj_delete_mfile(int fd, inv_stream_t *strm, invt_mediafile_t *mf,
		    off64_t  mfileoff)
{

	return 1;

}




/* ARGSUSED */
bool_t
stobj_delete_sessinfo(int fd, /* kept locked EX by caller */
		       invt_sescounter_t *sescnt,
		       invt_session_t *ses, invt_seshdr_t *hdr)
{
	/* we change the sescnt here, but dont write it back to disk
	   until later */
	sescnt->ic_curnum--;

	/* there is really no need to take out / zero out the deleted
	   session. The seshdr and session will be over-written, but
	   space taken by the streams and mediafiles will be wasted. */
	return BOOL_TRUE;
}



/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/


off64_t
stobj_put_session(
	int fd,
	invt_sescounter_t *sescnt,
	invt_session_t *ses,
	invt_seshdr_t *hdr,
	invt_stream_t *strms,
	invt_mediafile_t *mfiles)
{
	off64_t hoff;

	/* figure out the place where the header will go */
	hoff =  STOBJ_OFFSET(sescnt->ic_curnum, 0);

	/* figure out where the session information is going to go. */
	if (hdr->sh_sess_off < 0)
		hdr->sh_sess_off = STOBJ_OFFSET(sescnt->ic_maxnum,
						 sescnt->ic_curnum);
	sescnt->ic_curnum++;

#ifdef INVT_DEBUG
	mlog (MLOG_VERBOSE,
	       "INV: create sess #%d @ offset %d ic_eof = %d\n",
	       sescnt->ic_curnum, (int) hdr->sh_sess_off,
	       (int) sescnt->ic_eof);
#endif

	/* we need to know where the streams begin, and where the
	   media files will begin, at the end of the streams */
	hdr->sh_streams_off = sescnt->ic_eof;

	if (strms == NULL) {
		sescnt->ic_eof += (off64_t)(ses->s_max_nstreams *
					     sizeof(invt_stream_t));
	} else {
		uint i;
		size_t nmf = 0;
		sescnt->ic_eof += (off64_t)(ses->s_cur_nstreams *
					     sizeof(invt_stream_t));
		for (i=0; i<ses->s_cur_nstreams; i++)
			nmf += (size_t) strms[i].st_nmediafiles;

		sescnt->ic_eof += (off64_t)(sizeof(invt_mediafile_t)
					     * nmf);
		if (stobj_put_streams(fd, hdr, ses, strms, mfiles) < 0)
			return -1;

	}

	/* we write back the counters of this storage object first */
	if (PUT_SESCOUNTERS(fd, sescnt) < 0)
		return -1;

	/* write the header next; lseek to the right position */
	if (PUT_REC_NOLOCK(fd, hdr, sizeof(invt_seshdr_t), hoff) < 0) {
		return -1;
	}

	/* write the header information to the storage object */
	if (PUT_REC_NOLOCK(fd, ses, sizeof(invt_session_t),
			     hdr->sh_sess_off) < 0) {
		return -1;
	}

	if (strms != NULL)
		stobj_sortheaders(fd, sescnt->ic_curnum);

	return hoff;
}




/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

int
stobj_sortheaders(int fd, uint num)
{
	size_t sz = sizeof(invt_seshdr_t) * num;
	invt_seshdr_t *hdrs;
#ifdef INVT_DEBUG
	int i;
#endif
	if (num < 2) return 1;

	hdrs = malloc(sz);
	assert(hdrs);

	if (GET_REC_NOLOCK(fd, hdrs, sz, STOBJ_OFFSET(0, 0)) < 0) {
		free (hdrs);
		return -1;
	}
#ifdef INVT_DEBUG
	printf("\nBEF\n");
	for (i=0; i<(int)num; i++)
		printf("%ld\n", (long) hdrs[i].sh_time);
#endif
	qsort((void*) hdrs, (size_t) num,
	      sizeof(invt_seshdr_t), stobj_hdrcmp);

#ifdef INVT_DEBUG
	printf("\n\nAFT\n");
	for (i=0; i<(int)num; i++)
		printf("%ld\n", (long) hdrs[i].sh_time);
#endif
	if (PUT_REC_NOLOCK(fd, hdrs, sz, STOBJ_OFFSET(0, 0)) < 0) {
		free (hdrs);
		return -1;
	}

	free (hdrs);
	return 1;

}



/*----------------------------------------------------------------------*/
/* stobj_put_streams                                                    */
/*                                                                      */
/* used only in reconstruction.                                         */
/* puts the array of streams and the array of mediafiles in the stobj,  */
/* after adjusting their offsets.                                       */
/*----------------------------------------------------------------------*/

int
stobj_put_streams(int fd, invt_seshdr_t *hdr, invt_session_t *ses,
		   invt_stream_t *strms,
		   invt_mediafile_t *mfiles)
{
	uint	nstm = ses->s_cur_nstreams;
	off64_t off  = hdr->sh_streams_off;
	off64_t mfileoff = off + (off64_t)(nstm * sizeof(invt_stream_t));
	uint nmfiles = 0;
	uint i, j;

	/* fix the offsets in streams */
	for (i = 0; i < nstm; i++) {
		strms[i].st_firstmfile = mfileoff +
		  (off64_t) ((size_t) nmfiles * sizeof(invt_mediafile_t));
		/* now fix the offsets in mediafiles */
		for (j = 0; j < strms[i].st_nmediafiles; j++) {

			/* no need to fix the last element's next ptr */
			if (j < strms[i].st_nmediafiles - 1)
				mfiles[nmfiles + j].mf_nextmf =
				        strms[i].st_firstmfile +
			      (off64_t) ((j+1) * sizeof(invt_mediafile_t));

			/* no need to fix the first element's prev ptr */
			if (j)
				mfiles[nmfiles + j].mf_prevmf =
				        strms[i].st_firstmfile +
			    (off64_t) ((j-1) * sizeof(invt_mediafile_t));
		}

		/* adjust the offsets of the first and the last elements
		   in the mediafile chain */
		mfiles[nmfiles].mf_prevmf = 0;
		nmfiles += strms[i].st_nmediafiles;
		mfiles[nmfiles - 1].mf_nextmf = 0;

	}

	/* first put the streams. hdr already points at the right place */
	if (PUT_REC_NOLOCK(fd, strms, nstm * sizeof(invt_stream_t),
			     off) < 0)
		return -1;

	if (PUT_REC_NOLOCK(fd, mfiles, nmfiles * sizeof(invt_mediafile_t),
			     mfileoff) < 0)
		return -1;

	return 1;

}




/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

void
stobj_makefname(char *fname)
{
	/* come up with a new unique name */
	uuid_t	fn;
	char str[UUID_STR_LEN + 1];

	uuid_generate(fn);
        uuid_unparse(fn, str);

	strcpy(fname, INV_DIRPATH);
	strcat(fname, "/");
	strcat(fname, str);
	strcat(fname, INV_STOBJ_PREFIX);

	assert((int) strlen(fname) < INV_STRLEN);
}







/* NOTE: this doesnt update counters or headers in the inv_index */
int
stobj_create(char *fname)
{
	int fd;
	invt_sescounter_t sescnt;
	inv_oflag_t forwhat = INV_SEARCH_N_MOD;

#ifdef INVT_DEBUG
	mlog(MLOG_VERBOSE | MLOG_INV, "INV: creating storage obj %s\n", fname);
#endif

	/* create the new storage object */
	if ((fd = open(fname, INV_OFLAG(forwhat) | O_EXCL | O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
		INV_PERROR (fname);
		memset(fname, 0, INV_STRLEN);
		return -1;
	}

	INVLOCK(fd, LOCK_EX);
	fchmod(fd, INV_PERMS);

	sescnt.ic_vernum = INV_VERSION;
	sescnt.ic_curnum = 0; /* there are no sessions as yet */
	sescnt.ic_maxnum = INVT_STOBJ_MAXSESSIONS;
	sescnt.ic_eof = SC_EOF_INITIAL_POS;

	if (PUT_SESCOUNTERS (fd, &sescnt) < 0) {
		memset(fname, 0, INV_STRLEN);
		INVLOCK(fd, LOCK_UN);
		close(fd);
		return -1;
	}

	INVLOCK(fd, LOCK_UN);
	return fd;
}



/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/


int
stobj_create_session(
	inv_sestoken_t tok,
	int fd, /* kept locked EX by caller */
	invt_sescounter_t *sescnt,
	invt_session_t *ses,
	invt_seshdr_t *hdr)
{
	off64_t hoff;

	assert(tok && sescnt && ses && hdr);

	hdr->sh_sess_off = -1;
	ses->s_cur_nstreams = 0;

	if ((hoff = stobj_put_session(fd, sescnt, ses, hdr, NULL, NULL))
	    < 0) {
		return -1;
	}

	tok->sd_sesshdr_off = hoff;
	tok->sd_session_off = hdr->sh_sess_off;

	return 1;
}



/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/* The stobj_fd in the stream token is kept locked EX by caller.        */
/*----------------------------------------------------------------------*/

int
stobj_put_mediafile(inv_stmtoken_t tok, invt_mediafile_t *mf)
{
	int  rval;
	invt_sescounter_t *sescnt = NULL;
	invt_stream_t stream;
	inv_sestoken_t sestok = tok->md_sesstok;
	int fd =  sestok->sd_invtok->d_stobj_fd;
	off64_t pos;

	/* first we need to find out where the current write-position is.
	   so, we first read the sescounter that is at the top of this
	   storage object */
	if (GET_SESCOUNTERS(fd, &sescnt) < 0)
		return -1;

	pos = sescnt->ic_eof;

	/* increment the pointer to give space for this media file */
	sescnt->ic_eof += (off64_t) sizeof(invt_mediafile_t);

	if (PUT_SESCOUNTERS(fd, sescnt) < 0)
		return -1;

	/* get the stream information, and update number of mediafiles.
	   we also need to link the new mediafile into the linked-list of
	   media files of this stream */

	if (GET_REC_NOLOCK(fd, &stream, sizeof(stream),
			     tok->md_stream_off) < 0)
		return -1;

	/* We need to update the last ino of this STREAM, which is now the
	   last ino of the new mediafile. If this is the first mediafile, we
	   have to update the startino as well. Note that ino is a <ino, off>
	   tuple */
	if (!(mf->mf_flag & INVT_MFILE_INVDUMP)) {
		if (stream.st_nmediafiles == 0)
			stream.st_startino = mf->mf_startino;
		stream.st_endino = mf->mf_endino;
	}

	stream.st_nmediafiles++;
#ifdef INVT_DEBUG
	mlog (MLOG_VERBOSE, "#################### mediafile #%d "
	      "###################\n", stream.st_nmediafiles);
#endif
	/* add the new mediafile at the tail of the list */

	mf->mf_nextmf = tok->md_stream_off;
	mf->mf_prevmf = stream.st_lastmfile;


	if (tok->md_lastmfile)
		tok->md_lastmfile->mf_nextmf = pos;
	else {
		stream.st_firstmfile = pos;
	}

	stream.st_lastmfile = pos;


	/* write the stream to disk */
	if (PUT_REC_NOLOCK(fd, &stream, sizeof(stream),
			     tok->md_stream_off) < 0)
		return -1;

	/* write the prev media file to disk too */
	if (tok->md_lastmfile) {
		rval = PUT_REC_NOLOCK(fd, tok->md_lastmfile,
				       sizeof(invt_mediafile_t),
				       mf->mf_prevmf);
		free (tok->md_lastmfile);
		if (rval < 0)
			return -1;
	}

	if (!(mf->mf_flag & INVT_MFILE_INVDUMP)) {
		tok->md_lastmfile = mf;
	} else {
		tok->md_lastmfile = NULL;
	}

	/* at last, write the new media file to disk */
	rval = PUT_REC_NOLOCK(fd, mf, sizeof(invt_mediafile_t), pos);
	if (rval < 0) {
		return -1;
	}

	return rval;
}








/*----------------------------------------------------------------------*/
/* get_sessinfo                                                         */
/*                                                                      */
/* get both the seshdr and session infomation.                          */
/* caller takes the responsibility of locking.                          */
/*----------------------------------------------------------------------*/

int
stobj_get_sessinfo (inv_sestoken_t tok, invt_seshdr_t *hdr,
		     invt_session_t *ses)
{
	int rval;
	int fd = tok->sd_invtok->d_stobj_fd;

	/* get the session header first */
	if ((rval = GET_REC_NOLOCK(fd, hdr, sizeof(invt_seshdr_t),
			     tok->sd_sesshdr_off)) > 0) {
		rval = GET_REC_NOLOCK(fd, ses, sizeof(invt_session_t),
			     tok->sd_session_off);
	}

	return rval;
}



/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

bool_t
stobj_pack_sessinfo(int fd, invt_session_t *ses, invt_seshdr_t *hdr,
		     void  **bufpp, size_t *bufszp)
{
	size_t	      	stmsz;
	uint		i, j;
	size_t		sessz;
	invt_stream_t  *strms;
	char	       *sesbuf, *sesbufcp;
	off64_t		off;
	invt_mediafile_t mf;

	stmsz = sizeof(invt_stream_t) * ses->s_cur_nstreams;

	/* the initial size without the mediafiles */
	sessz = strlen(INVTSESS_COOKIE) * sizeof(char) +
		sizeof(inv_version_t) +
		sizeof(inv_version_t) +  /* added to fix 64 bit alignment prob */
		sizeof(invt_session_t) + sizeof(invt_seshdr_t) + stmsz;

	/* now get all the streams of this session */
	strms = calloc (ses->s_cur_nstreams, sizeof(invt_stream_t));
	if (GET_REC_NOLOCK(fd, strms, stmsz, hdr->sh_streams_off) < 0) {
		free (strms);
		return BOOL_FALSE;
	}

	for (i = 0; i < ses->s_cur_nstreams; i++)
		sessz += sizeof(invt_mediafile_t) *
			 (size_t) strms[i].st_nmediafiles;

	/* Now we know how big this entire thing is going to be */
	sesbufcp = sesbuf = calloc(1, sessz);
	assert(sesbuf);

	/* Copy everything. Note that we don't bother to adjust the offsets
	   either in the seshdr or in the mediafiles, because we don't need
	   those in order to restore this session (since everything's
	   contiguous) */

	/* magic cookie that we put for sanity checking in case of an
	   earthquake or something :) */
	strcpy(sesbuf, INVTSESS_COOKIE);
	sesbuf += (size_t)(strlen(INVTSESS_COOKIE) * sizeof(char));

	/* This was originally INV_VERSION. Changed it to mean packed inventory
	 * version number and added another inv_version_t to contain the INV_VERSION.
	 * The primary intent of this change was to make everything 64 bit aligned,
	 * but we also got the advantage of separating the packed inv version from
	 * the general inventory version
	 */
	*(inv_version_t *)sesbuf = INT_GET(PACKED_INV_VERSION, ARCH_CONVERT);
	sesbuf += sizeof(inv_version_t);

	/* This has the INV_VERSION */
	*(inv_version_t *)sesbuf = INT_GET(INV_VERSION, ARCH_CONVERT);
	sesbuf += sizeof(inv_version_t);

	xlate_invt_seshdr(hdr, (invt_seshdr_t *)sesbuf, 1);
	sesbuf += sizeof(invt_seshdr_t);

	xlate_invt_session(ses, (invt_session_t *)sesbuf, 1);
	sesbuf += sizeof(invt_session_t);

	for (i = 0; i < ses->s_cur_nstreams; i++) {
		xlate_invt_stream(strms, (invt_stream_t *)sesbuf, 1);
		sesbuf += sizeof(invt_stream_t);
	}

	/* now append all the mediafiles */
	for (i = 0; i < ses->s_cur_nstreams; i++) {
		off = strms[i].st_firstmfile;
		for (j = 0; j < strms[i].st_nmediafiles;
		     j++,
		     off = mf.mf_nextmf) {
			assert(off);
			if (GET_REC_NOLOCK(fd, &mf,
					     sizeof(invt_mediafile_t),
					     off) <= 0) {
				free(strms);
				free(sesbuf);
				return BOOL_FALSE;
			}
			xlate_invt_mediafile(&mf, (invt_mediafile_t *)sesbuf, 1);
			sesbuf += sizeof(invt_mediafile_t);
		}
	}

	free(strms);
	*bufpp = sesbufcp;
	*bufszp = sessz;

	return BOOL_TRUE;
}


/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

bool_t
stobj_getsession_byuuid(
	int fd,
	invt_seshdr_t *hdr,
	void *sesid,
	void **buf)
{
	invt_session_t ses;

	/* retrieve the session */
	if (GET_REC_NOLOCK(fd, &ses, sizeof(invt_session_t),
			     hdr->sh_sess_off) < 0)
		return -1;

	/* now see if this is the one that caller is askin for */
	if (uuid_compare(ses.s_sesid, *((uuid_t *)sesid))) {
		return BOOL_FALSE;
	}

	/* yay. we found the session. so, make the session struct and
	   put it in the buffer */
	stobj_copy_invsess(fd, hdr, &ses, (inv_session_t **)buf);

	return BOOL_TRUE;

}




/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

bool_t
stobj_getsession_bylabel(
	int fd,
	invt_seshdr_t *hdr,
	void *seslabel,
	void **buf)
{
	invt_session_t ses;

	/* retrieve the session */
	if (GET_REC_NOLOCK(fd, &ses, sizeof(invt_session_t),
			     hdr->sh_sess_off) < 0)
		return -1;

	/* now see if this is the one that caller is askin for */
	if (!STREQL(ses.s_label, (char *)seslabel)) {
		return BOOL_FALSE;
	}

	/* yay. we found the session. so, make the session struct and
	   put it in the buffer */
	stobj_copy_invsess(fd, hdr, &ses, (inv_session_t **)buf);

	return BOOL_TRUE;

}


/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

/* ARGSUSED */
bool_t
stobj_delete_mobj(int fd,
		  invt_seshdr_t *hdr,
		  void *arg,
		  void **buf)
{
	/* XXX fd needs to be locked EX, not SH */
	uuid_t *moid = *buf;
	invt_session_t ses;
	invt_stream_t  *strms;
	off64_t		off;
	invt_mediafile_t *mf, *mfiles;
	uint 		nmfiles;
	uint		i, j;
	bool_t 		dirty;

	if (GET_REC_NOLOCK(fd, &ses, sizeof(invt_session_t),
			     hdr->sh_sess_off) < 0)
		return -1;

	/* now get all the streams of this session */
	strms = calloc (ses.s_cur_nstreams, sizeof(invt_stream_t));
	if (GET_REC_NOLOCK(fd, strms,
			     sizeof(invt_stream_t) * ses.s_cur_nstreams,
			     hdr->sh_streams_off) < 0) {
		free (strms);
		return BOOL_FALSE;
	}

	/* now look at all the mediafiles in all the streams */
	for (i = 0; i < ses.s_cur_nstreams; i++) {
		off = strms[i].st_firstmfile;
		nmfiles = strms[i].st_nmediafiles;
		mfiles = mf = calloc(nmfiles, sizeof(invt_mediafile_t));
		for (j = 0; j < nmfiles;
		     j++,
		     off = mf->mf_nextmf,
		     mf++) {

/*  The prob is that we need to keep track of where we got these mfiles from
    as we get them, or we wont know how to put them back if they are dirty.
*/
			assert(off);
			if (GET_REC_NOLOCK(fd, mf,
					     sizeof(invt_mediafile_t),
					     off) <= 0) {
				free(strms);
				free(mfiles);
				return BOOL_FALSE;
			}
		}

		/* We have all the media files of this stream. Make another
		   pass, checking to see if we need to remove any mfiles */
 		dirty = BOOL_FALSE;

		for (j = 0; j < nmfiles; j++) {
			mf = &mfiles[j];
			if (!uuid_compare(mf->mf_moid, *moid)) {
#ifdef INVT_DEBUG
				printf(" found one\n");
#endif

/*                                dirty = BOOL_TRUE;

				if (j == 0)
				       strms[i].st_firstmfile = mf->mf_nextmf;
				else
				       mfiles[j-1].mf_nextmf = mf->mf_nextmf;

				if (j == nmfiles - 1)
				       strms[i].st_lastmfile =;
*/
			}

		}
		free (mfiles);
		if (dirty);
	}

	free (strms);

	return BOOL_FALSE; /* ret FALSE, or it'll stop iteration */
}






/*----------------------------------------------------------------------*/
/* stobj_unpack_sessinfo                                                */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

bool_t
stobj_unpack_sessinfo(
        void              *bufp,
        size_t             bufsz,
	invt_sessinfo_t   *s)
{
	uint 		 i;
	char	         *tmpbuf;
	char 		 *p = (char *)bufp;

	assert (bufp);

	tmpbuf = (char *)malloc(bufsz);

	/* first make sure that the magic cookie at the beginning is right.
	   this isn't null-terminated */
	if (strncmp(p, INVTSESS_COOKIE, strlen(INVTSESS_COOKIE)) != 0) {
		mlog(MLOG_NORMAL | MLOG_INV, _(
			"INV: inv_put_session: unknown cookie\n"));
		return BOOL_FALSE;
	}
	/* skip the cookie */
	p += strlen(INVTSESS_COOKIE) * sizeof(char);

	/* Check the packing version number. In version 1, this was the only version number.
	 * see the comment in stobj_pack_sessinfo().
	 */
	if (INT_GET(*(inv_version_t *) p, ARCH_CONVERT) == PACKED_INV_VERSION_1) {
		char *temp_p;
		size_t		 tempsz;

	        mlog(MLOG_DEBUG | MLOG_INV,"INV: packed inventory version  = 1\n");

		p += sizeof(inv_version_t);

		/* We hit a 64 bit alignment issue at this point leading to a
		 * SIGBUS and core dump. The best way to handle it is to
		 * bcopy the remaining part of bufp to a new malloc'ed area
		 * which will be 64 bit aligned. This is a memory leak, but not much.
		 * Have to do this because xfsrestore does another round of
		 * unpack later, so can't disturb the original data.
		 * This is fixed in PACKED_INV_VERSION_2 by adding another (inv_version_t) to
		 * have the INV_VERSION. This makes everything 64 bit aligned.
		 */

		tempsz = bufsz - (strlen(INVTSESS_COOKIE) * sizeof(char))
			       - sizeof(inv_version_t);
		temp_p = calloc(1, tempsz);
		bcopy(p, temp_p, tempsz);
		p = temp_p;
	} else if (INT_GET(*(inv_version_t *) p, ARCH_CONVERT) == PACKED_INV_VERSION_2) {
	        mlog(MLOG_DEBUG | MLOG_INV,"INV: packed inventory version = 2\n");

		p += sizeof(inv_version_t); /* skip the packed inventory version */
		/* At this point, don't care about the INV_VERSION. Maybe in future */
		p += sizeof(inv_version_t); /* skip the inventory version */
	} else {
	        mlog(MLOG_NORMAL | MLOG_INV, _(
		      "INV: inv_put_session: unknown packed inventory version"
		      " %d\n"), *( inv_version_t *) p);
		return BOOL_FALSE;
	}

	xlate_invt_seshdr((invt_seshdr_t *)p, (invt_seshdr_t *)tmpbuf, 1);
	bcopy(tmpbuf, p, sizeof(invt_seshdr_t));

	/* get the seshdr and then, the remainder of the session */
	s->seshdr = (invt_seshdr_t *)p;
	s->seshdr->sh_sess_off = -1;
	p += sizeof(invt_seshdr_t);


	xlate_invt_session((invt_session_t *)p, (invt_session_t *)tmpbuf, 1);
	bcopy (tmpbuf, p, sizeof(invt_session_t));
	s->ses = (invt_session_t *)p;
	p += sizeof(invt_session_t);

	/* the array of all the streams belonging to this session */
	xlate_invt_stream((invt_stream_t *)p, (invt_stream_t *)tmpbuf, 1);
	bcopy(tmpbuf, p, sizeof(invt_stream_t));
	s->strms = (invt_stream_t *)p;
	p += s->ses->s_cur_nstreams * sizeof(invt_stream_t);

	/* all the media files */
	s->mfiles = (invt_mediafile_t *)p;

#ifdef INVT_DELETION
	{
		int tmpfd = open("moids", O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
		uint j;
		invt_mediafile_t *mmf = s->mfiles;
		for (i=0; i< s->ses->s_cur_nstreams; i++) {
			for (j=0; j< s->strms[i].st_nmediafiles;
			     j++, mmf++)
				xlate_invt_mediafile((invt_mediafile_t *)mmf, (invt_mediafile_t *)tmpbuf, 1);
				bcopy(tmpbuf, mmf, sizeof(invt_mediafile_t));
				put_invtrecord(tmpfd, &mmf->mf_moid,
					 sizeof(uuid_t), 0, SEEK_END, 0);
		}
		close(tmpfd);
	}
#endif
	for (i = 0; i < s->ses->s_cur_nstreams; i++) {
		p += (size_t) (s->strms[i].st_nmediafiles)
			* sizeof(invt_mediafile_t);
	}

	/* sanity check the size of the buffer given to us vs. the size it
	   should be */
	if ((size_t) (p - (char *) bufp) != bufsz) {
		mlog(MLOG_DEBUG | MLOG_INV, "p-bufp %d != bufsz %d entsz %d\n",
		      (int)(p - (char *) bufp), (int) bufsz,
	      (int) (sizeof(invt_entry_t)));
	}
	assert((size_t) (p - (char *) bufp) == bufsz);

	return BOOL_TRUE;
}


/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

int
stobj_make_invsess(int fd, inv_session_t **buf, invt_seshdr_t *hdr)
{
	invt_session_t ses;

	/* load in the rest of the session, but not the streams */
	if (GET_REC_NOLOCK(fd, &ses, sizeof(ses), hdr->sh_sess_off)
	    < 0) {
		return -1;
	}

	return stobj_copy_invsess(fd, hdr, &ses, buf);
}


void
stobj_convert_mfile(inv_mediafile_t *expmf, invt_mediafile_t *mf)
{
	/* copy the mediafile into the exported structure */
	memcpy(&expmf->m_moid, &mf->mf_moid, sizeof(uuid_t));
	strcpy(expmf->m_label, mf->mf_label);
	expmf->m_mfile_index = mf->mf_mfileidx;
	expmf->m_startino = mf->mf_startino.ino;
	expmf->m_startino_off = mf->mf_startino.offset;
	expmf->m_endino = mf->mf_endino.ino;
	expmf->m_endino_off = mf->mf_endino.offset;
	expmf->m_size = mf->mf_size;
	expmf->m_isgood = (mf->mf_flag & INVT_MFILE_GOOD) ?
		BOOL_TRUE : BOOL_FALSE;
	expmf->m_isinvdump = (mf->mf_flag & INVT_MFILE_INVDUMP)?
		BOOL_TRUE : BOOL_FALSE;

}

void
stobj_convert_strm(inv_stream_t *expstrm, invt_stream_t *strm)
{

	expstrm->st_interrupted = strm->st_interrupted;
	expstrm->st_startino = strm->st_startino.ino;
	expstrm->st_startino_off =
		strm->st_startino.offset;
	expstrm->st_endino = strm->st_endino.ino;
	expstrm->st_endino_off = strm->st_endino.offset;
	strcpy(expstrm->st_cmdarg, strm->st_cmdarg);

	expstrm->st_nmediafiles = strm->st_nmediafiles;

	/* caller is responsible for initializing this */
	expstrm->st_mediafiles = NULL;
}

void
stobj_convert_session(inv_session_t *ises, invt_session_t *ses,
		      invt_seshdr_t *hdr)
{
	ises->s_time = hdr->sh_time;
	ises->s_level = hdr->sh_level;
	ises->s_ispartial = IS_PARTIAL_SESSION(hdr)? BOOL_TRUE: BOOL_FALSE;
	ises->s_isresumed = IS_RESUMED_SESSION(hdr)? BOOL_TRUE: BOOL_FALSE;
	ises->s_nstreams = ses->s_cur_nstreams;
	memcpy(&ises->s_sesid, &ses->s_sesid, sizeof(uuid_t));
	memcpy(&ises->s_fsid, &ses->s_fsid, sizeof(uuid_t));
	strcpy(ises->s_mountpt, ses->s_mountpt);
	strcpy(ises->s_devpath, ses->s_devpath);
	strcpy(ises->s_label, ses->s_label);

	/* caller is responsible for initializing this */
	ises->s_streams = NULL;

}


/*----------------------------------------------------------------------*/
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

int
stobj_copy_invsess(int fd,
		   invt_seshdr_t *hdr,
		   invt_session_t *ses,
		   inv_session_t **buf)
{
	inv_session_t  *ises;
	invt_stream_t  *strms;
	int i;
	invt_mediafile_t mf;

	strms = calloc (ses->s_cur_nstreams, sizeof(invt_stream_t));

	/* load in all the stream-headers */
	if (GET_REC_NOLOCK(fd, strms,
			     ses->s_cur_nstreams * sizeof(invt_stream_t),
			     hdr->sh_streams_off) < 0) {
		free (strms);
		return -1;
	}

	ises = calloc(1, sizeof(inv_session_t));
	stobj_convert_session(ises, ses, hdr);
	ises->s_streams = calloc(ses->s_cur_nstreams, sizeof(inv_stream_t));

	/* fill in the stream structures too */
	i = (int) ses->s_cur_nstreams;
	while (i--) {
		off64_t		 off;
		uint            j, nmf;

		stobj_convert_strm(&ises->s_streams[i], &strms[i]);
		nmf = strms[i].st_nmediafiles;
		off = strms[i].st_firstmfile;

		if (nmf)
			ises->s_streams[i].st_mediafiles = calloc(nmf,
						    sizeof(inv_mediafile_t));
		assert(!nmf || ises->s_streams[i].st_mediafiles);

		for (j = 0; j < nmf;
		      j++,
		      off = mf.mf_nextmf) {
			assert(off);
			if (GET_REC_NOLOCK(fd, &mf,
					     sizeof(invt_mediafile_t),
					     off) <= 0) {
				INV_PERROR("stobj::make_invsess\n");
				free(strms);
				free(ises);
				return -1;
			}

			/* copy the mediafile into the exported structure */
			if (ises->s_streams[i].st_mediafiles) {
				stobj_convert_mfile(&ises->s_streams[i].st_mediafiles[j],
						&mf);
			} else {
				mlog(MLOG_ERROR, _(
					"Failed to get data from media file: "
					"possible file corruption\n"));
				mlog_exit_hint(RV_CORRUPT);
				return -1;
			}
		}

	}


	free(strms);
	*buf = ises;

	return 1;
}


/*----------------------------------------------------------------------*/
/*  Given a sessinfo structure, this makes an exportable inv_session.   */
/*                                                                      */
/*                                                                      */
/*----------------------------------------------------------------------*/

void
stobj_convert_sessinfo(inv_session_t **buf, invt_sessinfo_t *sinfo)
{
	inv_session_t  *ises;
	int i, j, nmf;
	int nstreams;
	invt_mediafile_t *mf;

	ises = calloc(1, sizeof(inv_session_t));

	stobj_convert_session(ises, sinfo->ses, sinfo->seshdr);
	ises->s_streams = calloc(ises->s_nstreams, sizeof(inv_stream_t));
	mf = sinfo->mfiles;
	nstreams = (int) ises->s_nstreams;
	for (i = 0; i < nstreams; i++) {
		stobj_convert_strm(&ises->s_streams[i], &sinfo->strms[i]);
		nmf = (int) ises->s_streams[i].st_nmediafiles;
		ises->s_streams[i].st_mediafiles = calloc((uint) nmf,
						    sizeof(inv_mediafile_t));

		for (j = 0; j < nmf; j++) {
			stobj_convert_mfile(&ises->s_streams[i].st_mediafiles[j],
					     mf++);
		}
	}

	*buf = ises;
}



int
stobj_hdrcmp(const void *h1, const void *h2)
{
	return (int) (((invt_seshdr_t *)h1)->sh_time -
		       ((invt_seshdr_t *)h2)->sh_time);
}








void
DEBUG_sessprint(invt_session_t *ses)
{
	char str[UUID_STR_LEN + 1];
	uuid_unparse(ses->s_fsid, str);

	printf("-------- session -------------\n");
	printf("label\t%s\n", ses->s_label);
	printf("mount\t%s\n", ses->s_mountpt);
	printf("devpath\t%s\n", ses->s_devpath);
	printf("strms\t%d\n", ses->s_cur_nstreams);
	printf("fsid\t%s\n", str);
	printf("-------- end -------------\n");
}

bool_t
mobj_eql (inv_mediafile_t *mfp, invt_mobjinfo_t *mobj)
{

	if (mobj->type == INVT_MOID) {
		if (!uuid_compare(*((uuid_t*) mobj->value),
			      mfp->m_moid)) {
			return BOOL_TRUE;
		}
	} else {
		if (STREQL((char*) mobj->value,
			    mfp->m_label)) {
			return BOOL_TRUE;
		}
	}

	return BOOL_FALSE;
}


bool_t
check_for_mobj (inv_session_t *ses, invt_mobjinfo_t *mobj)
{
	int i;
	uint j;
	inv_mediafile_t *mfp;

	for (i = 0; i < (int) ses->s_nstreams; i++) {
		for (j = 0; j < ses->s_streams[i].st_nmediafiles; j++) {
			mfp = &ses->s_streams[i].st_mediafiles[j];
			if (mobj_eql(mfp, mobj))
				return BOOL_TRUE;
		}
	}

	return BOOL_FALSE; /* There are no matching media objects to print */
}




void
DEBUG_sessionprint(inv_session_t *ses, uint ref, invt_pr_ctx_t *prctx)
{
	char str[UUID_STR_LEN + 1];
	int i;
	inv_mediafile_t *mfp;
	static uint fsidxprinted = -1;

       invt_mobjinfo_t *mobj = &prctx->mobj;

	bool_t  moidsearch = (mobj && mobj->type != INVT_NULLTYPE);
	if (moidsearch){
		if (!check_for_mobj (ses, mobj))
			return;
	}

	if (ref == 0 || fsidxprinted != (uint) prctx->index) {
		fsidxprinted = (uint) prctx->index;

		printf("file system %d:\n", prctx->index);
		uuid_unparse(ses->s_fsid, str);
		printf("\tfs id:\t\t%s\n", str);
	}

	if (prctx->depth == PR_FSONLY)
		return;

	if ((prctx->level != PR_MAXLEVEL) && (prctx->level != ses->s_level))
		return;

	printf("\tsession %d:\n", ref);
	printf("\t\tmount point:\t%s\n", ses->s_mountpt);
	printf("\t\tdevice:\t\t%s\n", ses->s_devpath);
	printf("\t\ttime:\t\t%s", ctime32( &ses->s_time));
	printf("\t\tsession label:\t\"%s\"\n", ses->s_label);
	uuid_unparse(ses->s_sesid, str);
	printf("\t\tsession id:\t%s\n", str);
	printf("\t\tlevel:\t\t%d\n", ses->s_level);
	printf("\t\tresumed:\t%s\n", ses->s_isresumed ? "YES" : "NO");
	printf("\t\tsubtree:\t%s\n", ses->s_ispartial ? "YES" : "NO");
	printf("\t\tstreams:\t%d\n", ses->s_nstreams);

	if (prctx->depth == PR_SESSONLY)
		return;

	for (i = 0; i < (int) ses->s_nstreams; i++) {
		uint j;
		printf("\t\tstream %d:\n", i);
		printf("\t\t\tpathname:\t%s\n", ses->s_streams[i].st_cmdarg);
		printf("\t\t\tstart:\t\tino %llu offset %lld\n",
		        (unsigned long long)ses->s_streams[i].st_startino,
		        (long long)ses->s_streams[i].st_startino_off);
		printf("\t\t\tend:\t\tino %llu offset %lld\n",
		        (unsigned long long)ses->s_streams[i].st_endino,
		        (long long)ses->s_streams[i].st_endino_off);
		printf("\t\t\tinterrupted:\t%s\n",
		        ses->s_streams[i].st_interrupted ? "YES" : "NO");
		printf("\t\t\tmedia files:\t%d\n",
		        ses->s_streams[i].st_nmediafiles);

		if (prctx->depth == PR_STRMSONLY)
			continue;

		for (j = 0; j < ses->s_streams[i].st_nmediafiles; j++) {
			mfp = &ses->s_streams[i].st_mediafiles[j];
			if (moidsearch) {
				if (!mobj_eql(mfp, mobj))
					continue;
			}
			printf("\t\t\tmedia file %d:", j);
			/*
			if (mfp->m_isinvdump)
				printf(" SESSION INVENTORY");
			*/
			printf("\n");
			printf("\t\t\t\tmfile index:\t%d\n", mfp->m_mfile_index);
			printf("\t\t\t\tmfile type:\t");
			if (mfp->m_isinvdump) {
				printf("inventory\n");
			} else {
				printf("data\n");
			}
			printf("\t\t\t\tmfile size:\t%llu\n",
				(unsigned long long)mfp->m_size);

			if (!mfp->m_isinvdump) {
				printf("\t\t\t\tmfile start:"
					"\tino %llu offset %lld\n",
					(unsigned long long)mfp->m_startino,
					(long long)mfp->m_startino_off);
				printf("\t\t\t\tmfile end:"
					"\tino %llu offset %lld\n",
					(unsigned long long)mfp->m_endino,
					(long long)mfp->m_endino_off);
			}

			printf("\t\t\t\tmedia label:\t\"%s\"\n",
				mfp->m_label);
			uuid_unparse(mfp->m_moid, str);
			printf("\t\t\t\tmedia id:\t%s\n", str);
		}
	}
}