xref: /titanic_41/usr/src/lib/libnisdb/db_pickle.cc (revision 1c42de6d020629af774dd9e9fc81be3f3ed9398e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	db_pickle.cc
24  *
25  *	Copyright (c) 1988-2000 by Sun Microsystems, Inc.
26  *	All Rights Reserved.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /* #include <sys/types.h> */
32 #include <stdio.h>
33 /* #include <syslog.h> */
34 #include <string.h>
35 #include <unistd.h>
36 #include "db_headers.h"
37 #include "db_pickle.h"
38 #include "nisdb_mt.h"
39 
40 /* Constructor.  Creates pickle_file with given name and mode. */
41 pickle_file::pickle_file(char* f, pickle_mode m)
42 {
43 	if ((filename = strdup(f)) == NULL) {
44 		FATAL("pickle_file::pickle_file: cannot allocate space",
45 			DB_MEMORY_LIMIT);
46 	}
47 
48 	INITRW(pickle);
49 
50 	mode = m;
51 }
52 
53 /*
54  * Opens pickle_file with mode specified with constructor.
55  * Returns TRUE if open was successful; FALSE otherwise.
56  */
57 bool_t
58 pickle_file::open()
59 {
60 	WRITELOCK(this, FALSE, "w pickle_file::open");
61 	if (mode == PICKLE_READ) {
62 		file = fopen(filename, "r");
63 		if (file)
64 			xdrstdio_create(&(xdr), file, XDR_DECODE);
65 	} else if (mode == PICKLE_WRITE) {
66 		file = fopen(filename, "w");
67 		if (file) {
68 			setvbuf(file, NULL, _IOFBF, 81920);
69 			xdrstdio_create(&(xdr), file, XDR_ENCODE);
70 		}
71 	} else if (mode == PICKLE_APPEND) {
72 		file = fopen(filename, "a");
73 		if (file)
74 			xdrstdio_create(&(xdr), file, XDR_ENCODE);
75 	}
76 	if (file == NULL) {
77 		WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
78 		return (FALSE);
79 	}
80 	WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
81 	return (TRUE);
82 }
83 
84 
85 /* Closes pickle_file.  Returns 0 if successful; -1 otherwise. */
86 int
87 pickle_file::close()
88 {
89 	int	ret;
90 
91 	WRITELOCK(this, EOF, "w pickle_file::close");
92 	xdr_destroy(&(xdr));
93 	ret = fclose(file);
94 	WRITEUNLOCK(this, EOF, "wu pickle_file::close");
95 	return (ret);
96 }
97 
98 
99 /*
100  * dump or load data structure to/from 'filename' using function 'f'.
101  * dump or load is determined by 'mode' with which pickle_file was created.
102  * Returns 0 if successful; 1 if file cannot be opened in mode
103  * specified; -1 if transfer failed do to encoding/decoding errors.
104 */
105 int
106 pickle_file::transfer(pptr p, bool_t (*f) (XDR*, pptr))
107 {
108 	WRITELOCK(this, -1, "w pickle_file::transfer");
109 	if (open()) {
110 		if ((f)(&xdr, p) == FALSE) {
111 			close();
112 			WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
113 			return (-1);
114 		} else {
115 			fsync(fileno(file));
116 			WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
117 			return (close());
118 		}
119 	}
120 	WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
121 	return (1);
122 }
123