xref: /illumos-gate/usr/src/lib/libnisdb/db_pickle.cc (revision 1da57d55)
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 /* #include <sys/types.h> */
30 #include <stdio.h>
31 /* #include <syslog.h> */
32 #include <string.h>
33 #include <unistd.h>
34 #include "db_headers.h"
35 #include "db_pickle.h"
36 #include "nisdb_mt.h"
37 
38 /* Constructor.  Creates pickle_file with given name and mode. */
pickle_file(char * f,pickle_mode m)39 pickle_file::pickle_file(char* f, pickle_mode m)
40 {
41 	if ((filename = strdup(f)) == NULL) {
42 		FATAL("pickle_file::pickle_file: cannot allocate space",
43 			DB_MEMORY_LIMIT);
44 	}
45 
46 	INITRW(pickle);
47 
48 	mode = m;
49 }
50 
51 /*
52  * Opens pickle_file with mode specified with constructor.
53  * Returns TRUE if open was successful; FALSE otherwise.
54  */
55 bool_t
open()56 pickle_file::open()
57 {
58 	WRITELOCK(this, FALSE, "w pickle_file::open");
59 	if (mode == PICKLE_READ) {
60 		file = fopen(filename, "r");
61 		if (file)
62 			xdrstdio_create(&(xdr), file, XDR_DECODE);
63 	} else if (mode == PICKLE_WRITE) {
64 		file = fopen(filename, "w");
65 		if (file) {
66 			setvbuf(file, NULL, _IOFBF, 81920);
67 			xdrstdio_create(&(xdr), file, XDR_ENCODE);
68 		}
69 	} else if (mode == PICKLE_APPEND) {
70 		file = fopen(filename, "a");
71 		if (file)
72 			xdrstdio_create(&(xdr), file, XDR_ENCODE);
73 	}
74 	if (file == NULL) {
75 		WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
76 		return (FALSE);
77 	}
78 	WRITEUNLOCK(this, FALSE, "wu pickle_file::open");
79 	return (TRUE);
80 }
81 
82 
83 /* Closes pickle_file.  Returns 0 if successful; -1 otherwise. */
84 int
close()85 pickle_file::close()
86 {
87 	int	ret;
88 
89 	WRITELOCK(this, EOF, "w pickle_file::close");
90 	xdr_destroy(&(xdr));
91 	ret = fclose(file);
92 	WRITEUNLOCK(this, EOF, "wu pickle_file::close");
93 	return (ret);
94 }
95 
96 
97 /*
98  * dump or load data structure to/from 'filename' using function 'f'.
99  * dump or load is determined by 'mode' with which pickle_file was created.
100  * Returns 0 if successful; 1 if file cannot be opened in mode
101  * specified; -1 if transfer failed do to encoding/decoding errors.
102 */
103 int
transfer(pptr p,bool_t (* f)(XDR *,pptr))104 pickle_file::transfer(pptr p, bool_t (*f) (XDR*, pptr))
105 {
106 	WRITELOCK(this, -1, "w pickle_file::transfer");
107 	if (open()) {
108 		if ((f)(&xdr, p) == FALSE) {
109 			close();
110 			WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
111 			return (-1);
112 		} else {
113 			fsync(fileno(file));
114 			WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
115 			return (close());
116 		}
117 	}
118 	WRITEUNLOCK(this, -1, "wu pickle_file::transfer");
119 	return (1);
120 }
121