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 #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifndef PICKLE_H 327c478bd9Sstevel@tonic-gate #define PICKLE_H 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "nisdb_rw.h" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 37*a506a34cSth160488 * 'pickle' is the package for storing data structures into files. 38*a506a34cSth160488 * 'pickle_file' is the base class. Classes that inherit this base 39*a506a34cSth160488 * class need to instantiate the virtual function 'dump'. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate enum pickle_mode { 437c478bd9Sstevel@tonic-gate PICKLE_READ, PICKLE_WRITE, PICKLE_APPEND 447c478bd9Sstevel@tonic-gate }; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate typedef enum pickle_mode pickle_mode; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate typedef void* pptr; /* pickle pointer */ 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate class pickle_file { 517c478bd9Sstevel@tonic-gate protected: 527c478bd9Sstevel@tonic-gate FILE *file; /* file handle */ 537c478bd9Sstevel@tonic-gate pickle_mode mode; 547c478bd9Sstevel@tonic-gate XDR xdr; 557c478bd9Sstevel@tonic-gate char *filename; 567c478bd9Sstevel@tonic-gate STRUCTRWLOCK(pickle); 577c478bd9Sstevel@tonic-gate public: 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* Constructor. Creates pickle_file with given name and mode. */ 607c478bd9Sstevel@tonic-gate pickle_file(char *, pickle_mode); 617c478bd9Sstevel@tonic-gate ~pickle_file()627c478bd9Sstevel@tonic-gate ~pickle_file() { delete filename; DESTROYRW(pickle); } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate * Opens pickle_file with mode specified with constructor. 667c478bd9Sstevel@tonic-gate * Returns TRUE if open was successful; FALSE otherwise. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate bool_t open(); 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate /* Closes pickle_file. Returns 0 if successful; -1 otherwise. */ 717c478bd9Sstevel@tonic-gate int close(); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 74*a506a34cSth160488 * dump or load data structure to/from 'filename' using function 'f'. 75*a506a34cSth160488 * dump/load is determined by 'mode' with which pickle_file was created. 76*a506a34cSth160488 * Returns 0 if successful; 1 if file cannot be opened in mode 77*a506a34cSth160488 * specified; -1 if transfer failed do to encoding/decoding errors. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate int transfer(pptr, bool_t (*f) (XDR*, pptr)); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* Exclusive access */ acqexcl(void)827c478bd9Sstevel@tonic-gate int acqexcl(void) { 837c478bd9Sstevel@tonic-gate return (WLOCK(pickle)); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate relexcl(void)867c478bd9Sstevel@tonic-gate int relexcl(void) { 877c478bd9Sstevel@tonic-gate return (WULOCK(pickle)); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate }; 90*a506a34cSth160488 #endif /* PICKLE_H */ 91