xref: /titanic_50/usr/src/lib/libnisdb/db_vers.cc (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	db_vers.cc
24*7c478bd9Sstevel@tonic-gate  *
25*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26*7c478bd9Sstevel@tonic-gate  *	All Rights Reserved.
27*7c478bd9Sstevel@tonic-gate  */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "db_headers.h"
35*7c478bd9Sstevel@tonic-gate #include "db_vers.h"
36*7c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate const long unsigned MAXLOW = 32768*32768;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /* Constructor that makes copy of 'other'. */
vers(vers * other)41*7c478bd9Sstevel@tonic-gate vers::vers(vers* other)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	INITRW(vers);
44*7c478bd9Sstevel@tonic-gate 	assign(other);
45*7c478bd9Sstevel@tonic-gate }
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate void
assign(vers * other)48*7c478bd9Sstevel@tonic-gate vers::assign(vers* other)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "w vers::assign");
51*7c478bd9Sstevel@tonic-gate 	if (other == NULL) {
52*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "vers::vers: making copy of null vers?");
53*7c478bd9Sstevel@tonic-gate 		vers_high = vers_low = time_sec = time_usec = 0;
54*7c478bd9Sstevel@tonic-gate 	} else {
55*7c478bd9Sstevel@tonic-gate 		time_sec = other->time_sec;
56*7c478bd9Sstevel@tonic-gate 		time_usec = other->time_usec;
57*7c478bd9Sstevel@tonic-gate 		vers_low = other->vers_low;
58*7c478bd9Sstevel@tonic-gate 		vers_high = other->vers_high;
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "wu vers::assign");
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Creates new 'vers' with next higher minor version.
65*7c478bd9Sstevel@tonic-gate  * If minor version exceeds MAXLOW, bump up major version instead.
66*7c478bd9Sstevel@tonic-gate  * Set timestamp to that of the current time.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate vers*
nextminor()69*7c478bd9Sstevel@tonic-gate vers::nextminor()
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r vers::nextminor");
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	vers * newvers = new vers;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (newvers == NULL) {
76*7c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru vers::nextminor DB_MEMORY_LIMIT");
77*7c478bd9Sstevel@tonic-gate 		FATAL3("vers::nextminor: cannot allocation space",
78*7c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, NULL);
79*7c478bd9Sstevel@tonic-gate 	}
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	struct timeval mt;
82*7c478bd9Sstevel@tonic-gate 	gettimeofday(&mt, NULL);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	newvers->time_sec = (unsigned int) mt.tv_sec;
85*7c478bd9Sstevel@tonic-gate 	newvers->time_usec = (unsigned int) mt.tv_usec;
86*7c478bd9Sstevel@tonic-gate 	newvers->vers_low = (this->vers_low + 1);
87*7c478bd9Sstevel@tonic-gate 	newvers->vers_high = (this->vers_high);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (newvers->vers_low >= MAXLOW){
90*7c478bd9Sstevel@tonic-gate 		newvers->vers_high++;
91*7c478bd9Sstevel@tonic-gate 		newvers->vers_low = 0;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	READUNLOCK(this, newvers, "ru vers::nextminor");
95*7c478bd9Sstevel@tonic-gate 	return (newvers);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate /*
99*7c478bd9Sstevel@tonic-gate  * Creates new 'vers' with next higher major version.
100*7c478bd9Sstevel@tonic-gate  * Set timestamp to that of the current time.
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate vers*
nextmajor()103*7c478bd9Sstevel@tonic-gate vers::nextmajor()
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	READLOCK(this, NULL, "r vers::nextmajor");
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	vers * newvers = new vers;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (newvers == NULL) {
110*7c478bd9Sstevel@tonic-gate 		READUNLOCK(this, NULL, "ru vers::nextmajor DB_MEMORY_LIMIT");
111*7c478bd9Sstevel@tonic-gate 		FATAL3("vers::nextminor: cannot allocation space",
112*7c478bd9Sstevel@tonic-gate 			DB_MEMORY_LIMIT, NULL);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	struct timeval mt;
116*7c478bd9Sstevel@tonic-gate 	gettimeofday(&mt, NULL);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	newvers->time_sec = (unsigned int) mt.tv_sec;
119*7c478bd9Sstevel@tonic-gate 	newvers->time_usec = (unsigned int) mt.tv_usec;
120*7c478bd9Sstevel@tonic-gate 	newvers->vers_low = 0;
121*7c478bd9Sstevel@tonic-gate 	newvers->vers_high = (this->vers_high+1);
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	READUNLOCK(this, newvers, "ru vers::nextmajor");
124*7c478bd9Sstevel@tonic-gate 	return (newvers);
125*7c478bd9Sstevel@tonic-gate }
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * Predicate indicating whether this vers is earlier than 'other' in
129*7c478bd9Sstevel@tonic-gate  * terms of version numbers.
130*7c478bd9Sstevel@tonic-gate */
131*7c478bd9Sstevel@tonic-gate bool_t
earlier_than(vers * other)132*7c478bd9Sstevel@tonic-gate vers::earlier_than(vers *other)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	int	ret, lret;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	if (other == NULL) {
137*7c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
138*7c478bd9Sstevel@tonic-gate 			"vers::earlier_than: comparing against null vers");
139*7c478bd9Sstevel@tonic-gate 		return (FALSE);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "r vers::earlier_than");
143*7c478bd9Sstevel@tonic-gate 	READLOCKNR(other, lret, "r other vers::earlier_than");
144*7c478bd9Sstevel@tonic-gate 	if (lret != 0) {
145*7c478bd9Sstevel@tonic-gate 		READUNLOCK(this, FALSE, "ru + r other vers::earlier_than");
146*7c478bd9Sstevel@tonic-gate 		return (FALSE);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	if (other->vers_high > vers_high) ret = TRUE;
150*7c478bd9Sstevel@tonic-gate 	else if (other->vers_high < vers_high) ret = FALSE;
151*7c478bd9Sstevel@tonic-gate 	else if (other->vers_low > vers_low) ret = TRUE;
152*7c478bd9Sstevel@tonic-gate 	else ret = FALSE;
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	READUNLOCKNR(other, lret, "ru other vers::earlier_than");
155*7c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, ((lret != 0) ?
156*7c478bd9Sstevel@tonic-gate 				"ru + ru other vers::earlier_than" :
157*7c478bd9Sstevel@tonic-gate 				"ru vers::earlier_than"));
158*7c478bd9Sstevel@tonic-gate 	return (ret);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /* Print the value of this 'vers' to specified file. */
162*7c478bd9Sstevel@tonic-gate void
print(FILE * file)163*7c478bd9Sstevel@tonic-gate vers::print(FILE* file)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	char *thetime;
166*7c478bd9Sstevel@tonic-gate 	thetime = ctime((long *) (&(time_sec)));
167*7c478bd9Sstevel@tonic-gate 	thetime[strlen(thetime)-1] = 0;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	READLOCKV(this, "r vers::print");
170*7c478bd9Sstevel@tonic-gate 	fprintf(file, "version=%u.%u %s:%u",
171*7c478bd9Sstevel@tonic-gate 		vers_high,
172*7c478bd9Sstevel@tonic-gate 		vers_low,
173*7c478bd9Sstevel@tonic-gate 		/* time_sec, */
174*7c478bd9Sstevel@tonic-gate 		thetime,
175*7c478bd9Sstevel@tonic-gate 		time_usec);
176*7c478bd9Sstevel@tonic-gate 	READUNLOCKV(this, "ru vers::print");
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate void
zero()180*7c478bd9Sstevel@tonic-gate vers::zero() {
181*7c478bd9Sstevel@tonic-gate 	WRITELOCKV(this, "r vers::zero");
182*7c478bd9Sstevel@tonic-gate 	vers_high = vers_low = time_sec = time_usec = 0;
183*7c478bd9Sstevel@tonic-gate 	WRITEUNLOCKV(this, "ru vers::zero");
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate bool_t
equal(vers * other)187*7c478bd9Sstevel@tonic-gate vers::equal( vers *other) {
188*7c478bd9Sstevel@tonic-gate 	READLOCK(this, FALSE, "r vers::equal");
189*7c478bd9Sstevel@tonic-gate 	bool_t ret = other != NULL &&
190*7c478bd9Sstevel@tonic-gate 		vers_high == other->vers_high &&
191*7c478bd9Sstevel@tonic-gate 		vers_low == other->vers_low &&
192*7c478bd9Sstevel@tonic-gate 		time_sec == other->time_sec &&
193*7c478bd9Sstevel@tonic-gate 		time_usec == other->time_usec;
194*7c478bd9Sstevel@tonic-gate 	READUNLOCK(this, ret, "ru vers::equal");
195*7c478bd9Sstevel@tonic-gate 	return (ret);
196*7c478bd9Sstevel@tonic-gate };
197