xref: /illumos-gate/usr/src/lib/libnisdb/db_pickle.h (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	db_pickle.h
247c478bd9Sstevel@tonic-gate  *
25*a506a34cSth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*a506a34cSth160488  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #ifndef PICKLE_H
307c478bd9Sstevel@tonic-gate #define	PICKLE_H
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "nisdb_rw.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
35*a506a34cSth160488  * 'pickle' is the package for storing data structures into files.
36*a506a34cSth160488  * 'pickle_file' is the base class.  Classes that inherit this base
37*a506a34cSth160488  * class need to instantiate the virtual function 'dump'.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate enum pickle_mode {
417c478bd9Sstevel@tonic-gate 	PICKLE_READ, PICKLE_WRITE, PICKLE_APPEND
427c478bd9Sstevel@tonic-gate };
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate typedef enum pickle_mode pickle_mode;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate typedef void* pptr;		/* pickle pointer */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate class pickle_file {
497c478bd9Sstevel@tonic-gate     protected:
507c478bd9Sstevel@tonic-gate 	FILE *file;		/* file handle */
517c478bd9Sstevel@tonic-gate 	pickle_mode mode;
527c478bd9Sstevel@tonic-gate 	XDR xdr;
537c478bd9Sstevel@tonic-gate 	char *filename;
547c478bd9Sstevel@tonic-gate 	STRUCTRWLOCK(pickle);
557c478bd9Sstevel@tonic-gate     public:
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/* Constructor.  Creates pickle_file with given name and mode. */
587c478bd9Sstevel@tonic-gate 	pickle_file(char *, pickle_mode);
597c478bd9Sstevel@tonic-gate 
~pickle_file()607c478bd9Sstevel@tonic-gate 	~pickle_file()  { delete filename; DESTROYRW(pickle); }
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/*
637c478bd9Sstevel@tonic-gate 	 * Opens pickle_file with mode specified with constructor.
647c478bd9Sstevel@tonic-gate 	 * Returns TRUE if open was successful; FALSE otherwise.
657c478bd9Sstevel@tonic-gate 	 */
667c478bd9Sstevel@tonic-gate 	bool_t open();
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* Closes pickle_file.  Returns 0 if successful; -1 otherwise. */
697c478bd9Sstevel@tonic-gate 	int close();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/*
72*a506a34cSth160488 	 * dump or load data structure to/from 'filename' using function 'f'.
73*a506a34cSth160488 	 * dump/load is determined by 'mode' with which pickle_file was created.
74*a506a34cSth160488 	 * Returns 0 if successful; 1 if file cannot be opened in mode
75*a506a34cSth160488 	 * specified; -1 if transfer failed do to encoding/decoding errors.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	int transfer(pptr, bool_t (*f) (XDR*, pptr));
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	/* Exclusive access */
acqexcl(void)807c478bd9Sstevel@tonic-gate 	int acqexcl(void) {
817c478bd9Sstevel@tonic-gate 		return (WLOCK(pickle));
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
relexcl(void)847c478bd9Sstevel@tonic-gate 	int relexcl(void) {
857c478bd9Sstevel@tonic-gate 		return (WULOCK(pickle));
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate };
88*a506a34cSth160488 #endif /* PICKLE_H */
89