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