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