xref: /illumos-gate/usr/src/lib/libnisdb/db_pickle.h (revision 37e2cd25d56b334a2403f2540a0b0a1e6a40bcd1)
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.h
24  *
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #ifndef PICKLE_H
32 #define	PICKLE_H
33 
34 #include "nisdb_rw.h"
35 
36 /*
37  * 'pickle' is the package for storing data structures into files.
38  * 'pickle_file' is the base class.  Classes that inherit this base
39  * class need to instantiate the virtual function 'dump'.
40  */
41 
42 enum pickle_mode {
43 	PICKLE_READ, PICKLE_WRITE, PICKLE_APPEND
44 };
45 
46 typedef enum pickle_mode pickle_mode;
47 
48 typedef void* pptr;		/* pickle pointer */
49 
50 class pickle_file {
51     protected:
52 	FILE *file;		/* file handle */
53 	pickle_mode mode;
54 	XDR xdr;
55 	char *filename;
56 	STRUCTRWLOCK(pickle);
57     public:
58 
59 	/* Constructor.  Creates pickle_file with given name and mode. */
60 	pickle_file(char *, pickle_mode);
61 
62 	~pickle_file()  { delete filename; DESTROYRW(pickle); }
63 
64 	/*
65 	 * Opens pickle_file with mode specified with constructor.
66 	 * Returns TRUE if open was successful; FALSE otherwise.
67 	 */
68 	bool_t open();
69 
70 	/* Closes pickle_file.  Returns 0 if successful; -1 otherwise. */
71 	int close();
72 
73 	/*
74 	 * dump or load data structure to/from 'filename' using function 'f'.
75 	 * dump/load is determined by 'mode' with which pickle_file was created.
76 	 * Returns 0 if successful; 1 if file cannot be opened in mode
77 	 * specified; -1 if transfer failed do to encoding/decoding errors.
78 	 */
79 	int transfer(pptr, bool_t (*f) (XDR*, pptr));
80 
81 	/* Exclusive access */
82 	int acqexcl(void) {
83 		return (WLOCK(pickle));
84 	}
85 
86 	int relexcl(void) {
87 		return (WULOCK(pickle));
88 	}
89 };
90 #endif /* PICKLE_H */
91