summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpkg/common/vfpops.c
blob: 4c4b49c47ca21fdb286df228ce8d05dedb0d3809 (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
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */



/*
 * Module:	vfpops.c
 * Synopsis:	Implements virtual file protocol operations
 * Description:
 *
 * This module implements the "Virtual File protocol" operations. These
 * operations are intended to provide very fast access to file data,
 * allowing a file to be accessed in very efficient ways with extremely
 * low-cpu intensive operations. If possible file data is mapped directly
 * into memory allowing the data to be accessed directly. If the data
 * cannot be mapped directly into memory, memory will be allocated and
 * the file data read directly into memory. If that fails currently the
 * file data is not accessible. Other methods of making the file could
 * be implemented in the future (e.g. stdio if all else fails).
 *
 * In general any code that uses stdio to access a file can be changed
 * to use the various "vfp" operations to access a file, with a resulting
 * increase in performance and decrease in cpu time required to access
 * the file contents.
 *
 * Public Methods:
 *
 *   vfpCheckpointFile - Create new VFP that checkpoints existing VFP
 *   vfpCheckpointOpen - open file, allocate storage, return pointer to VFP_T
 *   vfpClose - close file associated with vfp
 *   vfpDecCurrPtr - decrement current character pointer
 *   vfpGetBytesRemaining - get number of bytes remaining to read
 *   vfpGetCurrCharPtr - get pointer to current character
 *   vfpGetCurrPtrDelta - get number of bytes between current and specified char
 *   vfpGetFirstCharPtr - get pointer to first character
 *   vfpGetLastCharPtr - get pointer to last character
 *   vfpGetModifiedLen - get highest modified byte (length) contained in vfp
 *   vfpGetPath - get the path associated with the vfp
 *   vfpGetc - get current character and increment to next
 *   vfpGetcNoInc - get current character - do not increment
 *   vfpGets - get a string from the vfp into a fixed size buffer
 *   vfpIncCurrPtr - increment current character pointer
 *   vfpIncCurrPtrBy - increment current pointer by specified delta
 *   vfpOpen - open file on vfp
 *   vfpPutBytes - put fixed number of bytes to current character and increment
 *   vfpPutFormat - put format one arg to current character and increment
 *   vfpPutInteger - put integer to current character and increment
 *   vfpPutLong - put long to current character and increment
 *   vfpPutc - put current character and increment to next
 *   vfpPuts - put string to current character and increment
 *   vfpRewind - rewind file to first byte
 *   vfpSeekToEnd - seek to end of file
 *   vfpSetCurrCharPtr - set pointer to current character
 *   vfpSetFlags - set flags that affect file access
 *   vfpSetSize - set size of file (for writing)
 *   vfpTruncate - truncate file
 *   vfpWriteToFile - write data contained in vfp to specified file
 */

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#include <libintl.h>
#include "pkglib.h"
#include "pkgstrct.h"
#include "pkglocale.h"

/*
 * These are internal flags that occupy the high order byte of the VFPFLAGS_T
 * flags element of the vfp. These flags may only occupy the high order order
 * 16 bits of the 32-bit unsigned vfp "flags" object.
 */

#define	_VFP_MMAP	0x00010000	/* mmap used */
#define	_VFP_MALLOC	0x00020000	/* malloc used */
#define	_VFP_WRITE	0x00040000	/* file opened for write */
#define	_VFP_READ	0x00080000	/* file opened for reading */
#define	_VFP_MODIFIED	0x00100000	/* contents are marked modified */

/* path name given to "anonymous" (string) vfp */

#define	VFP_ANONYMOUS_PATH	"<<string>>"

/* minimum size file to mmap (64mb) */

#define	MIN_MMAP_SIZE	(64*1024)

/*
 * *****************************************************************************
 * global external (public) functions
 * *****************************************************************************
 */

/*
 * Name:	vfpOpen
 * Description:	Open file on vfp, allocate storage, return pointer to VFP_T
 *		that can be used to access/modify file contents.
 * Arguments:	VFP_T **r_vfp - pointer to pointer to VFP_T
 *		char *a_path - path of file to open and associate with this VFP.
 *			- if the path is (char *)NULL then no file is associated
 *			  with this VFP - this is a way to create a fixed length
 *			  string that can be manipulated with the VFP operators.
 *			  Before the VFP can be used "vfpSetSize" must be called
 *			  to set the size of the string buffer.
 *		char *a_mode - fopen mode to open the file with
 *		VFPFLAGS_T a_flags - one or more flags to control the operation:
 *			- VFP_NONE - no special flags
 *			- VFP_NEEDNOW - file data needed in memory now
 *			- VFP_SEQUENTIAL - memory will be sequentially accessed
 *			- VFP_RANDOM - memory will be randomly accessed
 *			- VFP_NOMMAP - do not use mmap to access file
 *			- VFP_NOMALLOC - do not use malloc to buffer file
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 * Side Effects: r_vfp -- filled in with a pointer to a newly allocated vfp
 *			which can be used with the various vfp functions.
 *		errno -- contains system error number if return is != 0
 */

int
vfpOpen(VFP_T **r_vfp, char *a_path, char *a_mode, VFPFLAGS_T a_flags)
{
	FILE		*fp = (FILE *)NULL;
	VFP_T		*vfp;
	int		lerrno;
	struct stat	statbuf;
	int		pagesize = getpagesize();

	/* reset return VFP/FILE pointers */

	(*r_vfp) = (VFP_T *)NULL;

	/* allocate pre-zeroed vfp object */

	vfp = (VFP_T *)calloc(sizeof (VFP_T), 1);
	if (vfp == (VFP_T *)NULL) {
		return (-1);
	}

	/* create "string" vfp if no path specified */

	if (a_path == (char *)NULL) {
		/*
		 * no path specified - no open file associated with vfp
		 * The vfp is initialized to all zeros - initialize just those
		 * values that need to be non-zero.
		 */

		vfp->_vfpFlags = _VFP_MALLOC;
		vfp->_vfpPath = strdup(VFP_ANONYMOUS_PATH);
		(*r_vfp) = vfp;
		return (0);
	}

	/*
	 * path specified - associate open file with vfp;
	 * return an error if no path or mode specified
	 */

	if (a_mode == (char *)NULL) {
		errno = EFAULT;		/* Bad address */
		(void) free(vfp);
		return (-1);
	}

	/* return an error if an empty path or mode specified */

	if ((*a_path == '\0') || (*a_mode == '\0')) {
		errno = EINVAL;		/* Invalid argument */
		(void) free(vfp);
		return (-1);
	}

	/* open the file */

	fp = fopen(a_path, a_mode);
	if (fp == (FILE *)NULL) {
		lerrno = errno;
		(void) free(vfp);
		errno = lerrno;
		return (-1);
	}

	/* Get the file size */

	if (fstat(fileno(fp), &statbuf) != 0) {
		lerrno = errno;
		(void) fclose(fp);
		(void) free(vfp);
		errno = lerrno;
		return (-1);
	}

	/*
	 * Obtain access to existing file contents:
	 *  -> plan a: map contents file into memory
	 *  -> plan b: on failure just read into large buffer
	 */

	/* attempt to mmap file if mmap is allowed */

	vfp->_vfpStart = MAP_FAILED;	/* assume map failed if not allowed */

	/*
	 * if file is a regular file, and if mmap allowed,
	 * and (malloc not forbidden or size is > minumum size to mmap)
	 */

	if ((S_ISREG(statbuf.st_mode)) && (!(a_flags & VFP_NOMMAP)) &&
		((a_flags & VFP_NOMALLOC) || statbuf.st_size > MIN_MMAP_SIZE)) {
		char *p;
		/* set size to current size of file */

		vfp->_vfpMapSize = statbuf.st_size;

		/*
		 * compute proper size for mapping for the file contents;
		 * add in one extra page so falling off end when file size is
		 * exactly modulo page size does not cause a page fault to
		 * guarantee that the end of the file contents will always
		 * contain a '\0' null character.
		 */

		vfp->_vfpSize = (statbuf.st_size + pagesize +
				(pagesize-(statbuf.st_size % pagesize)));

		/*
		 * mmap allowed: mmap file into memory
		 * first allocate space on top of which the mapping can be done;
		 * this way we can guarantee that if the mapping happens to be
		 * an exact multiple of a page size, that there will be at least
		 * one byte past the end of the mapping that can be accessed and
		 * that is guaranteed to be zero.
		 */

		/* allocate backing space */

		p = (char *)memalign(pagesize, vfp->_vfpSize);
		if (p == (char *)NULL) {
			vfp->_vfpStart = MAP_FAILED;
		} else {
			/* guarantee first byte after end of data is zero */

			p[vfp->_vfpMapSize] = '\0';

			/* map file on top of the backing space */

			vfp->_vfpStart = mmap(p, vfp->_vfpMapSize, PROT_READ,
				MAP_PRIVATE|MAP_FIXED, fileno(fp), (off_t)0);

			/* if mmap succeeded set mmap used flag in vfp */

			if (vfp->_vfpStart != MAP_FAILED) {
				vfp->_vfpFlags |= _VFP_MMAP;
			}
		}
	}

	/* if map failed (or not allowed) attempt malloc (if allowed) */

	if ((vfp->_vfpStart == MAP_FAILED) && (!(a_flags & VFP_NOMALLOC))) {
		/* mmap failed - plan b: read directly into memory */
		ssize_t	rlen;

		/*
		 * compute proper size for allocating storage for file contents;
		 * add in one extra page so falling off end when file size is
		 * exactly modulo page size does not cause a page fault to
		 * guarantee that the end of the file contents will always
		 * contain a '\0' null character.
		 */

		vfp->_vfpSize = statbuf.st_size+pagesize;

		/* allocate buffer to hold file data */

		vfp->_vfpStart = memalign((size_t)pagesize, vfp->_vfpSize);
		if (vfp->_vfpStart == (char *)NULL) {
			lerrno = errno;
			(void) fclose(fp);
			(void) free(vfp);
			errno = lerrno;
			return (-1);
		}

		/* read the file into the buffer */

		if (statbuf.st_size != 0) {
			rlen = read(fileno(fp), vfp->_vfpStart,
							statbuf.st_size);
			if (rlen != statbuf.st_size) {
				lerrno = errno;
				if (lerrno == 0) {
					lerrno = EIO;
				}
				(void) free(vfp->_vfpStart);
				(void) fclose(fp);
				(void) free(vfp);
				errno = lerrno;
				return (-1);
			}

			/* assure last byte+1 is null character */

			((char *)vfp->_vfpStart)[statbuf.st_size] = '\0';
		}

		/* set malloc used flag in vfp */

		vfp->_vfpFlags |= _VFP_MALLOC;
	}

	/* if no starting address all read methods failed */

	if (vfp->_vfpStart == MAP_FAILED) {
		/* no mmap() - no read() - cannot allocate memory */
		(void) fclose(fp);
		(void) free(vfp);
		errno = ENOMEM;
		return (-1);
	}

	/*
	 * initialize vfp contents
	 */

	/* _vfpCurr -> next byte to read */
	vfp->_vfpCurr = (char *)vfp->_vfpStart;

	/* _vfpEnd -> last data byte */
	vfp->_vfpEnd = (((char *)vfp->_vfpStart) + statbuf.st_size)-1;

	/* _vfpHighWater -> last byte written */
	vfp->_vfpHighWater = (char *)vfp->_vfpEnd;

	/* _vfpFile -> associated FILE* object */
	vfp->_vfpFile = fp;

	/* set flags as appropriate */

	(void) vfpSetFlags(vfp, a_flags);

	/* retain path name */

	vfp->_vfpPath = strdup(a_path ? a_path : "");

	/* set read/write flags */

	if (*a_mode == 'w') {
		vfp->_vfpFlags |= _VFP_WRITE;
	}

	if (*a_mode == 'r') {
		vfp->_vfpFlags |= _VFP_READ;
	}

	/* set return vfp pointer */

	(*r_vfp) = vfp;

	/* All OK */

	return (0);
}

/*
 * Name:	vfpClose
 * Description:	Close an open vfp, causing any modified data to be written out
 *		to the file associated with the vfp.
 * Arguments:	VFP_T **r_vfp - pointer to pointer to VFP_T returned by vfpOpen
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 * Side Effects: r_vfp is set to (VFP_T)NULL
 */

int
vfpClose(VFP_T **r_vfp)
{
	int	ret;
	int	lerrno;
	VFP_T	*vfp;

	/* return error if NULL VFP_T** provided */

	if (r_vfp == (VFP_T **)NULL) {
		errno = EFAULT;
		return (-1);
	}

	/* localize access to VFP_T */

	vfp = *r_vfp;

	/* return successful if NULL VFP_T* provided */

	if (vfp == (VFP_T *)NULL) {
		return (0);
	}

	/* reset return VFP_T* handle */

	*r_vfp = (VFP_T *)NULL;

	/*
	 * if closing a file that is open for writing, commit all data if the
	 * backing memory is volatile and if there is a file open to write
	 * the data to.
	 */

	if (vfp->_vfpFlags & _VFP_WRITE) {
		if ((vfp->_vfpFlags & _VFP_MALLOC) &&
				(vfp->_vfpFile != (FILE *)NULL)) {
			size_t	len;

			/* determine number of bytes to write */
			len = vfpGetModifiedLen(vfp);

			/* if modified bytes present commit data to the file */
			if (len > 0) {
				(void) vfpSafePwrite(fileno(vfp->_vfpFile),
						vfp->_vfpStart, len, (off_t)0);
			}
		}
	}

	/* deallocate any allocated storage/mappings/etc */

	if (vfp->_vfpFlags & _VFP_MALLOC) {
		(void) free(vfp->_vfpStart);
	} else if (vfp->_vfpFlags & _VFP_MMAP) {
		/* unmap the file mapping */

		(void) munmap(vfp->_vfpStart, vfp->_vfpMapSize);

		/* free the backing allocation */

		(void) free(vfp->_vfpStart);
	}

	/* free up path */

	(void) free(vfp->_vfpPath);

	/* close the file */

	ret = 0;
	if (vfp->_vfpFile != (FILE *)NULL) {
		ret = fclose(vfp->_vfpFile);
		lerrno = errno;
	}

	/* deallocate the vfp itself */

	(void) free(vfp);

	/* if the fclose() failed, return error and errno */

	if (ret != 0) {
		errno = lerrno;
		return (-1);
	}

	return (0);
}

/*
 * Name:	vfpSetFlags
 * Description:	Modify operation of VFP according to flags specified
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to set flags
 *		VFPFLAGS_T a_flags - one or more flags to control the operation:
 *			- VFP_NEEDNOW - file data needed in memory now
 *			- VFP_SEQUENTIAL - file data sequentially accessed
 *			- VFP_RANDOM - file data randomly accessed
 *			Any other flags specified are silently ignored.
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 */

int
vfpSetFlags(VFP_T *a_vfp, VFPFLAGS_T a_flags)
{
	/* return if no vfp specified */

	if (a_vfp == (VFP_T *)NULL) {
		return (0);
	}

	/* if file data mapped into memory, apply vm flags */

	if ((a_vfp->_vfpSize != 0) && (a_vfp->_vfpFlags & _VFP_MMAP)) {
		/* mmap succeeded: properly advise vm system */

		if (a_flags & VFP_NEEDNOW) {
			/* advise vm system data is needed now */
			(void) madvise(a_vfp->_vfpStart, a_vfp->_vfpMapSize,
							MADV_WILLNEED);
		}
		if (a_flags & VFP_SEQUENTIAL) {
			/* advise vm system data access is sequential */
			(void) madvise(a_vfp->_vfpStart, a_vfp->_vfpSize,
							MADV_SEQUENTIAL);
		}
		if (a_flags & VFP_RANDOM) {
			/* advise vm system data access is random */
			(void) madvise(a_vfp->_vfpStart, a_vfp->_vfpSize,
							MADV_RANDOM);
		}
	}

	return (0);
}

/*
 * Name:	vfpRewind
 * Description:	Reset default pointer for next read/write to start of file data
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to rewind
 * Returns:	void
 *			Operation is always successful
 */

void
vfpRewind(VFP_T *a_vfp)
{
	/* return if no vfp specified */

	if (a_vfp == (VFP_T *)NULL) {
		return;
	}

	/* set high water mark of last modified data */

	if (a_vfp->_vfpCurr > a_vfp->_vfpHighWater) {
		a_vfp->_vfpHighWater = a_vfp->_vfpCurr;
	}

	/* reset next character pointer to start of file data */

	a_vfp->_vfpCurr = a_vfp->_vfpStart;
}

/*
 * Name:	vfpSetSize
 * Description:	Set size of in-memory image associated with VFP
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to set
 *		size_t a_size - number of bytes to associatge with VFP
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 * Side Effects:
 *		Currently only a file that is in malloc()ed memory can
 *		have its in-memory size changed.
 *		An error is returned If the file is mapped into memory.
 *		A file cannot be decreased in size - if the specified
 *		size is less than the current size, the operation is
 *		successful but no change in file size occurs.
 *		If no file is associated with the VFP (no "name" was
 *		given to vfpOpen) the first call to vfpSetSize allocates
 *		the initial size of the file data - effectively calling
 *		"malloc" to allocate the initial memory for the file data.
 *		Once an initial allocation has been made, subsequent calls
 *		to vfpSetSize are effectively a "realloc" of the existing
 *		file data.
 *		All existing file data is preserved.
 */

int
vfpSetSize(VFP_T *a_vfp, size_t a_size)
{
	char	*np;
	size_t	curSize;

	/* return if no vfp specified */

	if (a_vfp == (VFP_T *)NULL) {
		return (0);
	}

	/* if malloc not used don't know how to set size right now */

	if (!(a_vfp->_vfpFlags & _VFP_MALLOC)) {
		return (-1);
	}

	/* adjust size to reflect extra page of data maintained */

	a_size += getpagesize();

	/* if size is not larger than current nothing to do */

	if (a_size <= a_vfp->_vfpSize) {
		return (0);
	}

	/* remember new size */

	curSize = a_vfp->_vfpSize;
	a_vfp->_vfpSize = a_size;

	/* allocate/reallocate memory as appropriate */

	if (a_vfp->_vfpStart != (char *)NULL) {
		np = (char *)realloc(a_vfp->_vfpStart, a_vfp->_vfpSize+1);
		if (np == (char *)NULL) {
			return (-1);
		}
		np[curSize-1] = '\0';
	} else {
		np = (char *)malloc(a_vfp->_vfpSize+1);
		if (np == (char *)NULL) {
			return (-1);
		}
		np[0] = '\0';
	}

	/* make sure last allocated byte is a null */

	np[a_vfp->_vfpSize] = '\0';

	/*
	 * adjust all pointers to account for buffer address change
	 */

	/* _vfpCurr -> next byte to read */
	a_vfp->_vfpCurr = (char *)(((ptrdiff_t)a_vfp->_vfpCurr -
					(ptrdiff_t)a_vfp->_vfpStart) + np);

	/* _vfpHighWater -> last byte written */
	a_vfp->_vfpHighWater = (char *)(((ptrdiff_t)a_vfp->_vfpHighWater -
					(ptrdiff_t)a_vfp->_vfpStart) + np);

	/* _vfpEnd -> last data byte */
	a_vfp->_vfpEnd = (np + a_vfp->_vfpSize)-1;

	/* _vfpStart -> first data byte */
	a_vfp->_vfpStart = np;

	return (0);
}

/*
 * Name:	vfpTruncate
 * Description:	Truncate data associated with VFP
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to truncate
 * Returns:	void
 *			Operation is always successful.
 * Side Effects:
 *		In memory data associated with file is believed to be empty.
 *		Actual memory associated with file is not affected.
 *		If a file is associated with the VFP, it is truncated.
 */

void
vfpTruncate(VFP_T *a_vfp)
{
	/* return if no vfp specified */

	if (a_vfp == (VFP_T *)NULL) {
		return;
	}

	/*
	 * reset all pointers so that no data is associated with file
	 */

	/* current byte is start of data area */

	a_vfp->_vfpCurr = a_vfp->_vfpStart;

	/* last byte written is start of data area */

	a_vfp->_vfpHighWater = a_vfp->_vfpStart;

	/* current character is NULL */

	*a_vfp->_vfpCurr = '\0';

	/* if file associated with VFP, truncate actual file */

	if (a_vfp->_vfpFile != (FILE *)NULL) {
		(void) ftruncate(fileno(a_vfp->_vfpFile), 0);
	}
}

/*
 * Name:	vfpWriteToFile
 * Description:	Write data associated with VFP to specified file
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to write
 *		char *a_path - path of file to write file data to
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 */

int
vfpWriteToFile(VFP_T *a_vfp, char *a_path)
{
	int	fd;
	int	lerrno = 0;
	size_t	len;
	ssize_t	result = 0;

	/* return if no vfp specified */

	if (a_vfp == (VFP_T *)NULL) {
		errno = EFAULT;
		return (-1);
	}

	/* on buffer overflow generate error */

	if ((a_vfp->_vfpOverflow != 0) || (vfpGetBytesAvailable(a_vfp) < 1)) {
		errno = EFBIG;
		return (-1);
	}

	/* open file to write data to */

	fd = open(a_path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
	if (fd < 0) {
		return (-1);
	}

	/* determine number of bytes to write */

	len = vfpGetModifiedLen(a_vfp);

	/*
	 * if there is data associated with the file, write it out;
	 * if an error occurs, close the file and return failure.
	 */

	if (len > 0) {
		result = vfpSafeWrite(fd, a_vfp->_vfpStart, len);
		if (result != len) {
			/* error comitting data - return failure */
			lerrno = errno;
			(void) close(fd);
			errno = lerrno;
			return (-1);
		}
	}

	/* close the file */

	(void) close(fd);

	/* data committed to backing store - clear the modified flag */

	(void) vfpClearModified(a_vfp);

	/* return success */

	return (0);
}

/*
 * Name:	vfpCheckpointFile
 * Description:	Create new VFP that checkpoints existing VFP, can be used by
 *		subsequent call to vfpCheckpointOpen to open a file using the
 *		existing in-memory cache of the contents of the file
 * Arguments:	VFP_T **r_cpVfp - pointer to pointer to VFP_T to be filled in
 *			with "checkpointed file" VFP (backing store)
 *		VFP_T **a_vfp - pointer to pointer to VFP_T returned by vfpOpen
 *			representing the VFP to checkpoint
 *		char *a_path - path to file that is the backing store for the
 *			in-memory data represented by a_vfp - used to verify
 *			that the data in memory is not out of date with respect
 *			to the backing store when vfpCheckpointOpen is called
 *			== (char *)NULL - use path associated with a_vfp
 *				that is, the backing store file in use
 * Returns:	int	== 0 - operation was successful
 *				- r_destVfp contains a pointer to a new VFP that
 *					may be used in a subsequent call to
 *					vfpCheckpointOpen
 *				- the VFP referenced by *a_vfp is free()ed and
 *					must no longer be referenced
 *			!= 0 - operation failed, errno contains reason
 *				- the VFP referenced by *a_vfp is not affected;
 *					the caller may continue to use it
 * Notes:	If the data of a VFP to checkpoint is mmap()ed then this method
 *			returns failure - only malloc()ed data VFPs can be
 *			checkpointed.
 */

int
vfpCheckpointFile(VFP_T **r_cpVfp, VFP_T **a_vfp, char *a_path)
{
	VFP_T		*vfp;		/* newly allocated checkpointed VFP */
	VFP_T		*avfp;		/* local -> to a_vfp */
	struct stat	statbuf;	/* stat(2) info for backing store */

	/* return error if NULL VFP_T** to checkpoint provided */

	if (r_cpVfp == (VFP_T **)NULL) {
		errno = EFAULT;
		return (-1);
	}

	/* reset return checkpoint VFP pointer */

	(*r_cpVfp) = (VFP_T *)NULL;

	/* return error if no VFP to checkpoint specified */

	if (a_vfp == (VFP_T **)NULL) {
		errno = EFAULT;
		return (-1);
	}

	/* localize reference to a_vfp */

	avfp = *a_vfp;

	/* return error if no VFP to checkpoint specified */

	if (avfp == (VFP_T *)NULL) {
		errno = EFAULT;
		return (-1);
	}

	/* on buffer overflow generate error */

	if ((avfp->_vfpOverflow != 0) || (vfpGetBytesAvailable(avfp) < 1)) {
		errno = EFBIG;
		return (-1);
	}

	/* no checkpointing is possible if the existing VFP is mmap()ed */

	if (avfp->_vfpFlags & _VFP_MMAP) {
		errno = EIO;
		return (-1);
	}

	/* if no path specified, grab it from the VFP to checkpoint */

	if ((a_path == (char *)NULL) || (*a_path == '\0')) {
		a_path = avfp->_vfpPath;
	}

	/* backing store required: if VFP is "string" then this is an error */

	if ((a_path == (char *)NULL) ||
				strcmp(a_path, VFP_ANONYMOUS_PATH) == 0) {
		errno = EINVAL;
		return (-1);
	}

	/* Get the VFP to checkpoint (backing store) file size */

	if (stat(a_path, &statbuf) != 0) {
		return (-1);
	}

	/* allocate storage for checkpointed VFP (to return) */

	vfp = (VFP_T *)malloc(sizeof (VFP_T));
	if (vfp == (VFP_T *)NULL) {
		return (-1);
	}

	/*
	 * close any file that is on the VFP to checkpoint (backing store);
	 * subsequent processes can modify the backing store data, and
	 * then when vfpCheckpointOpen is called, either the in-memory
	 * cached data will be used (if backing store unmodified) or else
	 * the in-memory data is released and the backing store is used.
	 */

	if (avfp->_vfpFile != (FILE *)NULL) {
		(void) fclose(avfp->_vfpFile);
		avfp->_vfpFile = (FILE *)NULL;
	}

	/* free any path associated with VFP to checkpoint (backing store) */

	if (avfp->_vfpPath != (char *)NULL) {
		(void) free(avfp->_vfpPath);
		avfp->_vfpPath = (char *)NULL;
	}

	/* copy contents of VFP to checkpoint to checkpointed VFP */

	(void) memcpy(vfp, avfp, sizeof (VFP_T));

	/* free contents of VFP to checkpoint */

	(void) free(avfp);

	/* reset pointer to VFP that has been free'd */

	*a_vfp = (VFP_T *)NULL;

	/* remember path associated with the checkpointed VFP (backing store) */

	vfp->_vfpPath = strdup(a_path);

	/* save tokens that identify the backing store for the in-memory data */

	vfp->_vfpCkDev = statbuf.st_dev;	/* devid holding st_ino inode */
	vfp->_vfpCkIno = statbuf.st_ino;	/* backing store inode */
	vfp->_vfpCkMtime = statbuf.st_mtime;	/* last data modification */
	vfp->_vfpCkSize = statbuf.st_size;	/* backing store size (bytes) */
	vfp->_vfpCkStBlocks = statbuf.st_blocks; /* blocks allocated to file */

	/* pass checkpointed VFP to caller */

	(*r_cpVfp) = vfp;

	/* success! */

	return (0);
}

/*
 * Name:	vfpCheckpointOpen
 * Description:	Open file on vfp, allocate storage, return pointer to VFP_T
 *		that can be used to access/modify file contents. If a VFP_T to
 *		a checkpointed VFP is passed in, and the in memory contents of
 *		the VFP are not out of date with respect to the backing store
 *		file, use the existing in-memory contents - otherwise, discard
 *		the in-memory contents and reopen and reread the file.
 * Arguments:	VFP_T **a_cpVfp - pointer to pointer to VFP_T that represents
 *			checkpointed VFP to use to open the file IF the contents
 *			of the backing store are identical to the in-memory data
 *		VFP_T **r_vfp - pointer to pointer to VFP_T to open file on
 *		char *a_path - path of file to open and associate with this VFP.
 *			- if the path is (char *)NULL then no file is associated
 *			  with this VFP - this is a way to create a fixed length
 *			  string that can be manipulated with the VFP operators.
 *			  Before the VFP can be used "vfpSetSize" must be called
 *			  to set the size of the string buffer.
 *		char *a_mode - fopen mode to open the file with
 *		VFPFLAGS_T a_flags - one or more flags to control the operation:
 *			- VFP_NONE - no special flags
 *			- VFP_NEEDNOW - file data needed in memory now
 *			- VFP_SEQUENTIAL - memory will be sequentially accessed
 *			- VFP_RANDOM - memory will be randomly accessed
 *			- VFP_NOMMAP - do not use mmap to access file
 *			- VFP_NOMALLOC - do not use malloc to buffer file
 * Returns:	int	== 0 - operation was successful
 *			!= 0 - operation failed, errno contains reason
 * Side Effects: r_vfp -- filled in with a pointer to a newly allocated vfp
 *			which can be used with the various VFP functions.
 *		a_cpVfp -- contents reset to zero if used to open the file
 *		errno -- contains system error number if return is != 0
 */

int
vfpCheckpointOpen(VFP_T **a_cpVfp, VFP_T **r_vfp, char *a_path,
	char *a_mode, VFPFLAGS_T a_flags)
{
	FILE		*fp;	/* backing store */
	VFP_T		*cpVfp;	/* local -> to a_cpVfp checkpointed VFP */
	VFP_T		*vfp;	/* new VFP open on checkpointed backing store */
	struct stat	statbuf; /* stat(2) info on backing store */

	/*
	 * if no source VFP, or source VFP empty,
	 * or no backing store, just open file
	 */

	if ((a_cpVfp == (VFP_T **)NULL) || (*a_cpVfp == (VFP_T *)NULL) ||
		((*a_cpVfp)->_vfpStart == (char *)NULL)) {
		(void) vfpClose(a_cpVfp);
		return (vfpOpen(r_vfp, a_path, a_mode, a_flags));
	}

	/* localize access to checkpointed VFP_T (*a_cpVfp) */

	cpVfp = *a_cpVfp;

	/* if no path specified, grab it from the checkpointed VFP */

	if ((a_path == (char *)NULL) || (*a_path == '\0')) {
		a_path = cpVfp->_vfpPath;
	}

	/* return error if no path specified and no path in checkpointed VFP */

	if ((a_path == (char *)NULL) && (*a_path == '\0')) {
		errno = EINVAL;
		return (-1);
	}

	/* if no backing store path, then just open file */

	if (stat(a_path, &statbuf) != 0) {
		(void) vfpClose(a_cpVfp);
		return (vfpOpen(r_vfp, a_path, a_mode, a_flags));
	}

	/*
	 * if backing store tokens do not match checkpointed VFP,
	 * the backing store has been updated since the VFP was checkpointed;
	 * release the in-memory data, and open and read the backing store
	 */

	if ((statbuf.st_size != cpVfp->_vfpCkSize) ||
		(statbuf.st_mtime != cpVfp->_vfpCkMtime) ||
		(statbuf.st_blocks != cpVfp->_vfpCkStBlocks) ||
		(statbuf.st_ino != cpVfp->_vfpCkIno) ||
		(statbuf.st_dev != cpVfp->_vfpCkDev)) {
		(void) vfpClose(a_cpVfp);
		return (vfpOpen(r_vfp, a_path, a_mode, a_flags));
	}

	/*
	 * backing store has not been updated since the VFP was checkpointed;
	 * use the in-memory data without re-reading the backing store; open the
	 * backing store file (if no file already open on the checkpointed VFP)
	 * so there is an open file associated with the in-memory data
	 */

	fp = cpVfp->_vfpFile;
	if (fp == (FILE *)NULL) {
		fp = fopen(a_path, a_mode);
		if (fp == (FILE *)NULL) {
			int	lerrno;

			lerrno = errno;
			(void) vfpClose(a_cpVfp);
			errno = lerrno;
			return (-1);
		}
	}

	/* allocate new VFP object to return as open VFP */

	vfp = (VFP_T *)malloc(sizeof (VFP_T));
	if (vfp == (VFP_T *)NULL) {
		(void) vfpClose(a_cpVfp);
		return (vfpOpen(r_vfp, a_path, a_mode, a_flags));
	}

	/* copy cached checkpointed VFP to new VFP to return */

	(void) memcpy(vfp, cpVfp, sizeof (VFP_T));

	/*
	 * initialize VFP to return contents
	 */

	/* FILE -> file opened on the VFPs backing store */

	vfp->_vfpFile = fp;

	/* release any existing path associated with the VFP */

	if (vfp->_vfpPath != (char *)NULL) {
		(void) free(vfp->_vfpPath);
	}

	/* path associated with the backing store for this VFP */

	vfp->_vfpPath = strdup(a_path);

	/*
	 * data pointers associated with in memory copy of backing store
	 * (such as _vfpHighWater, _vfpEnd, _vfpStart, etc.)
	 * do not need to be modified because we are using the same backing
	 * store as was checkpointed in cpVfp that is pointed to by vfp.
	 */

	/* _vfpCurr -> next byte to read */
	vfp->_vfpCurr = (char *)vfp->_vfpStart;

	/* free checkpointed VFP as it is now open on "vfp" */

	(void) free(cpVfp);

	/* reset callers -> checkpointed VFP */

	(*a_cpVfp) = (VFP_T *)NULL;

	/* set return VFP pointer */

	(*r_vfp) = vfp;

	/* success! */

	return (0);
}

/*
 * Name:	vfpClearModified
 * Description:	Clear the "data is modified" indication from the VFP
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to clear
 *			the "data is modified" indication
 * Returns:	int	- previous setting of "data is modified" indication
 *			== 0 - "data is modified" was NOT previously set
 *			!= 0 - "data is modified" WAS previously set
 */

int
vfpClearModified(VFP_T *a_vfp)
{
	VFPFLAGS_T	flags;

	/* save current flags settings */

	flags = a_vfp->_vfpFlags;

	/* clear "data is modified" flag */

	a_vfp->_vfpFlags &= (~_VFP_MODIFIED);

	/* return previous "data is modified" flag setting */

	return ((flags & _VFP_MODIFIED) != 0);
}

/*
 * Name:	vfpSetModified
 * Description:	Set the "data is modified" indication from the VFP
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to set
 *			the "data is modified" indication
 * Returns:	int	- previous setting of "data is modified" indication
 *			== 0 - "data is modified" was NOT previously set
 *			!= 0 - "data is modified" WAS previously set
 */

int
vfpSetModified(VFP_T *a_vfp)
{
	VFPFLAGS_T	flags;

	/* save current flags settings */

	flags = a_vfp->_vfpFlags;

	/* set "data is modified" flag */

	a_vfp->_vfpFlags |= _VFP_MODIFIED;

	/* return previous "data is modified" flag setting */

	return ((flags & _VFP_MODIFIED) != 0);
}

/*
 * Name:	vfpGetModified
 * Description:	Get the "data is modified" indication from the VFP
 * Arguments:	VFP_T *a_vfp - VFP_T pointer associated with file to get
 *			the "data is modified" indication
 * Returns:	int	- current setting of "data is modified" indication
 *			== 0 - "data is modified" is NOT set
 *			!= 0 - "data is modified" IS set
 */

int
vfpGetModified(VFP_T *a_vfp)
{
	/* return current "data is modified" flag setting */

	return ((a_vfp->_vfpFlags & _VFP_MODIFIED) != 0);
}

/*
 * Name:	vfpSafeWrite
 * Description:	write data to open file safely
 * Arguments:	a_fildes - file descriptor to write data to
 *		a_buf - pointer to buffer containing data to write
 *		a_nbyte - number of bytes to write to open file
 * Returns:	int
 *		< 0 - error, errno set
 *		>= 0 - success
 * NOTE: unlike write(2), vfpSafeWrite() handles partial writes, and will
 * ----- restart the write() until all bytes are written, or an error occurs.
 */

ssize_t
vfpSafeWrite(int a_fildes, void *a_buf, size_t a_nbyte)
{
	ssize_t	r;
	size_t	bytes = a_nbyte;

	for (;;) {
		/* write bytes to file */
		r = write(a_fildes, a_buf, a_nbyte);

		/* return error on failure of write() */
		if (r < 0) {
			/* EAGAIN: try again */
			if (errno == EAGAIN) {
				continue;
			}
			/* EINTR: interrupted - try again */
			if (errno == EINTR) {
				continue;
			}
			return (r);
		}

		/* return total bytes written on success */
		if (r >= a_nbyte) {
			return (bytes);
		}

		/* partial write, adjust pointers, call write again */
		a_buf = (void *)((ptrdiff_t)a_buf + (ptrdiff_t)r);
		a_nbyte -= (size_t)r;
	}
}

/*
 * Name:	vfpSafePwrite
 * Description:	write data to open file safely
 * Arguments:	a_fildes - file descriptor to write data to
 *		a_buf - pointer to buffer containing data to write
 *		a_nbyte - number of bytes to write to open file
 *		a_offset - offset into open file to write the first byte to
 * Returns:	int
 *		< 0 - error, errno set
 *		>= 0 - success
 * NOTE: unlike pwrite(2), vfpSafePwrite() handles partial writes, and will
 * ----- restart the pwrite() until all bytes are written, or an error occurs.
 */

ssize_t
vfpSafePwrite(int a_fildes, void *a_buf, size_t a_nbyte, off_t a_offset)
{
	ssize_t	r;
	size_t	bytes = a_nbyte;

	for (;;) {
		/* write bytes to file */
		r = pwrite(a_fildes, a_buf, a_nbyte, a_offset);

		/* return error on failure of write() */
		if (r < 0) {
			/* EAGAIN: try again */
			if (errno == EAGAIN) {
				continue;
			}
			/* EINTR: interrupted - try again */
			if (errno == EINTR) {
				continue;
			}
			return (r);
		}

		/* return total bytes written on success */
		if (r >= a_nbyte) {
			return (bytes);
		}

		/* partial write, adjust pointers, call write again */
		a_buf = (void *)((ptrdiff_t)a_buf + (ptrdiff_t)r);
		a_nbyte -= (size_t)r;
		a_offset += (off_t)r;
	}
}